|
| 1 | +// Copyright © 2025 Kaleido, Inc. |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | + |
| 17 | +package dbsql |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/DATA-DOG/go-sqlmock" |
| 24 | + sq "github.com/Masterminds/squirrel" |
| 25 | + "github.com/hyperledger/firefly-common/pkg/config" |
| 26 | + "github.com/hyperledger/firefly-common/pkg/fftypes" |
| 27 | + "github.com/stretchr/testify/assert" |
| 28 | +) |
| 29 | + |
| 30 | +func init() { |
| 31 | + config.RootSection("").AddKnownKey(SQLConfHistogramsMaxChartRows, 100) |
| 32 | +} |
| 33 | + |
| 34 | +func newIntervals(n int) []fftypes.ChartHistogramInterval { |
| 35 | + ints := make([]fftypes.ChartHistogramInterval, 0, n) |
| 36 | + for i := 0; i < n; i++ { |
| 37 | + ints = append(ints, fftypes.ChartHistogramInterval{ |
| 38 | + StartTime: fftypes.Now(), |
| 39 | + EndTime: fftypes.Now(), |
| 40 | + }) |
| 41 | + } |
| 42 | + return ints |
| 43 | +} |
| 44 | + |
| 45 | +func TestBuildHistogramQueriesWithTypeAndNamespace(t *testing.T) { |
| 46 | + db, _ := NewMockProvider().UTInit() |
| 47 | + |
| 48 | + i := newIntervals(2) |
| 49 | + |
| 50 | + queries := db.buildHistogramQueries( |
| 51 | + "mytable", |
| 52 | + "created", |
| 53 | + "type", |
| 54 | + "namespace", |
| 55 | + "ns1", |
| 56 | + i, |
| 57 | + 10, |
| 58 | + ) |
| 59 | + |
| 60 | + assert.Len(t, queries, 2) |
| 61 | + |
| 62 | + sqlStr, args, err := queries[0].ToSql() |
| 63 | + assert.NoError(t, err) |
| 64 | + |
| 65 | + assert.Regexp(t, |
| 66 | + `SELECT created, type FROM mytable WHERE \(created >= .+ AND created < .+ AND namespace = .+\) ORDER BY created LIMIT 10`, |
| 67 | + sqlStr, |
| 68 | + ) |
| 69 | + |
| 70 | + assert.Len(t, args, 3) |
| 71 | + assert.NotNil(t, args[0]) |
| 72 | + assert.NotNil(t, args[1]) |
| 73 | + assert.Equal(t, "ns1", args[2]) |
| 74 | +} |
| 75 | + |
| 76 | +func TestBuildHistogramQueriesWithoutTypeOrNamespace(t *testing.T) { |
| 77 | + db, _ := NewMockProvider().UTInit() |
| 78 | + |
| 79 | + i := newIntervals(1) |
| 80 | + |
| 81 | + queries := db.buildHistogramQueries( |
| 82 | + "mytable", |
| 83 | + "created", |
| 84 | + "", |
| 85 | + "", |
| 86 | + "", |
| 87 | + i, |
| 88 | + 5, |
| 89 | + ) |
| 90 | + |
| 91 | + assert.Len(t, queries, 1) |
| 92 | + |
| 93 | + sqlStr, args, err := queries[0].ToSql() |
| 94 | + assert.NoError(t, err) |
| 95 | + |
| 96 | + assert.Regexp(t, |
| 97 | + `SELECT created FROM mytable WHERE \(created >= .+ AND created < .+\) ORDER BY created LIMIT 5`, |
| 98 | + sqlStr, |
| 99 | + ) |
| 100 | + |
| 101 | + assert.Len(t, args, 2) |
| 102 | + assert.NotNil(t, args[0]) |
| 103 | + assert.NotNil(t, args[1]) |
| 104 | +} |
| 105 | + |
| 106 | +func TestProcessHistogramRowsNoTypeColumn(t *testing.T) { |
| 107 | + db, mock := NewMockProvider().UTInit() |
| 108 | + ctx := context.Background() |
| 109 | + |
| 110 | + mock.ExpectQuery("SELECT.*FROM mytable"). |
| 111 | + WillReturnRows( |
| 112 | + sqlmock.NewRows([]string{"created"}). |
| 113 | + AddRow("2024-01-01T00:00:00Z"). |
| 114 | + AddRow("2024-01-02T00:00:00Z"), |
| 115 | + ) |
| 116 | + |
| 117 | + r, _, e := db.Query(ctx, "mytable", sq.Select("created").From("mytable")) |
| 118 | + assert.NoError(t, e) |
| 119 | + defer r.Close() |
| 120 | + |
| 121 | + typeMap, total, e := db.processHistogramRows(ctx, "mytable", r, false) |
| 122 | + assert.NoError(t, e) |
| 123 | + assert.Equal(t, int64(2), total) |
| 124 | + assert.Empty(t, typeMap) |
| 125 | + |
| 126 | + assert.NoError(t, mock.ExpectationsWereMet()) |
| 127 | +} |
| 128 | + |
| 129 | +func TestProcessHistogramRowsWithTypeColumn(t *testing.T) { |
| 130 | + db, mock := NewMockProvider().UTInit() |
| 131 | + ctx := context.Background() |
| 132 | + |
| 133 | + mock.ExpectQuery("SELECT.*FROM mytable"). |
| 134 | + WillReturnRows( |
| 135 | + sqlmock.NewRows([]string{"created", "type"}). |
| 136 | + AddRow("2024-01-01T00:00:00Z", "A"). |
| 137 | + AddRow("2024-01-01T00:01:00Z", "B"). |
| 138 | + AddRow("2024-01-01T00:02:00Z", "A"), |
| 139 | + ) |
| 140 | + |
| 141 | + rows, _, e := db.Query(ctx, "mytable", sq.Select("created", "type").From("mytable")) |
| 142 | + assert.NoError(t, e) |
| 143 | + defer rows.Close() |
| 144 | + |
| 145 | + typeMap, total, e := db.processHistogramRows(ctx, "mytable", rows, true) |
| 146 | + assert.NoError(t, e) |
| 147 | + assert.Equal(t, int64(3), total) |
| 148 | + assert.Equal(t, map[string]int{ |
| 149 | + "A": 2, |
| 150 | + "B": 1, |
| 151 | + }, typeMap) |
| 152 | + |
| 153 | + assert.NoError(t, mock.ExpectationsWereMet()) |
| 154 | +} |
| 155 | + |
| 156 | +func TestProcessHistogramRowsScanErrorwithoutTypeColumn(t *testing.T) { |
| 157 | + db, mock := NewMockProvider().UTInit() |
| 158 | + ctx := context.Background() |
| 159 | + |
| 160 | + mock.ExpectQuery("SELECT.*FROM mytable"). |
| 161 | + WillReturnRows( |
| 162 | + sqlmock.NewRows([]string{"created", "extra"}). |
| 163 | + AddRow("2024-01-01T00:00:00Z", "x"), |
| 164 | + ) |
| 165 | + |
| 166 | + r, _, err := db.Query(ctx, "mytable", sq.Select("created", "extra").From("mytable")) |
| 167 | + assert.NoError(t, err) |
| 168 | + defer r.Close() |
| 169 | + |
| 170 | + _, _, err = db.processHistogramRows(ctx, "mytable", r, false) |
| 171 | + assert.Error(t, err) |
| 172 | + |
| 173 | + assert.NoError(t, mock.ExpectationsWereMet()) |
| 174 | +} |
| 175 | + |
| 176 | +func TestProcessHistogramRowsScanErrorWithTypeColumn(t *testing.T) { |
| 177 | + db, mock := NewMockProvider().UTInit() |
| 178 | + ctx := context.Background() |
| 179 | + |
| 180 | + mock.ExpectQuery("SELECT.*FROM mytable"). |
| 181 | + WillReturnRows( |
| 182 | + sqlmock.NewRows([]string{"created"}). |
| 183 | + AddRow("2024-01-01T00:00:00Z"), |
| 184 | + ) |
| 185 | + |
| 186 | + r, _, e := db.Query(ctx, "mytable", sq.Select("created").From("mytable")) |
| 187 | + assert.NoError(t, e) |
| 188 | + defer r.Close() |
| 189 | + |
| 190 | + _, _, e = db.processHistogramRows(ctx, "mytable", r, true) |
| 191 | + assert.Error(t, e) |
| 192 | + |
| 193 | + assert.NoError(t, mock.ExpectationsWereMet()) |
| 194 | +} |
| 195 | + |
| 196 | +func TestGetChartHistogramNoTypeColumn(t *testing.T) { |
| 197 | + db, mock := NewMockProvider().UTInit() |
| 198 | + ctx := context.Background() |
| 199 | + |
| 200 | + i := newIntervals(1) |
| 201 | + |
| 202 | + mock.ExpectQuery("SELECT.*FROM hist_table"). |
| 203 | + WillReturnRows( |
| 204 | + sqlmock.NewRows([]string{"created"}). |
| 205 | + AddRow("2024-01-01T00:00:00Z"). |
| 206 | + AddRow("2024-01-01T00:01:00Z"). |
| 207 | + AddRow("2024-01-01T00:02:00Z"), |
| 208 | + ) |
| 209 | + |
| 210 | + hist, err := db.GetChartHistogram( |
| 211 | + ctx, |
| 212 | + "hist_table", |
| 213 | + "created", |
| 214 | + "", |
| 215 | + "", |
| 216 | + "", |
| 217 | + i, |
| 218 | + ) |
| 219 | + assert.NoError(t, err) |
| 220 | + assert.Len(t, hist, 1) |
| 221 | + |
| 222 | + assert.Equal(t, "3", hist[0].Count) |
| 223 | + assert.Equal(t, i[0].StartTime, hist[0].Timestamp) |
| 224 | + assert.Empty(t, hist[0].Types) |
| 225 | + |
| 226 | + assert.NoError(t, mock.ExpectationsWereMet()) |
| 227 | +} |
| 228 | + |
| 229 | +func TestGetChartHistogramWithTypeColumn(t *testing.T) { |
| 230 | + db, mock := NewMockProvider().UTInit() |
| 231 | + ctx := context.Background() |
| 232 | + |
| 233 | + i := newIntervals(2) |
| 234 | + |
| 235 | + // two rows A type |
| 236 | + mock.ExpectQuery("SELECT.*FROM hist_table"). |
| 237 | + WillReturnRows( |
| 238 | + sqlmock.NewRows([]string{"created", "type"}). |
| 239 | + AddRow("2024-01-01T00:00:00Z", "A"). |
| 240 | + AddRow("2024-01-01T00:01:00Z", "A"), |
| 241 | + ) |
| 242 | + |
| 243 | + // one A and one B |
| 244 | + mock.ExpectQuery("SELECT.*FROM hist_table"). |
| 245 | + WillReturnRows( |
| 246 | + sqlmock.NewRows([]string{"created", "type"}). |
| 247 | + AddRow("2024-01-01T00:02:00Z", "A"). |
| 248 | + AddRow("2024-01-01T00:03:00Z", "B"), |
| 249 | + ) |
| 250 | + |
| 251 | + hist, err := db.GetChartHistogram( |
| 252 | + ctx, |
| 253 | + "hist_table", |
| 254 | + "created", |
| 255 | + "type", |
| 256 | + "", |
| 257 | + "", |
| 258 | + i, |
| 259 | + ) |
| 260 | + assert.NoError(t, err) |
| 261 | + assert.Len(t, hist, 2) |
| 262 | + |
| 263 | + // only A |
| 264 | + assert.Equal(t, "2", hist[0].Count) |
| 265 | + assert.Equal(t, i[0].StartTime, hist[0].Timestamp) |
| 266 | + assert.Len(t, hist[0].Types, 1) |
| 267 | + assert.Equal(t, "A", hist[0].Types[0].Type) |
| 268 | + assert.Equal(t, "2", hist[0].Types[0].Count) |
| 269 | + |
| 270 | + // A=1 B=1 |
| 271 | + assert.Equal(t, "2", hist[1].Count) |
| 272 | + assert.Equal(t, i[1].StartTime, hist[1].Timestamp) |
| 273 | + |
| 274 | + typeCounts := map[string]string{} |
| 275 | + for _, ty := range hist[1].Types { |
| 276 | + typeCounts[ty.Type] = ty.Count |
| 277 | + } |
| 278 | + assert.Equal(t, map[string]string{"A": "1", "B": "1"}, typeCounts) |
| 279 | + |
| 280 | + assert.NoError(t, mock.ExpectationsWereMet()) |
| 281 | +} |
| 282 | + |
| 283 | +func TestGetChartHistogramQueryError(t *testing.T) { |
| 284 | + db, mock := NewMockProvider().UTInit() |
| 285 | + ctx := context.Background() |
| 286 | + |
| 287 | + i := newIntervals(1) |
| 288 | + |
| 289 | + mock.ExpectQuery("SELECT.*FROM hist_table"). |
| 290 | + WillReturnError(assert.AnError) |
| 291 | + |
| 292 | + _, err := db.GetChartHistogram( |
| 293 | + ctx, |
| 294 | + "hist_table", |
| 295 | + "created", |
| 296 | + "type", |
| 297 | + "", "", |
| 298 | + i, |
| 299 | + ) |
| 300 | + assert.Error(t, err) |
| 301 | + |
| 302 | + assert.NoError(t, mock.ExpectationsWereMet()) |
| 303 | +} |
| 304 | + |
| 305 | +func TestGetChartHistogramProcessError(t *testing.T) { |
| 306 | + db, mock := NewMockProvider().UTInit() |
| 307 | + ctx := context.Background() |
| 308 | + |
| 309 | + i := newIntervals(1) |
| 310 | + |
| 311 | + mock.ExpectQuery("SELECT.*FROM hist_table"). |
| 312 | + WillReturnRows( |
| 313 | + sqlmock.NewRows([]string{"created"}). |
| 314 | + AddRow("2024-01-01T00:00:00Z"), |
| 315 | + ) |
| 316 | + |
| 317 | + _, e := db.GetChartHistogram( |
| 318 | + ctx, |
| 319 | + "hist_table", |
| 320 | + "created", |
| 321 | + "type", |
| 322 | + "", "", |
| 323 | + i, |
| 324 | + ) |
| 325 | + assert.Error(t, e) |
| 326 | + |
| 327 | + assert.NoError(t, mock.ExpectationsWereMet()) |
| 328 | +} |
0 commit comments