-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
98 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package gorm_seeder | ||
|
||
import ( | ||
"gopkg.in/DATA-DOG/go-sqlmock.v1" | ||
"gorm.io/driver/postgres" | ||
"gorm.io/gorm" | ||
) | ||
|
||
func getDatabaseMock() (*gorm.DB, sqlmock.Sqlmock) { | ||
mockDB, mock, _ := sqlmock.New() | ||
|
||
dialector := postgres.New(postgres.Config{ | ||
DSN: "sqlmock_db", | ||
DriverName: "postgres", | ||
Conn: mockDB, | ||
PreferSimpleProtocol: true, | ||
}) | ||
|
||
db, _ := gorm.Open(dialector, &gorm.Config{}) | ||
return db, mock | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package gorm_seeder | ||
|
||
import ( | ||
"gorm.io/gorm" | ||
"time" | ||
) | ||
|
||
type StubUsersSeeder struct { | ||
SeederAbstract | ||
} | ||
|
||
func NewUsersSeeder(cfg SeederConfiguration) StubUsersSeeder { | ||
return StubUsersSeeder{NewSeederAbstract(cfg)} | ||
} | ||
|
||
type StubUser struct { | ||
Id uint64 `json:"id" gorm:"primaryKey"` | ||
Name string `json:"name"` | ||
Email string `json:"email"` | ||
Password string `json:"password"` | ||
CreatedAt time.Time `json:"created_at"` | ||
} | ||
|
||
func (u *StubUser) BeforeCreate(tx *gorm.DB) (err error) { | ||
u.CreatedAt = time.Now().UTC() | ||
return | ||
} | ||
|
||
// TableName overrides | ||
func (StubUser) TableName() string { | ||
return "users" | ||
} | ||
|
||
func (s *StubUsersSeeder) Seed1(db *gorm.DB) error { | ||
var users []StubUser | ||
for i := 0; i < s.Configuration.Rows; i++ { | ||
user := StubUser{ | ||
Name: "Name LastName", | ||
Email: "foo@bar.gov", | ||
Password: "password-hash-string", | ||
} | ||
users = append(users, user) | ||
} | ||
return db.CreateInBatches(users, s.Configuration.Rows).Error | ||
} | ||
|
||
func (s *StubUsersSeeder) Seed(db *gorm.DB) error { | ||
for i := 0; i < s.Configuration.Rows; i++ { | ||
user := StubUser{ | ||
Name: "Name LastName", | ||
Email: "foo@bar.gov", | ||
Password: "password-hash-string", | ||
} | ||
err := db.Create(&user).Error | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (s *StubUsersSeeder) Clear(db *gorm.DB) error { | ||
entity := StubUser{} | ||
return s.SeederAbstract.Delete(db, entity.TableName()) | ||
} |