|
1 | 1 | import re |
2 | 2 | import sys |
3 | 3 |
|
| 4 | +import pandas as pd |
4 | 5 | import pytest |
| 6 | +import sqlalchemy as sa |
| 7 | +from pandas._testing import assert_equal |
5 | 8 | from pueblo.testing.pandas import makeTimeDataFrame |
6 | 9 | from sqlalchemy.exc import ProgrammingError |
7 | 10 |
|
|
15 | 18 | df = makeTimeDataFrame(nper=INSERT_RECORDS, freq="S") |
16 | 19 | df["time"] = df.index |
17 | 20 |
|
| 21 | +float_double_data = { |
| 22 | + "col_1": [19556.88, 629414.27, 51570.0, 2933.52, 20338.98], |
| 23 | + "col_2": [ |
| 24 | + 15379.920000000002, |
| 25 | + 1107140.42, |
| 26 | + 8081.999999999999, |
| 27 | + 1570.0300000000002, |
| 28 | + 29468.539999999997, |
| 29 | + ], |
| 30 | +} |
| 31 | +float_double_df = pd.DataFrame.from_dict(float_double_data) |
| 32 | + |
18 | 33 |
|
19 | 34 | @pytest.mark.skipif( |
20 | 35 | sys.version_info < (3, 8), reason="Feature not supported on Python 3.7 and earlier" |
@@ -113,3 +128,34 @@ def test_table_kwargs_unknown(cratedb_service): |
113 | 128 | "passed to [ALTER | CREATE] TABLE statement]" |
114 | 129 | ) |
115 | 130 | ) |
| 131 | + |
| 132 | + |
| 133 | +@pytest.mark.skipif( |
| 134 | + sys.version_info < (3, 8), reason="Feature not supported on Python 3.7 and earlier" |
| 135 | +) |
| 136 | +@pytest.mark.skipif( |
| 137 | + SA_VERSION < SA_2_0, reason="Feature not supported on SQLAlchemy 1.4 and earlier" |
| 138 | +) |
| 139 | +def test_float_double(cratedb_service): |
| 140 | + """ |
| 141 | + Validate I/O with floating point numbers, specifically DOUBLE types. |
| 142 | +
|
| 143 | + Motto: Do not lose precision when DOUBLE is required. |
| 144 | + """ |
| 145 | + tablename = "pandas_double" |
| 146 | + engine = cratedb_service.database.engine |
| 147 | + float_double_df.to_sql( |
| 148 | + tablename, |
| 149 | + engine, |
| 150 | + if_exists="replace", |
| 151 | + index=False, |
| 152 | + ) |
| 153 | + with engine.connect() as conn: |
| 154 | + conn.execute(sa.text(f"REFRESH TABLE {tablename}")) |
| 155 | + df_load = pd.read_sql_table(tablename, engine) |
| 156 | + |
| 157 | + before = float_double_df.sort_values(by="col_1", ignore_index=True) |
| 158 | + after = df_load.sort_values(by="col_1", ignore_index=True) |
| 159 | + |
| 160 | + pd.options.display.float_format = "{:.12f}".format |
| 161 | + assert_equal(before, after, check_exact=True) |
0 commit comments