From c5982da6718fb7de18d20e7e303dd3d68dbf1bac Mon Sep 17 00:00:00 2001 From: Sergey Bykov Date: Thu, 8 Oct 2026 23:57:41 +0300 Subject: [PATCH] Fix int32/int64 cast order in dbtest faker - Cast the fake value before assignment in ByNameAndType and ByType: int64(in.X = ...) is not valid Go and aborted dbtest generation for models with int32/int64 columns since #62 - Add TestFakeFiller_IntCast for int32/int64 columns --- generators/dbtest/faker.go | 8 +++--- generators/dbtest/faker_test.go | 44 +++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 generators/dbtest/faker_test.go diff --git a/generators/dbtest/faker.go b/generators/dbtest/faker.go index cbb9e5a..00f80e7 100644 --- a/generators/dbtest/faker.go +++ b/generators/dbtest/faker.go @@ -88,9 +88,9 @@ func (ff FakeFiller) ByNameAndType(columnName, gotype string, maxFiledLen int) ( case model.TypeInt: return fakeIntRange.assign(columnName).Tmpl(), true case model.TypeInt32: - return fakeIntRange.assign(columnName).toInt32().Tmpl(), true + return fakeIntRange.toInt32().assign(columnName).Tmpl(), true case model.TypeInt64: - return fakeIntRange.assign(columnName).toInt64().Tmpl(), true + return fakeIntRange.toInt64().assign(columnName).Tmpl(), true case model.TypeFloat32: return fakeFloat32Range.assign(columnName).Tmpl(), true case model.TypeFloat64: @@ -157,9 +157,9 @@ func (ff FakeFiller) ByType(colName, goType, dbType string, isArray bool, maxFil case model.TypeInt: return fakeIntRange.assign(colName).Tmpl(), true case model.TypeInt32: - return fakeIntRange.assign(colName).toInt32().Tmpl(), true + return fakeIntRange.toInt32().assign(colName).Tmpl(), true case model.TypeInt64: - return fakeIntRange.assign(colName).toInt64().Tmpl(), true + return fakeIntRange.toInt64().assign(colName).Tmpl(), true case model.TypeFloat32: return fakeFloat32Range.assign(colName).Tmpl(), true case model.TypeFloat64: diff --git a/generators/dbtest/faker_test.go b/generators/dbtest/faker_test.go new file mode 100644 index 0000000..5037101 --- /dev/null +++ b/generators/dbtest/faker_test.go @@ -0,0 +1,44 @@ +package dbtest + +import ( + "go/parser" + "testing" + + "github.com/dizzyfool/genna/model" + . "github.com/smartystreets/goconvey/convey" +) + +func TestFakeFiller_IntCast(t *testing.T) { + Convey("int32 and int64 columns are cast before assignment", t, func() { + ff := NewFakeFiller() + + cases := []struct { + name, goType, want string + byName bool + }{ + {"Count", model.TypeInt32, "in.Count = int32(gofakeit.IntRange(1, 10))", false}, + {"Count", model.TypeInt64, "in.Count = int64(gofakeit.IntRange(1, 10))", false}, + {"Login", model.TypeInt32, "in.Login = int32(gofakeit.IntRange(1, 10))", true}, + {"Login", model.TypeInt64, "in.Login = int64(gofakeit.IntRange(1, 10))", true}, + } + for _, c := range cases { + var ( + got string + found bool + ) + if c.byName { + res, ok := ff.ByNameAndType(c.name, c.goType, 0) + got, found = string(res), ok + } else { + res, ok := ff.ByType(c.name, c.goType, model.TypePGInt8, false, 0) + got, found = string(res), ok + } + + So(found, ShouldBeTrue) + So(got, ShouldEqual, c.want) + + _, err := parser.ParseExpr("func() { " + got + " }") + So(err, ShouldBeNil) + } + }) +}