This repository has been archived by the owner on Feb 16, 2023. It is now read-only.
/
test_resource.py
141 lines (103 loc) · 3.32 KB
/
test_resource.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
test_eve_resource
----------------------------------
Tests for `eve_resource` module.
"""
import pytest
from eve import Eve
from eve_resource import resource
@pytest.fixture()
def accounts():
return resource.Resource('accounts', keys=('username', 'password'))
@pytest.fixture()
def definition():
return {
'item_title': 'account',
'public_methods': ['GET'],
}
@pytest.fixture()
def schema(accounts):
return {
accounts.key.username: {
'type': 'string',
'required': True,
},
accounts.key.password: {
'type': 'string',
'required': True,
},
}
class TestEve_resource(object):
@classmethod
def setup_class(cls):
pass
def test_key(self, accounts):
# accounts = resource.Resource('accounts', 'username', 'password')
assert accounts.name == 'accounts'
assert accounts.key.username == 'username'
assert accounts.key.password == 'password'
assert len(accounts.key) == 2
def test_definition(self, accounts, definition):
assert accounts.definition_value == {}
@accounts.definition
def wrapped_def():
return definition
assert accounts.definition_value == definition
del(accounts.definition_value)
with pytest.raises(TypeError):
accounts.definition_value = 'not a dict'
def test_schema(self, accounts, schema):
assert accounts.schema_value == {}
@accounts.schema
def wrapped_schema():
"""Wrapped schema doc"""
return schema
assert accounts.schema_value == schema
assert wrapped_schema.__doc__ == """Wrapped schema doc"""
del(accounts.schema_value)
with pytest.raises(TypeError):
accounts.schema_value = 'not a dict'
@accounts.schema
def schema2(key):
return {
key.username: {
'type': 'string',
'required': True,
},
key.password: {
'type': 'string',
'required': True,
},
}
assert accounts.schema_value == schema
def test_domain(self, accounts, definition, schema):
accounts.schema_value = schema
assert accounts.domain() == {'schema': schema}
accounts.definition_value = definition
value = definition.copy()
value['schema'] = schema
assert accounts.domain() == value
def test_init_api(self, accounts):
app = Eve(settings={'DOMAIN': {}})
assert len(app.on_insert) == 0
@accounts.hooks.mongo('insert')
def test(items):
pass
accounts.init_api(app)
assert len(app.on_insert) == 1
@accounts.hooks.request('pre_GET', 'post_POST')
def tes2(*args, **kwargs):
pass
assert len(app.on_pre_GET) == 0
assert len(app.on_post_POST) == 0
accounts.init_api(app)
assert len(app.on_pre_GET) == 1
assert len(app.on_post_POST) == 1
assert app.config['DOMAIN'].get('accounts') is not None
with pytest.raises(TypeError):
accounts.init_api(None)
@classmethod
def teardown_class(cls):
pass