This repository has been archived by the owner on Mar 8, 2018. It is now read-only.
/
test.py
176 lines (135 loc) · 3.84 KB
/
test.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from popit import *
from slumber.exceptions import *
import logging
from pprint import pprint
from oktest import test, ok, NG, DIFF
DIFF = repr
logging.basicConfig(level = logging.WARN, format=FORMAT)
# load configuration
import config_test
keys = [x for x in dir(config_test) if not x.startswith("__")]
vals = map(lambda x: eval('config_test.'+x), keys)
conf = dict(zip(keys, vals))
class GetterSetterTest(object):
## invoked only once before all tests
@classmethod
def before_all(cls):
cls.p = PopIt(**conf)
## invoked before each test
def before(self):
self.p = self.__class__.p
@test("constructor should set right members")
def _(self):
for i, k in enumerate(keys):
v = vals[i]
ok(getattr(self.p, k)) == v
@test("should raise error when non existent schema called")
def _(self):
def f():
self.p.sadfhoi83jhk3323
ok (f).raises(SchemaError)
class StatusTest(object):
@classmethod
def before_all(cls):
cls.p = PopIt(**conf)
def before(self):
self.p = self.__class__.p
@test("is_online should return true")
def _(self):
ok(self.p.is_online()) == True
@test("api_version should return right version")
def _(self):
ok(self.p.get_api_version()) == conf['api_version']
@test("get_url should return a string")
def _(self):
ok(self.p.get_url()).is_a(str)
class LazyTest(object):
def before(self):
self.p = PopIt(lazy=True)
@test("lazy init should not set anything")
def _(self):
ok(self.p.initialized) == False
@test("set_up should initialize the wrapper")
def _(self):
self.p.set_up(**conf)
ok(self.p.initialized) == True
class AuthenticationTest(object):
@classmethod
def before_all(cls):
wrong_conf = conf.copy()
wrong_conf['password'] = '3h45hk345h'
cls.p = PopIt(**wrong_conf)
def before(self):
self.p = self.__class__.p
@test("wrong credentials should not fail when only reading")
def _(self):
def f():
self.p.person.get()
NG (f).raises(HttpClientError)
@test("wrong credentials should raise exception when saving")
def _(self):
def f():
self.p.person.post({'name': 'Albert Keinstein'})
ok (f).raises(HttpClientError)
class CreateTest(object):
@classmethod
def before_all(cls):
cls.p = PopIt(**conf)
def before(self):
self.p = self.__class__.p
@test("can create person")
def _(self):
self.p.person.post({'name': 'Albert Keinstein'})
@test("can create organisation")
def _(self):
self.p.organisation.post({'name': 'Space Party'})
class ReadUpdateDeleteTest(object):
@classmethod
def before_all(cls):
cls.p = PopIt(**conf)
def before(self):
self.p = self.__class__.p
new = self.p.person.post({
'name': 'Albert Keinstein',
'links': [{
'url': 'http://www.wikipedia.com/AlbertEinstein',
'comment': 'Wikipedia'
}]
})
self.id = new['result']['_id']
@test("can read person's name")
def _(self):
result = self.p.person(self.id).get()
data = result['result']
ok(data['name']) == "Albert Keinstein"
@test("can read person's links")
def _(self):
result = self.p.person(self.id).get()
data = result['result']
ok(data['links'][0]['url']) == "http://www.wikipedia.com/AlbertEinstein"
ok(data['links'][0]['comment']) == "Wikipedia"
@test("can edit person's name")
def _(self):
self.p.person(self.id).put({"name": "Albert Einstein"})
result = self.p.person(self.id).get()
ok(result['result']['name']) == "Albert Einstein"
@test("can delete person")
def _(self):
result = self.p.person(self.id).delete()
ok(result) == True
def f():
result = self.p.person(self.id).get()
ok (f).raises(HttpClientError)
@test("cannot delete person twice")
def _(self):
result = self.p.person(self.id).delete()
ok(result) == True
def f():
self.p.person(self.id).delete()
ok (f).raises(HttpClientError)
## invoke tests
if __name__ == '__main__':
import oktest
oktest.main()