|
| 1 | +import { FieldBindType, TDengineTypeCode } from "@src/common/constant"; |
| 2 | +import { StmtFieldInfo } from "@src/stmt/wsProto"; |
| 3 | +import { Stmt1BindParams } from "@src/stmt/wsParams1"; |
| 4 | +import { Stmt2BindParams } from "@src/stmt/wsParams2"; |
| 5 | +import { WsStmt2 } from "@src/stmt/wsStmt2"; |
| 6 | + |
| 7 | +function createMockWsClient() { |
| 8 | + return { |
| 9 | + getState: jest.fn(() => 1), |
| 10 | + connect: jest.fn(async () => { }), |
| 11 | + checkVersion: jest.fn(async () => { }), |
| 12 | + exec: jest.fn(async () => ({ totalTime: 0, msg: { code: 0, message: "", timing: 0, stmt_id: 1 } })), |
| 13 | + execNoResp: jest.fn(async () => { }), |
| 14 | + sendBinaryMsg: jest.fn(async () => ({ totalTime: 0, msg: { code: 0, message: "", timing: 0, stmt_id: 1 } })), |
| 15 | + waitForReady: jest.fn(async () => { }), |
| 16 | + isNetworkError: jest.fn((_err: unknown) => false), |
| 17 | + getReconnectRetries: jest.fn(() => 5), |
| 18 | + }; |
| 19 | +} |
| 20 | + |
| 21 | +function createInsertFields(): StmtFieldInfo[] { |
| 22 | + return [ |
| 23 | + { |
| 24 | + name: "tbname", |
| 25 | + field_type: TDengineTypeCode.VARCHAR, |
| 26 | + precision: 0, |
| 27 | + scale: 0, |
| 28 | + bytes: 0, |
| 29 | + bind_type: FieldBindType.TAOS_FIELD_TBNAME, |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "location", |
| 33 | + field_type: TDengineTypeCode.VARCHAR, |
| 34 | + precision: 0, |
| 35 | + scale: 0, |
| 36 | + bytes: 0, |
| 37 | + bind_type: FieldBindType.TAOS_FIELD_TAG, |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "gid", |
| 41 | + field_type: TDengineTypeCode.INT, |
| 42 | + precision: 0, |
| 43 | + scale: 0, |
| 44 | + bytes: 0, |
| 45 | + bind_type: FieldBindType.TAOS_FIELD_TAG, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "d64", |
| 49 | + field_type: TDengineTypeCode.DECIMAL64, |
| 50 | + precision: 0, |
| 51 | + scale: 0, |
| 52 | + bytes: 0, |
| 53 | + bind_type: FieldBindType.TAOS_FIELD_COL, |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "v", |
| 57 | + field_type: TDengineTypeCode.INT, |
| 58 | + precision: 0, |
| 59 | + scale: 0, |
| 60 | + bytes: 0, |
| 61 | + bind_type: FieldBindType.TAOS_FIELD_COL, |
| 62 | + }, |
| 63 | + ]; |
| 64 | +} |
| 65 | + |
| 66 | +function createBareStmt(fields: StmtFieldInfo[], isInsert: boolean = true) { |
| 67 | + const stmt = new (WsStmt2 as any)(createMockWsClient()); |
| 68 | + stmt._stmt_id = 1n; |
| 69 | + stmt.fields = fields; |
| 70 | + stmt._isInsert = isInsert; |
| 71 | + stmt._toBeBindTableNameIndex = 0; |
| 72 | + stmt._toBeBindTagCount = fields.filter( |
| 73 | + (f) => f.bind_type === FieldBindType.TAOS_FIELD_TAG |
| 74 | + ).length; |
| 75 | + stmt._toBeBindColCount = fields.filter( |
| 76 | + (f) => f.bind_type === FieldBindType.TAOS_FIELD_COL |
| 77 | + ).length; |
| 78 | + return stmt; |
| 79 | +} |
| 80 | + |
| 81 | +describe("Stmt2 decimal bind behavior (mock)", () => { |
| 82 | + test("stmt1 setDecimal should throw unsupported error", () => { |
| 83 | + const params = new Stmt1BindParams(); |
| 84 | + expect(() => { |
| 85 | + params.setDecimal(["1.2300"]); |
| 86 | + }).toThrow("setDecimal is not supported in stmt1, please use stmt2 instead!"); |
| 87 | + }); |
| 88 | + |
| 89 | + test("stmt2 setDecimal should reject non-string values", () => { |
| 90 | + const params = new Stmt2BindParams(); |
| 91 | + expect(() => { |
| 92 | + params.setDecimal([]); |
| 93 | + }).toThrow("SetDecimalColumn params is invalid!"); |
| 94 | + |
| 95 | + expect(() => { |
| 96 | + params.setDecimal([123]); |
| 97 | + }).toThrow("SetDecimalColumn params is invalid!"); |
| 98 | + }); |
| 99 | + |
| 100 | + test("stmt2 setDecimal should be encoded as variable-length data", () => { |
| 101 | + const params = new Stmt2BindParams(); |
| 102 | + params.setDecimal(["1.2300", null, "-0.0001"]); |
| 103 | + params.encode(); |
| 104 | + const cols = params.getParams(); |
| 105 | + expect(cols.length).toBe(1); |
| 106 | + expect(cols[0].type).toBe(TDengineTypeCode.DECIMAL); |
| 107 | + expect(cols[0]._haveLength).toBe(1); |
| 108 | + expect(cols[0]._rows).toBe(3); |
| 109 | + }); |
| 110 | + |
| 111 | + test("bind should override DECIMAL to real column decimal type in full-binding insert", async () => { |
| 112 | + const fields = createInsertFields(); |
| 113 | + const stmt = createBareStmt(fields, true); |
| 114 | + const params = new Stmt2BindParams(fields.length, 13, fields); |
| 115 | + |
| 116 | + params.setVarchar(["d0"]); |
| 117 | + params.setVarchar(["beijing"]); |
| 118 | + params.setInt([1]); |
| 119 | + params.setDecimal(["123.456700"]); |
| 120 | + params.setInt([10]); |
| 121 | + |
| 122 | + await stmt.bind(params); |
| 123 | + |
| 124 | + const tableInfo = stmt._stmtTableInfo.get("d0"); |
| 125 | + const colParams = tableInfo?.getParams(); |
| 126 | + expect(colParams?._fieldParams?.length).toBe(2); |
| 127 | + expect(colParams?._fieldParams?.[0].columnType).toBe( |
| 128 | + TDengineTypeCode.DECIMAL64 |
| 129 | + ); |
| 130 | + }); |
| 131 | + |
| 132 | + test("bind should override DECIMAL to real column decimal type in non-full-binding insert", async () => { |
| 133 | + const fields = createInsertFields(); |
| 134 | + const stmt = createBareStmt(fields, true); |
| 135 | + const params = new Stmt2BindParams(); |
| 136 | + |
| 137 | + params.setDecimal(["-0.000001"]); |
| 138 | + params.setInt([11]); |
| 139 | + |
| 140 | + await stmt.bind(params); |
| 141 | + |
| 142 | + const bindParams = stmt._currentTableInfo.getParams(); |
| 143 | + expect(bindParams?._fieldParams?.[0].columnType).toBe( |
| 144 | + TDengineTypeCode.DECIMAL64 |
| 145 | + ); |
| 146 | + expect(bindParams?._fieldParams?.[1].columnType).toBe( |
| 147 | + TDengineTypeCode.INT |
| 148 | + ); |
| 149 | + }); |
| 150 | + |
| 151 | + test("query mode should keep DECIMAL default type", async () => { |
| 152 | + const fields = createInsertFields(); |
| 153 | + const stmt = createBareStmt(fields, false); |
| 154 | + const params = new Stmt2BindParams(); |
| 155 | + params.setDecimal(["99.0001"]); |
| 156 | + |
| 157 | + await stmt.bind(params); |
| 158 | + |
| 159 | + const bindParams = stmt._currentTableInfo.getParams(); |
| 160 | + expect(bindParams?._fieldParams?.[0].columnType).toBe( |
| 161 | + TDengineTypeCode.DECIMAL |
| 162 | + ); |
| 163 | + }); |
| 164 | +}); |
0 commit comments