2015-11-13 19:11:28 +00:00
|
|
|
// Copyright 2015 clair authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2015-12-07 21:38:50 +00:00
|
|
|
"github.com/coreos/clair/config"
|
2015-11-13 19:11:28 +00:00
|
|
|
"github.com/google/cayley"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestHealthcheck(t *testing.T) {
|
2015-12-07 21:38:50 +00:00
|
|
|
Open(&config.DatabaseConfig{Type: "memstore"})
|
2015-11-13 19:11:28 +00:00
|
|
|
defer Close()
|
|
|
|
|
|
|
|
b := Healthcheck()
|
|
|
|
assert.True(t, b.IsHealthy, "Healthcheck failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestToValue(t *testing.T) {
|
2015-12-07 21:38:50 +00:00
|
|
|
Open(&config.DatabaseConfig{Type: "memstore"})
|
2015-11-13 19:11:28 +00:00
|
|
|
defer Close()
|
|
|
|
|
|
|
|
// toValue()
|
|
|
|
v, err := toValue(cayley.StartPath(store, "tests").Out("are"))
|
|
|
|
assert.Nil(t, err, "toValue should work even if the requested path leads to nothing")
|
|
|
|
assert.Equal(t, "", v, "toValue should return an empty string if the requested path leads to nothing")
|
|
|
|
|
2015-12-05 07:42:37 +00:00
|
|
|
store.AddQuad(cayley.Triple("tests", "are", "awesome"))
|
2015-11-13 19:11:28 +00:00
|
|
|
v, err = toValue(cayley.StartPath(store, "tests").Out("are"))
|
|
|
|
assert.Nil(t, err, "toValue should have worked")
|
|
|
|
assert.Equal(t, "awesome", v, "toValue did not return the expected value")
|
|
|
|
|
2015-12-05 07:42:37 +00:00
|
|
|
store.AddQuad(cayley.Triple("tests", "are", "running"))
|
2015-11-13 19:11:28 +00:00
|
|
|
v, err = toValue(cayley.StartPath(store, "tests").Out("are"))
|
|
|
|
assert.NotNil(t, err, "toValue should return an error and an empty string if the path leads to multiple values")
|
|
|
|
assert.Equal(t, "", v, "toValue should return an error and an empty string if the path leads to multiple values")
|
|
|
|
|
|
|
|
// toValues()
|
2015-11-20 20:03:20 +00:00
|
|
|
vs, err := toValues(cayley.StartPath(store, "CoreOS").Out(fieldIs))
|
2015-11-13 19:11:28 +00:00
|
|
|
assert.Nil(t, err, "toValues should work even if the requested path leads to nothing")
|
|
|
|
assert.Len(t, vs, 0, "toValue should return an empty array if the requested path leads to nothing")
|
|
|
|
words := []string{"powerful", "lightweight"}
|
|
|
|
for i, word := range words {
|
2015-12-05 07:42:37 +00:00
|
|
|
store.AddQuad(cayley.Triple("CoreOS", fieldIs, word))
|
2015-11-20 20:03:20 +00:00
|
|
|
v, err := toValues(cayley.StartPath(store, "CoreOS").Out(fieldIs))
|
2015-11-13 19:11:28 +00:00
|
|
|
assert.Nil(t, err, "toValues should have worked")
|
|
|
|
assert.Len(t, v, i+1, "toValues did not return the right amount of values")
|
|
|
|
for _, e := range words[:i+1] {
|
|
|
|
assert.Contains(t, v, e, "toValues did not return the values we expected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// toValue(s)() and empty values
|
2015-12-05 07:42:37 +00:00
|
|
|
store.AddQuad(cayley.Triple("bob", "likes", ""))
|
2015-11-13 19:11:28 +00:00
|
|
|
v, err = toValue(cayley.StartPath(store, "bob").Out("likes"))
|
|
|
|
assert.Nil(t, err, "toValue should work even if the requested path leads to nothing")
|
|
|
|
assert.Equal(t, "", v, "toValue should return an empty string if the requested path leads to nothing")
|
|
|
|
|
2015-12-05 07:42:37 +00:00
|
|
|
store.AddQuad(cayley.Triple("bob", "likes", "running"))
|
2015-11-13 19:11:28 +00:00
|
|
|
v, err = toValue(cayley.StartPath(store, "bob").Out("likes"))
|
|
|
|
assert.Nil(t, err, "toValue should have worked")
|
|
|
|
assert.Equal(t, "running", v, "toValue did not return the expected value")
|
|
|
|
|
2015-12-05 07:42:37 +00:00
|
|
|
store.AddQuad(cayley.Triple("bob", "likes", "swimming"))
|
2015-11-13 19:11:28 +00:00
|
|
|
va, err := toValues(cayley.StartPath(store, "bob").Out("likes"))
|
|
|
|
assert.Nil(t, err, "toValues should have worked")
|
|
|
|
assert.Len(t, va, 2, "toValues should have returned 2 values")
|
|
|
|
}
|