def test_rs_values_list_pagination(mocker, rs_factory): mocker.patch( 'connect.client.models.iterators.parse_content_range', side_effect=[ ContentRange(0, 99, 200), ContentRange(100, 199, 200), ], ) rs = rs_factory() rs._client.get = mocker.MagicMock(side_effect=[ [ { 'id': i, 'name': f'name {i}', 'inner': { 'title': f'title {i}', }, } for i in range(100) ], [ { 'id': i, 'name': f'name {i}', 'inner': { 'title': f'title {i}', }, } for i in range(100, 200) ], ]) expected = [ { 'id': i, 'inner.title': f'title {i}', } for i in range(200) ] assert list(rs.values_list('id', 'inner.title')) == expected
def test_rs_count(mocker, rs_factory): content_range = ContentRange(0, 9, 100) mocker.patch( 'connect.client.models.resourceset.parse_content_range', return_value=content_range, ) rs = rs_factory() rs._client.get = mocker.MagicMock(return_value=[]) assert rs.count() == 100 assert rs.content_range == content_range
def test_rs_iterate(mocker, rs_factory): mocker.patch( 'connect.client.models.iterators.parse_content_range', return_value=ContentRange(0, 9, 10), ) expected = [{'id': i} for i in range(10)] rs = rs_factory() rs._client.get = mocker.MagicMock(return_value=expected) results = [resource for resource in rs] assert results == expected
async def test_rs_pagination(mocker, async_client_mock, async_rs_factory): mocker.patch( 'connect.client.models.iterators.parse_content_range', side_effect=[ ContentRange(0, 99, 200), ContentRange(100, 199, 200), ], ) rs = async_rs_factory( client=async_client_mock(methods=['get']), ) rs._client.get.side_effect = [ [{'id': i} for i in range(100)], [{'id': i} for i in range(100, 200)], ] results = [item async for item in rs] assert results == [{'id': i} for i in range(200)] assert rs._limit == 100 assert rs._offset == 0
def test_rs_with_queries(mocker, rs_factory): mocker.patch( 'connect.client.models.resourceset.parse_content_range', return_value=ContentRange(0, 0, 0), ) rs = rs_factory(query='eq(status,approved)') get_mock = mocker.MagicMock(return_value=[]) rs._client.get = get_mock bool(rs) assert rs._client.get.called_once_with(f'{rs.path}?{rs.query}')
def test_rs_getitem_slice(mocker, rs_factory): mocker.patch( 'connect.client.models.resourceset.parse_content_range', return_value=ContentRange(0, 9, 10), ) expected = [{'id': i} for i in range(10)] rs = rs_factory() rs._client.get = mocker.MagicMock(return_value=expected) sliced = rs[2:4] assert isinstance(sliced, ResourceSet) assert sliced._limit == 2 assert sliced._offset == 2
async def test_rs_bool_falsy(mocker, async_client_mock, async_rs_factory): mocker.patch( 'connect.client.models.iterators.parse_content_range', return_value=ContentRange(0, 0, 0), ) rs = async_rs_factory( client=async_client_mock(methods=['get']), ) rs._client.get.return_value = [] results = [item async for item in rs] assert results == [] assert bool(rs) is False
async def test_rs_iterate_already_evaluated(mocker, async_client_mock, async_rs_factory): mocker.patch( 'connect.client.models.iterators.parse_content_range', return_value=ContentRange(0, 9, 10), ) expected = [{'id': i} for i in range(10)] rs = async_rs_factory( client=async_client_mock(methods=['get']), ) rs._client.get.return_value = expected assert [item async for item in rs] == expected assert [item async for item in rs] == expected
def test_rs_bool_truthy_already_evaluated(mocker, rs_factory): mocker.patch( 'connect.client.models.resourceset.parse_content_range', return_value=ContentRange(0, 9, 10), ) expected = [{'id': i} for i in range(10)] rs = rs_factory() mocked = mocker.patch.object(ResourceSet, '_execute_request', wraps=rs._execute_request) rs._client.get = mocker.MagicMock(return_value=expected) assert bool(rs) is True assert bool(rs) is True mocked.assert_called_once()
async def test_rs_count(mocker, async_client_mock, async_rs_factory): content_range = ContentRange(0, 9, 100) mocker.patch( 'connect.client.models.resourceset.parse_content_range', return_value=content_range, ) rs = async_rs_factory( client=async_client_mock(methods=['get']), ) rs._client.get.return_value = [] assert await rs.count() == 100 assert rs.content_range == content_range
async def test_rs_bool_truthy(mocker, async_client_mock, async_rs_factory): mocker.patch( 'connect.client.models.iterators.parse_content_range', return_value=ContentRange(0, 9, 10), ) expected = [{'id': i} for i in range(10)] rs = async_rs_factory( client=async_client_mock(methods=['get']), ) rs._client.get.return_value = expected results = [item async for item in rs] assert results == expected assert bool(rs) is True
def test_rs_pagination(mocker, rs_factory): mocker.patch( 'connect.client.models.resourceset.parse_content_range', side_effect=[ ContentRange(0, 99, 200), ContentRange(100, 199, 200), ], ) rs = rs_factory() rs._client.get = mocker.MagicMock(side_effect=[ [{ 'id': i } for i in range(100)], [{ 'id': i } for i in range(100, 200)], ]) results = list(rs) assert results == [{'id': i} for i in range(200)] assert rs._limit == 100 assert rs._offset == 0
async def test_rs_with_queries(mocker, async_client_mock, async_rs_factory): mocker.patch( 'connect.client.models.iterators.parse_content_range', return_value=ContentRange(0, 0, 0), ) rs = async_rs_factory( client=async_client_mock(methods=['get']), query='eq(status,approved)', ) rs._client.get.return_value = [] items = [item async for item in rs] assert items == [] rs._client.get.assert_awaited_once_with( f'{rs.path}?{rs.query}', params={'limit': 100, 'offset': 0}, )
def test_rs_getitem_slice(mocker, async_client_mock, async_rs_factory): mocker.patch( 'connect.client.models.resourceset.parse_content_range', return_value=ContentRange(0, 9, 10), ) expected = [{'id': i} for i in range(10)] rs = async_rs_factory( client=async_client_mock(methods=['get']), ) rs._client.get.return_value = expected sliced = rs[2:4] assert isinstance(sliced, AsyncResourceSet) assert sliced._limit == 2 assert sliced._offset == 2 rs._client.get.assert_not_awaited()
def test_rs_first(mocker, rs_factory): content_range = ContentRange(0, 9, 10) mocker.patch( 'connect.client.models.resourceset.parse_content_range', return_value=content_range, ) expected = [{'id': i} for i in range(10)] rs = rs_factory() rs._client.get = mocker.MagicMock(return_value=expected) first = rs.first() assert first == expected[0] rs = rs_factory() rs._client.get = mocker.MagicMock(return_value=[]) first = rs.first() assert first is None
def test_rs_values_list_evaluated(mocker, rs_factory): mocker.patch( 'connect.client.models.resourceset.parse_content_range', return_value=ContentRange(0, 9, 10), ) return_value = [{ 'id': i, 'name': f'name {i}', 'inner': { 'title': f'title {i}', }, } for i in range(10)] expected = [{ 'id': i, 'inner.title': f'title {i}', } for i in range(10)] rs = rs_factory() rs._client.get = mocker.MagicMock(return_value=return_value) bool(rs) values = rs.values_list('id', 'inner.title') assert values == expected
def test_rs_request(mocker, rs_factory): rs = rs_factory() content_range = ContentRange(0, 0, 0) mocker.patch( 'connect.client.models.resourceset.parse_content_range', return_value=content_range, ) rs._client.get = mocker.MagicMock(return_value=[]) rs = (rs.filter(field='value', field2__in=('a', 'b')).search('search term').select( 'obj1', '-obj2').order_by('field1', '-field2')) rs._client.get.assert_not_called() list(rs) rs._client.get.assert_called_once() assert rs._client.get.call_args[0][0] == ( 'resources?select(obj1,-obj2)' '&and(eq(field,value),in(field2,(a,b)))' '&ordering(field1,-field2)')
async def test_rs_first(mocker, async_client_mock, async_rs_factory): content_range = ContentRange(0, 9, 10) mocker.patch( 'connect.client.models.resourceset.parse_content_range', return_value=content_range, ) expected = [{'id': i} for i in range(10)] rs = async_rs_factory( client=async_client_mock(methods=['get']), ) rs._client.get.return_value = expected first = await rs.first() assert first == expected[0] rs = async_rs_factory( client=async_client_mock(methods=['get']), ) rs._client.get.return_value = [] first = await rs.first() assert first is None