def test_generate_dataframe_when_aggregation_nodepoint_with_many_entries_same_subkey( monkeypatch): content_list = [ '''[{ "id": "chain_1", "shops": [ {"id": "shop_1", "name": "shopname_1"} ] }]''', '''[ { "sales": [ { "billing": 111 }, { "billing": 222 }, { "billing": 333 } ] } ]''' ] nodepoint_specs = [{ 'name': 'test', 'type': 'aggregation', 'aggregation_key': 'billing', 'column_suffix': 'billing', 'subkey': 'sales' }] mock_request = build_mock_request(content_list) monkeypatch.setattr(requests, 'request', mock_request) expected = pd.DataFrame([ ['chain_1', 'shop_1', 'shopname_1', 3, 666, 0], ], columns=[ 'chain_id', 'shop_id', 'shop_name', 'test_count', 'test_billing', 'test_malformed' ]) found = generate_dataframe(nodepoint_specs) pd.testing.assert_frame_equal(expected, found, check_dtype=False)
def test_generate_dataframe_when_one_shop_one_nodepoint(monkeypatch): content_list = [ '[{ "id": "chain_1", "shops": [ {"id": "shop_id_1", "name":"shop_name_1"} ] }]', '''[ { "originalId": "oid1", "name":"name1" }, { "originalId": "oid1", "name":"name1" }, { "id": "badentry" }, { "originalId": "oid2", "name":"name2" } ]''' ] nodepoint_specs = [ { "name": "sellers", "type": "raw", "column_suffix": "distinct", "equality_key": "originalId" }, ] mock_request = build_mock_request(content_list) monkeypatch.setattr(requests, 'request', mock_request) expected = pd.DataFrame([['chain_1', 'shop_id_1', 'shop_name_1', 3, 2, 1]], columns=[ 'chain_id', 'shop_id', 'shop_name', 'sellers_count', 'sellers_distinct', 'sellers_malformed', ]) found = generate_dataframe(nodepoint_specs) pd.testing.assert_frame_equal(expected, found, check_dtype=False)
def test_generate_dataframe_when_no_shops(monkeypatch): content_list = ['[{ "id": "anychain", "shops": [] }]'] nodepoint_specs = [ { "name": "products", "type": "raw", "column_suffix": "distinct", "equality_key": "originalId" }, { "name": "sellers", "type": "raw", "column_suffix": "distinct", "equality_key": "originalId" }, ] mock_request = build_mock_request(content_list) monkeypatch.setattr(requests, 'request', mock_request) expected = pd.DataFrame(columns=[ 'chain_id', 'shop_id', 'shop_name', 'products_count', 'products_distinct', 'products_malformed', 'sellers_count', 'sellers_distinct', 'sellers_malformed', ]) found = generate_dataframe(nodepoint_specs) pd.testing.assert_frame_equal(expected, found, check_dtype=False)
def test_generate_dataframe_when_response_error(monkeypatch): def mock_request(*args, **kwargs): return MockResponse(status_code=400) monkeypatch.setattr(requests, 'request', mock_request) nodepoint_specs = [ { "name": "products", "type": "raw", "column_suffix": "distinct", "equality_key": "originalId" }, { "name": "sellers", "type": "raw", "column_suffix": "distinct", "equality_key": "originalId" }, ] expected = pd.DataFrame(columns=[ 'chain_id', 'shop_id', 'shop_name', 'products_count', 'products_distinct', 'products_malformed', 'sellers_count', 'sellers_distinct', 'sellers_malformed', ]) found = generate_dataframe(nodepoint_specs) pd.testing.assert_frame_equal(expected, found, check_dtype=False)
def test_generate_dataframe_when_raw_and_aggregation_many_nodepoints_many_shops_and_chains( monkeypatch): content_list = [ '''[{ "id": "chain_1", "shops": [ {"id": "shop_1", "name": "shopname_1"}, {"id": "shop_2", "name": "shopname_2"} ] }, { "id": "chain_2", "shops": [ {"id": "shop_3", "name": "shopname_3"} ]} ]''', '''[ { "whatis": "for chain_1 shop_1 test_1", "sales": [ { "billing": 1 } ] }, { "whatis": "for chain_1 shop_1 test_1", "sales": [ { "billing": 10 } ] }, { "whatis": "for chain_1 shop_1 test_1", "sales": [ { "billing": 100 } ] } ]''', '''[ { "whatis": "for chain_1 shop_1 test_2", "originalId": "oid1"}, { "whatis": "for chain_1 shop_1 test_2", "originalId": "oid2"}, { "whatis": "for chain_1 shop_1 test_2", "originalId": "oid2"}, { "whatis": "for chain_1 shop_1 test_2", "unexpected key": "oid3"} ]''', '''[ { "whatis": "for chain_1 shop_1 test_3","billing": 111 }, { "whatis": "for chain_1 shop_1 test_3","billing": 222 }, { "whatis": "for chain_1 shop_1 test_3","non expected": 555 } ]''', '''[ { "whatis": "for chain_1 shop_2 test_1", "sales": [ { "billing": 1000 } ] } ]''', '''[ { "whatis": "for chain_1 shop_2 test_2", "originalId": "oid122"} ]''', '''[ { "whatis": "for chain_1 shop_2 test_3", "billing": 3000 } ]''', '''[ { "whatis": "for chain_2 shop_3 test_1", "sales": [ { "billing": 10000 } ] } ]''', '''[ { "whatis": "for chain_1 shop_3 test_2", "originalId": "oid232"} ]''', '''[ { "whatis": "for chain_2 shop_3 test_3", "billing": 30000 } ]''', ] nodepoint_specs = [{ 'name': 'test_1', 'type': 'aggregation', 'aggregation_key': 'billing', 'column_suffix': 'billing', 'subkey': 'sales' }, { 'name': 'test_2', 'type': 'raw', 'equality_key': 'originalId', 'column_suffix': 'distinct' }, { 'name': 'test_3', 'type': 'aggregation', 'aggregation_key': 'billing', 'column_suffix': 'billing', 'subkey': None }] mock_request = build_mock_request(content_list) monkeypatch.setattr(requests, 'request', mock_request) expected = pd.DataFrame([[ 'chain_1', 'shop_1', 'shopname_1', 3, 111, 0, 3, 2, 1, 2, 333, 1 ], [ 'chain_1', 'shop_2', 'shopname_2', 1, 1000, 0, 1, 1, 0, 1, 3000, 0 ], ['chain_2', 'shop_3', 'shopname_3', 1, 10000, 0, 1, 1, 0, 1, 30000, 0]], columns=[ 'chain_id', 'shop_id', 'shop_name', 'test_1_count', 'test_1_billing', 'test_1_malformed', 'test_2_count', 'test_2_distinct', 'test_2_malformed', 'test_3_count', 'test_3_billing', 'test_3_malformed' ]) found = generate_dataframe(nodepoint_specs) pd.testing.assert_frame_equal(expected, found, check_dtype=False)
def test_generate_dataframe_when_aggregation_mani_nodepoints_same_shop( monkeypatch): content_list = [ '''[{ "id": "chain_1", "shops": [ {"id": "shop_1", "name": "shopname_1"} ] }]''', '''[ { "sales": [ { "billing": 1 } ] }, { "sales": [ { "billing": 10 } ] }, { "sales": [ { "billing": 100 } ] } ]''', '''[ { "sales": [ { "billing": 2 } ] }, { "sales": [ { "billing": 200 } ] } ]''', '''[ { "billing": 111 }, { "billing": 222 }, { "non expected": 555 } ]''' ] nodepoint_specs = [{ 'name': 'test_1', 'type': 'aggregation', 'aggregation_key': 'billing', 'column_suffix': 'billing', 'subkey': 'sales' }, { 'name': 'test_2', 'type': 'aggregation', 'aggregation_key': 'billing', 'column_suffix': 'billing', 'subkey': 'sales' }, { 'name': 'test_3', 'type': 'aggregation', 'aggregation_key': 'billing', 'column_suffix': 'billing', 'subkey': None }] mock_request = build_mock_request(content_list) monkeypatch.setattr(requests, 'request', mock_request) expected = pd.DataFrame([ ['chain_1', 'shop_1', 'shopname_1', 3, 111, 0, 2, 202, 0, 2, 333, 1], ], columns=[ 'chain_id', 'shop_id', 'shop_name', 'test_1_count', 'test_1_billing', 'test_1_malformed', 'test_2_count', 'test_2_billing', 'test_2_malformed', 'test_3_count', 'test_3_billing', 'test_3_malformed' ]) found = generate_dataframe(nodepoint_specs) pd.testing.assert_frame_equal(expected, found, check_dtype=False)