NormalizeExpectedRow normalizes each value's type, as the tests only want to compare values. Returns a new row.
(fds []pgconn.FieldDescription, rows []sql.Row)
| 103 | |
| 104 | // NormalizeExpectedRow normalizes each value's type, as the tests only want to compare values. Returns a new row. |
| 105 | func NormalizeExpectedRow(fds []pgconn.FieldDescription, rows []sql.Row) []sql.Row { |
| 106 | newRows := make([]sql.Row, len(rows)) |
| 107 | for ri, row := range rows { |
| 108 | if len(row) == 0 { |
| 109 | newRows[ri] = nil |
| 110 | } else if len(row) != len(fds) { |
| 111 | // Return if the expected row count does not match the field description count, we'll error elsewhere |
| 112 | return rows |
| 113 | } else { |
| 114 | newRow := make(sql.Row, len(row)) |
| 115 | for i := range row { |
| 116 | oid := fds[i].DataTypeOID |
| 117 | typ, ok := defaultMap.TypeForOID(oid) |
| 118 | if !ok { |
| 119 | panic(fmt.Sprintf("unknown oid: %v", fds[i].DataTypeOID)) |
| 120 | } |
| 121 | if strings.EqualFold(typ.Name, "json") { |
| 122 | newRow[i] = UnmarshalAndMarshalJsonString(row[i].(string)) |
| 123 | } else if strings.EqualFold(typ.Name, "_json") { // Array of JSON |
| 124 | bytes, err := defaultMap.Encode(oid, pgtype.TextFormatCode, row[i], nil) |
| 125 | if err != nil { |
| 126 | panic(fmt.Errorf("failed to encode json array: %w", err)) |
| 127 | } |
| 128 | var arr []string |
| 129 | if err := defaultMap.Scan(oid, pgtype.TextFormatCode, bytes, &arr); err != nil { |
| 130 | panic(fmt.Errorf("failed to scan json array: %w", err)) |
| 131 | } |
| 132 | newArr := make([]string, len(arr)) |
| 133 | for j, el := range arr { |
| 134 | newArr[j] = UnmarshalAndMarshalJsonString(el) |
| 135 | } |
| 136 | |
| 137 | bytes, err = defaultMap.Encode(oid, pgtype.TextFormatCode, newArr, nil) |
| 138 | if err != nil { |
| 139 | panic(fmt.Errorf("failed to encode json array: %w", err)) |
| 140 | } |
| 141 | |
| 142 | newRow[i] = string(bytes) |
| 143 | } else { |
| 144 | newRow[i] = NormalizeIntsAndFloats(row[i]) |
| 145 | } |
| 146 | } |
| 147 | newRows[ri] = newRow |
| 148 | } |
| 149 | } |
| 150 | return newRows |
| 151 | } |
| 152 | |
| 153 | // UnmarshalAndMarshalJsonString is used to normalize expected json type value to compare the actual value. |
| 154 | // JSON type value is in string format, and since Postrges JSON type preserves the input string if valid, |