revise database testing

This commit is contained in:
Sonja Happ 2019-09-03 14:16:01 +02:00
parent 95dc4998cf
commit dee277bf19

View file

@ -2,11 +2,29 @@ package common
import (
"fmt"
"github.com/jinzhu/gorm"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
var db *gorm.DB
// find model string lambda
func fM(s string, id uint) string {
return fmt.Sprintf("Find %s with ID=%d", s, id)
}
func TestMain(m *testing.M) {
db = DummyInitDB()
defer db.Close()
DummyPopulateDB(db)
os.Exit(m.Run())
}
// Verify that you can connect to the database
func TestDBConnection(t *testing.T) {
db := InitDB()
@ -15,133 +33,121 @@ func TestDBConnection(t *testing.T) {
assert.NoError(t, VerifyConnection(db), "DB must ping")
}
// Verify that the associations between each model are done properly
func TestDummyDBAssociations(t *testing.T) {
a := assert.New(t)
func TestUserAssociations(t *testing.T) {
var usr1 User
assert.NoError(t, db.Find(&usr1, "ID = ?", 2).Error, fM("User", 2))
assert.EqualValues(t, "User_A", usr1.Username)
// find model string lambda
fM := func(s string) string { return fmt.Sprintf("Find %s with ID=1", s) }
// Get scenarios of usr1
var scenarios []Scenario
assert.NoError(t, db.Model(&usr1).Related(&scenarios, "Scenarios").Error)
if len(scenarios) != 2 {
assert.Fail(t, "User Associations",
"Expected to have %v Scenarios. Has %v.", 2, len(scenarios))
}
}
db := DummyInitDB()
defer db.Close()
func TestScenarioAssociations(t *testing.T) {
var scenario1 Scenario
assert.NoError(t, db.Find(&scenario1, 1).Error, fM("Scenario", 1))
assert.EqualValues(t, "Scenario_A", scenario1.Name)
DummyPopulateDB(db)
// Get users of scenario1
var users []User
assert.NoError(t, db.Model(&scenario1).Association("Users").Find(&users).Error)
if len(users) != 2 {
assert.Fail(t, "Scenario Associations",
"Expected to have %v Users. Has %v.", 2, len(users))
}
// Variables for tests
var simr Simulator
var mo SimulationModel
var file File
var so Scenario
var usr User
var usrs []User
var dab Dashboard
var widg Widget
// Get simulation models of scenario1
var models []SimulationModel
assert.NoError(t, db.Model(&scenario1).Related(&models, "SimulationModels").Error)
if len(models) != 2 {
assert.Fail(t, "Scenario Associations",
"Expected to have %v simulation models. Has %v.", 2, len(models))
}
var sigs []Signal
var mos []SimulationModel
// Get dashboards of scenario1
var dashboards []Dashboard
assert.NoError(t, db.Model(&scenario1).Related(&dashboards, "Dashboards").Error)
if len(dashboards) != 2 {
assert.Fail(t, "Scenario Associations",
"Expected to have %v Dashboards. Has %v.", 2, len(dashboards))
}
}
func TestSimulatorAssociations(t *testing.T) {
var simulator1 Simulator
assert.NoError(t, db.Find(&simulator1, 1).Error, fM("Simulator", 1))
assert.EqualValues(t, "Host_A", simulator1.Host)
// Get simulation models of simulator1
var models []SimulationModel
assert.NoError(t, db.Model(&simulator1).Association("SimulationModels").Find(&models).Error)
if len(models) != 2 {
assert.Fail(t, "Simulator Associations",
"Expected to have %v SimulationModels. Has %v.", 2, len(models))
}
}
func TestSimulationModelAssociations(t *testing.T) {
var model1 SimulationModel
assert.NoError(t, db.Find(&model1, 1).Error, fM("SimulationModel", 1))
assert.EqualValues(t, "SimulationModel_A", model1.Name)
// Check simulator ID
if model1.SimulatorID != 1 {
assert.Fail(t, "Simulation Model expected to have Simulator ID 1, but is %v", model1.SimulatorID)
}
// Get OutputMapping signals of model1
var signals []Signal
assert.NoError(t, db.Model(&model1).Where("Direction = ?", "out").Related(&signals, "OutputMapping").Error)
if len(signals) != 2 {
assert.Fail(t, "SimulationModel Associations",
"Expected to have %v Output Signals. Has %v.", 2, len(signals))
}
// Get files of model1
var files []File
var files_sm []File
var sos []Scenario
var dabs []Dashboard
var widgs []Widget
// User
a.NoError(db.Find(&usr, 2).Error, fM("User"))
a.EqualValues("User_A", usr.Username)
// User Associations
a.NoError(db.Model(&usr).Related(&sos, "Scenarios").Error)
if len(sos) != 2 {
a.Fail("User Associations",
"Expected to have %v Scenarios. Has %v.", 2, len(sos))
}
// Scenario
a.NoError(db.Find(&so, 1).Error, fM("Scenario"))
a.EqualValues("Scenario_A", so.Name)
// Scenario Associations
a.NoError(db.Model(&so).Association("Users").Find(&usrs).Error)
if len(usrs) != 2 {
a.Fail("Scenario Associations",
"Expected to have %v Users. Has %v.", 2, len(usrs))
}
a.NoError(db.Model(&so).Related(&mos, "SimulationModels").Error)
if len(mos) != 2 {
a.Fail("Scenario Associations",
"Expected to have %v simulation models. Has %v.", 2, len(mos))
}
a.NoError(db.Model(&so).Related(&dabs, "Dashboards").Error)
if len(dabs) != 2 {
a.Fail("Scenario Associations",
"Expected to have %v Dashboards. Has %v.", 2, len(dabs))
}
// Simulator
a.NoError(db.Find(&simr, 1).Error, fM("Simulator"))
a.EqualValues("Host_A", simr.Host)
// Simulator Associations
a.NoError(db.Model(&simr).Association("SimulationModels").Find(&mos).Error)
if len(mos) != 2 {
a.Fail("Simulator Associations",
"Expected to have %v SimulationModels. Has %v.", 2, len(mos))
}
// SimulationModel
a.NoError(db.Find(&mo, 1).Error, fM("SimulationModel"))
a.EqualValues("SimulationModel_A", mo.Name)
// SimulationModel Associations
a.NoError(db.Model(&mo).Where("Direction = ?", "out").Related(&sigs, "OutputMapping").Error)
if len(sigs) != 2 {
a.Fail("SimulationModel Associations",
"Expected to have %v Output Signals. Has %v.", 2, len(sigs))
}
a.NoError(db.Model(&mo).Related(&files_sm, "Files").Error)
if len(files_sm) != 2 {
a.Fail("SimulationModel Associations",
"Expected to have %v Files. Has %v.", 2, len(files_sm))
}
fmt.Println("SimulatorID: ", mo.SimulatorID)
// Dashboard
a.NoError(db.Find(&dab, 1).Error, fM("Dashboard"))
a.EqualValues("Dashboard_A", dab.Name)
// Dashboard Associations
a.NoError(db.Model(&dab).Related(&widgs, "Widgets").Error)
if len(widgs) != 2 {
a.Fail("Widget Associations",
"Expected to have %v Widget. Has %v.", 2, len(widgs))
}
// Widget
a.NoError(db.Find(&widg, 1).Error, fM("Widget"))
a.EqualValues("Widget_A", widg.Name)
// Widget Association
a.NoError(db.Model(&widg).Related(&files, "Files").Error)
assert.NoError(t, db.Model(&model1).Related(&files, "Files").Error)
if len(files) != 2 {
a.Fail("Widget Associations",
assert.Fail(t, "SimulationModel Associations",
"Expected to have %v Files. Has %v.", 2, len(files))
}
// File
a.NoError(db.Find(&file, 1).Error, fM("File"))
a.EqualValues("File_A", file.Name)
}
func TestDashboardAssociations(t *testing.T) {
var dashboard1 Dashboard
assert.NoError(t, db.Find(&dashboard1, 1).Error, fM("Dashboard", 1))
assert.EqualValues(t, "Dashboard_A", dashboard1.Name)
//Get widgets of dashboard1
var widgets []Widget
assert.NoError(t, db.Model(&dashboard1).Related(&widgets, "Widgets").Error)
if len(widgets) != 2 {
assert.Fail(t, "Dashboard Associations",
"Expected to have %v Widget. Has %v.", 2, len(widgets))
}
}
func TestWidgetAssociations(t *testing.T) {
var widget1 Widget
assert.NoError(t, db.Find(&widget1, 1).Error, fM("Widget", 1))
assert.EqualValues(t, "Widget_A", widget1.Name)
// Get files of widget
var files []File
assert.NoError(t, db.Model(&widget1).Related(&files, "Files").Error)
if len(files) != 2 {
assert.Fail(t, "Widget Associations",
"Expected to have %v Files. Has %v.", 2, len(files))
}
}
func TestFileAssociations(t *testing.T) {
var file1 File
assert.NoError(t, db.Find(&file1, 1).Error, fM("File", 1))
assert.EqualValues(t, "File_A", file1.Name)
}