Skip to content
This repository has been archived by the owner on Jul 17, 2023. It is now read-only.

Commit

Permalink
add validate_or_make_namedtuples
Browse files Browse the repository at this point in the history
  • Loading branch information
skoudoro committed Jan 31, 2022
1 parent 71c0884 commit 44a0d95
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
8 changes: 8 additions & 0 deletions mailerlite/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,11 @@
for nt in [Subscriber, Field, Group, Activity, Segment, Meta, Pagination,
Campaign, Stats, Webhook]:
nt.__new__.__defaults__ = (None,) * len(nt._fields)


def validate_or_make_namedtuples(obj, keys):

if not set(keys).issubset(obj._fields):
diff = set(list(obj._fields)).symmetric_difference(set(keys))
return namedtuple(f'{obj.__name__}2', obj._fields + tuple(diff))
return obj
23 changes: 23 additions & 0 deletions mailerlite/tests/test_constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import ipdb
from mailerlite.constants import validate_or_make_namedtuples, Segment


def test_validate_or_make_namedtuples():

new_segment = validate_or_make_namedtuples(Segment,
['id', 'title', 'filter',
'total', 'sent', 'opened',
'clicked','created_at',
'updated_at', 'timed_out'])

assert new_segment == Segment

new_segment = validate_or_make_namedtuples(Segment,
['id', 'title', 'filter',
'total', 'sent',
'opened', 'clicked',
'created_at', 'updated_at',
'timed_out', 'custom'])

assert new_segment != Segment
assert 'custom' in new_segment._fields

0 comments on commit 44a0d95

Please sign in to comment.