|
| 1 | +package ocsql |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "database/sql/driver" |
| 6 | + "errors" |
| 7 | + "io" |
| 8 | + "reflect" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +var errDummy = errors.New("dummy") |
| 13 | + |
| 14 | +type stubRows struct{} |
| 15 | + |
| 16 | +func (stubRows) Columns() []string { return []string{"dummy"} } |
| 17 | +func (stubRows) Close() error { return errDummy } |
| 18 | +func (stubRows) Next([]driver.Value) error { return errDummy } |
| 19 | +func (stubRows) HasNextResultSet() bool { return true } |
| 20 | +func (stubRows) NextResultSet() error { return errDummy } |
| 21 | +func (stubRows) ColumnTypeScanType(int) reflect.Type { return reflect.TypeOf(stubRows{}) } |
| 22 | +func (stubRows) ColumnTypeDatabaseTypeName(index int) string { return "dummy" } |
| 23 | +func (stubRows) ColumnTypeLength(index int) (length int64, ok bool) { return 1, true } |
| 24 | +func (stubRows) ColumnTypeNullable(index int) (nullable, ok bool) { return true, true } |
| 25 | +func (stubRows) ColumnTypePrecisionScale(index int) (precision, scale int64, ok bool) { |
| 26 | + return 1, 1, true |
| 27 | +} |
| 28 | + |
| 29 | +func TestWrappingTransparency(t *testing.T) { |
| 30 | + var ( |
| 31 | + ctx = context.Background() |
| 32 | + oRows = &stubRows{} |
| 33 | + wRows = wrapRows(ctx, oRows, AllTraceOptions) |
| 34 | + ) |
| 35 | + |
| 36 | + if want, have := oRows.Columns(), wRows.Columns(); len(want) != len(have) { |
| 37 | + t.Errorf("rows.Column want: %v, have: %v", want, have) |
| 38 | + } |
| 39 | + |
| 40 | + if want, have := oRows.Close(), wRows.Close(); want != have { |
| 41 | + t.Errorf("rows.Close want: %v, have: %v", want, have) |
| 42 | + } |
| 43 | + |
| 44 | + if want, have := oRows.Next(nil), wRows.Next(nil); want != have { |
| 45 | + t.Errorf("rows.Next want: %v, have: %v", want, have) |
| 46 | + } |
| 47 | + |
| 48 | + if want, have := oRows.HasNextResultSet(), wRows.(driver.RowsNextResultSet).HasNextResultSet(); want != have { |
| 49 | + t.Errorf("rows.HasNextResultSet want: %t, have: %t", want, have) |
| 50 | + } |
| 51 | + |
| 52 | + if want, have := oRows.NextResultSet(), wRows.(driver.RowsNextResultSet).NextResultSet(); want != have { |
| 53 | + t.Errorf("rows.NextResultSet want: %v, have: %v", want, have) |
| 54 | + } |
| 55 | + |
| 56 | + if want, have := oRows.ColumnTypeScanType(1), wRows.(driver.RowsColumnTypeScanType).ColumnTypeScanType(1); want != have { |
| 57 | + t.Errorf("rows.ColumnTypeScanType want: %v, have: %v", want, have) |
| 58 | + } |
| 59 | + |
| 60 | + if want, have := oRows.ColumnTypeDatabaseTypeName(1), wRows.(driver.RowsColumnTypeDatabaseTypeName).ColumnTypeDatabaseTypeName(1); want != have { |
| 61 | + t.Errorf("rows.ColumnTypeDatabaseTypeName want: %s, have: %s", want, have) |
| 62 | + } |
| 63 | + |
| 64 | + oLength, oOk := oRows.ColumnTypeLength(1) |
| 65 | + wLength, wOk := wRows.(driver.RowsColumnTypeLength).ColumnTypeLength(1) |
| 66 | + if oLength != wLength || oOk != wOk { |
| 67 | + t.Errorf("rows.ColumnTypeLength want: %d:%t, have %d:%t", oLength, oOk, wLength, wOk) |
| 68 | + } |
| 69 | + |
| 70 | + oNullable, oOk := oRows.ColumnTypeNullable(1) |
| 71 | + wNullable, wOk := wRows.(driver.RowsColumnTypeNullable).ColumnTypeNullable(1) |
| 72 | + if oNullable != wNullable || oOk != wOk { |
| 73 | + t.Errorf("rows.ColumnTypeNullable want: %t:%t, have %t:%t", oNullable, oOk, wNullable, wOk) |
| 74 | + } |
| 75 | + |
| 76 | + oPrecision, oScale, oOk := oRows.ColumnTypePrecisionScale(1) |
| 77 | + wPrecision, wScale, wOk := wRows.(driver.RowsColumnTypePrecisionScale).ColumnTypePrecisionScale(1) |
| 78 | + if oPrecision != wPrecision || oScale != wScale || oOk != wOk { |
| 79 | + t.Errorf("rows.ColumnTypePrecisionScale want: %d:%d:%t, have %d:%d:%t", oPrecision, oScale, oOk, wPrecision, wScale, wOk) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func TestWrappingFallback(t *testing.T) { |
| 84 | + var ( |
| 85 | + ctx = context.Background() |
| 86 | + oRows = struct{ driver.Rows }{&stubRows{}} |
| 87 | + wRows = wrapRows(ctx, oRows, AllTraceOptions) |
| 88 | + ) |
| 89 | + |
| 90 | + if want, have := oRows.Columns(), wRows.Columns(); len(want) != len(have) { |
| 91 | + t.Errorf("rows.Column want: %v, have: %v", want, have) |
| 92 | + } |
| 93 | + |
| 94 | + if want, have := oRows.Close(), wRows.Close(); want != have { |
| 95 | + t.Errorf("rows.Close want: %v, have: %v", want, have) |
| 96 | + } |
| 97 | + |
| 98 | + if want, have := oRows.Next(nil), wRows.Next(nil); want != have { |
| 99 | + t.Errorf("rows.Next want: %v, have: %v", want, have) |
| 100 | + } |
| 101 | + |
| 102 | + if want, have := false, wRows.(driver.RowsNextResultSet).HasNextResultSet(); want != have { |
| 103 | + t.Errorf("rows.HasNextResultSet want: %t, have: %t", want, have) |
| 104 | + } |
| 105 | + |
| 106 | + if want, have := io.EOF, wRows.(driver.RowsNextResultSet).NextResultSet(); want != have { |
| 107 | + t.Errorf("rows.NextResultSet want: %v, have: %v", want, have) |
| 108 | + } |
| 109 | + |
| 110 | + if _, ok := wRows.(driver.RowsColumnTypeScanType); ok { |
| 111 | + t.Error("rows.ColumnTypeScanType unexpected interface implementation found") |
| 112 | + } |
| 113 | + |
| 114 | + if want, have := "", wRows.(driver.RowsColumnTypeDatabaseTypeName).ColumnTypeDatabaseTypeName(1); want != have { |
| 115 | + t.Errorf("rows.ColumnTypeDatabaseTypeName want: %s, have: %s", want, have) |
| 116 | + } |
| 117 | + |
| 118 | + oLength, oOk := int64(0), false |
| 119 | + wLength, wOk := wRows.(driver.RowsColumnTypeLength).ColumnTypeLength(1) |
| 120 | + if oLength != wLength || oOk != wOk { |
| 121 | + t.Errorf("rows.ColumnTypeLength want: %d:%t, have %d:%t", oLength, oOk, wLength, wOk) |
| 122 | + } |
| 123 | + |
| 124 | + oNullable, oOk := false, false |
| 125 | + wNullable, wOk := wRows.(driver.RowsColumnTypeNullable).ColumnTypeNullable(1) |
| 126 | + if oNullable != wNullable || oOk != wOk { |
| 127 | + t.Errorf("rows.ColumnTypeNullable want: %t:%t, have %t:%t", oNullable, oOk, wNullable, wOk) |
| 128 | + } |
| 129 | + |
| 130 | + oPrecision, oScale, oOk := int64(0), int64(0), false |
| 131 | + wPrecision, wScale, wOk := wRows.(driver.RowsColumnTypePrecisionScale).ColumnTypePrecisionScale(1) |
| 132 | + if oPrecision != wPrecision || oScale != wScale || oOk != wOk { |
| 133 | + t.Errorf("rows.ColumnTypePrecisionScale want: %d:%d:%t, have %d:%d:%t", oPrecision, oScale, oOk, wPrecision, wScale, wOk) |
| 134 | + } |
| 135 | +} |
0 commit comments