Import sources from Kontor
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
mgo "gopkg.in/mgo.v2"
|
||||
)
|
||||
|
||||
// BaseDAO definess the connection parameters to a MongoDB instance.
|
||||
type BaseDAO struct {
|
||||
Server string
|
||||
Database string
|
||||
MongoDb *mgo.Database
|
||||
}
|
||||
|
||||
var (
|
||||
// KalorienmanagerDb has the database connection to the productive MongoDB instance.
|
||||
KalorienmanagerDb = BaseDAO{Server: "localhost", Database: "kalorienmanager"}
|
||||
// TestDb has the database connection to the test MongoDB instance.
|
||||
TestDb = BaseDAO{Server: "localhost", Database: "kalorienmanager_test"}
|
||||
)
|
||||
|
||||
// Connect instantiates the database session.
|
||||
func (m *BaseDAO) Connect() {
|
||||
session, err := mgo.Dial(m.Server)
|
||||
if err != nil {
|
||||
//util.PrintDebug("Connect: %v", err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
m.MongoDb = session.DB(m.Database)
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var baseDaoTestTable = []struct {
|
||||
name string
|
||||
typeName string
|
||||
}{
|
||||
{"Server", "string"},
|
||||
{"Database", "string"},
|
||||
{"MongoDb", "ptr"},
|
||||
}
|
||||
|
||||
func TestCheckBaseDao(t *testing.T) {
|
||||
d := BaseDAO{}
|
||||
for index, testData := range baseDaoTestTable {
|
||||
givenType := reflect.TypeOf(d).Field(index).Type.Kind().String()
|
||||
if givenType != testData.typeName {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestConnectDb(t *testing.T) {
|
||||
d := BaseDAO{}
|
||||
d.Connect()
|
||||
if d.MongoDb == nil {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestDatabasesConfig(t *testing.T) {
|
||||
kalorienmanagerDb := KalorienmanagerDb
|
||||
if kalorienmanagerDb.Server != "localhost" {
|
||||
t.Fail()
|
||||
}
|
||||
if kalorienmanagerDb.Database != "kalorienmanager" {
|
||||
t.Fail()
|
||||
}
|
||||
testDb := TestDb
|
||||
if testDb.Server != "localhost" {
|
||||
t.Fail()
|
||||
}
|
||||
if testDb.Database != "kalorienmanager_test" {
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user