Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add tests for uint columns #125

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 52 additions & 3 deletions migrator_emulator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/shopspring/decimal"
"gorm.io/datatypes"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"gorm.io/gorm/migrator"
)

Expand All @@ -46,7 +47,7 @@ type Album struct {
gorm.Model
Title string
MarketingBudget decimal.NullDecimal
ReleaseDate datatypes.Date
ReleaseDate *datatypes.Date
CoverPicture []byte
Singer Singer
SingerId int64
Expand All @@ -55,11 +56,15 @@ type Album struct {

type Track struct {
gorm.Model
TrackNumber int64
TrackNumber uint
Title string
SampleRate float64
Album Album
AlbumId int64
Test1 uint8
Test2 uint16
Test3 uint32
Test4 uint64
}

type Venue struct {
Expand Down Expand Up @@ -138,6 +143,50 @@ func TestAutoMigrate_CreateDataModel(t *testing.T) {
if !db.Migrator().HasIndex(&singer{}, "idx_singers_last_name") {
t.Fatalf("idx_singers_last_name not found")
}

// Verify that we can insert some data.
s := Singer{
FirstName: sql.NullString{String: "First", Valid: true},
LastName: "Last",
Active: true,
Albums: []Album{
{Title: "Album 1", Tracks: []Track{
{TrackNumber: 1, Title: "Track 1", Test1: 1, Test2: 2, Test3: 3, Test4: 4},
{TrackNumber: 2, Title: "Track 2", Test1: 1, Test2: 2, Test3: 3, Test4: 4},
{TrackNumber: 3, Title: "Track 3", Test1: 1, Test2: 2, Test3: 3, Test4: 4},
}},
},
}
db = db.Session(&gorm.Session{FullSaveAssociations: true}).Save(&s)
if db.Error != nil {
t.Fatal(db.Error)
}

// Read the data back from the database to verify that it was really saved.
s2 := Singer{Model: gorm.Model{ID: s.ID}}
db.Preload(clause.Associations).Find(&s2)
if s2.ID <= 0 {
t.Errorf("unexpected singer id: %v", s2.ID)
}
if len(s2.Albums) == 0 {
t.Fatalf("missing albums")
}
for _, a := range s2.Albums {
if a.ID <= 0 {
t.Errorf("unexpected album id: %v", a.ID)
}
for _, track := range a.Tracks {
if track.ID <= 0 {
t.Errorf("unexpected track id: %v", track.ID)
}
if track.TrackNumber <= 0 {
t.Errorf("unexpected track number: %v", track.TrackNumber)
}
if track.Test1 <= 0 || track.Test2 <= 0 || track.Test3 <= 0 || track.Test4 <= 0 {
t.Errorf("unexpected test number: %v %v %v %v", track.Test1, track.Test2, track.Test3, track.Test4)
}
}
}
}

func verifyDatabaseSchema(t *testing.T, dsn string) {
Expand Down Expand Up @@ -166,7 +215,7 @@ func verifyDatabaseSchema(t *testing.T, dsn string) {
"CREATE INDEX idx_singers_deleted_at ON singers(deleted_at)",
"CREATE TABLE albums (\n id INT64 DEFAULT (GET_NEXT_SEQUENCE_VALUE(Sequence albums_seq)),\n created_at TIMESTAMP,\n updated_at TIMESTAMP,\n deleted_at TIMESTAMP,\n title STRING(MAX),\n marketing_budget BOOL,\n release_date DATE,\n cover_picture BYTES(MAX),\n singer_id INT64,\n CONSTRAINT fk_singers_albums FOREIGN KEY(singer_id) REFERENCES singers(id),\n) PRIMARY KEY(id)",
"CREATE INDEX idx_albums_deleted_at ON albums(deleted_at)",
"CREATE TABLE tracks (\n id INT64 DEFAULT (GET_NEXT_SEQUENCE_VALUE(Sequence tracks_seq)),\n created_at TIMESTAMP,\n updated_at TIMESTAMP,\n deleted_at TIMESTAMP,\n track_number INT64,\n title STRING(MAX),\n sample_rate FLOAT64,\n album_id INT64,\n CONSTRAINT fk_albums_tracks FOREIGN KEY(album_id) REFERENCES albums(id),\n) PRIMARY KEY(id)",
"CREATE TABLE tracks (\n id INT64 DEFAULT (GET_NEXT_SEQUENCE_VALUE(Sequence tracks_seq)),\n created_at TIMESTAMP,\n updated_at TIMESTAMP,\n deleted_at TIMESTAMP,\n track_number INT64,\n title STRING(MAX),\n sample_rate FLOAT64,\n album_id INT64,\n test1 INT64,\n test2 INT64,\n test3 INT64,\n test4 INT64,\n CONSTRAINT fk_albums_tracks FOREIGN KEY(album_id) REFERENCES albums(id),\n) PRIMARY KEY(id)",
"CREATE INDEX idx_tracks_deleted_at ON tracks(deleted_at)",
"CREATE TABLE venues (\n id INT64 DEFAULT (GET_NEXT_SEQUENCE_VALUE(Sequence venues_seq)),\n created_at TIMESTAMP,\n updated_at TIMESTAMP,\n deleted_at TIMESTAMP,\n name STRING(MAX),\n description JSON,\n) PRIMARY KEY(id)",
"CREATE INDEX idx_venues_deleted_at ON venues(deleted_at)",
Expand Down
Loading