-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathtest_text_generation.py
579 lines (469 loc) · 28.9 KB
/
test_text_generation.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
import re
import numpy as np
import pandas as pd
import pyspark.sql.functions as F
import pytest
from pyspark.sql.types import BooleanType, DateType
from pyspark.sql.types import StructType, StructField, IntegerType, StringType, TimestampType
import dbldatagen as dg
from dbldatagen import TemplateGenerator, TextGenerator
schema = StructType([
StructField("PK1", StringType(), True),
StructField("LAST_MODIFIED_UTC", TimestampType(), True),
StructField("date", DateType(), True),
StructField("str1", StringType(), True),
StructField("nint", IntegerType(), True),
StructField("nstr1", StringType(), True),
StructField("nstr2", StringType(), True),
StructField("nstr3", StringType(), True),
StructField("nstr4", StringType(), True),
StructField("nstr5", StringType(), True),
StructField("nstr6", StringType(), True),
StructField("email", StringType(), True),
StructField("ip_addr", StringType(), True),
StructField("phone", StringType(), True),
StructField("isDeleted", BooleanType(), True)
])
# add the following if using pandas udfs
# .config("spark.sql.execution.arrow.maxRecordsPerBatch", "1000") \
spark = dg.SparkSingleton.getLocalInstance("unit tests")
spark.conf.set("spark.sql.execution.arrow.maxRecordsPerBatch", "500")
spark.conf.set("spark.sql.execution.arrow.pyspark.enabled", "true")
# Test manipulation and generation of test data for a large schema
class TestTextGeneration:
testDataSpec = None
row_count = 100000
partitions_requested = 4
class TestTextGenerator(TextGenerator):
def pandasGenerateText(self, v): # pylint: disable=useless-parent-delegation
return super().pandasGenerateText(v)
def test_text_generator_basics(self):
# test the random humber generator
tg1 = self.TestTextGenerator()
# test the repr
desc = repr(tg1)
assert desc is not None and len(desc.strip()) > 0
rng1 = tg1.getNPRandomGenerator()
# get some integers
random_values1 = rng1.integers(10, 20, dtype=np.int32)
assert 10 <= random_values1 <= 20
@pytest.mark.parametrize("template, escapeSpecial, low, high, useSystemLib",
[
(r'\n.\n.\n.\n', False, 0, 15, False),
(r'\n.\n.\n.\n', False, 20, 35, False),
(r'\n.\n.\n.\n', False, 15, None, False),
(r'\n.\n.\n.\n', False, 15, -1, False),
(r'\n.\n.\n.\n', False, 0, 15, True),
(r'\n.\n.\n.\n', False, 20, 35, True),
(r'\n.\n.\n.\n', False, 15, None, True),
(r'\n.\n.\n.\n', False, 15, -1, True),
])
def test_random_number_generator(self, template, escapeSpecial, low, high, useSystemLib):
""" As the test coverage tools dont detect code only used in UDFs,
lets add some explicit tests for the underlying code"""
test_template = TemplateGenerator(template, escapeSpecialChars=escapeSpecial)
rng1 = test_template.getNPRandomGenerator()
for x in range(1, 1000):
if useSystemLib:
if high is not None:
random_value = test_template._getRandomInt(low, high)
else:
random_value = test_template._getRandomInt(low)
else:
if high is not None:
random_value = test_template._getRandomInt(low, high, rng1)
else:
random_value = test_template._getRandomInt(low, rng=rng1)
if high is None or high == -1:
high = low
low = 0
assert low <= random_value <= high
@pytest.mark.parametrize("template, escapeSpecial, expectedTemplates",
[(r'\n.\n.\n.\n', True, 1),
(r'(ddd)-ddd-dddd|1(ddd) ddd-dddd|ddd ddddddd', False, 3),
(r'(\d\d\d)-\d\d\d-\d\d\d\d|1(\d\d\d) \d\d\d-\d\d\d\d|\d\d\d \d\d\d\d\d\d\d', True, 3),
(r'\dr_\v', False, 1),
(r'\w.\w@\w.com|\w@\w.co.u\k', False, 2),
])
def test_template_generator_properties(self, template, escapeSpecial, expectedTemplates):
test_template = TemplateGenerator(template, escapeSpecialChars=escapeSpecial)
repr_desc = repr(test_template)
assert repr_desc is not None and len(repr_desc.strip()) > 0
str_desc = str(test_template)
assert str_desc is not None and len(str_desc.strip()) > 0
assert test_template.templates is not None
assert len(test_template.templates) == expectedTemplates
def test_simple_data_template(self):
testDataSpec = (dg.DataGenerator(sparkSession=spark, name="test_data_set1", rows=self.row_count,
partitions=self.partitions_requested)
.withSchema(schema)
.withIdOutput()
.withColumnSpec("date", percentNulls=0.1)
.withColumnSpec("nint", percentNulls=0.1, minValue=1, maxValue=9, step=2)
.withColumnSpec("nstr1", percentNulls=0.1, minValue=1, maxValue=9, step=2)
.withColumnSpec("nstr2", percentNulls=0.1, minValue=1.5, maxValue=2.5, step=0.3,
format="%04f")
.withColumnSpec("nstr3", minValue=1.0, maxValue=9.0, step=2.0)
.withColumnSpec("nstr4", percentNulls=0.1, minValue=1, maxValue=9, step=2, format="%04d")
.withColumnSpec("nstr5", percentNulls=0.1, minValue=1.5, maxValue=2.5, step=0.3, random=True)
.withColumnSpec("nstr6", percentNulls=0.1, minValue=1.5, maxValue=2.5, step=0.3, random=True,
format="%04f")
.withColumnSpec("email", template=r'\w.\w@\w.com|\w@\w.co.u\k')
.withColumnSpec("ip_addr", template=r'\n.\n.\n.\n')
.withColumnSpec("phone", template=r'(ddd)-ddd-dddd|1(ddd) ddd-dddd|ddd ddddddd')
)
df_template_data = testDataSpec.build().cache()
# check templated columns
counts = df_template_data.agg(
F.countDistinct("email").alias("email_count"),
F.countDistinct("ip_addr").alias("ip_addr_count"),
F.countDistinct("phone").alias("phone_count")
).collect()[0]
assert counts['email_count'] >= 10
assert counts['ip_addr_count'] >= 10
assert counts['phone_count'] >= 10
rows = df_template_data.select("email", "ip_addr", "phone").collect()
mail_patt = re.compile(r"[a-z\.@]+")
ipaddr_patt = re.compile(r"[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+")
phone_patt = re.compile(r"[0-9\(\) ]+")
for r in rows:
assert mail_patt.match(r["email"]), "check mail"
assert ipaddr_patt.match(r["ip_addr"]), "check ip address"
assert phone_patt.match(r["phone"]), "check phone"
def test_large_template_driven_data_generation(self):
testDataSpec = (dg.DataGenerator(sparkSession=spark, name="test_data_set1", rows=1000000,
partitions=24)
.withSchema(schema)
.withIdOutput()
.withColumnSpec("date", percent_nulls=0.1)
.withColumnSpec("nint", percent_nulls=0.1, minValue=1, maxValue=9, step=2)
.withColumnSpec("nstr1", percent_nulls=0.1, minValue=1, maxValue=9, step=2)
.withColumnSpec("nstr2", percent_nulls=0.1, minValue=1.5, maxValue=2.5, step=0.3,
format="%04f")
.withColumnSpec("nstr3", minValue=1.0, maxValue=9.0, step=2.0)
.withColumnSpec("nstr4", percent_nulls=0.1, minValue=1, maxValue=9, step=2, format="%04d")
.withColumnSpec("nstr5", percent_nulls=0.1, minValue=1.5, maxValue=2.5, step=0.3, random=True)
.withColumnSpec("nstr6", percent_nulls=0.1, minValue=1.5, maxValue=2.5, step=0.3, random=True,
format="%04f")
.withColumnSpec("email", template=r'\w.\w@\w.com|\w@\w.co.u\k')
.withColumnSpec("ip_addr", template=r'\n.\n.\n.\n')
.withColumnSpec("phone", template=r'(ddd)-ddd-dddd|1(ddd) ddd-dddd|ddd ddddddd')
)
df_template_data = testDataSpec.build()
counts = df_template_data.agg(
F.countDistinct("email").alias("email_count"),
F.countDistinct("ip_addr").alias("ip_addr_count"),
F.countDistinct("phone").alias("phone_count")
).collect()[0]
assert counts['email_count'] >= 100
assert counts['ip_addr_count'] >= 100
assert counts['phone_count'] >= 100
def test_raw_iltext_text_generation(self):
""" As the test coverage tools dont detect code only used in UDFs,
lets add some explicit tests for the underlying code"""
# test the IL Text generator
tg1 = dg.ILText(paragraphs=(1, 4), sentences=(2, 6), words=(1, 8))
# test the repr
desc = repr(tg1)
assert desc is not None and len(desc.strip()) > 0
# now test generation of text
base_rows = np.arange(1000)
test_values = tg1.generateText(base_rows, base_rows.size)
data = test_values.tolist()
match_pattern = re.compile(r"(\s?[A-Z]([a-z ]+)\.\s*)+")
# check that paras only contains text
for test_value in data:
assert test_value is not None
assert match_pattern.match(test_value)
def test_large_ILText_driven_data_generation(self):
testDataSpec = (dg.DataGenerator(sparkSession=spark, name="test_data_set1", rows=1000000,
partitions=8)
.withSchema(schema)
.withIdOutput()
.withColumnSpec("date", percentNulls=0.1)
.withColumnSpec("nint", percentNulls=0.1, minValue=1, maxValue=9, step=2)
.withColumnSpec("nstr1", percentNulls=0.1, minValue=1, maxValue=9, step=2)
.withColumnSpec("nstr2", percentNulls=0.1, minValue=1.5, maxValue=2.5, step=0.3,
format="%04f")
.withColumnSpec("nstr3", minValue=1.0, maxValue=9.0, step=2.0)
.withColumnSpec("nstr4", percentNulls=0.1, minValue=1, maxValue=9, step=2, format="%04d")
.withColumnSpec("nstr5", percentNulls=0.1, minValue=1.5, maxValue=2.5, step=0.3, random=True)
.withColumnSpec("nstr6", percentNulls=0.1, minValue=1.5, maxValue=2.5, step=0.3, random=True,
format="%04f")
.withColumn("paras", text=dg.ILText(paragraphs=(1, 4), sentences=(2, 6), words=(1, 8)))
)
df_template_data = testDataSpec.build()
counts = df_template_data.agg(
F.countDistinct("paras").alias("paragraphs_count")
).collect()[0]
assert counts['paragraphs_count'] >= 100
df_template_data = testDataSpec.build()
counts = df_template_data.agg(
F.countDistinct("email").alias("email_count"),
F.countDistinct("ip_addr").alias("ip_addr_count"),
F.countDistinct("phone").alias("phone_count")
).collect()[0]
assert counts['email_count'] >= 100
assert counts['ip_addr_count'] >= 100
assert counts['phone_count'] >= 100
def test_small_ILText_driven_data_generation(self):
testDataSpec = (dg.DataGenerator(sparkSession=spark, name="test_data_set1", rows=100000,
partitions=8)
.withSchema(schema)
.withIdOutput()
.withColumnSpec("date", percentNulls=0.1)
.withColumnSpec("nint", percentNulls=0.1, minValue=1, maxValue=9, step=2)
.withColumnSpec("nstr1", percentNulls=0.1, minValue=1, maxValue=9, step=2)
.withColumnSpec("nstr2", percentNulls=0.1, minValue=1.5, maxValue=2.5, step=0.3,
format="%04f")
.withColumnSpec("nstr3", minValue=1.0, maxValue=9.0, step=2.0)
.withColumnSpec("nstr4", percentNulls=0.1, minValue=1, maxValue=9, step=2, format="%04d")
.withColumnSpec("nstr5", percentNulls=0.1, minValue=1.5, maxValue=2.5, step=0.3, random=True)
.withColumnSpec("nstr6", percentNulls=0.1, minValue=1.5, maxValue=2.5, step=0.3, random=True,
format="%04f")
.withColumn("paras", text=dg.ILText(paragraphs=(1, 4), sentences=(2, 6), words=(1, 8)))
)
df_iltext_data = testDataSpec.build()
counts = df_iltext_data.agg(
F.countDistinct("paras").alias("paragraphs_count")
).collect()[0]
assert counts['paragraphs_count'] >= 10
data = df_iltext_data.limit(1000).collect()
match_pattern = re.compile(r"(\s?[A-Z]([a-z ]+)\.\s*)+")
# check that paras only contains text
for r in data:
test_value = r['paras']
assert test_value is not None
assert match_pattern.match(test_value)
@pytest.mark.parametrize("template, expectedOutput, escapeSpecial",
[(r'\n.\n.\n.\n', r"[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+", False),
(r'(ddd)-ddd-dddd|1(ddd) ddd-dddd|ddd ddddddd',
r"(\([0-9]+\)-[0-9]+-[0-9]+)|(1\([0-9]+\) [0-9]+-[0-9]+)|([0-9]+ [0-9]+)", False),
(r'(\d\d\d)-\d\d\d-\d\d\d\d|1(\d\d\d) \d\d\d-\d\d\d\d|\d\d\d \d\d\d\d\d\d\d',
r"(\([0-9]+\)-[0-9]+-[0-9]+)|(1\([0-9]+\) [0-9]+-[0-9]+)|([0-9]+ [0-9]+)", True),
(r'\dr_\v', r"dr_[0-9]+", False),
(r'\w.\w@\w.com|\w@\w.co.u\k', r"[a-z\.\@]+", False),
])
def test_raw_template_text_generation1(self, template, expectedOutput, escapeSpecial):
""" As the test coverage tools dont detect code only used in UDFs,
lets add some explicit tests for the underlying code"""
match_pattern = re.compile(expectedOutput)
test_template = TemplateGenerator(template, escapeSpecialChars=escapeSpecial)
test_values = test_template.pandasGenerateText(pd.Series(list(range(1, 1000))))
def check_pattern(x):
assert match_pattern.match(x), f"test pattern '{expectedOutput}' does not match result '{x}'"
test_values.apply(lambda x: check_pattern(x))
for test_value in test_values:
assert test_value is not None
assert match_pattern.match(test_value), f"pattern '{expectedOutput}' doesn't match result '{test_value}'"
def test_raw_template_text_generation3(self):
""" As the test coverage tools don't detect code only used in UDFs,
lets add some explicit tests for the underlying code"""
pattern = r'\w.\w@\w.com|\w@\w.co.u\k'
match_pattern = re.compile(r"[a-z\.\@]+")
test_template = TemplateGenerator(pattern)
test_values = [test_template.classicGenerateText(x) for x in range(1, 1000)]
for test_value in test_values:
assert test_value is not None
assert match_pattern.match(test_value)
def test_simple_data2(self):
testDataSpec2 = (dg.DataGenerator(sparkSession=spark, name="test_data_set2", rows=self.row_count,
partitions=self.partitions_requested)
.withSchema(schema)
.withIdOutput()
.withColumnSpec("date", percent_nulls=0.1)
.withColumnSpecs(patterns="n.*", match_types=StringType(),
percent_nulls=0.1, minValue=1, maxValue=9, step=2)
.withColumnSpecs(patterns="n.*", match_types=IntegerType(),
percent_nulls=0.1, minValue=1, maxValue=200, step=-2)
.withColumnSpec("email", template=r'\w.\w@\w.com|\w@\w.co.u\k')
.withColumnSpec("ip_addr", template=r'\n.\n.\n.\n')
.withColumnSpec("phone", template=r'(ddd)-ddd-dddd|1(ddd) ddd-dddd|ddd ddddddd')
)
testDataSpec2.build().show()
df_template_data = testDataSpec2.build()
counts = df_template_data.agg(
F.countDistinct("email").alias("email_count"),
F.countDistinct("ip_addr").alias("ip_addr_count"),
F.countDistinct("phone").alias("phone_count")
).collect()[0]
assert counts['email_count'] >= 100
assert counts['ip_addr_count'] >= 100
assert counts['phone_count'] >= 100
def test_multi_columns(self):
testDataSpec3 = (dg.DataGenerator(sparkSession=spark, name="test_data_set3", rows=self.row_count,
partitions=self.partitions_requested, verbose=True)
.withIdOutput()
.withColumn("val1", IntegerType(), percentNulls=0.1)
.withColumn("val2", IntegerType(), percentNulls=0.1)
.withColumn("val3", StringType(), baseColumn=["val1", "val2"], baseColumnType="values",
template=r"\v-1")
)
testDataSpec3.build().show()
def test_multi_columns2(self):
testDataSpec4 = (dg.DataGenerator(sparkSession=spark, name="test_data_set3", rows=self.row_count,
partitions=self.partitions_requested, verbose=True)
.withIdOutput()
.withColumn("val1", IntegerType(), percentNulls=0.1)
.withColumn("val2", IntegerType(), percentNulls=0.1)
.withColumn("val3", StringType(), baseColumn=["val1", "val2"], baseColumnType="values",
template=r"\v0-\v1")
)
# in this case we expect values of the form `<first-value> - <second-value>`
testDataSpec4.build().show()
def test_multi_columns3(self):
testDataSpec5 = (dg.DataGenerator(sparkSession=spark, name="test_data_set3", rows=self.row_count,
partitions=self.partitions_requested, verbose=True)
.withIdOutput()
.withColumn("val1", IntegerType(), percentNulls=0.1)
.withColumn("val2", IntegerType(), percentNulls=0.1)
.withColumn("val3", StringType(), baseColumn=["val1", "val2"], baseColumnType="values",
template=r"\v\0-\v\1")
)
# in this case we expect values of the form `[ array of values]0 - [array of values]1`
testDataSpec5.build().show()
def test_multi_columns4(self):
testDataSpec6 = (dg.DataGenerator(sparkSession=spark, name="test_data_set3", rows=self.row_count,
partitions=self.partitions_requested, verbose=True)
.withIdOutput()
.withColumn("val1", IntegerType(), percentNulls=0.1)
.withColumn("val2", IntegerType(), percentNulls=0.1)
.withColumn("val3", StringType(), baseColumn=["val1", "val2"], baseColumnType="hash",
template=r"\v0-\v1")
)
# here the values for val3 are undefined as the base value for the column is a hash of the base columns
testDataSpec6.build().show()
def test_multi_columns5(self):
testDataSpec7 = (dg.DataGenerator(sparkSession=spark, name="test_data_set3", rows=self.row_count,
partitions=self.partitions_requested, verbose=True)
.withIdOutput()
.withColumn("val1", IntegerType(), percentNulls=0.1)
.withColumn("val2", IntegerType(), percentNulls=0.1)
.withColumn("val3", StringType(), baseColumn=["val1", "val2"], baseColumnType="hash",
format="%s")
)
# here the values for val3 are undefined as the base value for the column is a hash of the base columns
testDataSpec7.build().show()
def test_multi_columns6(self):
testDataSpec8 = (dg.DataGenerator(sparkSession=spark, name="test_data_set3", rows=self.row_count,
partitions=self.partitions_requested, verbose=True)
.withIdOutput()
.withColumn("val1", IntegerType(), percentNulls=0.1)
.withColumn("val2", IntegerType(), percentNulls=0.1)
.withColumn("val3", StringType(), baseColumn=["val1", "val2"], baseColumnType="values",
format="%s")
)
# here the values for val3 are undefined as the base value for the column is a hash of the base columns
testDataSpec8.build().show()
def test_multi_columns7(self):
testDataSpec9 = (dg.DataGenerator(sparkSession=spark, name="test_data_set3", rows=self.row_count,
partitions=self.partitions_requested, verbose=True)
.withIdOutput()
.withColumn("val1", IntegerType(), percentNulls=0.1)
.withColumn("val2", IntegerType(), percentNulls=0.1)
.withColumn("val3", StringType(), baseColumn=["val1", "val2"], format="%s")
)
# here the values for val3 are undefined as the base value for the column is a hash of the base columns
testDataSpec9.build().show()
def test_prefix(self):
# will have implied column `id` for ordinal of row
testdata_generator = (
dg.DataGenerator(sparkSession=spark, name="test_dataset1", rows=100000, partitions=20)
.withIdOutput() # id column will be emitted in the output
.withColumn("code1", "integer", minValue=1, maxValue=20, step=1)
.withColumn("code2", "integer", minValue=1, maxValue=20, step=1)
.withColumn("code3", "integer", minValue=1, maxValue=20, step=1)
.withColumn("code4", "integer", minValue=1, maxValue=20, step=1)
# base column specifies dependent column
.withColumn("site_cd", "string", prefix='site', baseColumn='code1')
.withColumn("device_status", "string", minValue=1, maxValue=200, step=1, prefix='status', random=True)
.withColumn("site_cd2", "string", prefix='site', baseColumn='code1', text_separator=":")
.withColumn("device_status2", "string", minValue=1, maxValue=200, step=1,
prefix='status', text_separator=":")
)
df = testdata_generator.build() # build our dataset
numRows = df.count()
assert numRows == 100000
df2 = testdata_generator.option("startingId", 200000).build() # build our dataset
df2.count()
# check `code` values
code1_values = [r[0] for r in df.select("code1").distinct().collect()]
assert set(code1_values) == set(range(1, 21))
code2_values = [r[0] for r in df.select("code2").distinct().collect()]
assert set(code2_values) == set(range(1, 21))
code3_values = [r[0] for r in df.select("code3").distinct().collect()]
assert set(code3_values) == set(range(1, 21))
code4_values = [r[0] for r in df.select("code3").distinct().collect()]
assert set(code4_values) == set(range(1, 21))
site_codes = [f"site_{x}" for x in range(1, 21)]
site_code_values = [r[0] for r in df.select("site_cd").distinct().collect()]
assert set(site_code_values) == set(site_codes)
status_codes = [f"status_{x}" for x in range(1, 201)]
status_code_values = [r[0] for r in df.select("device_status").distinct().collect()]
assert set(status_code_values) == set(status_codes)
site_codes = [f"site:{x}" for x in range(1, 21)]
site_code_values = [r[0] for r in df.select("site_cd2").distinct().collect()]
assert set(site_code_values) == set(site_codes)
status_codes = [f"status:{x}" for x in range(1, 201)]
status_code_values = [r[0] for r in df.select("device_status2").distinct().collect()]
assert set(status_code_values) == set(status_codes)
def test_suffix(self):
# will have implied column `id` for ordinal of row
testdata_generator = (
dg.DataGenerator(sparkSession=spark, name="test_dataset1", rows=100000, partitions=20)
.withIdOutput() # id column will be emitted in the output
.withColumn("code1", "integer", minValue=1, maxValue=20, step=1)
.withColumn("code2", "integer", minValue=1, maxValue=20, step=1)
.withColumn("code3", "integer", minValue=1, maxValue=20, step=1)
.withColumn("code4", "integer", minValue=1, maxValue=20, step=1)
# base column specifies dependent column
.withColumn("site_cd", "string", suffix='site', baseColumn='code1')
.withColumn("device_status", "string", minValue=1, maxValue=200, step=1, suffix='status', random=True)
.withColumn("site_cd2", "string", suffix='site', baseColumn='code1', text_separator=":")
.withColumn("device_status2", "string", minValue=1, maxValue=200, step=1,
suffix='status', text_separator=":")
)
df = testdata_generator.build() # build our dataset
numRows = df.count()
assert numRows == 100000
site_codes = [f"{x}_site" for x in range(1, 21)]
site_code_values = [r[0] for r in df.select("site_cd").distinct().collect()]
assert set(site_code_values) == set(site_codes)
status_codes = [f"{x}_status" for x in range(1, 201)]
status_code_values = [r[0] for r in df.select("device_status").distinct().collect()]
assert set(status_code_values) == set(status_codes)
site_codes = [f"{x}:site" for x in range(1, 21)]
site_code_values = [r[0] for r in df.select("site_cd2").distinct().collect()]
assert set(site_code_values) == set(site_codes)
status_codes = [f"{x}:status" for x in range(1, 201)]
status_code_values = [r[0] for r in df.select("device_status2").distinct().collect()]
assert set(status_code_values) == set(status_codes)
def test_prefix_and_suffix(self):
# will have implied column `id` for ordinal of row
testdata_generator = (
dg.DataGenerator(sparkSession=spark, name="test_dataset1", rows=100000, partitions=20)
.withIdOutput() # id column will be emitted in the output
.withColumn("code1", "integer", minValue=1, maxValue=20, step=1)
.withColumn("code2", "integer", minValue=1, maxValue=20, step=1)
.withColumn("code3", "integer", minValue=1, maxValue=20, step=1)
.withColumn("code4", "integer", minValue=1, maxValue=20, step=1)
# base column specifies dependent column
.withColumn("site_cd", "string", suffix='site', baseColumn='code1', prefix="test")
.withColumn("device_status", "string", minValue=1, maxValue=200, step=1, suffix='status', prefix="test")
.withColumn("site_cd2", "string", suffix='site', baseColumn='code1', text_separator=":", prefix="test")
.withColumn("device_status2", "string", minValue=1, maxValue=200, step=1,
suffix='status', text_separator=":",
prefix="test")
)
df = testdata_generator.build() # build our dataset
numRows = df.count()
assert numRows == 100000
status_codes = [f"test_{x}_status" for x in range(1, 201)]
status_code_values = [r[0] for r in df.select("device_status").distinct().collect()]
assert set(status_code_values) == set(status_codes)
site_codes = [f"test:{x}:site" for x in range(1, 21)]
site_code_values = [r[0] for r in df.select("site_cd2").distinct().collect()]
assert set(site_code_values) == set(site_codes)
status_codes = [f"test:{x}:status" for x in range(1, 201)]
status_code_values = [r[0] for r in df.select("device_status2").distinct().collect()]
assert set(status_code_values) == set(status_codes)