-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathbase.py
More file actions
620 lines (568 loc) · 25.2 KB
/
Copy pathbase.py
File metadata and controls
620 lines (568 loc) · 25.2 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
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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
import unittest
import os
import tap_tester.connections as connections
import tap_tester.menagerie as menagerie
import tap_tester.runner as runner
from datetime import datetime as dt
from datetime import timedelta
import dateutil.parser
import pytz
class ActiveCampaignTest(unittest.TestCase):
start_date = ""
START_DATE_FORMAT = "%Y-%m-%dT00:00:00Z"
PRIMARY_KEYS = "table-key-properties"
REPLICATION_METHOD = "forced-replication-method"
REPLICATION_KEYS = "valid-replication-keys"
FULL_TABLE = "FULL_TABLE"
INCREMENTAL = "INCREMENTAL"
OBEYS_START_DATE = "obey-start-date"
BOOKMARK_COMPARISON_FORMAT = "%Y-%m-%dT%H:%M:%S.%fZ"
def tap_name(self):
"""The name of the tap"""
return "tap-activecampaign"
def setUp(self):
required_env = {
"TAP_ACTIVECAMPAIGN_API_TOKEN",
"TAP_ACTIVECAMPAIGN_API_URL",
}
missing_envs = [v for v in required_env if not os.getenv(v)]
if missing_envs:
raise Exception("set " + ", ".join(missing_envs))
def get_type(self):
return "platform.activecampaign"
def get_credentials(self):
"""Authentication information for the test account"""
return {
'api_url': os.getenv('TAP_ACTIVECAMPAIGN_API_URL'),
'api_token': os.getenv('TAP_ACTIVECAMPAIGN_API_TOKEN')
}
def get_properties(self, original: bool = True):
"""Configuration properties required for the tap."""
return_value = {
"start_date" : "2021-12-01T00:00:00Z",
}
if original:
return return_value
# Reassign start date
return_value["start_date"] = self.start_date
return return_value
def expected_metadata(self):
"""The expected streams and metadata about the streams"""
return {
'accounts': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.INCREMENTAL,
self.REPLICATION_KEYS: {'updated_timestamp'},
self.OBEYS_START_DATE: True
},
'account_contacts': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.INCREMENTAL,
self.REPLICATION_KEYS: {'updated_timestamp'},
self.OBEYS_START_DATE: True
},
'account_custom_fields': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.INCREMENTAL,
self.REPLICATION_KEYS: {'updated_timestamp'},
self.OBEYS_START_DATE: True
},
'account_custom_field_values': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.INCREMENTAL,
self.REPLICATION_KEYS: {'updated_timestamp'},
self.OBEYS_START_DATE: True
},
'addresses': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.FULL_TABLE,
self.OBEYS_START_DATE: False
},
'automations': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.INCREMENTAL,
self.REPLICATION_KEYS: {'mdate'},
self.OBEYS_START_DATE: True
},
'brandings': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.FULL_TABLE,
self.OBEYS_START_DATE: False
},
'calendars': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.INCREMENTAL,
self.REPLICATION_KEYS: {'mdate'},
self.OBEYS_START_DATE: True
},
'campaigns': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.INCREMENTAL,
self.REPLICATION_KEYS: {'updated_timestamp'},
self.OBEYS_START_DATE: True
},
'campaign_links': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.INCREMENTAL,
self.REPLICATION_KEYS: {'updated_timestamp'},
self.OBEYS_START_DATE: True
},
'contacts': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.INCREMENTAL,
self.REPLICATION_KEYS: {'updated_timestamp'},
self.OBEYS_START_DATE: True
},
'contact_automations': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.INCREMENTAL,
self.REPLICATION_KEYS: {'lastdate'},
self.OBEYS_START_DATE: True
},
'contact_custom_fields': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.FULL_TABLE,
self.OBEYS_START_DATE: False
},
'contact_custom_field_options': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.FULL_TABLE,
self.OBEYS_START_DATE: False
},
'contact_custom_field_rels': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.FULL_TABLE,
self.OBEYS_START_DATE: False
},
'contact_custom_field_values': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.INCREMENTAL,
self.REPLICATION_KEYS: {'udate'},
self.OBEYS_START_DATE: True
},
'contact_deals': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.INCREMENTAL,
self.REPLICATION_KEYS: {'updated_timestamp'},
self.OBEYS_START_DATE: True
},
'deal_stages': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.INCREMENTAL,
self.REPLICATION_KEYS: {'udate'},
self.OBEYS_START_DATE: True
},
'deal_groups': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.INCREMENTAL,
self.REPLICATION_KEYS: {'udate'},
self.OBEYS_START_DATE: True
},
'deal_custom_fields': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.INCREMENTAL,
self.REPLICATION_KEYS: {'updated_timestamp'},
self.OBEYS_START_DATE: True
},
'deal_custom_field_values': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.INCREMENTAL,
self.REPLICATION_KEYS: {'updated_timestamp'},
self.OBEYS_START_DATE: True
},
'deals': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.INCREMENTAL,
self.REPLICATION_KEYS: {'mdate'},
self.OBEYS_START_DATE: True
},
'ecommerce_connections': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.INCREMENTAL,
self.REPLICATION_KEYS: {'udate'},
self.OBEYS_START_DATE: True
},
'ecommerce_customers': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.INCREMENTAL,
self.REPLICATION_KEYS: {'tstamp'},
self.OBEYS_START_DATE: True
},
'ecommerce_orders': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.INCREMENTAL,
self.REPLICATION_KEYS: {'updated_date'},
self.OBEYS_START_DATE: True
},
'ecommerce_order_products': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.FULL_TABLE,
self.OBEYS_START_DATE: False
},
'forms': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.INCREMENTAL,
self.REPLICATION_KEYS: {'udate'},
self.OBEYS_START_DATE: True
},
'groups': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.FULL_TABLE,
self.OBEYS_START_DATE: False
},
'lists': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.INCREMENTAL,
self.REPLICATION_KEYS: {'updated_timestamp'},
self.OBEYS_START_DATE: True
},
'messages': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.INCREMENTAL,
self.REPLICATION_KEYS: {'mdate'},
self.OBEYS_START_DATE: True
},
'saved_responses': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.INCREMENTAL,
self.REPLICATION_KEYS: {'mdate'},
self.OBEYS_START_DATE: True
},
'scores': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.INCREMENTAL,
self.REPLICATION_KEYS: {'mdate'},
self.OBEYS_START_DATE: True
},
'segments': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.FULL_TABLE,
self.OBEYS_START_DATE: False
},
'tags': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.FULL_TABLE,
self.OBEYS_START_DATE: False
},
'task_types': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.FULL_TABLE,
self.OBEYS_START_DATE: False
},
'tasks': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.INCREMENTAL,
self.REPLICATION_KEYS: {'udate'},
self.OBEYS_START_DATE: True
},
'templates': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.INCREMENTAL,
self.REPLICATION_KEYS: {'mdate'},
self.OBEYS_START_DATE: True
},
'users': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.FULL_TABLE,
self.OBEYS_START_DATE: False
},
'webhooks': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.FULL_TABLE,
self.OBEYS_START_DATE: False
},
'activities': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.INCREMENTAL,
self.REPLICATION_KEYS: {'tstamp'},
self.OBEYS_START_DATE: True
},
'automation_blocks': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.INCREMENTAL,
self.REPLICATION_KEYS: {'mdate'},
self.OBEYS_START_DATE: True
},
'bounce_logs': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.INCREMENTAL,
self.REPLICATION_KEYS: {'updated_timestamp'},
self.OBEYS_START_DATE: True
},
'campaign_lists': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.FULL_TABLE,
self.OBEYS_START_DATE: False
},
'campaign_messages': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.FULL_TABLE,
self.OBEYS_START_DATE: False
},
'configs': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.INCREMENTAL,
self.REPLICATION_KEYS: {'updated_timestamp'},
self.OBEYS_START_DATE: True
},
'contact_data': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.INCREMENTAL,
self.REPLICATION_KEYS: {'tstamp'},
self.OBEYS_START_DATE: True
},
'contact_emails': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.INCREMENTAL,
self.REPLICATION_KEYS: {'sdate'},
self.OBEYS_START_DATE: True
},
'contact_lists': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.INCREMENTAL,
self.REPLICATION_KEYS: {'updated_timestamp'},
self.OBEYS_START_DATE: True
},
'contact_tags': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.INCREMENTAL,
self.REPLICATION_KEYS: {'updated_timestamp'},
self.OBEYS_START_DATE: True
},
'contact_conversions': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.INCREMENTAL,
self.REPLICATION_KEYS: {'cdate'},
self.OBEYS_START_DATE: True
},
'conversions': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.INCREMENTAL,
self.REPLICATION_KEYS: {'udate'},
self.OBEYS_START_DATE: True
},
'conversion_triggers': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.INCREMENTAL,
self.REPLICATION_KEYS: {'udate'},
self.OBEYS_START_DATE: True
},
'deal_activities': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.INCREMENTAL,
self.REPLICATION_KEYS: {'cdate'},
self.OBEYS_START_DATE: True
},
'deal_group_users': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.FULL_TABLE,
self.OBEYS_START_DATE: False
},
'ecommerce_order_activities': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.INCREMENTAL,
self.REPLICATION_KEYS: {'updated_date'},
self.OBEYS_START_DATE: True
},
'email_activities': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.INCREMENTAL,
self.REPLICATION_KEYS: {'tstamp'},
self.OBEYS_START_DATE: True
},
'goals': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.FULL_TABLE,
self.OBEYS_START_DATE: False
},
'site_messages': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.INCREMENTAL,
self.REPLICATION_KEYS: {'ldate'},
self.OBEYS_START_DATE: True
},
'sms': {
self.PRIMARY_KEYS: {'id'},
self.REPLICATION_METHOD: self.INCREMENTAL,
self.REPLICATION_KEYS: {'tstamp'},
self.OBEYS_START_DATE: True
}
}
def expected_check_streams(self):
"""A set of expected stream names"""
return set(self.expected_metadata().keys())
def expected_replication_keys(self):
"""return a dictionary with key of table name and value as a set of replication key fields"""
return {table: properties.get(self.REPLICATION_KEYS, set()) for table, properties
in self.expected_metadata().items()}
def expected_primary_keys(self):
"""return a dictionary with key of table name and value as a set of primary key fields"""
return {table: properties.get(self.PRIMARY_KEYS, set()) for table, properties
in self.expected_metadata().items()}
def expected_replication_method(self):
"""return a dictionary with key of table name nd value of replication method"""
return {table: properties.get(self.REPLICATION_METHOD, set()) for table, properties
in self.expected_metadata().items()}
def expected_automatic_fields(self):
"""return a dictionary with key of table name and set of value of automatic(primary key and bookmark field) fields"""
auto_fields = {}
for k, v in self.expected_metadata().items():
auto_fields[k] = v.get(self.PRIMARY_KEYS, set()) | v.get(self.REPLICATION_KEYS, set())
return auto_fields
def run_and_verify_check_mode(self, conn_id):
"""
Run the tap in check mode and verify it succeeds.
This should be ran prior to field selection and initial sync.
Return the connection id and found catalogs from menagerie.
"""
# run in check mode
check_job_name = runner.run_check_mode(self, conn_id)
# verify check exit codes
exit_status = menagerie.get_exit_status(conn_id, check_job_name)
menagerie.verify_check_exit_status(self, exit_status, check_job_name)
found_catalogs = menagerie.get_catalogs(conn_id)
self.assertGreater(len(found_catalogs), 0, msg="unable to locate schemas for connection {}".format(conn_id))
found_catalog_names = set(map(lambda c: c['stream_name'], found_catalogs))
self.assertSetEqual(self.expected_check_streams(), found_catalog_names, msg="discovered schemas do not match")
print("discovered schemas are OK")
return found_catalogs
def run_and_verify_sync(self, conn_id):
"""
Run a sync job and make sure it exited properly.
Return a dictionary with keys of streams synced
and values of records synced for each stream
"""
# Run a sync job using orchestrator
sync_job_name = runner.run_sync_mode(self, conn_id)
# verify tap and target exit codes
exit_status = menagerie.get_exit_status(conn_id, sync_job_name)
menagerie.verify_sync_exit_status(self, exit_status, sync_job_name)
sync_record_count = runner.examine_target_output_file(self,
conn_id,
self.expected_check_streams(),
self.expected_primary_keys())
self.assertGreater(
sum(sync_record_count.values()), 0,
msg="failed to replicate any data: {}".format(sync_record_count)
)
print("total replicated row count: {}".format(sum(sync_record_count.values())))
return sync_record_count
def perform_and_verify_table_and_field_selection(self,
conn_id,
test_catalogs,
select_all_fields=True):
"""
Perform table and field selection based off of the streams to select
set and field selection parameters.
Verify this results in the expected streams selected and all or no
fields selected for those streams.
"""
# Select all available fields or select no fields from all testable streams
self.select_all_streams_and_fields(
conn_id=conn_id, catalogs=test_catalogs, select_all_fields=select_all_fields
)
catalogs = menagerie.get_catalogs(conn_id)
# Ensure our selection affects the catalog
expected_selected = [tc.get('stream_name') for tc in test_catalogs]
for cat in catalogs:
catalog_entry = menagerie.get_annotated_schema(conn_id, cat['stream_id'])
# Verify all testable streams are selected
selected = catalog_entry.get('annotated-schema').get('selected')
print("Validating selection on {}: {}".format(cat['stream_name'], selected))
if cat['stream_name'] not in expected_selected:
self.assertFalse(selected, msg="Stream selected, but not testable.")
continue # Skip remaining assertions if we aren't selecting this stream
self.assertTrue(selected, msg="Stream not selected.")
if select_all_fields:
# Verify all fields within each selected stream are selected
for field, field_props in catalog_entry.get('annotated-schema').get('properties').items():
field_selected = field_props.get('selected')
print("\tValidating selection on {}.{}: {}".format(
cat['stream_name'], field, field_selected))
self.assertTrue(field_selected, msg="Field not selected.")
else:
# Verify only automatic fields are selected
expected_automatic_fields = self.expected_automatic_fields().get(cat['stream_name'])
selected_fields = self.get_selected_fields_from_metadata(catalog_entry['metadata'])
self.assertEqual(expected_automatic_fields, selected_fields)
@staticmethod
def get_selected_fields_from_metadata(metadata):
selected_fields = set()
for field in metadata:
is_field_metadata = len(field['breadcrumb']) > 1
inclusion_automatic_or_selected = (
field['metadata']['selected'] is True or \
field['metadata']['inclusion'] == 'automatic'
)
if is_field_metadata and inclusion_automatic_or_selected:
selected_fields.add(field['breadcrumb'][1])
return selected_fields
@staticmethod
def select_all_streams_and_fields(conn_id, catalogs, select_all_fields: bool = True):
"""Select all streams and all fields within streams"""
for catalog in catalogs:
schema = menagerie.get_annotated_schema(conn_id, catalog['stream_id'])
non_selected_properties = []
if not select_all_fields:
# get a list of all properties so that none are selected
non_selected_properties = schema.get('annotated-schema', {}).get(
'properties', {}).keys()
connections.select_catalog_and_fields_via_metadata(
conn_id, catalog, schema, [], non_selected_properties)
def calculated_states_by_stream(self, current_state, currently_syncing_stream=None):
timedelta_by_stream = {stream: [0,0,1] # {stream_name: [days, hours, minutes], ...}
for stream in self.expected_check_streams()}
stream_to_calculated_state = {stream: "" for stream in current_state['bookmarks'].keys()}
for stream, state in current_state['bookmarks'].items():
# Skip the currently syncing stream
if (currently_syncing_stream and stream == currently_syncing_stream) or self.expected_replication_method()[stream] == 'FULL_TABLE':
continue
state_as_datetime = dateutil.parser.parse(state)
days, hours, minutes = timedelta_by_stream[stream]
calculated_state_as_datetime = state_as_datetime - timedelta(days=days, hours=hours, minutes=minutes)
calculated_state_formatted = dt.strftime(calculated_state_as_datetime, self.BOOKMARK_COMPARISON_FORMAT)
stream_to_calculated_state[stream] = calculated_state_formatted
return stream_to_calculated_state
def convert_state_to_utc(self, date_str):
"""
Convert a saved bookmark value of the form '2020-08-25T13:17:36-07:00' to
a string formatted utc datetime,
in order to compare aginast json formatted datetime values
"""
date_object = dateutil.parser.parse(date_str)
date_object_utc = date_object.astimezone(tz=pytz.UTC)
return dt.strftime(date_object_utc, "%Y-%m-%dT%H:%M:%SZ")
def timedelta_formatted(self, dtime, days=0):
try:
date_stripped = dt.strptime(dtime, self.START_DATE_FORMAT)
return_date = date_stripped + timedelta(days=days)
return dt.strftime(return_date, self.START_DATE_FORMAT)
except ValueError:
return Exception("Datetime object is not of the format: {}".format(self.START_DATE_FORMAT))
def parse_date(self, date_value):
"""
Pass in string-formatted-datetime, parse the value, and return it as an unformatted datetime object.
"""
date_formats = {
"%Y-%m-%dT%H:%M:%S.%fZ",
"%Y-%m-%dT%H:%M:%SZ",
"%Y-%m-%dT%H:%M:%S.%f+00:00",
"%Y-%m-%dT%H:%M:%S+00:00",
"%Y-%m-%d"
}
for date_format in date_formats:
try:
date_stripped = dt.strptime(date_value, date_format)
return date_stripped
except ValueError:
continue
raise NotImplementedError(
"Tests do not account for dates of this format: {}".format(date_value))
@staticmethod
def assertIsDateFormat(value, str_format):
"""Assertion Method that verifies a string value is a formatted
datetime with the specified format."""
try:
dt.strptime(value, str_format)
except ValueError as err:
raise AssertionError(f"Value: {value} does not conform to expected format: {str_format}") from err