Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add historical #88

Open
wants to merge 2 commits into
base: use_requests
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions openprocurement_client/api_base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,10 @@ def _get_resource_item(self, url, headers=None):
_headers.update(headers or {})
response_item = self.request('GET', url, headers=_headers)
if response_item.status_code == 200:
return munchify(loads(response_item.text))
data = loads(response_item.text)
if "x-revision-n" in response_item.headers:
data["x_revision_n"] = response_item.headers.get("x-revision-n")
return munchify(data)
raise InvalidResponse(response_item)

def _patch_resource_item(self, url, payload, headers=None):
Expand Down Expand Up @@ -238,10 +241,16 @@ def upload_document(self, file_, obj, use_ds_client=True,
doc_registration=doc_registration
)

def get_resource_item(self, id, headers=None):
return self._get_resource_item('{}/{}'.format(self.prefix_path, id),
def get_resource_item(self, item_id, headers=None):
return self._get_resource_item('{}/{}'.format(self.prefix_path, item_id),
headers=headers)

def get_resource_item_historical(self, item_id, revision="", headers=None):
_headers = {"x-revision-n": str(revision)}
if headers:
_headers.update(headers)
return self._get_resource_item('{}/{}/historical'.format(self.prefix_path, item_id), headers=_headers)

def patch_credentials(self, id, access_token):
return self._patch_resource_item(
'{}/{}/credentials'.format(self.prefix_path, id),
Expand Down
3 changes: 2 additions & 1 deletion openprocurement_client/tests/main.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import unittest

from openprocurement_client.tests import tests, tests_sync
from openprocurement_client.tests import tests, tests_sync, tests_api_base_client
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А де сам тест?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

упс
вже додав



def suite():
suite = unittest.TestSuite()
suite.addTest(tests.suite())
suite.addTest(tests_sync.suite())
suite.addTest(tests_api_base_client.suite())
return suite


Expand Down
69 changes: 69 additions & 0 deletions openprocurement_client/tests/tests_api_base_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import unittest
import uuid
from mock import MagicMock
from json import dumps
from copy import deepcopy

from openprocurement_client.api_base_client import APIBaseClient
from openprocurement_client.exceptions import InvalidResponse


CLIENT_CONFIG = {
"key": "",
"resource": "tenders",
"host_url": "http://lb.api-sandbox.openprocurement.org/",
"api_version": "2.4"
}


class TestAPIBaseClient(unittest.TestCase):
def setUp(self):
self.client = APIBaseClient(**CLIENT_CONFIG)

def test_get_resource_item_historical(self):
class Response(object):
def __init__(self, status_code, text=None, headers=None):
self.status_code = status_code
self.text = text
self.headers = headers

revisions_limit = 42
response_text = {
"id": uuid.uuid4().hex,
"rev": 24
}

side_effect = [
Response(200, dumps(response_text), {"x-revision-n": str(revisions_limit)}),
Response(200, dumps(response_text), {"x-revision-n": str(revisions_limit)}),
Response(200, dumps(response_text), {"x-revision-n": str(revisions_limit - 1)}),
Response(404),
Response(404),
Response(404),
]

self.client.request = MagicMock(side_effect=side_effect)

actual_response = deepcopy(response_text)
actual_response["x_revision_n"] = str(revisions_limit)
item_id = response_text["id"]
self.assertEqual(self.client.get_resource_item_historical(item_id, revision=""), actual_response)
self.assertEqual(self.client.get_resource_item_historical(item_id, revision=revisions_limit), actual_response)
actual_response["x_revision_n"] = str(revisions_limit - 1)
self.assertEqual(self.client.get_resource_item_historical(
item_id, revision=revisions_limit - 1), actual_response)

for revision in (0, revisions_limit + 1, None):
with self.assertRaises(InvalidResponse) as e:
self.client.get_resource_item_historical(item_id, revision=revision)
self.assertEqual(e.exception.status_code, 404)


def suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestAPIBaseClient))
return suite


if __name__ == "__main__":
unittest.main(defaultTest='suite')