master
Raw Download raw file
 1// tests the database connections
 2package main
 3
 4import (
 5	"gopkg.in/mgo.v2"
 6	"strings"
 7	"testing"
 8)
 9
10// initDBForTest creates a session for the database testing
11func initDBForTest(t *testing.T) *mgo.Session {
12	serverConfig = SERVER_CONFIG{DB_ADDRESS: "127.0.0.1",
13		DB_PORT: 27017}
14	session, err := InitDBSession()
15	if err != nil {
16		t.Errorf("Error for initDB: %+v", err)
17		t.FailNow()
18	}
19	return session
20}
21
22// Test_AddAndRetriveForDB
23func TestAddAndRetriveForDB(t *testing.T) {
24	t.SkipNow()
25	DBsession = initDBForTest(t)
26	var recordTest = Record{Question: "Question",
27		Reference: "Reference",
28		Answer:    "Answer",
29		Path:      "linux:file:thing"}
30	if err := addRecordToDB(&recordTest); err != nil {
31		if !strings.Contains(err.Error(), "duplicate key error") {
32			t.Errorf("Error for adding record: %+v", err)
33			t.FailNow()
34		}
35	}
36	results, err := returnForQuery([]string{"linux:file:thing"})
37	if err != nil {
38		t.Errorf("Error for returning initial query: %+v", err)
39	}
40	if len(results) != 1 {
41		t.Errorf("Wrong length of results: %d: %+v", len(results), results)
42	}
43	if results[0] != recordTest {
44		t.Errorf("Unexpected result: %+v", results[0])
45	}
46	results, err = returnForQuery([]string{"linux:networking:test"})
47	if err != nil {
48		t.Errorf("Error for returning test query: %+v", err)
49	}
50	if len(results) != 2 {
51		t.Errorf("Wrong length of test results: %d: %+v", len(results), results)
52	}
53}
54
55// TestRetrieveCategories verifies that categories can be
56// retrieved from the database
57func TestRetrieveCategories(t *testing.T) {
58	initDBForTest(t)
59	categories = nil
60	categories, err := getCategoriesFromDB()
61	if err != nil {
62		t.Errorf("Error getting categories: %+v", err)
63	}
64	if len(categories) <= 0 {
65		t.Error("No categories found.")
66		t.Fail()
67	}
68	testCase := "linux:filesystem:acl"
69	testCaseFound := false
70	for i, cat := range categories {
71		if cat == testCase {
72			testCaseFound = true
73		}
74		for j, secondCat := range categories { //check for duplicates
75			if j != i && cat == secondCat {
76				t.Errorf("Duplicate categories: %+v\t%d and %d", cat, i, j)
77			}
78		}
79	}
80	if !testCaseFound {
81		t.Errorf("No test case was found: %+v", categories)
82	}
83}