Import sources from Kontor

This commit is contained in:
Thomas Peetz
2019-06-18 18:33:38 +02:00
parent fa3286c8e2
commit f326e994da
20 changed files with 1262 additions and 3 deletions
+31
View File
@@ -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)
}
+50
View File
@@ -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()
}
}