|
|
|
''' Tests for ordr2.resources.base '''
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
from pyramid.testing import DummyRequest, DummyResource
|
|
|
|
|
|
|
|
|
|
|
|
# test for base resources
|
|
|
|
|
|
|
|
def test_base_resource_init():
|
|
|
|
''' test __init__ function of base resource '''
|
|
|
|
from ordr2.resources import BaseResource, RootResource
|
|
|
|
|
|
|
|
root = RootResource('request object')
|
|
|
|
resource = BaseResource('resource name', root, 'model instance')
|
|
|
|
|
|
|
|
assert resource.__name__ == 'resource name'
|
|
|
|
assert resource.__parent__ == root
|
|
|
|
assert resource.request == 'request object'
|
|
|
|
assert resource.model == 'model instance'
|
|
|
|
|
|
|
|
|
|
|
|
def test_base_resource_acl():
|
|
|
|
''' test __acl__ function of base resource '''
|
|
|
|
from ordr2.resources import BaseResource, RootResource
|
|
|
|
from pyramid.security import DENY_ALL
|
|
|
|
|
|
|
|
root = RootResource('request object')
|
|
|
|
resource = BaseResource('resource name', root, 'model instance')
|
|
|
|
|
|
|
|
with pytest.raises(NotImplementedError):
|
|
|
|
assert resource.__acl__()
|
|
|
|
|
|
|
|
|
|
|
|
def test_base_resource_getitem_ok():
|
|
|
|
''' test the __getitem__ function of base resource '''
|
|
|
|
from ordr2.resources import BaseResource, RootResource
|
|
|
|
|
|
|
|
root = RootResource('request object')
|
|
|
|
root.nodes = {'known': BaseResource}
|
|
|
|
resource = root['known']
|
|
|
|
|
|
|
|
assert resource.__name__ == 'known'
|
|
|
|
assert resource.__parent__ == root
|
|
|
|
assert resource.request == 'request object'
|
|
|
|
|
|
|
|
|
|
|
|
def test_base_resource_getitem_raises_key_error():
|
|
|
|
''' test the __getitem__ function of base resource '''
|
|
|
|
from ordr2.resources import BaseResource, RootResource
|
|
|
|
|
|
|
|
root = RootResource('request object')
|
|
|
|
root.nodes = {'known': BaseResource}
|
|
|
|
|
|
|
|
with pytest.raises(KeyError):
|
|
|
|
resource = root['unknown']
|
|
|
|
|
|
|
|
|
|
|
|
# tests for pagination class
|
|
|
|
|
|
|
|
def test_pagination_init():
|
|
|
|
''' test object creation of Pagination class with zero total items'''
|
|
|
|
from ordr2.resources.base import Pagination
|
|
|
|
|
|
|
|
pages = Pagination(1, 0, 25, 7)
|
|
|
|
|
|
|
|
assert pages.count == 0
|
|
|
|
assert pages.items_per_page == 25
|
|
|
|
assert pages.first is None
|
|
|
|
assert pages.current is None
|
|
|
|
assert pages.last is None
|
|
|
|
assert pages.previous is None
|
|
|
|
assert pages.next is None
|
|
|
|
assert pages.window == []
|
|
|
|
|
|
|
|
|
|
|
|
def test_pagination_init_auto_calc():
|
|
|
|
''' test object creation with more then zero total items'''
|
|
|
|
from ordr2.resources.base import Pagination
|
|
|
|
|
|
|
|
pages = Pagination(1, 1, 25, 7)
|
|
|
|
|
|
|
|
assert pages.count == 1
|
|
|
|
assert pages.items_per_page == 25
|
|
|
|
assert pages.first is not None
|
|
|
|
assert pages.current is not None
|
|
|
|
assert pages.last is not None
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
'count, page, items, expected', [
|
|
|
|
(1, 1, 3, [1, None, 1, None, 1]),
|
|
|
|
(2, 1, 3, [1, None, 1, None, 1]),
|
|
|
|
(3, 1, 3, [1, None, 1, None, 1]),
|
|
|
|
(4, 1, 3, [1, None, 1, 2, 2]),
|
|
|
|
(5, 1, 3, [1, None, 1, 2, 2]),
|
|
|
|
(6, 1, 3, [1, None, 1, 2, 2]),
|
|
|
|
(7, 1, 3, [1, None, 1, 2, 3]),
|
|
|
|
(1, 1, 10, [1, None, 1, None, 1]),
|
|
|
|
(2, 1, 10, [1, None, 1, None, 1]),
|
|
|
|
(9, 1, 10, [1, None, 1, None, 1]),
|
|
|
|
(10, 1, 10, [1, None, 1, None, 1]),
|
|
|
|
(11, 1, 10, [1, None, 1, 2, 2]),
|
|
|
|
(11, 2, 10, [1, 1, 2, None, 2]),
|
|
|
|
(20, 2, 10, [1, 1, 2, None, 2]),
|
|
|
|
(21, 2, 10, [1, 1, 2, 3, 3]),
|
|
|
|
(100, 5, 10, [1, 4, 5, 6, 10]),
|
|
|
|
(10, 5, 10, [1, None, 1, None, 1]),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
def test_pagination_calculate_page_numbers(count, page, items, expected):
|
|
|
|
''' calculation of related pages '''
|
|
|
|
from ordr2.resources.base import Pagination
|
|
|
|
|
|
|
|
pages = Pagination(None, 0, items, None)
|
|
|
|
pages.count = count
|
|
|
|
pages.calculate(page, 7)
|
|
|
|
|
|
|
|
result = [
|
|
|
|
pages.first,
|
|
|
|
pages.previous,
|
|
|
|
pages.current,
|
|
|
|
pages.next,
|
|
|
|
pages.last
|
|
|
|
]
|
|
|
|
assert result == expected
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
'count, page, size, expected', [
|
|
|
|
(100, 1, 7, [1, 2, 3, 4]),
|
|
|
|
(100, 2, 7, [1, 2, 3, 4, 5]),
|
|
|
|
(100, 3, 7, [1, 2, 3, 4, 5, 6]),
|
|
|
|
(100, 4, 7, [1, 2, 3, 4, 5, 6, 7]),
|
|
|
|
(100, 5, 7, [2, 3, 4, 5, 6, 7, 8]),
|
|
|
|
(100, 6, 7, [3, 4, 5, 6, 7, 8, 9]),
|
|
|
|
(100, 7, 7, [4, 5, 6, 7, 8, 9, 10]),
|
|
|
|
(100, 8, 7, [5, 6, 7, 8, 9, 10]),
|
|
|
|
(100, 9, 7, [6, 7, 8, 9, 10]),
|
|
|
|
(100, 10, 7, [7, 8, 9, 10]),
|
|
|
|
(100, 1, 5, [1, 2, 3]),
|
|
|
|
(100, 2, 5, [1, 2, 3, 4]),
|
|
|
|
(100, 3, 5, [1, 2, 3, 4, 5]),
|
|
|
|
(100, 4, 5, [2, 3, 4, 5, 6]),
|
|
|
|
(100, 5, 5, [3, 4, 5, 6, 7]),
|
|
|
|
(100, 6, 5, [4, 5, 6, 7, 8]),
|
|
|
|
(100, 7, 5, [5, 6, 7, 8, 9]),
|
|
|
|
(100, 8, 5, [6, 7, 8, 9, 10]),
|
|
|
|
(100, 9, 5, [7, 8, 9, 10]),
|
|
|
|
(100, 10, 5, [8, 9, 10]),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
def test_pagination_calculate_window(count, page, size, expected):
|
|
|
|
''' calculation of related pages '''
|
|
|
|
from ordr2.resources.base import Pagination
|
|
|
|
|
|
|
|
pages = Pagination(None, 0, 10, None)
|
|
|
|
pages.count = count
|
|
|
|
pages.calculate(page, size)
|
|
|
|
|
|
|
|
assert pages.window == expected
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
'first, last, check, expected', [
|
|
|
|
(1, 1, 0, None),
|
|
|
|
(1, 1, 1, 1),
|
|
|
|
(1, 1, 2, None),
|
|
|
|
(2, 4, 1, None),
|
|
|
|
(2, 4, 2, 2),
|
|
|
|
(2, 4, 3, 3),
|
|
|
|
(2, 4, 4, 4),
|
|
|
|
(2, 4, 5, None),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
def test_pagination_is_valid(first, last, check, expected):
|
|
|
|
''' check is_valid function without default value '''
|
|
|
|
from ordr2.resources.base import Pagination
|
|
|
|
|
|
|
|
pages = Pagination(None, 0, 10, None)
|
|
|
|
pages.count = True
|
|
|
|
pages.first = first
|
|
|
|
pages.last = last
|
|
|
|
|
|
|
|
assert pages.is_valid(check) == expected
|
|
|
|
|
|
|
|
|
|
|
|
def test_pagination_is_valid_returns_default():
|
|
|
|
''' check is_valid function with default value '''
|
|
|
|
from ordr2.resources.base import Pagination
|
|
|
|
|
|
|
|
pages = Pagination(None, 0, 10, None)
|
|
|
|
pages.count = True
|
|
|
|
pages.first = 1
|
|
|
|
pages.last = 1
|
|
|
|
|
|
|
|
assert pages.is_valid(2, 'default') == 'default'
|
|
|
|
|
|
|
|
|
|
|
|
# test BaseListResource
|
|
|
|
|
|
|
|
def test_base_list_init():
|
|
|
|
''' Test object creation of base class
|
|
|
|
|
|
|
|
all other tests must be implemented in a child class of BaseListResource
|
|
|
|
'''
|
|
|
|
from ordr2.resources.base import BaseListResource
|
|
|
|
|
|
|
|
request = DummyRequest()
|
|
|
|
parent = DummyResource(request=request)
|
|
|
|
|
|
|
|
with pytest.raises(NotImplementedError):
|
|
|
|
resource = BaseListResource('John Cleese', parent)
|