github.com/mailru/activerecord@v1.12.2/internal/pkg/generator/octopus_b_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 TestGenerateOctopus(t *testing.T) { 12 type args struct { 13 params PkgData 14 } 15 16 namespaceStr := "2" 17 18 packageName := "foo" 19 20 tests := []struct { 21 name string 22 args args 23 want *arerror.ErrGeneratorPhases 24 wantStr map[string][]string 25 }{ 26 { 27 name: "fieldsPkg", 28 want: nil, 29 args: args{ 30 params: PkgData{ 31 ARPkg: packageName, 32 ARPkgTitle: "Foo", 33 Indexes: []ds.IndexDeclaration{ 34 { 35 Name: "Field1", 36 Num: 0, 37 Selector: "SelectByField1", 38 Fields: []int{0}, 39 FieldsMap: map[string]ds.IndexField{ 40 "Field1": {IndField: 0, Order: 0}, 41 }, 42 Primary: true, 43 Unique: true, 44 }, 45 { 46 Name: "Field2", 47 Num: 1, 48 Selector: "SelectByField2", 49 Fields: []int{1}, 50 FieldsMap: map[string]ds.IndexField{ 51 "Field1": {IndField: 1, Order: 0}, 52 }, 53 Primary: false, 54 Unique: false, 55 }, 56 }, 57 FieldList: []ds.FieldDeclaration{ 58 { 59 Name: "Field1", 60 Format: "int", 61 PrimaryKey: true, 62 Mutators: []string{ds.IncMutator}, 63 Serializer: []string{}, 64 ObjectLink: "", 65 }, 66 { 67 Name: "Field2", 68 Format: "bool", 69 PrimaryKey: true, 70 Mutators: []string{}, 71 Serializer: []string{}, 72 ObjectLink: "", 73 }, 74 { 75 Name: "Fs", 76 Format: "string", 77 PrimaryKey: true, 78 Mutators: []string{"FsMutator"}, 79 Serializer: []string{}, 80 ObjectLink: "", 81 }, 82 }, 83 FieldObject: map[string]ds.FieldObject{}, 84 Server: ds.ServerDeclaration{Timeout: 500, Host: "127.0.0.1", Port: "11011"}, 85 Container: ds.NamespaceDeclaration{ObjectName: "2", PublicName: "Testmodel", PackageName: "testmodel"}, 86 Serializers: map[string]ds.SerializerDeclaration{}, 87 Mutators: map[string]ds.MutatorDeclaration{ 88 "FsMutator": { 89 Name: "FsMutator", 90 Pkg: "github.com/mailru/activerecord/internal/pkg/conv", 91 Type: "*parser_test.Foo", 92 ImportName: "mutatorFooMutatorField", 93 Update: "updateFunc", 94 Replace: "replaceFunc", 95 PartialFields: []ds.PartialFieldDeclaration{ 96 {Name: "Bar", Type: "ds.AppInfo"}, 97 {Name: "BeerData", Type: "[]Beer"}, 98 {Name: "MapData", Type: "map[string]any"}, 99 }, 100 }, 101 }, 102 Imports: []ds.ImportDeclaration{}, 103 Triggers: map[string]ds.TriggerDeclaration{}, 104 Flags: map[string]ds.FlagDeclaration{}, 105 }, 106 }, 107 wantStr: map[string][]string{ 108 "octopus": { 109 `Code generated by argen. DO NOT EDIT.`, 110 `func (obj *Foo) insertReplace(ctx context.Context, insertMode octopus.InsertMode) error {`, 111 `func (obj *Foo) InsertOrReplace(ctx context.Context) error {`, 112 `func (obj *Foo) Replace(ctx context.Context) error {`, 113 `func (obj *Foo) Insert(ctx context.Context) error {`, 114 `func (obj *Foo) Update(ctx context.Context) error {`, 115 `func (obj *Foo) Delete(ctx context.Context) error {`, 116 `func (obj *Foo) packPk() ([][]byte, error) {`, 117 `func (obj *Foo) Equal (anotherObjI any) bool {`, 118 `func (obj *Foo) PrimaryString() string {`, 119 `func selectBox (ctx context.Context, indexnum uint32, keysPacked [][][]byte, limiter activerecord.SelectorLimiter) ([]*Foo, error) {`, 120 `func (obj *Foo) SetField1(Field1 int) error {`, 121 `func (obj *Foo) GetField1() int {`, 122 `type Mutators struct {`, 123 `newObj.FsMutator.OpFunc`, 124 `newObj.FsMutator.PartialFields`, 125 `func (obj *Foo) SetFsMutatorBar(Bar ds.AppInfo) error {`, 126 `func (obj *Foo) SetFsMutatorBeerData(BeerData []Beer) error {`, 127 `func (obj *Foo) SetFsMutatorMapData(MapData map[string]any) error {`, 128 `func (obj *Foo) packFsPartialFields(op octopus.OpCode) error {`, 129 `func UnpackField1(r *bytes.Reader) (ret int, errRet error) {`, 130 `func packField1(w []byte, Field1 int) ([]byte, error) {`, 131 `func NewFromBox(ctx context.Context, tuples []octopus.TupleData) ([]*Foo, error) {`, 132 `func TupleToStruct(ctx context.Context, tuple octopus.TupleData) (*Foo, error) {`, 133 `func New(ctx context.Context) *Foo {`, 134 `func (obj *Foo) IncField1(mutArg int) error {`, 135 `namespace uint32 = ` + namespaceStr, 136 `type Foo struct {`, 137 `package ` + packageName, 138 }, 139 "mock": { 140 `func (obj *Foo) mockInsertReplace(ctx context.Context, insertMode octopus.InsertMode) []byte {`, 141 `func (obj *Foo) MockReplace(ctx context.Context) []byte {`, 142 `func (obj *Foo) MockInsert(ctx context.Context) []byte {`, 143 `func (obj *Foo) MockInsertOrReplace(ctx context.Context) []byte {`, 144 `func (obj *Foo) MockUpdate(ctx context.Context) []byte {`, 145 `func (obj *Foo) RepoSelector(ctx context.Context) (any, error) {`, 146 `func SelectByField1MockerLogger(keys [], res FooList) func() (activerecord.MockerLogger, error) {`, 147 `func (obj *Foo) MockSelectByField1sRequest(ctx context.Context, keys [], ) []byte {`, 148 `func (obj *Foo) MockSelectResponse() ([][]byte, error) {`, 149 `func (obj *Foo) MockMutatorFsMutatorUpdate(ctx context.Context) [][]byte {`, 150 }, 151 "fixture": { 152 `type FooFT struct {`, 153 `func MarshalFixtures(objs []*Foo) ([]byte, error) {`, 154 `func UnmarshalFromYaml(source []byte) []*Foo {`, 155 `func (objs FooList) String() string {`, 156 }, 157 }, 158 }, 159 { 160 name: "simpleProcPkg", 161 want: nil, 162 args: args{ 163 params: PkgData{ 164 ARPkg: packageName, 165 ARPkgTitle: "Foo", 166 FieldList: []ds.FieldDeclaration{}, 167 FieldMap: map[string]int{}, 168 ProcInFieldList: []ds.ProcFieldDeclaration{}, 169 ProcOutFieldList: []ds.ProcFieldDeclaration{ 170 { 171 Name: "Output", 172 Format: "string", 173 Type: ds.OUT, 174 Serializer: []string{}, 175 }, 176 }, 177 Server: ds.ServerDeclaration{Timeout: 500, Host: "127.0.0.1", Port: "11011"}, 178 Container: ds.NamespaceDeclaration{ObjectName: "simpleProc", PublicName: "Testmodel", PackageName: "testmodel"}, 179 Indexes: []ds.IndexDeclaration{}, 180 Serializers: map[string]ds.SerializerDeclaration{}, 181 Imports: []ds.ImportDeclaration{}, 182 Triggers: map[string]ds.TriggerDeclaration{}, 183 Flags: map[string]ds.FlagDeclaration{}, 184 AppInfo: "", 185 }, 186 }, 187 wantStr: map[string][]string{ 188 "octopus": { 189 `Code generated by argen. DO NOT EDIT.`, 190 `func (obj *Foo) GetOutput() string {`, 191 `func Call(ctx context.Context) (*Foo, error)`, 192 `func TupleToStruct(ctx context.Context, tuple octopus.TupleData) (*Foo, error) {`, 193 `procName string = "simpleProc"`, 194 `type Foo struct {`, 195 `type FooParams struct {`, 196 `package ` + packageName, 197 }, 198 "mock": { 199 `func (obj *Foo) RepoSelector(ctx context.Context) (any, error) {`, 200 `func CallMockerLogger(res FooList) func() (activerecord.MockerLogger, error) {`, 201 `func MockCallRequest(ctx context.Context) []byte {`, 202 `func (obj *Foo) MockSelectResponse() ([][]byte, error) {`, 203 }, 204 "fixture": { 205 `type FooFTPK struct {`, 206 `type FooFT struct {`, 207 `func MarshalFixtures(objs []*Foo) ([]byte, error) {`, 208 `func UnmarshalFromYaml(source []byte) []*Foo {`, 209 `func (objs FooList) String() string {`, 210 }, 211 }, 212 }, 213 { 214 name: "procPkg", 215 want: nil, 216 args: args{ 217 params: PkgData{ 218 ARPkg: packageName, 219 ARPkgTitle: "Foo", 220 FieldList: []ds.FieldDeclaration{}, 221 FieldMap: map[string]int{}, 222 ProcInFieldList: []ds.ProcFieldDeclaration{ 223 { 224 Name: "Input", 225 Format: "[]string", 226 Type: ds.IN, 227 Serializer: []string{}, 228 }, 229 { 230 Name: "InputOutput", 231 Format: "string", 232 Type: ds.OUT, 233 Serializer: []string{}, 234 }, 235 }, 236 ProcOutFieldList: []ds.ProcFieldDeclaration{ 237 { 238 Name: "InputOutput", 239 Format: "string", 240 Type: ds.OUT, 241 Serializer: []string{}, 242 }, 243 { 244 Name: "Output", 245 Format: "string", 246 Type: ds.INOUT, 247 Serializer: []string{"s2i"}, 248 }, 249 }, 250 Server: ds.ServerDeclaration{Timeout: 500, Host: "127.0.0.1", Port: "11011"}, 251 Container: ds.NamespaceDeclaration{ObjectName: "bar", PublicName: "Testmodel", PackageName: "testmodel"}, 252 Indexes: []ds.IndexDeclaration{}, 253 Serializers: map[string]ds.SerializerDeclaration{ 254 "s2i": { 255 Name: "Output", 256 Pkg: "github.com/mailru/activerecord/pkg/serializer", 257 Type: "int", 258 ImportName: "serializerOutput", 259 Marshaler: "OutputMarshal", 260 Unmarshaler: "OutputUnmarshal", 261 }, 262 }, 263 Imports: []ds.ImportDeclaration{}, 264 Triggers: map[string]ds.TriggerDeclaration{}, 265 Flags: map[string]ds.FlagDeclaration{}, 266 AppInfo: "", 267 }, 268 }, 269 wantStr: map[string][]string{ 270 "octopus": { 271 `Code generated by argen. DO NOT EDIT.`, 272 `func (obj *Foo) GetOutput() int {`, 273 `func (obj *Foo) GetInputOutput() string {`, 274 `func Call(ctx context.Context, params FooParams) (*Foo, error)`, 275 `func TupleToStruct(ctx context.Context, tuple octopus.TupleData) (*Foo, error) {`, 276 `procName string = "bar"`, 277 `type Foo struct {`, 278 `type FooParams struct {`, 279 `func (obj *FooParams) arrayValues() ([]string, error)`, 280 `package ` + packageName, 281 }, 282 "mock": { 283 `func (obj *Foo) RepoSelector(ctx context.Context) (any, error) {`, 284 `func CallMockerLogger(params FooParams, res FooList) func() (activerecord.MockerLogger, error) {`, 285 `func MockCallRequest(ctx context.Context, params FooParams) []byte {`, 286 `func (obj *Foo) MockSelectResponse() ([][]byte, error) {`, 287 }, 288 "fixture": { 289 `type FooFTPK struct {`, 290 `type FooFT struct {`, 291 `func MarshalFixtures(objs []*Foo) ([]byte, error) {`, 292 `func UnmarshalFromYaml(source []byte) []*Foo {`, 293 `func (objs FooList) String() string {`, 294 }, 295 }, 296 }, 297 } 298 299 for _, tt := range tests { 300 t.Run(tt.name, func(t *testing.T) { 301 ret, got := GenerateOctopus(tt.args.params) 302 if got != tt.want { 303 t.Errorf("GenerateOctopus() = %v, want %v", got, tt.want) 304 } 305 306 for name, strs := range tt.wantStr { 307 buff, ex := ret[name] 308 if !ex { 309 t.Errorf("GenerateOctopus() Name %s not generated", name) 310 return 311 } 312 313 for _, substr := range strs { 314 if !strings.Contains(buff.String(), substr) { 315 t.Errorf("GenerateOctopus() %s = %v, want %v", name, buff.String(), substr) 316 } 317 } 318 } 319 }) 320 } 321 }