repogen/internal/generator/generator_test.go

242 lines
6.6 KiB
Go
Raw Normal View History

2021-01-23 13:03:16 +00:00
package generator_test
import (
"os"
2021-01-23 13:03:16 +00:00
"testing"
"github.com/sunboyy/repogen/internal/code"
"github.com/sunboyy/repogen/internal/generator"
"github.com/sunboyy/repogen/internal/spec"
"github.com/sunboyy/repogen/internal/testutils"
2021-01-23 13:03:16 +00:00
)
var (
idField = code.StructField{
Name: "ID",
Type: code.ExternalType{PackageAlias: "primitive", Name: "ObjectID"},
Tags: map[string][]string{"bson": {"_id", "omitempty"}},
}
genderField = code.StructField{
Name: "Gender",
Type: code.SimpleType("Gender"),
Tags: map[string][]string{"bson": {"gender"}},
}
ageField = code.StructField{
Name: "Age",
Type: code.TypeInt,
Tags: map[string][]string{"bson": {"age"}},
}
)
2021-01-23 13:03:16 +00:00
func TestGenerateMongoRepository(t *testing.T) {
userModel := code.Struct{
Name: "UserModel",
Fields: code.StructFields{
idField,
2022-05-16 15:37:03 +00:00
code.StructField{
2021-01-23 13:03:16 +00:00
Name: "Username",
Type: code.TypeString,
2021-01-23 13:03:16 +00:00
Tags: map[string][]string{"bson": {"username"}},
},
genderField,
ageField,
2021-01-23 13:03:16 +00:00
},
}
methods := []spec.MethodSpec{
// test find: One mode
{
Name: "FindByID",
Params: []code.Param{
{Name: "ctx", Type: code.ExternalType{PackageAlias: "context", Name: "Context"}},
{Name: "id", Type: code.ExternalType{PackageAlias: "primitive", Name: "ObjectID"}},
},
Returns: []code.Type{code.PointerType{ContainedType: code.SimpleType("UserModel")}, code.TypeError},
2021-01-23 13:03:16 +00:00
Operation: spec.FindOperation{
Mode: spec.QueryModeOne,
Query: spec.QuerySpec{
Predicates: []spec.Predicate{
{FieldReference: spec.FieldReference{idField}, Comparator: spec.ComparatorEqual, ParamIndex: 1},
2021-01-23 13:03:16 +00:00
},
},
},
},
// test find: Many mode, And operator, NOT and LessThan comparator
{
Name: "FindByGenderNotAndAgeLessThan",
Params: []code.Param{
{Name: "ctx", Type: code.ExternalType{PackageAlias: "context", Name: "Context"}},
{Name: "gender", Type: code.SimpleType("Gender")},
{Name: "age", Type: code.TypeInt},
2021-01-23 13:03:16 +00:00
},
Returns: []code.Type{
code.PointerType{ContainedType: code.SimpleType("UserModel")},
code.TypeError,
2021-01-23 13:03:16 +00:00
},
Operation: spec.FindOperation{
Mode: spec.QueryModeMany,
Query: spec.QuerySpec{
Operator: spec.OperatorAnd,
Predicates: []spec.Predicate{
{
FieldReference: spec.FieldReference{genderField},
Comparator: spec.ComparatorNot,
ParamIndex: 1,
},
{
FieldReference: spec.FieldReference{ageField},
Comparator: spec.ComparatorLessThan,
ParamIndex: 2,
},
2021-01-23 13:03:16 +00:00
},
},
},
},
{
Name: "FindByAgeLessThanEqualOrderByAge",
2021-01-23 13:03:16 +00:00
Params: []code.Param{
{Name: "ctx", Type: code.ExternalType{PackageAlias: "context", Name: "Context"}},
{Name: "age", Type: code.TypeInt},
2021-01-23 13:03:16 +00:00
},
Returns: []code.Type{
code.ArrayType{ContainedType: code.PointerType{ContainedType: code.SimpleType("UserModel")}},
code.TypeError,
2021-01-23 13:03:16 +00:00
},
Operation: spec.FindOperation{
Mode: spec.QueryModeMany,
Query: spec.QuerySpec{
Predicates: []spec.Predicate{
{
FieldReference: spec.FieldReference{ageField},
Comparator: spec.ComparatorLessThanEqual,
ParamIndex: 1,
},
2021-01-23 13:03:16 +00:00
},
},
Sorts: []spec.Sort{
{FieldReference: spec.FieldReference{ageField}, Ordering: spec.OrderingAscending},
},
2021-01-23 13:03:16 +00:00
},
},
{
Name: "FindByAgeGreaterThanOrderByAgeAsc",
2021-01-23 13:03:16 +00:00
Params: []code.Param{
{Name: "ctx", Type: code.ExternalType{PackageAlias: "context", Name: "Context"}},
{Name: "age", Type: code.TypeInt},
2021-01-23 13:03:16 +00:00
},
Returns: []code.Type{
code.ArrayType{ContainedType: code.PointerType{ContainedType: code.SimpleType("UserModel")}},
code.TypeError,
2021-01-23 13:03:16 +00:00
},
Operation: spec.FindOperation{
Mode: spec.QueryModeMany,
Query: spec.QuerySpec{
Predicates: []spec.Predicate{
{
FieldReference: spec.FieldReference{ageField},
Comparator: spec.ComparatorGreaterThan,
ParamIndex: 1,
},
2021-01-23 13:03:16 +00:00
},
},
Sorts: []spec.Sort{
{FieldReference: spec.FieldReference{ageField}, Ordering: spec.OrderingAscending},
},
2021-01-23 13:03:16 +00:00
},
},
{
Name: "FindByAgeGreaterThanEqualOrderByAgeDesc",
2021-01-23 13:03:16 +00:00
Params: []code.Param{
{Name: "ctx", Type: code.ExternalType{PackageAlias: "context", Name: "Context"}},
{Name: "age", Type: code.TypeInt},
2021-01-23 13:03:16 +00:00
},
Returns: []code.Type{
code.ArrayType{ContainedType: code.PointerType{ContainedType: code.SimpleType("UserModel")}},
code.TypeError,
2021-01-23 13:03:16 +00:00
},
Operation: spec.FindOperation{
Mode: spec.QueryModeMany,
Query: spec.QuerySpec{
Predicates: []spec.Predicate{
{
FieldReference: spec.FieldReference{ageField},
Comparator: spec.ComparatorGreaterThanEqual,
ParamIndex: 1,
},
2021-01-23 13:03:16 +00:00
},
},
Sorts: []spec.Sort{
{FieldReference: spec.FieldReference{ageField}, Ordering: spec.OrderingDescending},
},
2021-01-23 13:03:16 +00:00
},
},
{
Name: "FindByAgeBetween",
Params: []code.Param{
{Name: "ctx", Type: code.ExternalType{PackageAlias: "context", Name: "Context"}},
{Name: "fromAge", Type: code.TypeInt},
{Name: "toAge", Type: code.TypeInt},
},
Returns: []code.Type{
code.ArrayType{ContainedType: code.PointerType{ContainedType: code.SimpleType("UserModel")}},
code.TypeError,
},
Operation: spec.FindOperation{
Mode: spec.QueryModeMany,
Query: spec.QuerySpec{
Predicates: []spec.Predicate{
{
FieldReference: spec.FieldReference{ageField},
Comparator: spec.ComparatorBetween,
ParamIndex: 1,
},
},
},
},
},
2021-01-23 13:03:16 +00:00
{
Name: "FindByGenderOrAge",
Params: []code.Param{
{Name: "ctx", Type: code.ExternalType{PackageAlias: "context", Name: "Context"}},
{Name: "gender", Type: code.SimpleType("Gender")},
{Name: "age", Type: code.TypeInt},
2021-01-23 13:03:16 +00:00
},
Returns: []code.Type{
code.ArrayType{ContainedType: code.PointerType{ContainedType: code.SimpleType("UserModel")}},
code.TypeError,
2021-01-23 13:03:16 +00:00
},
Operation: spec.FindOperation{
Mode: spec.QueryModeMany,
Query: spec.QuerySpec{
Operator: spec.OperatorOr,
Predicates: []spec.Predicate{
{
FieldReference: spec.FieldReference{genderField},
Comparator: spec.ComparatorEqual,
ParamIndex: 1,
},
{
FieldReference: spec.FieldReference{ageField},
Comparator: spec.ComparatorEqual,
ParamIndex: 2,
},
2021-01-23 13:03:16 +00:00
},
},
},
},
}
expectedBytes, err := os.ReadFile("../../test/generator_test_expected.txt")
2021-01-23 13:03:16 +00:00
if err != nil {
t.Fatal(err)
2021-01-23 13:03:16 +00:00
}
expectedCode := string(expectedBytes)
2021-01-23 13:03:16 +00:00
code, err := generator.GenerateRepository("user", userModel, "UserRepository", methods)
2021-01-23 13:03:16 +00:00
if err != nil {
t.Fatal(err)
}
if err := testutils.ExpectMultiLineString(expectedCode, code); err != nil {
t.Error(err)
2021-01-23 13:03:16 +00:00
}
}