2021-01-23 13:03:16 +00:00
|
|
|
package generator_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/sunboyy/repogen/internal/code"
|
|
|
|
"github.com/sunboyy/repogen/internal/generator"
|
|
|
|
"github.com/sunboyy/repogen/internal/spec"
|
|
|
|
)
|
|
|
|
|
2021-03-03 14:01:32 +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.SimpleType("int"),
|
|
|
|
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{
|
2021-03-03 14:01:32 +00:00
|
|
|
idField,
|
2021-01-23 13:03:16 +00:00
|
|
|
{
|
|
|
|
Name: "Username",
|
|
|
|
Type: code.SimpleType("string"),
|
|
|
|
Tags: map[string][]string{"bson": {"username"}},
|
|
|
|
},
|
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"}},
|
|
|
|
},
|
|
|
|
Returns: []code.Type{code.PointerType{ContainedType: code.SimpleType("UserModel")}, code.SimpleType("error")},
|
|
|
|
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")},
|
|
|
|
{Name: "age", Type: code.SimpleType("int")},
|
|
|
|
},
|
|
|
|
Returns: []code.Type{
|
|
|
|
code.PointerType{ContainedType: code.SimpleType("UserModel")},
|
|
|
|
code.SimpleType("error"),
|
|
|
|
},
|
|
|
|
Operation: spec.FindOperation{
|
|
|
|
Mode: spec.QueryModeMany,
|
|
|
|
Query: spec.QuerySpec{
|
|
|
|
Operator: spec.OperatorAnd,
|
|
|
|
Predicates: []spec.Predicate{
|
2021-03-03 14:01:32 +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"}},
|
|
|
|
{Name: "age", Type: code.SimpleType("int")},
|
|
|
|
},
|
|
|
|
Returns: []code.Type{
|
|
|
|
code.ArrayType{ContainedType: code.PointerType{ContainedType: code.SimpleType("UserModel")}},
|
|
|
|
code.SimpleType("error"),
|
|
|
|
},
|
|
|
|
Operation: spec.FindOperation{
|
|
|
|
Mode: spec.QueryModeMany,
|
|
|
|
Query: spec.QuerySpec{
|
|
|
|
Predicates: []spec.Predicate{
|
2021-03-03 14:01:32 +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"}},
|
|
|
|
{Name: "age", Type: code.SimpleType("int")},
|
|
|
|
},
|
|
|
|
Returns: []code.Type{
|
|
|
|
code.ArrayType{ContainedType: code.PointerType{ContainedType: code.SimpleType("UserModel")}},
|
|
|
|
code.SimpleType("error"),
|
|
|
|
},
|
|
|
|
Operation: spec.FindOperation{
|
|
|
|
Mode: spec.QueryModeMany,
|
|
|
|
Query: spec.QuerySpec{
|
|
|
|
Predicates: []spec.Predicate{
|
2021-03-03 14:01:32 +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"}},
|
|
|
|
{Name: "age", Type: code.SimpleType("int")},
|
|
|
|
},
|
|
|
|
Returns: []code.Type{
|
|
|
|
code.ArrayType{ContainedType: code.PointerType{ContainedType: code.SimpleType("UserModel")}},
|
|
|
|
code.SimpleType("error"),
|
|
|
|
},
|
|
|
|
Operation: spec.FindOperation{
|
|
|
|
Mode: spec.QueryModeMany,
|
|
|
|
Query: spec.QuerySpec{
|
|
|
|
Predicates: []spec.Predicate{
|
2021-03-03 14:01:32 +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"}},
|
|
|
|
{Name: "fromAge", Type: code.SimpleType("int")},
|
|
|
|
{Name: "toAge", Type: code.SimpleType("int")},
|
|
|
|
},
|
|
|
|
Returns: []code.Type{
|
|
|
|
code.ArrayType{ContainedType: code.PointerType{ContainedType: code.SimpleType("UserModel")}},
|
|
|
|
code.SimpleType("error"),
|
|
|
|
},
|
|
|
|
Operation: spec.FindOperation{
|
|
|
|
Mode: spec.QueryModeMany,
|
|
|
|
Query: spec.QuerySpec{
|
|
|
|
Predicates: []spec.Predicate{
|
2021-03-03 14:01:32 +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")},
|
|
|
|
{Name: "age", Type: code.SimpleType("int")},
|
|
|
|
},
|
|
|
|
Returns: []code.Type{
|
|
|
|
code.ArrayType{ContainedType: code.PointerType{ContainedType: code.SimpleType("UserModel")}},
|
|
|
|
code.SimpleType("error"),
|
|
|
|
},
|
|
|
|
Operation: spec.FindOperation{
|
|
|
|
Mode: spec.QueryModeMany,
|
|
|
|
Query: spec.QuerySpec{
|
|
|
|
Operator: spec.OperatorOr,
|
|
|
|
Predicates: []spec.Predicate{
|
2021-03-03 14:01:32 +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
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
code, err := generator.GenerateRepository("user", userModel, "UserRepository", methods)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
expectedCode := `// Code generated by repogen. DO NOT EDIT.
|
|
|
|
package user
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
2021-02-26 03:27:00 +00:00
|
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
2021-01-23 13:03:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func NewUserRepository(collection *mongo.Collection) UserRepository {
|
|
|
|
return &UserRepositoryMongo{
|
|
|
|
collection: collection,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type UserRepositoryMongo struct {
|
|
|
|
collection *mongo.Collection
|
|
|
|
}
|
|
|
|
|
2021-01-27 12:15:25 +00:00
|
|
|
func (r *UserRepositoryMongo) FindByID(arg0 context.Context, arg1 primitive.ObjectID) (*UserModel, error) {
|
2021-01-23 13:03:16 +00:00
|
|
|
var entity UserModel
|
2021-01-27 12:15:25 +00:00
|
|
|
if err := r.collection.FindOne(arg0, bson.M{
|
|
|
|
"_id": arg1,
|
2021-02-26 03:27:00 +00:00
|
|
|
}, options.FindOne().SetSort(bson.M{})).Decode(&entity); err != nil {
|
2021-01-23 13:03:16 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &entity, nil
|
|
|
|
}
|
|
|
|
|
2021-01-27 12:15:25 +00:00
|
|
|
func (r *UserRepositoryMongo) FindByGenderNotAndAgeLessThan(arg0 context.Context, arg1 Gender, arg2 int) (*UserModel, error) {
|
|
|
|
cursor, err := r.collection.Find(arg0, bson.M{
|
2021-02-12 15:19:31 +00:00
|
|
|
"$and": []bson.M{
|
|
|
|
{"gender": bson.M{"$ne": arg1}},
|
|
|
|
{"age": bson.M{"$lt": arg2}},
|
|
|
|
},
|
2021-02-26 03:27:00 +00:00
|
|
|
}, options.Find().SetSort(bson.M{}))
|
2021-01-23 13:03:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var entities []*UserModel
|
2021-01-27 12:15:25 +00:00
|
|
|
if err := cursor.All(arg0, &entities); err != nil {
|
2021-01-23 13:03:16 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return entities, nil
|
|
|
|
}
|
|
|
|
|
2021-02-26 03:27:00 +00:00
|
|
|
func (r *UserRepositoryMongo) FindByAgeLessThanEqualOrderByAge(arg0 context.Context, arg1 int) ([]*UserModel, error) {
|
2021-01-27 12:15:25 +00:00
|
|
|
cursor, err := r.collection.Find(arg0, bson.M{
|
|
|
|
"age": bson.M{"$lte": arg1},
|
2021-02-26 03:27:00 +00:00
|
|
|
}, options.Find().SetSort(bson.M{
|
|
|
|
"age": 1,
|
|
|
|
}))
|
2021-01-23 13:03:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var entities []*UserModel
|
2021-01-27 12:15:25 +00:00
|
|
|
if err := cursor.All(arg0, &entities); err != nil {
|
2021-01-23 13:03:16 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return entities, nil
|
|
|
|
}
|
|
|
|
|
2021-02-26 03:27:00 +00:00
|
|
|
func (r *UserRepositoryMongo) FindByAgeGreaterThanOrderByAgeAsc(arg0 context.Context, arg1 int) ([]*UserModel, error) {
|
2021-01-27 12:15:25 +00:00
|
|
|
cursor, err := r.collection.Find(arg0, bson.M{
|
|
|
|
"age": bson.M{"$gt": arg1},
|
2021-02-26 03:27:00 +00:00
|
|
|
}, options.Find().SetSort(bson.M{
|
|
|
|
"age": 1,
|
|
|
|
}))
|
2021-01-23 13:03:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var entities []*UserModel
|
2021-01-27 12:15:25 +00:00
|
|
|
if err := cursor.All(arg0, &entities); err != nil {
|
2021-01-23 13:03:16 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return entities, nil
|
|
|
|
}
|
|
|
|
|
2021-02-26 03:27:00 +00:00
|
|
|
func (r *UserRepositoryMongo) FindByAgeGreaterThanEqualOrderByAgeDesc(arg0 context.Context, arg1 int) ([]*UserModel, error) {
|
2021-01-27 12:15:25 +00:00
|
|
|
cursor, err := r.collection.Find(arg0, bson.M{
|
|
|
|
"age": bson.M{"$gte": arg1},
|
2021-02-26 03:27:00 +00:00
|
|
|
}, options.Find().SetSort(bson.M{
|
|
|
|
"age": -1,
|
|
|
|
}))
|
2021-01-23 13:03:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var entities []*UserModel
|
2021-01-27 12:15:25 +00:00
|
|
|
if err := cursor.All(arg0, &entities); err != nil {
|
2021-01-23 13:03:16 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return entities, nil
|
|
|
|
}
|
|
|
|
|
2021-01-27 12:15:25 +00:00
|
|
|
func (r *UserRepositoryMongo) FindByAgeBetween(arg0 context.Context, arg1 int, arg2 int) ([]*UserModel, error) {
|
|
|
|
cursor, err := r.collection.Find(arg0, bson.M{
|
|
|
|
"age": bson.M{"$gte": arg1, "$lte": arg2},
|
2021-02-26 03:27:00 +00:00
|
|
|
}, options.Find().SetSort(bson.M{}))
|
2021-01-24 08:31:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var entities []*UserModel
|
2021-01-27 12:15:25 +00:00
|
|
|
if err := cursor.All(arg0, &entities); err != nil {
|
2021-01-24 08:31:21 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return entities, nil
|
|
|
|
}
|
|
|
|
|
2021-01-27 12:15:25 +00:00
|
|
|
func (r *UserRepositoryMongo) FindByGenderOrAge(arg0 context.Context, arg1 Gender, arg2 int) ([]*UserModel, error) {
|
|
|
|
cursor, err := r.collection.Find(arg0, bson.M{
|
2021-01-23 13:03:16 +00:00
|
|
|
"$or": []bson.M{
|
2021-01-27 12:15:25 +00:00
|
|
|
{"gender": arg1},
|
|
|
|
{"age": arg2},
|
2021-01-23 13:03:16 +00:00
|
|
|
},
|
2021-02-26 03:27:00 +00:00
|
|
|
}, options.Find().SetSort(bson.M{}))
|
2021-01-23 13:03:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var entities []*UserModel
|
2021-01-27 12:15:25 +00:00
|
|
|
if err := cursor.All(arg0, &entities); err != nil {
|
2021-01-23 13:03:16 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return entities, nil
|
|
|
|
}
|
|
|
|
`
|
|
|
|
expectedCodeLines := strings.Split(expectedCode, "\n")
|
|
|
|
actualCodeLines := strings.Split(code, "\n")
|
|
|
|
|
|
|
|
for i, line := range expectedCodeLines {
|
|
|
|
if line != actualCodeLines[i] {
|
|
|
|
t.Errorf("On line %d\nExpected = %v\nActual = %v", i, line, actualCodeLines[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|