Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove whitelist of receipt keys for canonicalize #53

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions tap_shopify/streams/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@
# prefer `token` if both are present and equal, convert `Token` -> `token`
# if only `Token` is present, and throw an error if both are present and
# their values are not equal
def canonicalize(transaction_dict, field_name):
def canonicalize_receipts(transaction_dict):
dict_keys = list(transaction_dict.get('receipt', {}).keys())
for field_name in dict_keys:
if field_name.capitalize() == field_name:
canonicalize_receipt_field(transaction_dict, field_name.lower())

def canonicalize_receipt_field(transaction_dict, field_name):
field_name_upper = field_name.capitalize()
value_lower = transaction_dict.get('receipt', {}).get(field_name)
value_upper = transaction_dict.get('receipt', {}).get(field_name_upper)
Expand All @@ -46,7 +52,6 @@ def canonicalize(transaction_dict, field_name):
elif value_upper:
transaction_dict["receipt"][field_name] = transaction_dict['receipt'].pop(field_name_upper)


class Transactions(Stream):
name = 'transactions'
replication_key = 'created_at'
Expand Down Expand Up @@ -89,8 +94,7 @@ def get_objects(self):
def sync(self):
for transaction in self.get_objects():
transaction_dict = transaction.to_dict()
for field_name in ['token', 'version', 'ack']:
canonicalize(transaction_dict, field_name)
canonicalize_receipts(transaction_dict)
yield transaction_dict

Context.stream_objects['transactions'] = Transactions
16 changes: 8 additions & 8 deletions tests/unittests/test_transaction_canonicalize.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
import unittest
from tap_shopify.streams.transactions import canonicalize
from tap_shopify.streams.transactions import canonicalize_receipts

class TestTransactionCanonicalize(unittest.TestCase):
class TestTransactionCanonicalizeReceipts(unittest.TestCase):
def test_unmodified_if_not_present(self):
# Note: Canonicalize has a side effect with pop(), must copy test
# Note: Canonicalize_Receipts has a side effect with pop(), must copy test
# record to compare
record = {"receipt": {"foo": "bar"}, "id": 2}
expected_record = {"receipt": {"foo": "bar"}, "id": 2}
canonicalize(record, "token")
canonicalize_receipts(record)
self.assertEqual(record, expected_record)

def test_unmodified_if_only_lower_exists(self):
record = {"receipt": {"foo": "bar"}, "id": 2}
expected_record = {"receipt": {"foo": "bar"}, "id": 2}
canonicalize(record, "foo")
canonicalize_receipts(record)
self.assertEqual(record, expected_record)

def test_lowercases_if_capital_only_exists(self):
record = {"receipt": {"Foo": "bar"}, "id": 2}
expected_record = {"receipt": {"foo": "bar"}, "id": 2}
canonicalize(record, "foo")
canonicalize_receipts(record)
self.assertEqual(record, expected_record)

def test_removes_uppercase_if_both_exist_and_are_equal(self):
record = {"receipt": {"Foo": "bar", "foo": "bar"}, "id": 2}
expected_record = {"receipt": {"foo": "bar"}, "id": 2}
canonicalize(record, "foo")
canonicalize_receipts(record)
self.assertEqual(record, expected_record)

def test_throws_if_both_exist_and_are_not_equal(self):
record = {"receipt": {"Foo": "bark", "foo": "bar"}, "id": 2}
with self.assertRaises(ValueError):
canonicalize(record, "foo")
canonicalize_receipts(record)