Skip to content

Commit 0c5ee7d

Browse files
committed
feat: enhance to_py_decimal for improved decimal handling and add test for missing decimal class
1 parent e9ff05e commit 0c5ee7d

4 files changed

Lines changed: 29 additions & 4 deletions

File tree

taos-ws-py/src/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ use taos::{taos_query::common::Timestamp, BorrowedValue, RawBlock};
1212
use crate::ConsumerException;
1313

1414
pub fn to_py_decimal(value: impl std::fmt::Display, py: Python<'_>) -> PyResult<PyObject> {
15-
let decimal = value.to_string();
1615
let decimal_mod = py.import("decimal")?;
1716
let decimal_cls = decimal_mod.getattr("Decimal")?;
17+
let decimal = value.to_string();
1818
Ok(decimal_cls.call1((decimal,))?.into_py(py))
1919
}
2020

taos-ws-py/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,9 @@ impl TaosResult {
280280
}
281281
BorrowedValue::VarChar(s) => s.into_py(py),
282282
BorrowedValue::NChar(v) => v.as_ref().into_py(py),
283-
BorrowedValue::Json(j) => std::str::from_utf8(&j).unwrap().into_py(py),
283+
BorrowedValue::Json(j) => std::str::from_utf8(&j)
284+
.map_err(|err| ProgrammingError::new_err(err.to_string()))?
285+
.into_py(py),
284286
BorrowedValue::VarBinary(v) => v.as_ref().into_py(py),
285287
BorrowedValue::Geometry(v) => v.as_ref().into_py(py),
286288
BorrowedValue::Decimal64(v) => common::to_py_decimal(v, py)?,

taos-ws-py/tests/test_consumer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def test_report_connector_info():
8686
consumer2.subscribe(["test_topic_1"])
8787
time.sleep(2)
8888
res = conn.query("show connections")
89-
assert sum(connector_info == col for row in res for col in row) >= 2
89+
assert sum(connector_info == col for row in res for col in row) >= 3
9090
consumer2.unsubscribe()
9191

9292
time.sleep(2)

taos-ws-py/tests/test_decimal.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import datetime
2-
import os
2+
import sys
3+
import types
34
import pytest
45
import taosws
56
import time
@@ -158,3 +159,25 @@ def test_decimal_tmq():
158159
conn.execute("drop topic if exists topic_1775631845")
159160
conn.execute("drop database if exists test_1775631845")
160161
conn.close()
162+
163+
164+
@pytest.mark.skipif(utils.TEST_TD_3360, reason="skip for TD-3360")
165+
def test_decimal_fetch_reports_python_error_when_decimal_class_missing(monkeypatch):
166+
conn = taosws.connect("ws://localhost:6041")
167+
cursor = conn.cursor()
168+
try:
169+
cursor.execute("drop database if exists test_1775636199")
170+
cursor.execute("create database test_1775636199")
171+
cursor.execute("use test_1775636199")
172+
cursor.execute("create table t0 (ts timestamp, c1 decimal(10, 2))")
173+
cursor.execute("insert into t0 values(1752218982762, 99.9876)")
174+
175+
fake_decimal_module = types.ModuleType("decimal")
176+
monkeypatch.setitem(sys.modules, "decimal", fake_decimal_module)
177+
178+
cursor.execute("select c1 from t0")
179+
with pytest.raises(AttributeError, match=r"Decimal"):
180+
cursor.fetchall()
181+
finally:
182+
cursor.execute("drop database if exists test_1775636199")
183+
conn.close()

0 commit comments

Comments
 (0)