|
| 1 | +package dotsql |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "reflect" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +type PrepareCalls []struct { |
| 10 | + Query string |
| 11 | +} |
| 12 | + |
| 13 | +func comparePrepareCalls(t *testing.T, ff PrepareCalls, template string) bool { |
| 14 | + t.Helper() |
| 15 | + if len(ff) != 1 { |
| 16 | + t.Errorf("prepare was expected to be called only once, but was called %d times", len(ff)) |
| 17 | + return false |
| 18 | + } else if ff[0].Query != template { |
| 19 | + t.Errorf("prepare was expected to be called with %q query, got %q", template, ff[0].Query) |
| 20 | + return false |
| 21 | + } |
| 22 | + return true |
| 23 | +} |
| 24 | + |
| 25 | +type PrepareContextCalls []struct { |
| 26 | + Ctx context.Context |
| 27 | + Query string |
| 28 | +} |
| 29 | + |
| 30 | +func comparePrepareContextCalls(t *testing.T, ff PrepareContextCalls, ctx context.Context, template string) bool { |
| 31 | + t.Helper() |
| 32 | + if len(ff) != 1 { |
| 33 | + t.Errorf("prepare was expected to be called only once, but was called %d times", len(ff)) |
| 34 | + return false |
| 35 | + } else if ff[0].Query != template { |
| 36 | + t.Errorf("prepare was expected to be called with %q query, got %q", template, ff[0].Query) |
| 37 | + return false |
| 38 | + } else if !reflect.DeepEqual(ff[0].Ctx, ctx) { |
| 39 | + t.Error("prepare context does not match") |
| 40 | + return false |
| 41 | + } |
| 42 | + return true |
| 43 | +} |
| 44 | + |
| 45 | +type QueryCalls []struct { |
| 46 | + Query string |
| 47 | + Args []interface{} |
| 48 | +} |
| 49 | + |
| 50 | +func compareCalls(t *testing.T, ff QueryCalls, command, template, testArg string) bool { |
| 51 | + t.Helper() |
| 52 | + if len(ff) != 1 { |
| 53 | + t.Errorf("%s was expected to be called only once, but was called %d times", command, len(ff)) |
| 54 | + return false |
| 55 | + } else if ff[0].Query != template { |
| 56 | + t.Errorf("%s was expected to be called with %q query, got %q", command, template, ff[0].Query) |
| 57 | + return false |
| 58 | + } else if len(ff[0].Args) != 1 { |
| 59 | + t.Errorf("%s was expected to be called with 1 argument, got %d", command, len(ff[0].Args)) |
| 60 | + return false |
| 61 | + } else if !reflect.DeepEqual(ff[0].Args[0], testArg) { |
| 62 | + t.Errorf("%s was expected to be called with %q argument, got %v", command, testArg, ff[0].Args[0]) |
| 63 | + return false |
| 64 | + } |
| 65 | + return true |
| 66 | +} |
| 67 | + |
| 68 | +type QueryContextCalls []struct { |
| 69 | + Ctx context.Context |
| 70 | + Query string |
| 71 | + Args []interface{} |
| 72 | +} |
| 73 | + |
| 74 | +func compareContextCalls(t *testing.T, ff QueryContextCalls, ctx context.Context, command, template, testArg string) bool { |
| 75 | + t.Helper() |
| 76 | + if len(ff) != 1 { |
| 77 | + t.Errorf("%s was expected to be called only once, but was called %d times", command, len(ff)) |
| 78 | + return false |
| 79 | + } else if ff[0].Query != template { |
| 80 | + t.Errorf("%s was expected to be called with %q query, got %q", command, template, ff[0].Query) |
| 81 | + return false |
| 82 | + } else if len(ff[0].Args) != 1 { |
| 83 | + t.Errorf("%s was expected to be called with 1 argument, got %d", command, len(ff[0].Args)) |
| 84 | + return false |
| 85 | + } else if !reflect.DeepEqual(ff[0].Args[0], testArg) { |
| 86 | + t.Errorf("%s was expected to be called with %q argument, got %v", command, testArg, ff[0].Args[0]) |
| 87 | + return false |
| 88 | + } else if !reflect.DeepEqual(ff[0].Ctx, ctx) { |
| 89 | + t.Errorf("%s context does not match", command) |
| 90 | + return false |
| 91 | + } |
| 92 | + return true |
| 93 | +} |
0 commit comments