/
helpers.py
294 lines (228 loc) · 7.17 KB
/
helpers.py
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import sqlite3
from abc import ABC, abstractmethod
from collections import UserDict
import pandas as pd
from sqlalchemy.dialects import postgresql
from sqlalchemy.types import (
INTEGER,
TEXT,
)
import splink.duckdb.blocking_rule_library as brl_duckdb
import splink.duckdb.comparison_level_library as cll_duckdb
import splink.duckdb.comparison_library as cl_duckdb
import splink.duckdb.comparison_template_library as ctl_duckdb
import splink.postgres.blocking_rule_library as brl_postgres
import splink.postgres.comparison_level_library as cll_postgres
import splink.postgres.comparison_library as cl_postgres
import splink.postgres.comparison_template_library as ctl_postgres
import splink.spark.blocking_rule_library as brl_spark
import splink.spark.comparison_level_library as cll_spark
import splink.spark.comparison_library as cl_spark
import splink.spark.comparison_template_library as ctl_spark
import splink.sqlite.blocking_rule_library as brl_sqlite
import splink.sqlite.comparison_level_library as cll_sqlite
import splink.sqlite.comparison_library as cl_sqlite
import splink.sqlite.comparison_template_library as ctl_sqlite
from splink.duckdb.linker import DuckDBLinker
from splink.postgres.linker import PostgresLinker
from splink.spark.linker import SparkLinker
from splink.sqlite.linker import SQLiteLinker
class TestHelper(ABC):
@property
@abstractmethod
def Linker(self):
pass
def extra_linker_args(self):
return {}
@property
def date_format(self):
return "yyyy-mm-dd"
@abstractmethod
def convert_frame(self, df):
pass
def load_frame_from_csv(self, path):
return pd.read_csv(path)
def load_frame_from_parquet(self, path):
return pd.read_parquet(path)
@property
@abstractmethod
def cll(self):
pass
@property
@abstractmethod
def cl(self):
pass
@property
@abstractmethod
def ctl(self):
pass
@property
@abstractmethod
def brl(self):
pass
class DuckDBTestHelper(TestHelper):
@property
def Linker(self):
return DuckDBLinker
def convert_frame(self, df):
return df
@property
def date_format(self):
return "%Y-%m-%d"
@property
def cll(self):
return cll_duckdb
@property
def cl(self):
return cl_duckdb
@property
def ctl(self):
return ctl_duckdb
@property
def brl(self):
return brl_duckdb
class SparkTestHelper(TestHelper):
def __init__(self, spark_creator_function):
self.spark = spark_creator_function()
@property
def Linker(self):
return SparkLinker
def extra_linker_args(self):
return {"spark": self.spark, "num_partitions_on_repartition": 1}
def convert_frame(self, df):
spark_frame = self.spark.createDataFrame(df)
spark_frame.persist()
return spark_frame
def load_frame_from_csv(self, path):
df = self.spark.read.csv(path, header=True)
df.persist()
return df
def load_frame_from_parquet(self, path):
df = self.spark.read.parquet(path)
df.persist()
return df
@property
def cll(self):
return cll_spark
@property
def cl(self):
return cl_spark
@property
def ctl(self):
return ctl_spark
@property
def brl(self):
return brl_spark
class SQLiteTestHelper(TestHelper):
_frame_counter = 0
def __init__(self):
self.con = sqlite3.connect(":memory:")
self._frame_counter = 0
@property
def Linker(self):
return SQLiteLinker
def extra_linker_args(self):
return {"connection": self.con}
@classmethod
def _get_input_name(cls):
name = f"input_alias_{cls._frame_counter}"
cls._frame_counter += 1
return name
def convert_frame(self, df):
name = self._get_input_name()
df.to_sql(name, self.con, if_exists="replace")
return name
def load_frame_from_csv(self, path):
return self.convert_frame(super().load_frame_from_csv(path))
def load_frame_from_parquet(self, path):
return self.convert_frame(super().load_frame_from_parquet(path))
@property
def cll(self):
return cll_sqlite
@property
def cl(self):
return cl_sqlite
@property
def ctl(self):
return ctl_sqlite
@property
def brl(self):
return brl_sqlite
class PostgresTestHelper(TestHelper):
_frame_counter = 0
def __init__(self, pg_engine):
if pg_engine is None:
raise SplinkTestException("No Postgres connection found")
self.engine = pg_engine
@property
def Linker(self):
return PostgresLinker
def extra_linker_args(self):
return {"engine": self.engine}
@classmethod
def _get_input_name(cls):
name = f"input_alias_{cls._frame_counter}"
cls._frame_counter += 1
return name
def convert_frame(self, df):
name = self._get_input_name()
# workaround to handle array column conversion
# manually mark any list columns so type is handled correctly
dtypes = {}
for colname, values in df.items():
# TODO: will fail if first value is null
if isinstance(values[0], list):
# TODO: will fail if first array is empty
initial_array_val = values[0][0]
if isinstance(initial_array_val, int):
dtypes[colname] = postgresql.ARRAY(INTEGER)
elif isinstance(initial_array_val, str):
dtypes[colname] = postgresql.ARRAY(TEXT)
df.to_sql(name, con=self.engine, if_exists="replace", dtype=dtypes)
return name
def load_frame_from_csv(self, path):
return self.convert_frame(super().load_frame_from_csv(path))
def load_frame_from_parquet(self, path):
return self.convert_frame(super().load_frame_from_parquet(path))
@property
def cll(self):
return cll_postgres
@property
def cl(self):
return cl_postgres
@property
def ctl(self):
return ctl_postgres
@property
def brl(self):
return brl_postgres
class SplinkTestException(Exception):
pass
class LazyDict(UserDict):
"""
LazyDict
Like a dict, but values passed are tuples of the form (func, args)
getting returns the result of the function
only instantiate the result when we need it.
Need this for handling test fixtures.
Disallow setting/deleting entries to catch errors -
should be effectively immutable
"""
# write only in creation
def __init__(self, **kwargs):
self.data = {}
for key, val in kwargs.items():
self.data[key] = val
def __getitem__(self, key):
func, args = self.data[key]
return func(*args)
def __setitem__(self, key, value):
raise SplinkTestException(
"LazyDict does not support setting values. "
"Did you mean to read value instead?"
)
def __delitem__(self, key):
raise SplinkTestException(
"LazyDict does not support deleting items. "
"Did you mean to read value instead?"
)