Skip to content

Commit

Permalink
chore: make dummy populate function public
Browse files Browse the repository at this point in the history
  • Loading branch information
yashmehrotra authored and moshloop committed Feb 28, 2023
1 parent e3fd5e5 commit 1679a80
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 67 deletions.
70 changes: 70 additions & 0 deletions fixtures/dummy/all.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package dummy

import (
"github.com/flanksource/duty/models"
"gorm.io/gorm"
)

func PopulateDBWithDummyModels(gormDB *gorm.DB) error {
var err error
createTime := DummyCreatedAt
for _, c := range AllDummyPeople {
err = gormDB.Create(&c).Error
if err != nil {
return err
}
}
for _, c := range AllDummyComponents {
c.UpdatedAt = models.LocalTime(createTime)
err = gormDB.Create(&c).Error
if err != nil {
return err
}
}
for _, c := range AllDummyComponentRelationships {
c.UpdatedAt = createTime
err = gormDB.Create(&c).Error
if err != nil {
return err
}
}
for _, c := range AllDummyConfigs {
c.CreatedAt = createTime
err = gormDB.Create(&c).Error
if err != nil {
return err
}
}
for _, c := range AllDummyConfigAnalysis {
c.FirstObserved = &createTime
err = gormDB.Create(&c).Error
if err != nil {
return err
}
}
for _, c := range AllDummyConfigComponentRelationships {
err = gormDB.Create(&c).Error
if err != nil {
return err
}
}
for _, c := range AllDummyIncidents {
err = gormDB.Create(&c).Error
if err != nil {
return err
}
}
for _, c := range AllDummyHypotheses {
err = gormDB.Create(&c).Error
if err != nil {
return err
}
}
for _, c := range AllDummyEvidences {
err = gormDB.Create(&c).Error
if err != nil {
return err
}
}
return nil
}
69 changes: 2 additions & 67 deletions suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
. "github.com/fergusstrange/embedded-postgres"
"github.com/flanksource/commons/logger"
"github.com/flanksource/duty/fixtures/dummy"
"github.com/flanksource/duty/models"
_ "github.com/flanksource/duty/types"
ginkgo "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -60,7 +59,8 @@ var _ = ginkgo.BeforeSuite(func() {
testDB, err = NewGorm(pgUrl, DefaultGormConfig())
Expect(err).ToNot(HaveOccurred())

populateDBWithDummyModels()
err = dummy.PopulateDBWithDummyModels(testDB)
Expect(err).ToNot(HaveOccurred())
})

var _ = ginkgo.AfterSuite(func() {
Expand All @@ -70,71 +70,6 @@ var _ = ginkgo.AfterSuite(func() {
}
})

func populateDBWithDummyModels() {
var err error
createTime := dummy.DummyCreatedAt
for _, c := range dummy.AllDummyPeople {
err = testDB.Create(&c).Error
if err != nil {
panic(err)
}
}
for _, c := range dummy.AllDummyComponents {
c.UpdatedAt = models.LocalTime(createTime)
err = testDB.Create(&c).Error
if err != nil {
panic(err)
}
}
for _, c := range dummy.AllDummyComponentRelationships {
c.UpdatedAt = createTime
err = testDB.Create(&c).Error
if err != nil {
panic(err)
}
}
for _, c := range dummy.AllDummyConfigs {
c.CreatedAt = createTime
err = testDB.Create(&c).Error
if err != nil {
panic(err)
}
}
for _, c := range dummy.AllDummyConfigAnalysis {
c.FirstObserved = &createTime
err = testDB.Create(&c).Error
if err != nil {
panic(err)
}
}
for _, c := range dummy.AllDummyConfigComponentRelationships {
err = testDB.Create(&c).Error
if err != nil {
panic(err)
}
}
for _, c := range dummy.AllDummyIncidents {
err = testDB.Create(&c).Error
if err != nil {
panic(err)
}
}
for _, c := range dummy.AllDummyHypotheses {
err = testDB.Create(&c).Error
if err != nil {
panic(err)
}
}
for _, c := range dummy.AllDummyEvidences {
err = testDB.Create(&c).Error
if err != nil {
panic(err)
}
}

logger.Infof("Populated database with dummy models")
}

func readTestFile(path string) string {
d, err := os.ReadFile(path)
// We panic here because text fixtures should always be readable
Expand Down

0 comments on commit 1679a80

Please sign in to comment.