repogen/internal/mongo/generator_test.go

392 lines
9.5 KiB
Go
Raw Permalink Normal View History

package mongo_test
import (
"errors"
"reflect"
"testing"
2023-05-24 11:04:29 +00:00
"git.kmsign.ru/royalcat/repogen/internal/code"
"git.kmsign.ru/royalcat/repogen/internal/codegen"
"git.kmsign.ru/royalcat/repogen/internal/mongo"
"git.kmsign.ru/royalcat/repogen/internal/spec"
)
var (
idField = code.StructField{
Name: "ID",
Type: code.ExternalType{PackageAlias: "primitive", Name: "ObjectID"},
Tag: `bson:"_id,omitempty"`,
}
genderField = code.StructField{
Name: "Gender",
Type: code.SimpleType("Gender"),
Tag: `bson:"gender"`,
}
ageField = code.StructField{
Name: "Age",
Type: code.TypeInt,
Tag: `bson:"age"`,
}
nameField = code.StructField{
Name: "Name",
Type: code.SimpleType("NameModel"),
Tag: `bson:"name"`,
}
referrerField = code.StructField{
Name: "Referrer",
Type: code.PointerType{ContainedType: code.SimpleType("UserModel")},
Tag: `bson:"referrer"`,
}
2021-03-31 11:44:31 +00:00
consentHistoryField = code.StructField{
Name: "ConsentHistory",
Type: code.ArrayType{ContainedType: code.SimpleType("ConsentHistory")},
Tag: `bson:"consent_history"`,
2021-03-31 11:44:31 +00:00
}
enabledField = code.StructField{
Name: "Enabled",
Type: code.TypeBool,
Tag: `bson:"enabled"`,
}
accessTokenField = code.StructField{
Name: "AccessToken",
Type: code.TypeString,
}
firstNameField = code.StructField{
Name: "First",
Type: code.TypeString,
Tag: `bson:"first"`,
}
)
2021-01-26 13:23:52 +00:00
var userModel = code.Struct{
Name: "UserModel",
Fields: code.StructFields{
idField,
2022-05-16 15:37:03 +00:00
code.StructField{
2021-01-26 13:23:52 +00:00
Name: "Username",
Type: code.TypeString,
Tag: `bson:"username"`,
2021-01-26 13:23:52 +00:00
},
genderField,
ageField,
nameField,
referrerField,
2021-03-31 11:44:31 +00:00
consentHistoryField,
enabledField,
accessTokenField,
2021-01-26 13:23:52 +00:00
},
}
func TestImports(t *testing.T) {
generator := mongo.NewGenerator(userModel, "UserRepository")
expected := [][]code.Import{
{
{Path: "context"},
},
{
{Path: "go.mongodb.org/mongo-driver/bson"},
{Path: "go.mongodb.org/mongo-driver/bson/primitive"},
{Path: "go.mongodb.org/mongo-driver/mongo"},
{Path: "go.mongodb.org/mongo-driver/mongo/options"},
},
}
2021-01-23 13:03:16 +00:00
actual := generator.Imports()
if !reflect.DeepEqual(expected, actual) {
t.Errorf("incorrect imports: expected %+v, got %+v", expected, actual)
2021-01-23 13:03:16 +00:00
}
}
func TestGenerateStruct(t *testing.T) {
generator := mongo.NewGenerator(userModel, "UserRepository")
expected := codegen.StructBuilder{
Name: "UserRepositoryMongo",
Fields: []code.StructField{
{
Name: "collection",
Type: code.PointerType{
ContainedType: code.ExternalType{
PackageAlias: "mongo",
Name: "Collection",
},
},
},
},
}
actual := generator.GenerateStruct()
if expected.Name != actual.Name {
t.Errorf(
"incorrect struct name: expected %s, got %s",
expected.Name,
actual.Name,
)
}
if !reflect.DeepEqual(expected.Fields, actual.Fields) {
t.Errorf(
"incorrect struct fields: expected %+v, got %+v",
expected.Fields,
actual.Fields,
)
}
2021-01-23 13:03:16 +00:00
}
func TestGenerateConstructor(t *testing.T) {
generator := mongo.NewGenerator(userModel, "UserRepository")
expected := codegen.FunctionBuilder{
Name: "NewUserRepository",
Params: []code.Param{
{
Name: "collection",
Type: code.PointerType{
ContainedType: code.ExternalType{
PackageAlias: "mongo",
Name: "Collection",
},
},
},
},
Returns: []code.Type{
code.SimpleType("UserRepository"),
},
Body: codegen.FunctionBody{
codegen.ReturnStatement{
codegen.StructStatement{
Type: "&UserRepositoryMongo",
Pairs: []codegen.StructFieldPair{{
Key: "collection",
Value: codegen.Identifier("collection"),
}},
},
},
},
}
2021-01-23 13:03:16 +00:00
actual, err := generator.GenerateConstructor()
2021-01-23 13:03:16 +00:00
if err != nil {
t.Fatal(err)
}
if expected.Name != actual.Name {
t.Errorf(
"incorrect function name: expected %s, got %s",
expected.Name,
actual.Name,
)
}
if !reflect.DeepEqual(expected.Params, actual.Params) {
t.Errorf(
"incorrect struct params: expected %+v, got %+v",
expected.Params,
actual.Params,
)
}
if !reflect.DeepEqual(expected.Body, actual.Body) {
t.Errorf("incorrect function body: expected %+v got %+v",
expected.Body,
actual.Body,
)
2021-01-23 13:03:16 +00:00
}
}
type GenerateMethodTestCase struct {
Name string
MethodSpec spec.MethodSpec
ExpectedBody string
2021-01-23 13:03:16 +00:00
}
2021-01-26 13:23:52 +00:00
type GenerateMethodInvalidTestCase struct {
Name string
Method spec.MethodSpec
ExpectedError error
}
2021-02-14 04:48:09 +00:00
type StubOperation struct {
}
func (o StubOperation) Name() string {
return "Stub"
}
2021-01-26 13:23:52 +00:00
func TestGenerateMethod_Invalid(t *testing.T) {
testTable := []GenerateMethodInvalidTestCase{
{
Name: "operation not supported",
Method: spec.MethodSpec{
Name: "SearchByID",
Params: []code.Param{
{Type: code.ExternalType{PackageAlias: "context", Name: "Context"}},
{Type: code.ExternalType{PackageAlias: "primitive", Name: "ObjectID"}},
},
Returns: []code.Type{
code.ArrayType{ContainedType: code.PointerType{ContainedType: code.SimpleType("UserModel")}},
code.TypeError,
2021-01-26 13:23:52 +00:00
},
2021-02-14 04:48:09 +00:00
Operation: StubOperation{},
2021-01-26 13:23:52 +00:00
},
2021-02-14 04:48:09 +00:00
ExpectedError: mongo.NewOperationNotSupportedError("Stub"),
2021-01-26 13:23:52 +00:00
},
{
2021-01-27 12:15:25 +00:00
Name: "bson tag not found in query",
2021-01-26 13:23:52 +00:00
Method: spec.MethodSpec{
Name: "FindByAccessToken",
Params: []code.Param{
{Type: code.ExternalType{PackageAlias: "context", Name: "Context"}},
{Type: code.TypeString},
2021-01-26 13:23:52 +00:00
},
Returns: []code.Type{
code.ArrayType{ContainedType: code.PointerType{ContainedType: code.SimpleType("UserModel")}},
code.TypeError,
2021-01-26 13:23:52 +00:00
},
Operation: spec.FindOperation{
Mode: spec.QueryModeOne,
Query: spec.QuerySpec{
Predicates: []spec.Predicate{
{
FieldReference: spec.FieldReference{accessTokenField},
Comparator: spec.ComparatorEqual,
ParamIndex: 1,
},
2021-01-27 12:15:25 +00:00
},
},
},
},
2021-02-14 04:48:09 +00:00
ExpectedError: mongo.NewBsonTagNotFoundError("AccessToken"),
2021-01-27 12:15:25 +00:00
},
{
Name: "bson tag not found in sort",
Method: spec.MethodSpec{
Name: "FindAllOrderByAccessToken",
Params: []code.Param{
{Type: code.ExternalType{PackageAlias: "context", Name: "Context"}},
},
Returns: []code.Type{
code.ArrayType{ContainedType: code.PointerType{ContainedType: code.SimpleType("UserModel")}},
code.TypeError,
},
Operation: spec.FindOperation{
Mode: spec.QueryModeOne,
Sorts: []spec.Sort{
{FieldReference: spec.FieldReference{accessTokenField}, Ordering: spec.OrderingAscending},
},
},
},
ExpectedError: mongo.NewBsonTagNotFoundError("AccessToken"),
},
2021-01-27 12:15:25 +00:00
{
Name: "bson tag not found in update field",
Method: spec.MethodSpec{
Name: "UpdateAccessTokenByID",
Params: []code.Param{
{Type: code.ExternalType{PackageAlias: "context", Name: "Context"}},
{Type: code.TypeString},
2021-01-27 12:15:25 +00:00
{Type: code.ExternalType{PackageAlias: "primitive", Name: "ObjectID"}},
},
Returns: []code.Type{
code.TypeBool,
code.TypeError,
2021-01-27 12:15:25 +00:00
},
Operation: spec.UpdateOperation{
Update: spec.UpdateFields{
spec.UpdateField{
FieldReference: spec.FieldReference{accessTokenField},
ParamIndex: 1,
Operator: spec.UpdateOperatorSet,
},
2021-01-27 12:15:25 +00:00
},
Mode: spec.QueryModeOne,
Query: spec.QuerySpec{
Predicates: []spec.Predicate{
{
FieldReference: spec.FieldReference{idField},
Comparator: spec.ComparatorEqual,
ParamIndex: 2,
},
2021-01-26 13:23:52 +00:00
},
},
},
},
2021-02-14 04:48:09 +00:00
ExpectedError: mongo.NewBsonTagNotFoundError("AccessToken"),
2021-01-26 13:23:52 +00:00
},
{
Name: "update type not supported",
Method: spec.MethodSpec{
Name: "UpdateAgeByID",
Params: []code.Param{
{Type: code.ExternalType{PackageAlias: "context", Name: "Context"}},
{Type: code.TypeInt},
{Type: code.ExternalType{PackageAlias: "primitive", Name: "ObjectID"}},
},
Returns: []code.Type{
code.TypeBool,
code.TypeError,
},
Operation: spec.UpdateOperation{
Update: StubUpdate{},
Mode: spec.QueryModeOne,
Query: spec.QuerySpec{
Predicates: []spec.Predicate{
{
FieldReference: spec.FieldReference{idField},
Comparator: spec.ComparatorEqual,
ParamIndex: 2,
},
},
},
},
},
ExpectedError: mongo.NewUpdateTypeNotSupportedError(StubUpdate{}),
},
2021-03-31 11:44:31 +00:00
{
Name: "update operator not supported",
Method: spec.MethodSpec{
Name: "UpdateConsentHistoryAppendByID",
Params: []code.Param{
{Type: code.ExternalType{PackageAlias: "context", Name: "Context"}},
{Type: code.TypeInt},
2021-03-31 11:44:31 +00:00
{Type: code.ExternalType{PackageAlias: "primitive", Name: "ObjectID"}},
},
Returns: []code.Type{
code.TypeBool,
code.TypeError,
2021-03-31 11:44:31 +00:00
},
Operation: spec.UpdateOperation{
Update: spec.UpdateFields{
spec.UpdateField{
FieldReference: spec.FieldReference{consentHistoryField},
ParamIndex: 1,
Operator: "APPEND",
},
2021-03-31 11:44:31 +00:00
},
Mode: spec.QueryModeOne,
Query: spec.QuerySpec{
Predicates: []spec.Predicate{
{
FieldReference: spec.FieldReference{idField},
Comparator: spec.ComparatorEqual,
ParamIndex: 2,
},
2021-03-31 11:44:31 +00:00
},
},
},
},
ExpectedError: mongo.NewUpdateOperatorNotSupportedError("APPEND"),
},
2021-01-26 13:23:52 +00:00
}
for _, testCase := range testTable {
t.Run(testCase.Name, func(t *testing.T) {
generator := mongo.NewGenerator(userModel, "UserRepository")
_, err := generator.GenerateMethod(testCase.Method)
2021-01-26 13:23:52 +00:00
if !errors.Is(err, testCase.ExpectedError) {
2021-03-31 11:44:31 +00:00
t.Errorf("\nExpected = %+v\nReceived = %+v", testCase.ExpectedError, err)
2021-01-26 13:23:52 +00:00
}
})
}
}