|
3 | 3 | package sqlmock |
4 | 4 |
|
5 | 5 | import ( |
| 6 | + "database/sql" |
| 7 | + "encoding/json" |
6 | 8 | "fmt" |
7 | 9 | "testing" |
8 | 10 | ) |
@@ -90,3 +92,114 @@ func TestQueryMultiRows(t *testing.T) { |
90 | 92 | t.Errorf("there were unfulfilled expectations: %s", err) |
91 | 93 | } |
92 | 94 | } |
| 95 | + |
| 96 | +func TestQueryRowBytesInvalidatedByNext_jsonRawMessageIntoRawBytes(t *testing.T) { |
| 97 | + t.Parallel() |
| 98 | + replace := []byte(invalid) |
| 99 | + rows := NewRows([]string{"raw"}). |
| 100 | + AddRow(json.RawMessage(`{"thing": "one", "thing2": "two"}`)). |
| 101 | + AddRow(json.RawMessage(`{"that": "foo", "this": "bar"}`)) |
| 102 | + scan := func(rs *sql.Rows) ([]byte, error) { |
| 103 | + var raw sql.RawBytes |
| 104 | + return raw, rs.Scan(&raw) |
| 105 | + } |
| 106 | + want := []struct { |
| 107 | + Initial []byte |
| 108 | + Replaced []byte |
| 109 | + }{ |
| 110 | + {Initial: []byte(`{"thing": "one", "thing2": "two"}`), Replaced: replace[:len(replace)-6]}, |
| 111 | + {Initial: []byte(`{"that": "foo", "this": "bar"}`), Replaced: replace[:len(replace)-9]}, |
| 112 | + } |
| 113 | + queryRowBytesInvalidatedByNext(t, rows, scan, want) |
| 114 | +} |
| 115 | + |
| 116 | +func TestQueryRowBytesNotInvalidatedByNext_jsonRawMessageIntoBytes(t *testing.T) { |
| 117 | + t.Parallel() |
| 118 | + rows := NewRows([]string{"raw"}). |
| 119 | + AddRow(json.RawMessage(`{"thing": "one", "thing2": "two"}`)). |
| 120 | + AddRow(json.RawMessage(`{"that": "foo", "this": "bar"}`)) |
| 121 | + scan := func(rs *sql.Rows) ([]byte, error) { |
| 122 | + var b []byte |
| 123 | + return b, rs.Scan(&b) |
| 124 | + } |
| 125 | + want := [][]byte{[]byte(`{"thing": "one", "thing2": "two"}`), []byte(`{"that": "foo", "this": "bar"}`)} |
| 126 | + queryRowBytesNotInvalidatedByNext(t, rows, scan, want) |
| 127 | +} |
| 128 | + |
| 129 | +func TestQueryRowBytesNotInvalidatedByNext_bytesIntoCustomBytes(t *testing.T) { |
| 130 | + t.Parallel() |
| 131 | + rows := NewRows([]string{"raw"}). |
| 132 | + AddRow([]byte(`one binary value with some text!`)). |
| 133 | + AddRow([]byte(`two binary value with even more text than the first one`)) |
| 134 | + scan := func(rs *sql.Rows) ([]byte, error) { |
| 135 | + type customBytes []byte |
| 136 | + var b customBytes |
| 137 | + return b, rs.Scan(&b) |
| 138 | + } |
| 139 | + want := [][]byte{[]byte(`one binary value with some text!`), []byte(`two binary value with even more text than the first one`)} |
| 140 | + queryRowBytesNotInvalidatedByNext(t, rows, scan, want) |
| 141 | +} |
| 142 | + |
| 143 | +func TestQueryRowBytesNotInvalidatedByNext_jsonRawMessageIntoCustomBytes(t *testing.T) { |
| 144 | + t.Parallel() |
| 145 | + rows := NewRows([]string{"raw"}). |
| 146 | + AddRow(json.RawMessage(`{"thing": "one", "thing2": "two"}`)). |
| 147 | + AddRow(json.RawMessage(`{"that": "foo", "this": "bar"}`)) |
| 148 | + scan := func(rs *sql.Rows) ([]byte, error) { |
| 149 | + type customBytes []byte |
| 150 | + var b customBytes |
| 151 | + return b, rs.Scan(&b) |
| 152 | + } |
| 153 | + want := [][]byte{[]byte(`{"thing": "one", "thing2": "two"}`), []byte(`{"that": "foo", "this": "bar"}`)} |
| 154 | + queryRowBytesNotInvalidatedByNext(t, rows, scan, want) |
| 155 | +} |
| 156 | + |
| 157 | +func TestQueryRowBytesNotInvalidatedByClose_bytesIntoCustomBytes(t *testing.T) { |
| 158 | + t.Parallel() |
| 159 | + rows := NewRows([]string{"raw"}).AddRow([]byte(`one binary value with some text!`)) |
| 160 | + scan := func(rs *sql.Rows) ([]byte, error) { |
| 161 | + type customBytes []byte |
| 162 | + var b customBytes |
| 163 | + return b, rs.Scan(&b) |
| 164 | + } |
| 165 | + queryRowBytesNotInvalidatedByClose(t, rows, scan, []byte(`one binary value with some text!`)) |
| 166 | +} |
| 167 | + |
| 168 | +func TestQueryRowBytesInvalidatedByClose_jsonRawMessageIntoRawBytes(t *testing.T) { |
| 169 | + t.Parallel() |
| 170 | + replace := []byte(invalid) |
| 171 | + rows := NewRows([]string{"raw"}).AddRow(json.RawMessage(`{"thing": "one", "thing2": "two"}`)) |
| 172 | + scan := func(rs *sql.Rows) ([]byte, error) { |
| 173 | + var raw sql.RawBytes |
| 174 | + return raw, rs.Scan(&raw) |
| 175 | + } |
| 176 | + want := struct { |
| 177 | + Initial []byte |
| 178 | + Replaced []byte |
| 179 | + }{ |
| 180 | + Initial: []byte(`{"thing": "one", "thing2": "two"}`), |
| 181 | + Replaced: replace[:len(replace)-6], |
| 182 | + } |
| 183 | + queryRowBytesInvalidatedByClose(t, rows, scan, want) |
| 184 | +} |
| 185 | + |
| 186 | +func TestQueryRowBytesNotInvalidatedByClose_jsonRawMessageIntoBytes(t *testing.T) { |
| 187 | + t.Parallel() |
| 188 | + rows := NewRows([]string{"raw"}).AddRow(json.RawMessage(`{"thing": "one", "thing2": "two"}`)) |
| 189 | + scan := func(rs *sql.Rows) ([]byte, error) { |
| 190 | + var b []byte |
| 191 | + return b, rs.Scan(&b) |
| 192 | + } |
| 193 | + queryRowBytesNotInvalidatedByClose(t, rows, scan, []byte(`{"thing": "one", "thing2": "two"}`)) |
| 194 | +} |
| 195 | + |
| 196 | +func TestQueryRowBytesNotInvalidatedByClose_jsonRawMessageIntoCustomBytes(t *testing.T) { |
| 197 | + t.Parallel() |
| 198 | + rows := NewRows([]string{"raw"}).AddRow(json.RawMessage(`{"thing": "one", "thing2": "two"}`)) |
| 199 | + scan := func(rs *sql.Rows) ([]byte, error) { |
| 200 | + type customBytes []byte |
| 201 | + var b customBytes |
| 202 | + return b, rs.Scan(&b) |
| 203 | + } |
| 204 | + queryRowBytesNotInvalidatedByClose(t, rows, scan, []byte(`{"thing": "one", "thing2": "two"}`)) |
| 205 | +} |
0 commit comments