-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_support_pandas.py
More file actions
159 lines (131 loc) · 4.62 KB
/
test_support_pandas.py
File metadata and controls
159 lines (131 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import re
import sys
import pandas as pd
import pytest
from pandas._testing import assert_equal
from pueblo.testing.pandas import makeTimeDataFrame
from sqlalchemy.exc import ProgrammingError
from sqlalchemy_cratedb.sa_version import SA_2_0, SA_VERSION
from sqlalchemy_cratedb.support.pandas import table_kwargs
TABLE_NAME = "foobar"
INSERT_RECORDS = 42
# Create dataframe, to be used as input data.
df = makeTimeDataFrame(nper=INSERT_RECORDS, freq="S")
df["time"] = df.index
float_double_data = {
"col_1": [19556.88, 629414.27, 51570.0, 2933.52, 20338.98],
"col_2": [
15379.920000000002,
1107140.42,
8081.999999999999,
1570.0300000000002,
29468.539999999997,
],
}
float_double_df = pd.DataFrame.from_dict(float_double_data)
@pytest.mark.skipif(
sys.version_info < (3, 8), reason="Feature not supported on Python 3.7 and earlier"
)
@pytest.mark.skipif(
SA_VERSION < SA_2_0, reason="Feature not supported on SQLAlchemy 1.4 and earlier"
)
def test_table_kwargs_partitioned_by(cratedb_service):
"""
Validate adding CrateDB dialect table option `PARTITIONED BY` at runtime.
"""
engine = cratedb_service.database.engine
# Insert records from pandas dataframe.
with table_kwargs(crate_partitioned_by="time"):
df.to_sql(
TABLE_NAME,
engine,
if_exists="replace",
index=False,
)
# Synchronize writes.
cratedb_service.database.refresh_table(TABLE_NAME)
# Inquire table cardinality.
count = cratedb_service.database.count_records(TABLE_NAME)
# Compare outcome.
assert count == INSERT_RECORDS
# Validate SQL DDL.
ddl = cratedb_service.database.run_sql(f"SHOW CREATE TABLE {TABLE_NAME}")
assert 'PARTITIONED BY ("time")' in ddl[0][0]
@pytest.mark.skipif(
sys.version_info < (3, 8), reason="Feature not supported on Python 3.7 and earlier"
)
@pytest.mark.skipif(
SA_VERSION < SA_2_0, reason="Feature not supported on SQLAlchemy 1.4 and earlier"
)
def test_table_kwargs_translog_durability(cratedb_service):
"""
Validate adding CrateDB dialect table option `translog.durability` at runtime.
"""
engine = cratedb_service.database.engine
# Insert records from pandas dataframe.
with table_kwargs(**{'crate_"translog.durability"': "'async'"}):
df.to_sql(
TABLE_NAME,
engine,
if_exists="replace",
index=False,
)
# Synchronize writes.
cratedb_service.database.refresh_table(TABLE_NAME)
# Inquire table cardinality.
count = cratedb_service.database.count_records(TABLE_NAME)
# Compare outcome.
assert count == INSERT_RECORDS
# Validate SQL DDL.
ddl = cratedb_service.database.run_sql(f"SHOW CREATE TABLE {TABLE_NAME}")
assert """"translog.durability" = 'ASYNC'""" in ddl[0][0]
@pytest.mark.skipif(
sys.version_info < (3, 8), reason="Feature not supported on Python 3.7 and earlier"
)
@pytest.mark.skipif(
SA_VERSION < SA_2_0, reason="Feature not supported on SQLAlchemy 1.4 and earlier"
)
def test_table_kwargs_unknown(cratedb_service):
"""
Validate behaviour when adding an unknown CrateDB dialect table option.
"""
engine = cratedb_service.database.engine
with table_kwargs(crate_unknown_option="'bazqux'"):
with pytest.raises(ProgrammingError) as ex:
df.to_sql(
TABLE_NAME,
engine,
if_exists="replace",
index=False,
)
assert ex.match(
re.escape(
'SQLParseException[Invalid property "unknown_option" '
"passed to [ALTER | CREATE] TABLE statement]"
)
)
@pytest.mark.skipif(
sys.version_info < (3, 8), reason="Feature not supported on Python 3.7 and earlier"
)
@pytest.mark.skipif(
SA_VERSION < SA_2_0, reason="Feature not supported on SQLAlchemy 1.4 and earlier"
)
def test_float_double(cratedb_service):
"""
Validate I/O with floating point numbers, specifically DOUBLE types.
Motto: Do not lose precision when DOUBLE is required.
"""
tablename = "pandas_double"
engine = cratedb_service.database.engine
float_double_df.to_sql(
tablename,
engine,
if_exists="replace",
index=False,
)
cratedb_service.database.run_sql(f"REFRESH TABLE {tablename}")
df_load = pd.read_sql_table(tablename, engine)
before = float_double_df.sort_values(by="col_1", ignore_index=True)
after = df_load.sort_values(by="col_1", ignore_index=True)
pd.options.display.float_format = "{:.12f}".format
assert_equal(before, after, check_exact=True)