| Conditions | 7 | 
| Total Lines | 82 | 
| Code Lines | 58 | 
| 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 repository  | 
            ||
| 147 | func TestBookingPostgres_GetByRoomId(t *testing.T) { | 
            ||
| 148 | db, mock, err := sqlmock.Newx()  | 
            ||
| 149 | 	if err != nil { | 
            ||
| 150 | 		t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) | 
            ||
| 151 | }  | 
            ||
| 152 | defer db.Close()  | 
            ||
| 153 | |||
| 154 | r := NewBookingPostgres(db)  | 
            ||
| 155 | |||
| 156 | 	type args struct { | 
            ||
| 157 | roomId int  | 
            ||
| 158 | }  | 
            ||
| 159 | type mockBehavior func(args args)  | 
            ||
| 160 | |||
| 161 | 	tests := []struct { | 
            ||
| 162 | name string  | 
            ||
| 163 | mock mockBehavior  | 
            ||
| 164 | input args  | 
            ||
| 165 | want []*model.Booking  | 
            ||
| 166 | wantErr bool  | 
            ||
| 167 | 	}{ | 
            ||
| 168 | 		{ | 
            ||
| 169 | name: "Ok",  | 
            ||
| 170 | 			input: args{ | 
            ||
| 171 | roomId: 1,  | 
            ||
| 172 | },  | 
            ||
| 173 | 			mock: func(args args) { | 
            ||
| 174 | dateStart1 := time.Date(2021, time.January, 5, 0, 0, 0, 0, time.UTC)  | 
            ||
| 175 | dateEnd1 := time.Date(2021, time.January, 8, 0, 0, 0, 0, time.UTC)  | 
            ||
| 176 | |||
| 177 | dateStart2 := time.Date(2021, time.January, 25, 0, 0, 0, 0, time.UTC)  | 
            ||
| 178 | dateEnd2 := time.Date(2021, time.January, 28, 0, 0, 0, 0, time.UTC)  | 
            ||
| 179 | |||
| 180 | 				rows := sqlmock.NewRows([]string{"id", "room_id", "date_start", "date_end"}). | 
            ||
| 181 | AddRow(1, 1, dateStart1, dateEnd1).  | 
            ||
| 182 | AddRow(2, 1, dateStart2, dateEnd2)  | 
            ||
| 183 | |||
| 184 | 				mock.ExpectQuery(fmt.Sprintf("SELECT (.+) FROM %s WHERE (.+)", bookingsTable)). | 
            ||
| 185 | WithArgs(args.roomId).WillReturnRows(rows)  | 
            ||
| 186 | },  | 
            ||
| 187 | 			want: []*model.Booking{ | 
            ||
| 188 | 				{ | 
            ||
| 189 | Id: 1,  | 
            ||
| 190 | RoomId: 1,  | 
            ||
| 191 | DateStart: time.Date(2021, time.January, 5, 0, 0, 0, 0, time.UTC),  | 
            ||
| 192 | DateEnd: time.Date(2021, time.January, 8, 0, 0, 0, 0, time.UTC),  | 
            ||
| 193 | },  | 
            ||
| 194 | 				{ | 
            ||
| 195 | Id: 2,  | 
            ||
| 196 | RoomId: 1,  | 
            ||
| 197 | DateStart: time.Date(2021, time.January, 25, 0, 0, 0, 0, time.UTC),  | 
            ||
| 198 | DateEnd: time.Date(2021, time.January, 28, 0, 0, 0, 0, time.UTC),  | 
            ||
| 199 | },  | 
            ||
| 200 | },  | 
            ||
| 201 | wantErr: false,  | 
            ||
| 202 | },  | 
            ||
| 203 | 		{ | 
            ||
| 204 | name: "Ok Empty List",  | 
            ||
| 205 | 			input: args{ | 
            ||
| 206 | roomId: 1,  | 
            ||
| 207 | },  | 
            ||
| 208 | 			mock: func(args args) { | 
            ||
| 209 | 				rows := sqlmock.NewRows([]string{"id", "room_id", "date_start", "date_end"}) | 
            ||
| 210 | |||
| 211 | 				mock.ExpectQuery(fmt.Sprintf("SELECT (.+) FROM %s WHERE (.+)", bookingsTable)). | 
            ||
| 212 | WithArgs(args.roomId).WillReturnRows(rows)  | 
            ||
| 213 | },  | 
            ||
| 214 | want: nil,  | 
            ||
| 215 | wantErr: false,  | 
            ||
| 216 | },  | 
            ||
| 217 | }  | 
            ||
| 218 | |||
| 219 | 	for _, test := range tests { | 
            ||
| 220 | 		t.Run(test.name, func(t *testing.T) { | 
            ||
| 221 | test.mock(test.input)  | 
            ||
| 222 | |||
| 223 | got, err := r.GetByRoomId(test.input.roomId)  | 
            ||
| 224 | 			if test.wantErr { | 
            ||
| 225 | assert.Error(t, err)  | 
            ||
| 226 | 			} else { | 
            ||
| 227 | assert.NoError(t, err)  | 
            ||
| 228 | assert.Equal(t, test.want, got)  | 
            ||
| 229 | }  | 
            ||
| 306 |