|
| 1 | +package mysql |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/google/go-cmp/cmp" |
| 7 | + "github.com/kyleconroy/sqlc/internal/dinosql" |
| 8 | + "github.com/kyleconroy/sqlc/internal/pg" |
| 9 | + "vitess.io/vitess/go/vt/sqlparser" |
| 10 | +) |
| 11 | + |
| 12 | +func TestArgName(t *testing.T) { |
| 13 | + tcase := [...]struct { |
| 14 | + input string |
| 15 | + output string |
| 16 | + }{ |
| 17 | + { |
| 18 | + input: "get_users", |
| 19 | + output: "getUsers", |
| 20 | + }, |
| 21 | + { |
| 22 | + input: "get_users_by_id", |
| 23 | + output: "getUsersByID", |
| 24 | + }, |
| 25 | + { |
| 26 | + input: "get_all_", |
| 27 | + output: "getAll", |
| 28 | + }, |
| 29 | + } |
| 30 | + |
| 31 | + for _, tc := range tcase { |
| 32 | + name := argName(tc.input) |
| 33 | + if diff := cmp.Diff(name, tc.output); diff != "" { |
| 34 | + t.Errorf(diff) |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | +func TestEnumName(t *testing.T) { |
| 39 | + tcase := [...]struct { |
| 40 | + input sqlparser.ColumnDefinition |
| 41 | + output string |
| 42 | + }{ |
| 43 | + { |
| 44 | + input: sqlparser.ColumnDefinition{Name: sqlparser.NewColIdent("first_name")}, |
| 45 | + output: "FirstNameType", |
| 46 | + }, |
| 47 | + { |
| 48 | + input: sqlparser.ColumnDefinition{Name: sqlparser.NewColIdent("user_id")}, |
| 49 | + output: "UserIDType", |
| 50 | + }, |
| 51 | + { |
| 52 | + input: sqlparser.ColumnDefinition{Name: sqlparser.NewColIdent("last_name")}, |
| 53 | + output: "LastNameType", |
| 54 | + }, |
| 55 | + } |
| 56 | + |
| 57 | + for _, tc := range tcase { |
| 58 | + enumName := enumNameFromColDef(&tc.input, mockSettings) |
| 59 | + if diff := cmp.Diff(enumName, tc.output); diff != "" { |
| 60 | + t.Errorf(diff) |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func TestEnums(t *testing.T) { |
| 66 | + tcase := [...]struct { |
| 67 | + input Result |
| 68 | + output []dinosql.GoEnum |
| 69 | + }{ |
| 70 | + { |
| 71 | + input: Result{Schema: mockSchema}, |
| 72 | + output: []dinosql.GoEnum{ |
| 73 | + { |
| 74 | + Name: "JobStatusType", |
| 75 | + Constants: []dinosql.GoConstant{ |
| 76 | + {Name: "applied", Type: "JobStatusType", Value: "applied"}, |
| 77 | + {Name: "pending", Type: "JobStatusType", Value: "pending"}, |
| 78 | + {Name: "accepted", Type: "JobStatusType", Value: "accepted"}, |
| 79 | + {Name: "rejected", Type: "JobStatusType", Value: "rejected"}, |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + }, |
| 84 | + } |
| 85 | + for _, tc := range tcase { |
| 86 | + enums := tc.input.Enums(mockSettings) |
| 87 | + if diff := cmp.Diff(enums, tc.output); diff != "" { |
| 88 | + t.Errorf(diff) |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +func TestStructs(t *testing.T) { |
| 94 | + tcase := [...]struct { |
| 95 | + input Result |
| 96 | + output []dinosql.GoStruct |
| 97 | + }{ |
| 98 | + { |
| 99 | + input: Result{Schema: mockSchema}, |
| 100 | + output: []dinosql.GoStruct{ |
| 101 | + { |
| 102 | + Table: pg.FQN{Catalog: "orders"}, |
| 103 | + Name: "Order", |
| 104 | + Fields: []dinosql.GoField{ |
| 105 | + {Name: "ID", Type: "int", Tags: map[string]string{"json:": "id"}}, |
| 106 | + {Name: "Price", Type: "float64", Tags: map[string]string{"json:": "price"}}, |
| 107 | + {Name: "UserID", Type: "int", Tags: map[string]string{"json:": "user_id"}}, |
| 108 | + }, |
| 109 | + }, |
| 110 | + { |
| 111 | + Table: pg.FQN{Catalog: "users"}, |
| 112 | + Name: "User", |
| 113 | + Fields: []dinosql.GoField{ |
| 114 | + {Name: "FirstName", Type: "string", Tags: map[string]string{"json:": "first_name"}}, |
| 115 | + {Name: "LastName", Type: "sql.NullString", Tags: map[string]string{"json:": "last_name"}}, |
| 116 | + {Name: "ID", Type: "int", Tags: map[string]string{"json:": "id"}}, |
| 117 | + {Name: "Age", Type: "int", Tags: map[string]string{"json:": "age"}}, |
| 118 | + {Name: "JobStatus", Type: "JobStatusType", Tags: map[string]string{"json:": "job_status"}}, |
| 119 | + }}, |
| 120 | + }, |
| 121 | + }, |
| 122 | + } |
| 123 | + |
| 124 | + for _, tc := range tcase { |
| 125 | + structs := tc.input.Structs(mockSettings) |
| 126 | + if diff := cmp.Diff(structs, tc.output); diff != "" { |
| 127 | + t.Errorf(diff) |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments