Skip to content

Commit

Permalink
Add pagination tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yakky committed Jan 7, 2018
1 parent b805afb commit 499534f
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 7 deletions.
29 changes: 29 additions & 0 deletions tests/resources/fakes_list_success.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[
{
"id": 1
},
{
"id": 2
},
{
"id": 3
},
{
"id": 4
},
{
"id": 5
},
{
"id": 6
},
{
"id": 7
},
{
"id": 8
},
{
"id": 9
}
]
3 changes: 3 additions & 0 deletions tests/test_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ class TestIssues(unittest.TestCase):

@patch('taiga.requestmaker.RequestMaker.get')
def test_list_attachments(self, mock_requestmaker_get):
mock_requestmaker_get.return_value = MockResponse(200,
create_mock_json('tests/resources/issues_list_success.json'))
rm = RequestMaker('/api/v1', 'fakehost', 'faketoken')
Issue(rm, id=1).list_attachments()
mock_requestmaker_get.assert_called_with(
'issues/attachments',
query={"object_id": 1},
paginate=True
)

@patch('taiga.requestmaker.RequestMaker.post')
Expand Down
128 changes: 122 additions & 6 deletions tests/test_model_base.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# -*- coding: utf-8 -*-
import json

from taiga.models import Projects
from taiga.requestmaker import RequestMaker
from taiga.models.base import InstanceResource, ListResource, SearchableList
import unittest
from mock import patch
import datetime
from .tools import MockResponse
from .tools import MockResponse, create_mock_json


class Fake(InstanceResource):
Expand All @@ -18,14 +20,28 @@ class Fake(InstanceResource):

def my_method(self):
response = self.requester.get('/users/{id}/starred', id=self.id)
return projects.Projects.parse(response.json(), self.requester)
return Projects.parse(response.json(), self.requester)


class Fakes(ListResource):

instance = Fake


class FakeHeaders(dict):
sequence = []
counter = -1

def __init__(self, sequence=[], *args, **kwargs):
self.sequence = sequence
self.counter = -1
super(FakeHeaders, self).__init__(*args, **kwargs)

def get(self, k, d=None):
self.counter += 1
return self.sequence[self.counter]


class TestModelBase(unittest.TestCase):

def test_encoding(self):
Expand Down Expand Up @@ -136,12 +152,112 @@ def test_call_model_base_delete_element_from_list(self, mock_requestmaker_delete

@patch('taiga.requestmaker.RequestMaker.get')
def test_call_model_base_list_elements(self, mock_requestmaker_get):
js_list = json.loads(create_mock_json('tests/resources/fakes_list_success.json'))
rm = RequestMaker('/api/v1', 'fakehost', 'faketoken')
fakes = Fakes(rm)

data = json.dumps(js_list)
mock_requestmaker_get.return_value = MockResponse(200, data)
f_list = fakes.list()
mock_requestmaker_get.assert_called_with('fakes', query={}, paginate=True)
self.assertEqual(len(f_list), 9)

data = json.dumps(js_list[0])
mock_requestmaker_get.return_value = MockResponse(200, data)
f_list = fakes.list(id=1)
mock_requestmaker_get.assert_called_with('fakes', query={'id':1}, paginate=True)
self.assertEqual(len(f_list), 1)

@patch('taiga.requestmaker.RequestMaker.get')
def test_call_model_base_list_page_size(self, mock_requestmaker_get):
js_list = json.loads(create_mock_json('tests/resources/fakes_list_success.json'))
rm = RequestMaker('/api/v1', 'fakehost', 'faketoken')
fakes = Fakes(rm)

data = json.dumps(js_list)
mock_requestmaker_get.return_value = MockResponse(200, data, FakeHeaders(
[True, True, False],
**{'X-Pagination-Next': True}
))
f_list = fakes.list(page_size=2)
mock_requestmaker_get.assert_called_with('fakes', query={'page': 3, 'page_size': 2})
self.assertEqual(len(f_list), 27)

data = json.dumps(js_list)
mock_requestmaker_get.return_value = MockResponse(200, data)
f_list = fakes.list(page_size='wrong')
mock_requestmaker_get.assert_called_with('fakes', query={'page_size': 100}, paginate=True)
self.assertEqual(len(f_list), 9)

@patch('taiga.requestmaker.RequestMaker.get')
def test_call_model_base_list_elements_no_paginate(self, mock_requestmaker_get):
js_list = json.loads(create_mock_json('tests/resources/fakes_list_success.json'))
rm = RequestMaker('/api/v1', 'fakehost', 'faketoken')
fakes = Fakes(rm)

data = json.dumps(js_list)
mock_requestmaker_get.return_value = MockResponse(200, data)
f_list = fakes.list(pagination=False)
mock_requestmaker_get.assert_called_with('fakes', query={}, paginate=False)
self.assertEqual(len(f_list), 9)

data = json.dumps(js_list[0])
mock_requestmaker_get.return_value = MockResponse(200, data)
f_list = fakes.list(id=1, pagination=False)
mock_requestmaker_get.assert_called_with('fakes', query={'id':1}, paginate=False)
self.assertEqual(len(f_list), 1)

@patch('taiga.requestmaker.requests.get')
def test_call_model_base_list_elements_no_paginate_check_requests(self, mock_requestmaker_get):
js_list = json.loads(create_mock_json('tests/resources/fakes_list_success.json'))
rm = RequestMaker('/api/v1', 'fakehost', 'faketoken')
fakes = Fakes(rm)

data = json.dumps(js_list)
mock_requestmaker_get.return_value = MockResponse(200, data)
f_list = fakes.list(pagination=False)
mock_requestmaker_get.assert_called_with(
'fakehost/api/v1/fakes', verify=True, params={},
headers={
'x-disable-pagination': 'True', 'Content-type': 'application/json', 'Authorization': 'Bearer faketoken'
}
)
self.assertEqual(len(f_list), 9)

@patch('taiga.requestmaker.requests.get')
def test_call_model_base_list_elements_paginate_check_requests(self, mock_requestmaker_get):
js_list = json.loads(create_mock_json('tests/resources/fakes_list_success.json'))
rm = RequestMaker('/api/v1', 'fakehost', 'faketoken')
fakes = Fakes(rm)
fakes.list()
mock_requestmaker_get.assert_called_with('fakes', query={})
fakes.list(project_id=1)
mock_requestmaker_get.assert_called_with('fakes', query={'project_id':1})

data = json.dumps(js_list)
mock_requestmaker_get.return_value = MockResponse(200, data)
f_list = fakes.list()
mock_requestmaker_get.assert_called_with(
'fakehost/api/v1/fakes', verify=True, params={},
headers={
'x-lazy-pagination': 'True', 'Content-type': 'application/json', 'Authorization': 'Bearer faketoken'
}
)
self.assertEqual(len(f_list), 9)

@patch('taiga.requestmaker.RequestMaker.get')
def test_call_model_base_list_elements_single_page(self, mock_requestmaker_get):
js_list = json.loads(create_mock_json('tests/resources/fakes_list_success.json'))
rm = RequestMaker('/api/v1', 'fakehost', 'faketoken')
fakes = Fakes(rm)

data = json.dumps(js_list[:5])
mock_requestmaker_get.return_value = MockResponse(200, data)
f_list = fakes.list(page_size=5, page=1)
self.assertEqual(len(f_list), 5)
mock_requestmaker_get.assert_called_with('fakes', query={'page_size': 5}, paginate=True)

data = json.dumps(js_list[5:])
mock_requestmaker_get.return_value = MockResponse(200, data)
f_list = fakes.list(page_size=5, page=2)
self.assertEqual(len(f_list), 4)
mock_requestmaker_get.assert_called_with('fakes', query={'page_size': 5}, paginate=True)

def test_to_dict_method(self):
rm = RequestMaker('/api/v1', 'fakehost', 'faketoken')
Expand Down
5 changes: 5 additions & 0 deletions tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from mock import patch
import six

from tests.tools import MockResponse, create_mock_json

if six.PY2:
import_open = '__builtin__.open'
else:
Expand All @@ -14,11 +16,14 @@ class TestTasks(unittest.TestCase):

@patch('taiga.requestmaker.RequestMaker.get')
def test_list_attachments(self, mock_requestmaker_get):
mock_requestmaker_get.return_value = MockResponse(200,
create_mock_json('tests/resources/tasks_list_success.json'))
rm = RequestMaker('/api/v1', 'fakehost', 'faketoken')
Task(rm, id=1).list_attachments()
mock_requestmaker_get.assert_called_with(
'tasks/attachments',
query={"object_id": 1},
paginate=True
)

@patch(import_open)
Expand Down
3 changes: 3 additions & 0 deletions tests/test_user_stories.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ class TestUserStories(unittest.TestCase):

@patch('taiga.requestmaker.RequestMaker.get')
def test_list_attachments(self, mock_requestmaker_get):
mock_requestmaker_get.return_value = MockResponse(200,
create_mock_json('tests/resources/userstories_list_success.json'))
rm = RequestMaker('/api/v1', 'fakehost', 'faketoken')
UserStory(rm, id=1).list_attachments()
mock_requestmaker_get.assert_called_with(
'userstories/attachments',
query={"object_id": 1},
paginate=True
)

@patch('taiga.requestmaker.RequestMaker.get')
Expand Down
3 changes: 2 additions & 1 deletion tests/tools.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import json

class MockResponse():
def __init__(self, status_code, text):
def __init__(self, status_code, text, headers={}):
self.status_code = status_code
self.text = text
self.headers = headers

def json(self):
return json.loads(self.text)
Expand Down

0 comments on commit 499534f

Please sign in to comment.