2021-01-23 13:03:16 +00:00
|
|
|
package generator_test
|
|
|
|
|
|
|
|
import (
|
2022-11-09 04:21:11 +00:00
|
|
|
"os"
|
2021-01-23 13:03:16 +00:00
|
|
|
"testing"
|
|
|
|
|
2023-05-24 11:01:50 +00:00
|
|
|
"git.kmsign.com/royalcat/repogen/internal/code"
|
|
|
|
"git.kmsign.com/royalcat/repogen/internal/generator"
|
|
|
|
"git.kmsign.com/royalcat/repogen/internal/spec"
|
|
|
|
"git.kmsign.com/royalcat/repogen/internal/testutils"
|
2021-01-23 13:03:16 +00:00
|
|
|
)
|
|
|
|
|
2021-03-03 14:01:32 +00:00
|
|
|
var (
|
|
|
|
idField = code.StructField{
|
|
|
|
Name: "ID",
|
|
|
|
Type: code.ExternalType{PackageAlias: "primitive", Name: "ObjectID"},
|
2023-04-19 12:42:08 +00:00
|
|
|
Tag: `bson:"_id,omitempty"`,
|
2021-03-03 14:01:32 +00:00
|
|
|
}
|
|
|
|
genderField = code.StructField{
|
|
|
|
Name: "Gender",
|
|
|
|
Type: code.SimpleType("Gender"),
|
2023-04-19 12:42:08 +00:00
|
|
|
Tag: `bson:"gender"`,
|
2021-03-03 14:01:32 +00:00
|
|
|
}
|
|
|
|
ageField = code.StructField{
|
|
|
|
Name: "Age",
|
2022-10-18 12:37:50 +00:00
|
|
|
Type: code.TypeInt,
|
2023-04-19 12:42:08 +00:00
|
|
|
Tag: `bson:"age"`,
|
2021-03-03 14:01:32 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-01-23 13:03:16 +00:00
|
|
|
func TestGenerateMongoRepository(t *testing.T) {
|
|
|
|
userModel := code.Struct{
|
|
|
|
Name: "UserModel",
|
|
|
|
Fields: code.StructFields{
|
2021-03-03 14:01:32 +00:00
|
|
|
idField,
|
2022-05-16 15:37:03 +00:00
|
|
|
code.StructField{
|
2021-01-23 13:03:16 +00:00
|
|
|
Name: "Username",
|
2022-10-18 12:37:50 +00:00
|
|
|
Type: code.TypeString,
|
2023-04-19 12:42:08 +00:00
|
|
|
Tag: `bson:"username"`,
|
2021-01-23 13:03:16 +00:00
|
|
|
},
|
2021-03-03 14:01:32 +00:00
|
|
|
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"}},
|
|
|
|
},
|
2022-10-18 12:37:50 +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.QueryModeOne,
|
|
|
|
Query: spec.QuerySpec{
|
|
|
|
Predicates: []spec.Predicate{
|
2021-03-03 14:01:32 +00:00
|
|
|
{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")},
|
2022-10-18 12:37:50 +00:00
|
|
|
{Name: "age", Type: code.TypeInt},
|
2021-01-23 13:03:16 +00:00
|
|
|
},
|
|
|
|
Returns: []code.Type{
|
|
|
|
code.PointerType{ContainedType: code.SimpleType("UserModel")},
|
2022-10-18 12:37:50 +00:00
|
|
|
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{
|
2022-11-09 04:21:11 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-02-26 03:27:00 +00:00
|
|
|
Name: "FindByAgeLessThanEqualOrderByAge",
|
2021-01-23 13:03:16 +00:00
|
|
|
Params: []code.Param{
|
|
|
|
{Name: "ctx", Type: code.ExternalType{PackageAlias: "context", Name: "Context"}},
|
2022-10-18 12:37:50 +00:00
|
|
|
{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")}},
|
2022-10-18 12:37:50 +00:00
|
|
|
code.TypeError,
|
2021-01-23 13:03:16 +00:00
|
|
|
},
|
|
|
|
Operation: spec.FindOperation{
|
|
|
|
Mode: spec.QueryModeMany,
|
|
|
|
Query: spec.QuerySpec{
|
|
|
|
Predicates: []spec.Predicate{
|
2022-11-09 04:21:11 +00:00
|
|
|
{
|
|
|
|
FieldReference: spec.FieldReference{ageField},
|
|
|
|
Comparator: spec.ComparatorLessThanEqual,
|
|
|
|
ParamIndex: 1,
|
|
|
|
},
|
2021-01-23 13:03:16 +00:00
|
|
|
},
|
|
|
|
},
|
2021-02-26 03:27:00 +00:00
|
|
|
Sorts: []spec.Sort{
|
2021-03-03 14:01:32 +00:00
|
|
|
{FieldReference: spec.FieldReference{ageField}, Ordering: spec.OrderingAscending},
|
2021-02-26 03:27:00 +00:00
|
|
|
},
|
2021-01-23 13:03:16 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-02-26 03:27:00 +00:00
|
|
|
Name: "FindByAgeGreaterThanOrderByAgeAsc",
|
2021-01-23 13:03:16 +00:00
|
|
|
Params: []code.Param{
|
|
|
|
{Name: "ctx", Type: code.ExternalType{PackageAlias: "context", Name: "Context"}},
|
2022-10-18 12:37:50 +00:00
|
|
|
{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")}},
|
2022-10-18 12:37:50 +00:00
|
|
|
code.TypeError,
|
2021-01-23 13:03:16 +00:00
|
|
|
},
|
|
|
|
Operation: spec.FindOperation{
|
|
|
|
Mode: spec.QueryModeMany,
|
|
|
|
Query: spec.QuerySpec{
|
|
|
|
Predicates: []spec.Predicate{
|
2022-11-09 04:21:11 +00:00
|
|
|
{
|
|
|
|
FieldReference: spec.FieldReference{ageField},
|
|
|
|
Comparator: spec.ComparatorGreaterThan,
|
|
|
|
ParamIndex: 1,
|
|
|
|
},
|
2021-01-23 13:03:16 +00:00
|
|
|
},
|
|
|
|
},
|
2021-02-26 03:27:00 +00:00
|
|
|
Sorts: []spec.Sort{
|
2021-03-03 14:01:32 +00:00
|
|
|
{FieldReference: spec.FieldReference{ageField}, Ordering: spec.OrderingAscending},
|
2021-02-26 03:27:00 +00:00
|
|
|
},
|
2021-01-23 13:03:16 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-02-26 03:27:00 +00:00
|
|
|
Name: "FindByAgeGreaterThanEqualOrderByAgeDesc",
|
2021-01-23 13:03:16 +00:00
|
|
|
Params: []code.Param{
|
|
|
|
{Name: "ctx", Type: code.ExternalType{PackageAlias: "context", Name: "Context"}},
|
2022-10-18 12:37:50 +00:00
|
|
|
{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")}},
|
2022-10-18 12:37:50 +00:00
|
|
|
code.TypeError,
|
2021-01-23 13:03:16 +00:00
|
|
|
},
|
|
|
|
Operation: spec.FindOperation{
|
|
|
|
Mode: spec.QueryModeMany,
|
|
|
|
Query: spec.QuerySpec{
|
|
|
|
Predicates: []spec.Predicate{
|
2022-11-09 04:21:11 +00:00
|
|
|
{
|
|
|
|
FieldReference: spec.FieldReference{ageField},
|
|
|
|
Comparator: spec.ComparatorGreaterThanEqual,
|
|
|
|
ParamIndex: 1,
|
|
|
|
},
|
2021-01-23 13:03:16 +00:00
|
|
|
},
|
|
|
|
},
|
2021-02-26 03:27:00 +00:00
|
|
|
Sorts: []spec.Sort{
|
2021-03-03 14:01:32 +00:00
|
|
|
{FieldReference: spec.FieldReference{ageField}, Ordering: spec.OrderingDescending},
|
2021-02-26 03:27:00 +00:00
|
|
|
},
|
2021-01-23 13:03:16 +00:00
|
|
|
},
|
|
|
|
},
|
2021-01-24 08:31:21 +00:00
|
|
|
{
|
|
|
|
Name: "FindByAgeBetween",
|
|
|
|
Params: []code.Param{
|
|
|
|
{Name: "ctx", Type: code.ExternalType{PackageAlias: "context", Name: "Context"}},
|
2022-10-18 12:37:50 +00:00
|
|
|
{Name: "fromAge", Type: code.TypeInt},
|
|
|
|
{Name: "toAge", Type: code.TypeInt},
|
2021-01-24 08:31:21 +00:00
|
|
|
},
|
|
|
|
Returns: []code.Type{
|
|
|
|
code.ArrayType{ContainedType: code.PointerType{ContainedType: code.SimpleType("UserModel")}},
|
2022-10-18 12:37:50 +00:00
|
|
|
code.TypeError,
|
2021-01-24 08:31:21 +00:00
|
|
|
},
|
|
|
|
Operation: spec.FindOperation{
|
|
|
|
Mode: spec.QueryModeMany,
|
|
|
|
Query: spec.QuerySpec{
|
|
|
|
Predicates: []spec.Predicate{
|
2022-11-09 04:21:11 +00:00
|
|
|
{
|
|
|
|
FieldReference: spec.FieldReference{ageField},
|
|
|
|
Comparator: spec.ComparatorBetween,
|
|
|
|
ParamIndex: 1,
|
|
|
|
},
|
2021-01-24 08:31:21 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
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")},
|
2022-10-18 12:37:50 +00:00
|
|
|
{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")}},
|
2022-10-18 12:37:50 +00:00
|
|
|
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{
|
2022-11-09 04:21:11 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2022-11-09 04:21:11 +00:00
|
|
|
expectedBytes, err := os.ReadFile("../../test/generator_test_expected.txt")
|
2021-01-23 13:03:16 +00:00
|
|
|
if err != nil {
|
2022-11-09 04:21:11 +00:00
|
|
|
t.Fatal(err)
|
2021-01-23 13:03:16 +00:00
|
|
|
}
|
2022-11-09 04:21:11 +00:00
|
|
|
expectedCode := string(expectedBytes)
|
2021-01-23 13:03:16 +00:00
|
|
|
|
2022-11-09 04:21:11 +00:00
|
|
|
code, err := generator.GenerateRepository("user", userModel, "UserRepository", methods)
|
2021-01-23 13:03:16 +00:00
|
|
|
|
2021-01-24 08:31:21 +00:00
|
|
|
if err != nil {
|
2022-11-09 04:21:11 +00:00
|
|
|
t.Fatal(err)
|
2021-01-24 08:31:21 +00:00
|
|
|
}
|
2022-11-09 04:21:11 +00:00
|
|
|
if err := testutils.ExpectMultiLineString(expectedCode, code); err != nil {
|
|
|
|
t.Error(err)
|
2021-01-23 13:03:16 +00:00
|
|
|
}
|
|
|
|
}
|