1import pytest 2 3from aesqlapius import generate_api 4 5from .fixtures import * # noqa 6from .helpers import convert_api_to_async 7 8 9@pytest.fixture() 10async def api(queries_dir, dbenv): 11 api = generate_api(queries_dir / 'api', dbenv.driver, dbenv.db, hook=dbenv.get_query_preprocessor()) 12 convert_api_to_async(api) 13 await api.cleanup_test_table() 14 await api.create_test_table() 15 await api.fill_test_table() 16 yield api 17 await api.cleanup_test_table() 18 19 20@pytest.mark.asyncio 21async def test_pass_args(api): 22 assert await api.swap_args() == ('a', 0) 23 assert await api.swap_args(1) == ('a', 1) 24 assert await api.swap_args(a=1) == ('a', 1) 25 assert await api.swap_args(b='b') == ('b', 0) 26 assert await api.swap_args(1, 'b') == ('b', 1) 27 assert await api.swap_args(a=1, b='b') == ('b', 1) 28 assert await api.swap_args(b='b', a=1) == ('b', 1) 29 30 31@pytest.mark.asyncio 32async def test_get_nothing(api): 33 await api.get.nothing() 34 35 36@pytest.mark.asyncio 37async def test_get_single_tuple(api): 38 assert await api.get.single_tuple() == (0, 'a') 39 40 41@pytest.mark.asyncio 42async def test_get_single_dict(api): 43 assert await api.get.single_dict() == {'a': 0, 'b': 'a'} 44 45 46@pytest.mark.asyncio 47async def test_get_single_value(api): 48 assert await api.get.single_value() == 0 49 50 51@pytest.mark.asyncio 52async def test_get_iterator_tuple(api): 53 assert [v async for v in api.get.iterator_tuple()] == [ 54 (0, 'a'), 55 (1, 'b'), 56 (2, 'c'), 57 ] 58 59 60@pytest.mark.asyncio 61async def test_get_iterator_dict(api): 62 assert [v async for v in api.get.iterator_dict()] == [ 63 {'a': 0, 'b': 'a'}, 64 {'a': 1, 'b': 'b'}, 65 {'a': 2, 'b': 'c'}, 66 ] 67 68 69@pytest.mark.asyncio 70async def test_get_iterator_value(api): 71 assert [v async for v in api.get.iterator_value()] == [0, 1, 2] 72 73 74@pytest.mark.asyncio 75async def test_get_list_tuple(api): 76 assert await api.get.list_tuple() == [ 77 (0, 'a'), 78 (1, 'b'), 79 (2, 'c'), 80 ] 81 82 83@pytest.mark.asyncio 84async def test_get_list_dict(api): 85 assert await api.get.list_dict() == [ 86 {'a': 0, 'b': 'a'}, 87 {'a': 1, 'b': 'b'}, 88 {'a': 2, 'b': 'c'}, 89 ] 90 91 92@pytest.mark.asyncio 93async def test_get_list_value(api): 94 assert await api.get.list_value() == [0, 1, 2] 95 96 97@pytest.mark.asyncio 98@pytest.mark.parametrize( 99 'row_format,by,remove,expected', 100 [ 101 ('dicts', 'name', False, {0: {'a': 0, 'b': 'a'}, 1: {'a': 1, 'b': 'b'}, 2: {'a': 2, 'b': 'c'}}), 102 ('tuples', 'name', False, {0: (0, 'a'), 1: (1, 'b'), 2: (2, 'c')}), 103 ('values', 'name', False, {0: 0, 1: 1, 2: 2}), 104 105 ('dicts', 'index', False, {0: {'a': 0, 'b': 'a'}, 1: {'a': 1, 'b': 'b'}, 2: {'a': 2, 'b': 'c'}}), 106 ('tuples', 'index', False, {0: (0, 'a'), 1: (1, 'b'), 2: (2, 'c')}), 107 ('values', 'index', False, {0: 0, 1: 1, 2: 2}), 108 109 ('dicts', 'name', True, {0: {'b': 'a'}, 1: {'b': 'b'}, 2: {'b': 'c'}}), 110 ('tuples', 'name', True, {0: ('a',), 1: ('b',), 2: ('c',)}), 111 ('values', 'name', True, {0: 'a', 1: 'b', 2: 'c'}), 112 113 ('dicts', 'index', True, {0: {'b': 'a'}, 1: {'b': 'b'}, 2: {'b': 'c'}}), 114 ('tuples', 'index', True, {0: ('a',), 1: ('b',), 2: ('c',)}), 115 ('values', 'index', True, {0: 'a', 1: 'b', 2: 'c'}), 116 ] 117) 118async def test_get_dict_of(api, row_format, by, remove, expected): 119 method = f'dict_of_{row_format}_by_column_{by}{"_removed_key" if remove else ""}' 120 assert await getattr(api.get, method)() == expected 121 122 123@pytest.mark.asyncio 124async def test_get_dict_of_tuples_by_column_name_out_of_range(api): 125 with pytest.raises(KeyError): 126 await api.get.dict_of_tuples_by_column_name_out_of_range() 127 128 129@pytest.mark.asyncio 130async def test_get_dict_of_tuples_by_column_index_out_of_range(api): 131 with pytest.raises(IndexError): 132 await api.get.dict_of_tuples_by_column_index_out_of_range() 133 134 135@pytest.mark.asyncio 136async def test_get_dict_of_empties(api): 137 assert await api.get.dict_of_empty_dicts() == {0: {}, 1: {}, 2: {}} 138 assert await api.get.dict_of_empty_tuples() == {0: (), 1: (), 2: ()} 139 assert await api.get.dict_of_empty_singles() == {0: None, 1: None, 2: None} 140