def test_rs_values_list_pagination(mocker, rs_factory): mocker.patch( 'cnct.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, '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_bool_falsy(mocker, rs_factory): mocker.patch( 'cnct.client.models.resourceset.parse_content_range', return_value=ContentRange(0, 0, 0), ) rs = rs_factory() rs._client.get = mocker.MagicMock(return_value=[]) assert bool(rs) is False
def test_rs_getitem(mocker, rs_factory): mocker.patch( 'cnct.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) assert rs[0] == expected[0]
def test_rs_with_queries(mocker, rs_factory): mocker.patch( 'cnct.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_count(mocker, rs_factory): content_range = ContentRange(0, 9, 100) mocker.patch( 'cnct.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( 'cnct.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) results = [resource for resource in rs] assert results == expected
def test_rs_getitem_slice(mocker, rs_factory): mocker.patch( 'cnct.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
def test_rs_pagination(mocker, rs_factory): mocker.patch( 'cnct.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
def test_rs_first(mocker, rs_factory): content_range = ContentRange(0, 9, 10) mocker.patch( 'cnct.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( 'cnct.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( 'cnct.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)')