132 lines
2.7 KiB
Go
132 lines
2.7 KiB
Go
package lang
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestParseEntityDefinitions(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input string
|
|
want AST
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "entity with enhanced fields and relationships",
|
|
input: `entity User desc "User management" {
|
|
id: uuid required unique
|
|
email: string required validate email validate min_length "5"
|
|
name: string default "Anonymous"
|
|
profile_id: uuid relates to Profile as one via "user_id"
|
|
}`,
|
|
want: AST{
|
|
Definitions: []Definition{
|
|
{
|
|
Entity: &Entity{
|
|
Name: "User",
|
|
Description: stringPtr("User management"),
|
|
Fields: []Field{
|
|
{
|
|
Name: "id",
|
|
Type: "uuid",
|
|
Required: true,
|
|
Unique: true,
|
|
},
|
|
{
|
|
Name: "email",
|
|
Type: "string",
|
|
Required: true,
|
|
Validations: []Validation{
|
|
{Type: "email"},
|
|
{Type: "min_length", Value: stringPtr("5")},
|
|
},
|
|
},
|
|
{
|
|
Name: "name",
|
|
Type: "string",
|
|
Default: stringPtr("Anonymous"),
|
|
},
|
|
{
|
|
Name: "profile_id",
|
|
Type: "uuid",
|
|
Relationship: &Relationship{
|
|
Type: "Profile",
|
|
Cardinality: "one",
|
|
ForeignKey: stringPtr("user_id"),
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "simple entity with basic fields",
|
|
input: `entity Product {
|
|
id: uuid required unique
|
|
name: string required
|
|
price: decimal default "0.00"
|
|
}`,
|
|
want: AST{
|
|
Definitions: []Definition{
|
|
{
|
|
Entity: &Entity{
|
|
Name: "Product",
|
|
Fields: []Field{
|
|
{
|
|
Name: "id",
|
|
Type: "uuid",
|
|
Required: true,
|
|
Unique: true,
|
|
},
|
|
{
|
|
Name: "name",
|
|
Type: "string",
|
|
Required: true,
|
|
},
|
|
{
|
|
Name: "price",
|
|
Type: "decimal",
|
|
Default: stringPtr("0.00"),
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "entity without fields block",
|
|
input: `entity SimpleEntity`,
|
|
want: AST{
|
|
Definitions: []Definition{
|
|
{
|
|
Entity: &Entity{
|
|
Name: "SimpleEntity",
|
|
Fields: []Field{},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
wantErr: false,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := ParseInput(tt.input)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("ParseInput() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
|
|
if !tt.wantErr && !astEqual(got, tt.want) {
|
|
t.Errorf("ParseInput() mismatch.\nGot: %+v\nWant: %+v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|