github.com/mailru/activerecord@v1.12.2/internal/pkg/generator/fixture_test.go (about) 1 package generator 2 3 import ( 4 "strings" 5 "testing" 6 7 "github.com/mailru/activerecord/internal/pkg/arerror" 8 "github.com/mailru/activerecord/internal/pkg/ds" 9 ) 10 11 func TestGenerateFixture(t *testing.T) { 12 type args struct { 13 params FixturePkgData 14 } 15 16 packageName := "gift" 17 18 tests := []struct { 19 name string 20 args args 21 want *arerror.ErrGeneratorPhases 22 wantStr map[string][]string 23 }{ 24 { 25 name: "simplePkg", 26 want: nil, 27 args: args{ 28 params: FixturePkgData{ 29 FixturePkg: "simplefixture", 30 ARPkg: packageName, 31 ARPkgTitle: "Gift", 32 Indexes: []ds.IndexDeclaration{ 33 { 34 Name: "Id", 35 Num: 0, 36 Selector: "SelectById", 37 Fields: []int{0}, 38 FieldsMap: map[string]ds.IndexField{ 39 "Id": {IndField: 0, Order: 0}, 40 }, 41 Primary: true, 42 Unique: true, 43 Type: "string", 44 }, 45 { 46 Name: "Inv", 47 Num: 1, 48 Selector: "SelectByInv", 49 Fields: []int{2}, 50 FieldsMap: map[string]ds.IndexField{ 51 "Inv": {IndField: 2, Order: 0}, 52 }, 53 Primary: false, 54 Unique: false, 55 Type: "bool", 56 }, 57 }, 58 FieldList: []ds.FieldDeclaration{ 59 { 60 Name: "Id", 61 Format: "string", 62 PrimaryKey: true, 63 Mutators: []string{}, 64 Serializer: []string{}, 65 ObjectLink: "", 66 }, 67 { 68 Name: "Code", 69 Format: "string", 70 Mutators: []string{"Any"}, 71 Serializer: []string{}, 72 ObjectLink: "", 73 }, 74 { 75 Name: "Inv", 76 Format: "bool", 77 Mutators: []string{}, 78 Serializer: []string{}, 79 ObjectLink: "", 80 }, 81 }, 82 FieldObject: map[string]ds.FieldObject{}, 83 Container: ds.NamespaceDeclaration{ObjectName: "0", PublicName: "Testmodel", PackageName: "testmodel"}, 84 Serializers: map[string]ds.SerializerDeclaration{}, 85 Mutators: map[string]ds.MutatorDeclaration{ 86 "Any": { 87 Name: "Any", 88 Update: "any", 89 }, 90 }, 91 Imports: []ds.ImportDeclaration{ 92 { 93 ImportName: "obj", 94 Path: "github.com/foo/bar/baz.git/internal/pkg/model/repository/cmpl/gift", 95 }, 96 }, 97 }, 98 }, 99 wantStr: map[string][]string{ 100 "fixture": { 101 `Code generated by argen. DO NOT EDIT.`, 102 `package simplefixture`, 103 `type GiftBySelectByIdMocker struct {`, 104 `func GetGiftById(Id string) *gift.Gift`, 105 `type GiftBySelectByInvMocker struct {`, 106 `var giftStore map[string]int`, 107 `var giftFixtures []*gift.Gift`, 108 `func initGift() {`, 109 `func GetUpdateMutatorAnyFixtureById(ctx context.Context, Id string) (fxt octopus.FixtureType) {`, 110 }, 111 }, 112 }, 113 { 114 name: "simpleProcPkg", 115 want: nil, 116 args: args{ 117 params: FixturePkgData{ 118 FixturePkg: "procfixture", 119 ARPkg: packageName, 120 ARPkgTitle: "Gift", 121 FieldList: []ds.FieldDeclaration{}, 122 FieldMap: map[string]int{}, 123 FieldObject: nil, 124 ProcInFieldList: []ds.ProcFieldDeclaration{}, 125 ProcOutFieldList: []ds.ProcFieldDeclaration{ 126 { 127 Name: "Output", 128 Format: "string", 129 Type: ds.OUT, 130 Serializer: []string{}, 131 }, 132 }, 133 Container: ds.NamespaceDeclaration{ObjectName: "simpleProc", PublicName: "Testmodel", PackageName: "testmodel"}, 134 Indexes: []ds.IndexDeclaration{}, 135 Serializers: map[string]ds.SerializerDeclaration{}, 136 Imports: []ds.ImportDeclaration{}, 137 }, 138 }, 139 wantStr: map[string][]string{ 140 "fixture": { 141 `package procfixture`, 142 `var giftStore map[string]int`, 143 `var giftFixtures []*gift.Gift`, 144 `func initGift() {`, 145 `func GetGiftByParams(params gift.GiftParams) *gift.Gift {`, 146 `type GiftProcedureMocker struct {}`, 147 `func GetGiftProcedureMocker() GiftProcedureMocker {`, 148 `func (m GiftProcedureMocker) ByFixture(ctx context.Context) octopus.FixtureType {`, 149 `func (m GiftProcedureMocker) ByMocks(ctx context.Context, mocks []octopus.MockEntities) octopus.FixtureType {`, 150 }, 151 }, 152 }, 153 { 154 name: "procPkg", 155 want: nil, 156 args: args{ 157 params: FixturePkgData{ 158 FixturePkg: "procfixture", 159 ARPkg: packageName, 160 ARPkgTitle: "Gift", 161 FieldList: []ds.FieldDeclaration{}, 162 FieldMap: map[string]int{}, 163 ProcInFieldList: []ds.ProcFieldDeclaration{ 164 { 165 Name: "Input", 166 Format: "string", 167 Type: ds.IN, 168 Serializer: []string{}, 169 }, 170 { 171 Name: "InputOutput", 172 Format: "string", 173 Type: ds.OUT, 174 Serializer: []string{}, 175 }, 176 }, 177 ProcOutFieldList: []ds.ProcFieldDeclaration{ 178 { 179 Name: "InputOutput", 180 Format: "string", 181 Type: ds.OUT, 182 Serializer: []string{}, 183 }, 184 { 185 Name: "Output", 186 Format: "string", 187 Type: ds.INOUT, 188 Serializer: []string{"s2i"}, 189 }, 190 }, 191 Container: ds.NamespaceDeclaration{ObjectName: "bar", PublicName: "Testmodel", PackageName: "testmodel"}, 192 Indexes: []ds.IndexDeclaration{}, 193 Serializers: map[string]ds.SerializerDeclaration{ 194 "s2i": { 195 Name: "Output", 196 Pkg: "github.com/mailru/activerecord/pkg/serializer", 197 Type: "int", 198 ImportName: "serializerOutput", 199 Marshaler: "OutputMarshal", 200 Unmarshaler: "OutputUnmarshal", 201 }, 202 }, 203 Imports: []ds.ImportDeclaration{}, 204 AppInfo: "", 205 }, 206 }, 207 wantStr: map[string][]string{ 208 "fixture": { 209 `package procfixture`, 210 `var giftStore map[string]int`, 211 `var giftFixtures []*gift.Gift`, 212 `func initGift() {`, 213 `func GetGiftByParams(params gift.GiftParams) *gift.Gift {`, 214 `type GiftProcedureMocker struct {}`, 215 `func GetGiftProcedureMocker() GiftProcedureMocker {`, 216 `func (m GiftProcedureMocker) ByFixtureParams(ctx context.Context, params gift.GiftParams) octopus.FixtureType {`, 217 `func (m GiftProcedureMocker) ByParamsMocks(ctx context.Context, params gift.GiftParams, mocks []octopus.MockEntities) octopus.FixtureType {`, 218 }, 219 }, 220 }, 221 } 222 223 for _, tt := range tests { 224 t.Run(tt.name, func(t *testing.T) { 225 ret, got := GenerateOctopusFixtureStore(tt.args.params) 226 if got != tt.want { 227 t.Errorf("GenerateFixture() = %v, want %v", got, tt.want) 228 } 229 230 for name, strs := range tt.wantStr { 231 buff, ex := ret[name] 232 if !ex { 233 t.Errorf("GenerateFixture() Name %s not generated", name) 234 return 235 } 236 237 for _, substr := range strs { 238 if !strings.Contains(buff.String(), substr) { 239 t.Errorf("GenerateFixture() = %v, want %v", buff.String(), substr) 240 } 241 } 242 } 243 }) 244 } 245 }