"""Unit tests for query builder functions.""" import pytest from api.utils import query @pytest.mark.parametrize( 'upc, exclude_tt, include_tt, expected_param_values', [ (7777, None, None, [7777, [], []]), (8888, [], [], [8888, [], []]), (8888, None, [], [8888, [], []]), (8888, [], None, [8888, [], []]), (9999, [1, 2, 3], None, [9999, [1, 2, 3], []]), (9999, [1, 2, 3], [], [9999, [1, 2, 3], []]), (9999, None, [4, 5, 6], [9999, [], [4, 5, 6]]), (9999, [], [4, 5, 6], [9999, [], [4, 5, 6]]), (9999, [1, 2, 3], [4, 5, 6], [9999, [1, 2, 3], [4, 5, 6]]), (9999, '123', [4, 5, 6], [9999, ['123'], [4, 5, 6]]), (9999, [1, 2, 3], '456', [9999, [1, 2, 3], ['456']]), ] ) def test_build_where_clause( upc, exclude_tt, include_tt, expected_param_values, monkeypatch): """Test build_where_clause. The param expected_param_values format is [upc, exclude_tt, include_tt]. """ sql, params = query.build_where_clause(upc, exclude_tt, include_tt) e_upc, e_exc_tt, e_inc_tt = expected_param_values assert params['upc'] == e_upc for tt in e_exc_tt: assert tt in params.values() for tt in e_inc_tt: assert tt in params.values() @pytest.mark.parametrize( 'transaction_type_ids, expected', [ ( [1, 2, 3], ( 'transaction_type_id NOT IN ' '(%(tt_exc_id_0)s, %(tt_exc_id_1)s, %(tt_exc_id_2)s)', {'tt_exc_id_0': 1, 'tt_exc_id_1': 2, 'tt_exc_id_2': 3} ) ), ([], ('', {})) ] ) def test_build_not_in_transaction_types(transaction_type_ids, expected): """Test build_not_in_transaction_types.""" assert query.build_not_in_transaction_types( transaction_type_ids) == expected @pytest.mark.parametrize( 'transaction_type_ids, expected', [ ( [1, 2, 3], ( 'transaction_type_id IN ' '(%(tt_inc_id_0)s, %(tt_inc_id_1)s, %(tt_inc_id_2)s)', {'tt_inc_id_0': 1, 'tt_inc_id_1': 2, 'tt_inc_id_2': 3} ) ), ([], ('', {})) ] ) def test_build_in_transaction_types(transaction_type_ids, expected): """Test build_in_transaction_types.""" assert query.build_in_transaction_types( transaction_type_ids) == expected