1
|
|
|
#!/usr/bin/env python3 |
2
|
|
|
# -*- coding: utf-8 -*- |
3
|
|
|
""" |
4
|
|
|
Created on Tue Jan 7 14:56:39 2020 |
5
|
|
|
|
6
|
|
|
@author: Paolo Cozzi <[email protected]> |
7
|
|
|
""" |
8
|
|
|
|
9
|
|
|
import os |
10
|
|
|
import json |
11
|
|
|
import types |
12
|
|
|
|
13
|
|
|
from unittest.mock import patch, Mock |
14
|
|
|
from unittest import TestCase |
15
|
|
|
|
16
|
|
|
from pyUSIrest.auth import Auth |
17
|
|
|
from pyUSIrest.client import Document |
18
|
|
|
|
19
|
|
|
from .common import DATA_PATH |
20
|
|
|
from .test_auth import generate_token |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
class DocumentTest(TestCase): |
24
|
|
|
@classmethod |
25
|
|
|
def setup_class(cls): |
26
|
|
|
cls.mock_get_patcher = patch('requests.Session.get') |
27
|
|
|
cls.mock_get = cls.mock_get_patcher.start() |
28
|
|
|
|
29
|
|
|
# define an auth object |
30
|
|
|
token = generate_token() |
31
|
|
|
cls.auth = Auth(token=token) |
32
|
|
|
|
33
|
|
|
@classmethod |
34
|
|
|
def teardown_class(cls): |
35
|
|
|
cls.mock_get_patcher.stop() |
36
|
|
|
|
37
|
|
|
def test_create_document(self): |
38
|
|
|
# create a mock response |
39
|
|
|
with open(os.path.join(DATA_PATH, "root.json")) as handle: |
40
|
|
|
data = json.load(handle) |
41
|
|
|
|
42
|
|
|
# get a document instance |
43
|
|
|
document = Document(auth=self.auth, data=data) |
44
|
|
|
|
45
|
|
|
self.assertIsInstance(document, Document) |
46
|
|
|
|
47
|
|
|
def test_paginate(self): |
48
|
|
|
with open(os.path.join( |
49
|
|
|
DATA_PATH, "userSubmissionsPage1.json")) as handle: |
50
|
|
|
page1 = json.load(handle) |
51
|
|
|
|
52
|
|
|
with open(os.path.join( |
53
|
|
|
DATA_PATH, "userSubmissionsPage2.json")) as handle: |
54
|
|
|
page2 = json.load(handle) |
55
|
|
|
|
56
|
|
|
self.mock_get.return_value = Mock() |
57
|
|
|
|
58
|
|
|
# simulating two distinct replies with side_effect |
59
|
|
|
self.mock_get.return_value.json.side_effect = [page1, page2] |
60
|
|
|
self.mock_get.return_value.status_code = 200 |
61
|
|
|
|
62
|
|
|
# getting a document instance |
63
|
|
|
document = Document(auth=self.auth) |
64
|
|
|
|
65
|
|
|
# getting a documehnt |
66
|
|
|
document.get("https://submission-test.ebi.ac.uk/api/user/submissions") |
67
|
|
|
|
68
|
|
|
# ok parsing the response |
69
|
|
|
responses = document.paginate() |
70
|
|
|
|
71
|
|
|
# assering instances |
72
|
|
|
self.assertIsInstance(responses, types.GeneratorType) |
73
|
|
|
|
74
|
|
|
# reading objects and asserting lengths |
75
|
|
|
test = list(responses) |
76
|
|
|
|
77
|
|
|
self.assertEqual(len(test), 2) |
78
|
|
|
|