Conditions | 5 |
Total Lines | 131 |
Code Lines | 88 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | package sqlite_test |
||
9 | func TestShouldBuildStructureWithSuccess(t *testing.T) { |
||
10 | tests := []struct { |
||
11 | columns []string |
||
12 | query string |
||
13 | rows [][]any |
||
14 | columnExpects []string |
||
15 | rowsExpects [][]any |
||
16 | }{ |
||
17 | { |
||
18 | columns: []string{"column_1", "column_2"}, |
||
19 | query: "select * from rows;", |
||
20 | rows: [][]any{ |
||
21 | {"value_1", "value_2"}, |
||
22 | }, |
||
23 | columnExpects: []string{"column_1", "column_2"}, |
||
24 | rowsExpects: [][]any{ |
||
25 | {"value_1", "value_2"}, |
||
26 | }, |
||
27 | }, |
||
28 | { |
||
29 | columns: []string{"column_1", "column_2", "column_3"}, |
||
30 | query: "select * from rows;", |
||
31 | rows: [][]any{ |
||
32 | {"value_1", "value_2", "value_3"}, |
||
33 | }, |
||
34 | columnExpects: []string{"column_1", "column_2", "column_3"}, |
||
35 | rowsExpects: [][]any{ |
||
36 | {"value_1", "value_2", "value_3"}, |
||
37 | }, |
||
38 | }, |
||
39 | { |
||
40 | columns: []string{"column_1", "column_2", "column_3"}, |
||
41 | query: "select * from rows;", |
||
42 | rows: [][]any{ |
||
43 | {"value_3", "value_1", "value_2"}, |
||
44 | }, |
||
45 | columnExpects: []string{"column_1", "column_2", "column_3"}, |
||
46 | rowsExpects: [][]any{ |
||
47 | {"value_3", "value_1", "value_2"}, |
||
48 | }, |
||
49 | }, |
||
50 | { |
||
51 | columns: []string{"column_1", "column_2", "column_3"}, |
||
52 | query: "select column_3, column_1, column_2 from rows;", |
||
53 | rows: [][]any{ |
||
54 | {"value_1", "value_2", "value_3"}, |
||
55 | }, |
||
56 | columnExpects: []string{"column_3", "column_1", "column_2"}, |
||
57 | rowsExpects: [][]any{ |
||
58 | {"value_3", "value_1", "value_2"}, |
||
59 | }, |
||
60 | }, |
||
61 | { |
||
62 | columns: []string{"column_1", "column_2", "column_3"}, |
||
63 | query: "select count(1) total from rows;", |
||
64 | rows: [][]any{ |
||
65 | {"value_1", "value_2", "value_3"}, |
||
66 | }, |
||
67 | columnExpects: []string{"total"}, |
||
68 | rowsExpects: [][]any{ |
||
69 | {int64(1)}, |
||
70 | }, |
||
71 | }, |
||
72 | { |
||
73 | columns: []string{"column_1", "column_2", "column_3"}, |
||
74 | query: "select count(1) total from rows;", |
||
75 | rows: [][]any{ |
||
76 | {"value_11", "value_21", "value_31"}, |
||
77 | {"value_12", "value_22", "value_32"}, |
||
78 | {"value_13", "value_23", "value_33"}, |
||
79 | }, |
||
80 | columnExpects: []string{"total"}, |
||
81 | rowsExpects: [][]any{ |
||
82 | {int64(3)}, |
||
83 | }, |
||
84 | }, |
||
85 | { |
||
86 | columns: []string{"column_1"}, |
||
87 | query: "select column_1, count(1) total from rows group by column_1;", |
||
88 | rows: [][]any{ |
||
89 | {"Value Test"}, |
||
90 | {"Value Test"}, |
||
91 | {"Value Test"}, |
||
92 | }, |
||
93 | columnExpects: []string{"column_1", "total"}, |
||
94 | rowsExpects: [][]any{ |
||
95 | {"Value Test", int64(3)}, |
||
96 | }, |
||
97 | }, |
||
98 | } |
||
99 | |||
100 | for _, test := range tests { |
||
101 | storage, err := sqlite.NewSqLiteStorage(":memory:") |
||
102 | assert.NoError(t, err) |
||
103 | |||
104 | err = storage.SetColumns(test.columns).BuildStructure() |
||
105 | assert.NoError(t, err) |
||
106 | |||
107 | for _, row := range test.rows { |
||
108 | err = storage.InsertRow(row) |
||
109 | assert.NoError(t, err) |
||
110 | } |
||
111 | |||
112 | rows, err := storage.Query(test.query) |
||
113 | assert.NoError(t, err) |
||
114 | |||
115 | cols, err := rows.Columns() |
||
116 | assert.NoError(t, err) |
||
117 | assert.Equal(t, test.columnExpects, cols) |
||
118 | |||
119 | for _, expected := range test.rowsExpects { |
||
120 | rs := rows.Next() |
||
121 | assert.True(t, rs) |
||
122 | |||
123 | values := make([]interface{}, len(test.columnExpects)) |
||
124 | pointers := make([]interface{}, len(test.columnExpects)) |
||
125 | for i := range values { |
||
126 | pointers[i] = &values[i] |
||
127 | } |
||
128 | |||
129 | err = rows.Scan(pointers...) |
||
130 | assert.NoError(t, err) |
||
131 | |||
132 | assert.Equal(t, expected, values) |
||
133 | } |
||
134 | |||
135 | err = rows.Close() |
||
136 | assert.NoError(t, err) |
||
137 | |||
138 | err = storage.Close() |
||
139 | assert.NoError(t, err) |
||
140 | } |
||
142 |