func makePK(name string) *gorma.RelationalFieldDefinition { f := &gorma.RelationalFieldDefinition{} f.Name = dsl.SanitizeFieldName(name) f.DatabaseFieldName = dsl.SanitizeDBFieldName(f.Name) f.Datatype = gorma.PKInteger return f }
func makePK(name string) *gorma.RelationalFieldDefinition { f := &gorma.RelationalFieldDefinition{} f.FieldName = dsl.SanitizeFieldName(name) f.DatabaseFieldName = dsl.SanitizeDBFieldName(f.FieldName) f.Datatype = gorma.Integer f.PrimaryKey = true return f }
func TestPKWhereSingle(t *testing.T) { sg := &gorma.RelationalModelDefinition{} sg.RelationalFields = make(map[string]*gorma.RelationalFieldDefinition) f := &gorma.RelationalFieldDefinition{} f.Name = dsl.SanitizeFieldName("ID") f.DatabaseFieldName = dsl.SanitizeDBFieldName(f.Name) f.Datatype = gorma.PKInteger sg.RelationalFields[f.Name] = f sg.PrimaryKeys = append(sg.PrimaryKeys, f) pkw := sg.PKWhere() if pkw != "id = ?" { t.Errorf("Expected %s, got %s", "id = ?", pkw) } }
func TestPKUpdateFieldsSingle(t *testing.T) { sg := &gorma.RelationalModelDefinition{} sg.RelationalFields = make(map[string]*gorma.RelationalFieldDefinition) f := &gorma.RelationalFieldDefinition{} f.FieldName = dsl.SanitizeFieldName("ID") f.DatabaseFieldName = dsl.SanitizeDBFieldName(f.FieldName) f.Datatype = gorma.Integer f.PrimaryKey = true sg.RelationalFields[f.FieldName] = f sg.PrimaryKeys = append(sg.PrimaryKeys, f) pkw := sg.PKUpdateFields("model") if pkw != "model.ID" { t.Errorf("Expected %s, got %s", "model.ID", pkw) } }
func TestFieldDefinitions(t *testing.T) { var fieldtests = []struct { name string datatype gorma.FieldType description string nullable bool belongsto string hasmany string hasone string many2many string expected string }{ {"id", gorma.Integer, "description", false, "", "", "", "", "ID\tint // description\n"}, {"id", gorma.UUID, "description", false, "", "", "", "", "ID\tuuid.UUID // description\n"}, {"id", gorma.BigInteger, "description", false, "", "", "", "", "ID\tint64 // description\n"}, {"name", gorma.String, "name", true, "", "", "", "", "Name\t*string // name\n"}, {"user", gorma.HasOne, "has one", false, "", "", "User", "", "User\tUser // has one\n"}, {"user_id", gorma.BelongsTo, "belongs to", false, "", "", "", "", "UserID\tint // belongs to\n"}, } for _, tt := range fieldtests { f := &gorma.RelationalFieldDefinition{} f.FieldName = dsl.SanitizeFieldName(tt.name) f.Datatype = tt.datatype f.Description = tt.description f.Nullable = tt.nullable f.BelongsTo = tt.belongsto f.HasMany = tt.hasmany f.HasOne = tt.hasone f.Many2Many = tt.many2many def := f.FieldDefinition() if def != tt.expected { t.Errorf("expected %s,got %s", tt.expected, def) } } }