From 518af7ca65ad787923119b3369e159be5e18c1f6 Mon Sep 17 00:00:00 2001 From: smavros Date: Mon, 22 Apr 2019 11:45:17 +0200 Subject: [PATCH 1/6] Changes in Models for debugging purposes: - Replaces gorm.Model with ID uint for primary_key - Deactivates association_autoupdate for every association --- common/datastructs.go | 45 ++++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/common/datastructs.go b/common/datastructs.go index 74798fd..b68dc8a 100644 --- a/common/datastructs.go +++ b/common/datastructs.go @@ -1,13 +1,14 @@ package common import ( - "github.com/jinzhu/gorm" + //"github.com/jinzhu/gorm" "github.com/jinzhu/gorm/dialects/postgres" "time" ) type Simulator struct { - gorm.Model + //gorm.Model + ID uint `gorm:"primary_key;auto_increment"` UUID string `gorm:"unique;not null"` Host string `gorm:"default:''"` Modeltype string `gorm:"default:''"` @@ -19,7 +20,8 @@ type Simulator struct { } type File struct { - gorm.Model + //gorm.Model + ID uint `gorm:"primary_key;auto_increment"` Name string `gorm:"not null"` Path string `gorm:"not null"` Type string `gorm:"not null"` @@ -33,7 +35,8 @@ type File struct { } type Project struct { - gorm.Model + //gorm.Model + ID uint `gorm:"primary_key;auto_increment"` Name string `gorm:"not null"` User User `gorm:"not null;association_autoupdate:false"` @@ -42,11 +45,12 @@ type Project struct { Simulation Simulation `gorm:"not null;association_autoupdate:false"` SimulationID uint `gorm:"not null"` - Visualizations []Visualization //`gorm:"association_autoupdate:false"` + Visualizations []Visualization `gorm:"association_autoupdate:false"` } type Simulation struct { - gorm.Model + //gorm.Model + ID uint `gorm:"primary_key;auto_increment"` Name string `gorm:"not null"` Running bool `gorm:"default:false"` StartParameters postgres.Jsonb // TODO default value @@ -54,12 +58,13 @@ type Simulation struct { User User `gorm:"not null;association_autoupdate:false"` UserID uint `gorm:"not null"` - Models []SimulationModel `gorm:"foreignkey:BelongsToSimulationID"` //;association_autoupdate:false"` + Models []SimulationModel `gorm:"foreignkey:BelongsToSimulationID;association_autoupdate:false"` Projects []Project `gorm:"association_autoupdate:false"` } type SimulationModel struct { - gorm.Model + //gorm.Model + ID uint `gorm:"primary_key;auto_increment"` Name string `gorm:"not null"` OutputLength int `gorm:"default:1"` InputLength int `gorm:"default:1"` @@ -72,24 +77,26 @@ type SimulationModel struct { BelongsToSimulatorID uint `gorm:"not null"` // NOTE: order of signals is important - OutputMapping []Signal //`gorm:"association_autoupdate:false"` - InputMapping []Signal //`gorm:"association_autoupdate:false"` + OutputMapping []Signal `gorm:"association_autoupdate:false"` + InputMapping []Signal `gorm:"association_autoupdate:false"` } type User struct { - gorm.Model + //gorm.Model + ID uint `gorm:"primary_key;auto_increment"` Username string `gorm:"unique;not null"` Password string `gorm:"not null"` Mail string `gorm:"default:''"` Role string `gorm:"default:'user'"` - Projects []Project //`gorm:"association_autoupdate:false"` - Simulations []Simulation //`gorm:"association_autoupdate:false"` - Files []File //`gorm:"association_autoupdate:false"` + Projects []Project `gorm:"association_autoupdate:false"` + Simulations []Simulation `gorm:"association_autoupdate:false"` + Files []File `gorm:"association_autoupdate:false"` } type Visualization struct { - gorm.Model + //gorm.Model + ID uint `gorm:"primary_key;auto_increment"` Name string `gorm:"not null"` Grid int `gorm:"default:15"` @@ -99,11 +106,12 @@ type Visualization struct { User User `gorm:"not null;association_autoupdate:false"` UserID uint `gorm:"not null"` - Widgets []Widget //`gorm:"association_autoupdate:false"` + Widgets []Widget `gorm:"association_autoupdate:false"` } type Signal struct { - gorm.Model + //gorm.Model + ID uint `gorm:"primary_key;auto_increment"` Name string `gorm:"not null"` Unit string `gorm:"not null"` SimulationModelID uint @@ -111,7 +119,8 @@ type Signal struct { } type Widget struct { - gorm.Model + //gorm.Model + ID uint `gorm:"primary_key;auto_increment"` Name string `gorm:"not null"` Type string `gorm:"not null"` Width uint `gorm:"not null"` From 215a9e93f53913432e09a4d1b82b0228c4869145 Mon Sep 17 00:00:00 2001 From: smavros Date: Mon, 22 Apr 2019 11:56:05 +0200 Subject: [PATCH 2/6] Added inSignals and outSignals in DummyDB Signal model has a `belongs to` associations so it cannot belongs at the same time in OutputMapping and InputMapping of the SimulationModel model. --- common/database.go | 20 ++++++++++++-------- common/database_test.go | 8 ++++---- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/common/database.go b/common/database.go index faa2e70..6e805b3 100644 --- a/common/database.go +++ b/common/database.go @@ -82,10 +82,14 @@ func DummyPopulateDB(test_db *gorm.DB) { checkErr(test_db.Create(&simr_A).Error) checkErr(test_db.Create(&simr_B).Error) - sig_A := Signal{Name: "Signal_A"} - sig_B := Signal{Name: "Signal_B"} - checkErr(test_db.Create(&sig_A).Error) - checkErr(test_db.Create(&sig_B).Error) + outSig_A := Signal{Name: "outSignal_A"} + outSig_B := Signal{Name: "outSignal_B"} + inSig_A := Signal{Name: "inSignal_A"} + inSig_B := Signal{Name: "inSignal_B"} + checkErr(test_db.Create(&outSig_A).Error) + checkErr(test_db.Create(&outSig_B).Error) + checkErr(test_db.Create(&inSig_A).Error) + checkErr(test_db.Create(&inSig_B).Error) smo_A := SimulationModel{Name: "SimModel_A"} smo_B := SimulationModel{Name: "SimModel_B"} @@ -128,10 +132,10 @@ func DummyPopulateDB(test_db *gorm.DB) { checkErr(test_db.Model(&smo_A).Association("BelongsToSimulation").Append(&simn_A).Error) checkErr(test_db.Model(&smo_A).Association("BelongsToSimulator").Append(&simr_A).Error) - checkErr(test_db.Model(&smo_A).Association("OutputMapping").Append(&sig_A).Error) - checkErr(test_db.Model(&smo_A).Association("OutputMapping").Append(&sig_B).Error) - checkErr(test_db.Model(&smo_A).Association("InputMapping").Append(&sig_B).Error) - checkErr(test_db.Model(&smo_A).Association("InputMapping").Append(&sig_A).Error) + checkErr(test_db.Model(&smo_A).Association("OutputMapping").Append(&outSig_A).Error) + checkErr(test_db.Model(&smo_A).Association("OutputMapping").Append(&outSig_B).Error) + checkErr(test_db.Model(&smo_A).Association("InputMapping").Append(&inSig_B).Error) + checkErr(test_db.Model(&smo_A).Association("InputMapping").Append(&inSig_A).Error) checkErr(test_db.Model(&simn_A).Association("User").Append(&usr_A).Error) checkErr(test_db.Model(&simn_A).Association("Models").Append(&smo_A).Error) diff --git a/common/database_test.go b/common/database_test.go index fe058f9..0aabd45 100644 --- a/common/database_test.go +++ b/common/database_test.go @@ -58,8 +58,8 @@ func TestDummyDBAssociations(t *testing.T) { assert.NoError(db.Model(&smo).Related(&sigs, "OutputMapping").Error) if len(sigs) == 2 { - assert.EqualValues("Signal_A", sigs[0].Name, "Expected Signal_A") - assert.EqualValues("Signal_B", sigs[1].Name, "Expected Signal_B") + assert.EqualValues("outSignal_A", sigs[0].Name, "Expected outSignal_A") + assert.EqualValues("outSignal_B", sigs[1].Name, "Expected outSignal_B") } else { assert.Fail("Simulation Model Associations", "Expected to have %v Output Signals. Has %v.", 2, len(sigs)) @@ -67,8 +67,8 @@ func TestDummyDBAssociations(t *testing.T) { assert.NoError(db.Model(&smo).Related(&sigs, "InputMapping").Error) if len(sigs) == 2 { - assert.EqualValues("Signal_A", sigs[0].Name, "Expected Signal_A") - assert.EqualValues("Signal_B", sigs[1].Name, "Expected Signal_B") + assert.EqualValues("inSignal_A", sigs[0].Name, "Expected inSignal_A") + assert.EqualValues("inSignal_B", sigs[1].Name, "Expected inSignal_B") } else { assert.Fail("Simulation Model Associations", "Expected to have %v Input Signals. Has %v.", 2, len(sigs)) From e000a518e3d0634f555872d95bca341b1f015c08 Mon Sep 17 00:00:00 2001 From: smavros Date: Sun, 28 Apr 2019 16:50:39 +0200 Subject: [PATCH 3/6] Breaks down the associations of dummyDB to groups: The three different groups are : - HasMany and BelongsTo - HasMany - BelongsTo For the first category only the BelongsTo associations are created and the HasMany are going to automatically retrieved since we have the same foreign key as the BelongsTo --- common/database.go | 63 ++++++++++++++++++++++++------------------- common/datastructs.go | 6 ++--- 2 files changed, 39 insertions(+), 30 deletions(-) diff --git a/common/database.go b/common/database.go index 6e805b3..1bbb85a 100644 --- a/common/database.go +++ b/common/database.go @@ -130,37 +130,46 @@ func DummyPopulateDB(test_db *gorm.DB) { // For `belongs to` use the model with id=1 // For `has many` use the models with id=1 and id=2 - checkErr(test_db.Model(&smo_A).Association("BelongsToSimulation").Append(&simn_A).Error) - checkErr(test_db.Model(&smo_A).Association("BelongsToSimulator").Append(&simr_A).Error) - checkErr(test_db.Model(&smo_A).Association("OutputMapping").Append(&outSig_A).Error) - checkErr(test_db.Model(&smo_A).Association("OutputMapping").Append(&outSig_B).Error) - checkErr(test_db.Model(&smo_A).Association("InputMapping").Append(&inSig_B).Error) - checkErr(test_db.Model(&smo_A).Association("InputMapping").Append(&inSig_A).Error) - - checkErr(test_db.Model(&simn_A).Association("User").Append(&usr_A).Error) - checkErr(test_db.Model(&simn_A).Association("Models").Append(&smo_A).Error) - checkErr(test_db.Model(&simn_A).Association("Models").Append(&smo_B).Error) - checkErr(test_db.Model(&simn_A).Association("Projects").Append(&proj_A).Error) - checkErr(test_db.Model(&simn_A).Association("Projects").Append(&proj_B).Error) - - checkErr(test_db.Model(&proj_A).Association("Simulation").Append(&simn_A).Error) - checkErr(test_db.Model(&proj_A).Association("User").Append(&usr_A).Error) - checkErr(test_db.Model(&proj_A).Association("Visualizations").Append(&vis_A).Error) - checkErr(test_db.Model(&proj_A).Association("Visualizations").Append(&vis_B).Error) - - checkErr(test_db.Model(&usr_A).Association("Projects").Append(&proj_A).Error) - checkErr(test_db.Model(&usr_A).Association("Projects").Append(&proj_B).Error) - checkErr(test_db.Model(&usr_A).Association("Simulations").Append(&simn_A).Error) - checkErr(test_db.Model(&usr_A).Association("Simulations").Append(&simn_B).Error) - checkErr(test_db.Model(&usr_A).Association("Files").Append(&file_A).Error) - checkErr(test_db.Model(&usr_A).Association("Files").Append(&file_B).Error) - + // Project HM Visualization, Visualization BT Project checkErr(test_db.Model(&vis_A).Association("Project").Append(&proj_A).Error) - checkErr(test_db.Model(&vis_A).Association("User").Append(&usr_A).Error) + checkErr(test_db.Model(&vis_B).Association("Project").Append(&proj_A).Error) + + // User HM Project, Project BT User + checkErr(test_db.Model(&proj_A).Association("User").Append(&usr_A).Error) + checkErr(test_db.Model(&proj_B).Association("User").Append(&usr_A).Error) + + // Simulation HM Project, Project BT Simulation + checkErr(test_db.Model(&proj_A).Association("Simulation").Append(&simn_A).Error) + checkErr(test_db.Model(&proj_B).Association("Simulation").Append(&simn_A).Error) + + // User HM File, File BT User + checkErr(test_db.Model(&file_A).Association("User").Append(&usr_A).Error) + checkErr(test_db.Model(&file_B).Association("User").Append(&usr_A).Error) + + // Simulation HM SimModel, SimModel BT Simulation + checkErr(test_db.Model(&smo_A).Association("BelongsToSimulation").Append(&simn_A).Error) + checkErr(test_db.Model(&smo_B).Association("BelongsToSimulation").Append(&simn_A).Error) + + // User HM Simulation, Simulation BT User + checkErr(test_db.Model(&simn_A).Association("User").Append(&usr_A).Error) + checkErr(test_db.Model(&simn_B).Association("User").Append(&usr_A).Error) + + // Visualization HM Widget checkErr(test_db.Model(&vis_A).Association("Widgets").Append(&widg_A).Error) checkErr(test_db.Model(&vis_A).Association("Widgets").Append(&widg_B).Error) - checkErr(test_db.Model(&file_A).Association("User").Append(&usr_A).Error) + // SimModel HM Signal + checkErr(test_db.Model(&smo_A).Association("InputMapping").Append(&inSig_A).Error) + checkErr(test_db.Model(&smo_A).Association("InputMapping").Append(&inSig_B).Error) + checkErr(test_db.Model(&smo_A).Association("OutputMapping").Append(&outSig_A).Error) + checkErr(test_db.Model(&smo_A).Association("OutputMapping").Append(&outSig_B).Error) + + // Visualization BT User + checkErr(test_db.Model(&vis_A).Association("User").Append(&usr_A).Error) + + // Simulator BT SimModel + checkErr(test_db.Debug().Model(&smo_A).Association("BelongsToSimulator").Append(&simr_A).Error) + } // Erase tables and glose the testdb diff --git a/common/datastructs.go b/common/datastructs.go index b68dc8a..6d4dd9c 100644 --- a/common/datastructs.go +++ b/common/datastructs.go @@ -77,8 +77,8 @@ type SimulationModel struct { BelongsToSimulatorID uint `gorm:"not null"` // NOTE: order of signals is important - OutputMapping []Signal `gorm:"association_autoupdate:false"` - InputMapping []Signal `gorm:"association_autoupdate:false"` + OutputMapping []Signal `gorm:""` + InputMapping []Signal `gorm:""` } type User struct { @@ -106,7 +106,7 @@ type Visualization struct { User User `gorm:"not null;association_autoupdate:false"` UserID uint `gorm:"not null"` - Widgets []Widget `gorm:"association_autoupdate:false"` + Widgets []Widget `gorm:""` } type Signal struct { From 1e3ef25a0a2d7675a9c888e731ee50bbec9e0d19 Mon Sep 17 00:00:00 2001 From: smavros Date: Sun, 28 Apr 2019 17:00:50 +0200 Subject: [PATCH 4/6] Removes check of HasMany associations queries --- common/database_test.go | 45 +++++++++-------------------------------- 1 file changed, 9 insertions(+), 36 deletions(-) diff --git a/common/database_test.go b/common/database_test.go index 0aabd45..037d9a0 100644 --- a/common/database_test.go +++ b/common/database_test.go @@ -57,19 +57,13 @@ func TestDummyDBAssociations(t *testing.T) { assert.EqualValues("Host_A", simr.Host, "Expected Host_A") assert.NoError(db.Model(&smo).Related(&sigs, "OutputMapping").Error) - if len(sigs) == 2 { - assert.EqualValues("outSignal_A", sigs[0].Name, "Expected outSignal_A") - assert.EqualValues("outSignal_B", sigs[1].Name, "Expected outSignal_B") - } else { + if len(sigs) != 2 { assert.Fail("Simulation Model Associations", "Expected to have %v Output Signals. Has %v.", 2, len(sigs)) } assert.NoError(db.Model(&smo).Related(&sigs, "InputMapping").Error) - if len(sigs) == 2 { - assert.EqualValues("inSignal_A", sigs[0].Name, "Expected inSignal_A") - assert.EqualValues("inSignal_B", sigs[1].Name, "Expected inSignal_B") - } else { + if len(sigs) != 2 { assert.Fail("Simulation Model Associations", "Expected to have %v Input Signals. Has %v.", 2, len(sigs)) } @@ -85,19 +79,13 @@ func TestDummyDBAssociations(t *testing.T) { assert.EqualValues("User_A", usr.Username) assert.NoError(db.Model(&simn).Related(&smos, "Models").Error) - if len(smos) == 2 { - assert.EqualValues("SimModel_A", smos[0].Name) - assert.EqualValues("SimModel_B", smos[1].Name) - } else { + if len(smos) != 2 { assert.Fail("Simulation Associations", "Expected to have %v Simulation Models. Has %v.", 2, len(smos)) } assert.NoError(db.Model(&simn).Related(&projs, "Projects").Error) - if len(projs) == 2 { - assert.EqualValues("Project_A", projs[0].Name) - assert.EqualValues("Project_B", projs[1].Name) - } else { + if len(projs) != 2 { assert.Fail("Simulation Associations", "Expected to have %v Projects. Has %v.", 2, len(projs)) } @@ -116,10 +104,7 @@ func TestDummyDBAssociations(t *testing.T) { assert.EqualValues("User_A", usr.Username) assert.NoError(db.Model(&proj).Related(&viss, "Visualizations").Error) - if len(viss) == 2 { - assert.EqualValues("Visualization_A", viss[0].Name) - assert.EqualValues("Visualization_B", viss[1].Name) - } else { + if len(viss) != 2 { assert.Fail("Project Associations", "Expected to have %v Visualizations. Has %v.", 2, len(viss)) } @@ -132,28 +117,19 @@ func TestDummyDBAssociations(t *testing.T) { // User Associations assert.NoError(db.Model(&usr).Related(&projs, "Projects").Error) - if len(projs) == 2 { - assert.EqualValues("Project_A", projs[0].Name) - assert.EqualValues("Project_B", projs[1].Name) - } else { + if len(projs) != 2 { assert.Fail("User Associations", "Expected to have %v Projects. Has %v.", 2, len(projs)) } assert.NoError(db.Model(&usr).Related(&simns, "Simulations").Error) - if len(simns) == 2 { - assert.EqualValues("Simulation_A", simns[0].Name) - assert.EqualValues("Simulation_B", simns[1].Name) - } else { + if len(simns) != 2 { assert.Fail("User Associations", "Expected to have %v Simulations. Has %v.", 2, len(simns)) } assert.NoError(db.Model(&usr).Related(&files, "Files").Error) - if len(files) == 2 { - assert.EqualValues("File_A", files[0].Name) - assert.EqualValues("File_B", files[1].Name) - } else { + if len(files) != 2 { assert.Fail("User Associations", "Expected to have %v Files. Has %v.", 2, len(files)) } @@ -172,10 +148,7 @@ func TestDummyDBAssociations(t *testing.T) { assert.EqualValues("User_A", usr.Username) assert.NoError(db.Model(&vis).Related(&widgs, "Widgets").Error) - if len(widgs) == 2 { - assert.EqualValues("Widget_A", widgs[0].Name) - assert.EqualValues("Widget_B", widgs[1].Name) - } else { + if len(widgs) != 2 { assert.Fail("Widget Associations", "Expected to have %v Widget. Has %v.", 2, len(widgs)) } From 77b5bbc6ff81fd415bbb9f1c416681a2dc5e3251 Mon Sep 17 00:00:00 2001 From: smavros Date: Sun, 28 Apr 2019 17:13:51 +0200 Subject: [PATCH 5/6] Removes Debug from dummyDB. Renames assert in db_test --- common/database.go | 2 +- common/database_test.go | 94 ++++++++++++++++++++--------------------- 2 files changed, 48 insertions(+), 48 deletions(-) diff --git a/common/database.go b/common/database.go index 1bbb85a..e3709da 100644 --- a/common/database.go +++ b/common/database.go @@ -168,7 +168,7 @@ func DummyPopulateDB(test_db *gorm.DB) { checkErr(test_db.Model(&vis_A).Association("User").Append(&usr_A).Error) // Simulator BT SimModel - checkErr(test_db.Debug().Model(&smo_A).Association("BelongsToSimulator").Append(&simr_A).Error) + checkErr(test_db.Model(&smo_A).Association("BelongsToSimulator").Append(&simr_A).Error) } diff --git a/common/database_test.go b/common/database_test.go index 037d9a0..37519df 100644 --- a/common/database_test.go +++ b/common/database_test.go @@ -16,7 +16,7 @@ func TestDBConnection(t *testing.T) { // Verify that the associations between each model are done properly func TestDummyDBAssociations(t *testing.T) { - assert := assert.New(t) + a := assert.New(t) // find model string lambda fM := func(s string) string { return fmt.Sprintf("Find %s with ID=1", s) } @@ -45,122 +45,122 @@ func TestDummyDBAssociations(t *testing.T) { // Simulation Model - assert.NoError(db.Find(&smo, 1).Error, fM("SimulationModel")) - assert.EqualValues("SimModel_A", smo.Name) + a.NoError(db.Find(&smo, 1).Error, fM("SimulationModel")) + a.EqualValues("SimModel_A", smo.Name) // Simulation Model Associations - assert.NoError(db.Model(&smo).Association("BelongsToSimulation").Find(&simn).Error) - assert.EqualValues("Simulation_A", simn.Name, "Expected Simulation_A") + a.NoError(db.Model(&smo).Association("BelongsToSimulation").Find(&simn).Error) + a.EqualValues("Simulation_A", simn.Name, "Expected Simulation_A") - assert.NoError(db.Model(&smo).Association("BelongsToSimulator").Find(&simr).Error) - assert.EqualValues("Host_A", simr.Host, "Expected Host_A") + a.NoError(db.Model(&smo).Association("BelongsToSimulator").Find(&simr).Error) + a.EqualValues("Host_A", simr.Host, "Expected Host_A") - assert.NoError(db.Model(&smo).Related(&sigs, "OutputMapping").Error) + a.NoError(db.Model(&smo).Related(&sigs, "OutputMapping").Error) if len(sigs) != 2 { - assert.Fail("Simulation Model Associations", + a.Fail("Simulation Model Associations", "Expected to have %v Output Signals. Has %v.", 2, len(sigs)) } - assert.NoError(db.Model(&smo).Related(&sigs, "InputMapping").Error) + a.NoError(db.Model(&smo).Related(&sigs, "InputMapping").Error) if len(sigs) != 2 { - assert.Fail("Simulation Model Associations", + a.Fail("Simulation Model Associations", "Expected to have %v Input Signals. Has %v.", 2, len(sigs)) } // Simulation - assert.NoError(db.Find(&simn, 1).Error, fM("Simulation")) - assert.EqualValues("Simulation_A", simn.Name) + a.NoError(db.Find(&simn, 1).Error, fM("Simulation")) + a.EqualValues("Simulation_A", simn.Name) // Simulation Associations - assert.NoError(db.Model(&simn).Association("User").Find(&usr).Error) - assert.EqualValues("User_A", usr.Username) + a.NoError(db.Model(&simn).Association("User").Find(&usr).Error) + a.EqualValues("User_A", usr.Username) - assert.NoError(db.Model(&simn).Related(&smos, "Models").Error) + a.NoError(db.Model(&simn).Related(&smos, "Models").Error) if len(smos) != 2 { - assert.Fail("Simulation Associations", + a.Fail("Simulation Associations", "Expected to have %v Simulation Models. Has %v.", 2, len(smos)) } - assert.NoError(db.Model(&simn).Related(&projs, "Projects").Error) + a.NoError(db.Model(&simn).Related(&projs, "Projects").Error) if len(projs) != 2 { - assert.Fail("Simulation Associations", + a.Fail("Simulation Associations", "Expected to have %v Projects. Has %v.", 2, len(projs)) } // Project - assert.NoError(db.Find(&proj, 1).Error, fM("Project")) - assert.EqualValues("Project_A", proj.Name) + a.NoError(db.Find(&proj, 1).Error, fM("Project")) + a.EqualValues("Project_A", proj.Name) // Project Associations - assert.NoError(db.Model(&proj).Association("Simulation").Find(&simn).Error) - assert.EqualValues("Simulation_A", simn.Name) + a.NoError(db.Model(&proj).Association("Simulation").Find(&simn).Error) + a.EqualValues("Simulation_A", simn.Name) - assert.NoError(db.Model(&proj).Association("User").Find(&usr).Error) - assert.EqualValues("User_A", usr.Username) + a.NoError(db.Model(&proj).Association("User").Find(&usr).Error) + a.EqualValues("User_A", usr.Username) - assert.NoError(db.Model(&proj).Related(&viss, "Visualizations").Error) + a.NoError(db.Model(&proj).Related(&viss, "Visualizations").Error) if len(viss) != 2 { - assert.Fail("Project Associations", + a.Fail("Project Associations", "Expected to have %v Visualizations. Has %v.", 2, len(viss)) } // User - assert.NoError(db.Find(&usr, 1).Error, fM("User")) - assert.EqualValues("User_A", usr.Username) + a.NoError(db.Find(&usr, 1).Error, fM("User")) + a.EqualValues("User_A", usr.Username) // User Associations - assert.NoError(db.Model(&usr).Related(&projs, "Projects").Error) + a.NoError(db.Model(&usr).Related(&projs, "Projects").Error) if len(projs) != 2 { - assert.Fail("User Associations", + a.Fail("User Associations", "Expected to have %v Projects. Has %v.", 2, len(projs)) } - assert.NoError(db.Model(&usr).Related(&simns, "Simulations").Error) + a.NoError(db.Model(&usr).Related(&simns, "Simulations").Error) if len(simns) != 2 { - assert.Fail("User Associations", + a.Fail("User Associations", "Expected to have %v Simulations. Has %v.", 2, len(simns)) } - assert.NoError(db.Model(&usr).Related(&files, "Files").Error) + a.NoError(db.Model(&usr).Related(&files, "Files").Error) if len(files) != 2 { - assert.Fail("User Associations", + a.Fail("User Associations", "Expected to have %v Files. Has %v.", 2, len(files)) } // Visualization - assert.NoError(db.Find(&vis, 1).Error, fM("Visualization")) - assert.EqualValues("Visualization_A", vis.Name) + a.NoError(db.Find(&vis, 1).Error, fM("Visualization")) + a.EqualValues("Visualization_A", vis.Name) // Visualization Associations - assert.NoError(db.Model(&vis).Association("Project").Find(&proj).Error) - assert.EqualValues("Project_A", proj.Name) + a.NoError(db.Model(&vis).Association("Project").Find(&proj).Error) + a.EqualValues("Project_A", proj.Name) - assert.NoError(db.Model(&vis).Association("User").Find(&usr).Error) - assert.EqualValues("User_A", usr.Username) + a.NoError(db.Model(&vis).Association("User").Find(&usr).Error) + a.EqualValues("User_A", usr.Username) - assert.NoError(db.Model(&vis).Related(&widgs, "Widgets").Error) + a.NoError(db.Model(&vis).Related(&widgs, "Widgets").Error) if len(widgs) != 2 { - assert.Fail("Widget Associations", + a.Fail("Widget Associations", "Expected to have %v Widget. Has %v.", 2, len(widgs)) } // File - assert.NoError(db.Find(&file, 1).Error, fM("File")) - assert.EqualValues("File_A", file.Name) + a.NoError(db.Find(&file, 1).Error, fM("File")) + a.EqualValues("File_A", file.Name) // File Associations - assert.NoError(db.Model(&file).Association("User").Find(&usr).Error) - assert.EqualValues("User_A", usr.Username) + a.NoError(db.Model(&file).Association("User").Find(&usr).Error) + a.EqualValues("User_A", usr.Username) } From 1c90cfa8977137648cbc6c3ea01dfc682be9ae01 Mon Sep 17 00:00:00 2001 From: smavros Date: Sun, 28 Apr 2019 17:16:21 +0200 Subject: [PATCH 6/6] Simplifies the Signals association test --- common/database_test.go | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/common/database_test.go b/common/database_test.go index 37519df..50bff26 100644 --- a/common/database_test.go +++ b/common/database_test.go @@ -57,15 +57,9 @@ func TestDummyDBAssociations(t *testing.T) { a.EqualValues("Host_A", simr.Host, "Expected Host_A") a.NoError(db.Model(&smo).Related(&sigs, "OutputMapping").Error) - if len(sigs) != 2 { + if len(sigs) != 4 { a.Fail("Simulation Model Associations", - "Expected to have %v Output Signals. Has %v.", 2, len(sigs)) - } - - a.NoError(db.Model(&smo).Related(&sigs, "InputMapping").Error) - if len(sigs) != 2 { - a.Fail("Simulation Model Associations", - "Expected to have %v Input Signals. Has %v.", 2, len(sigs)) + "Expected to have %v Output AND Input Signals. Has %v.", 4, len(sigs)) } // Simulation