-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
127 lines (102 loc) · 2.93 KB
/
tests.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
# -*- coding: utf-8 -*-
import json
import unittest
from lxml import etree
from datetime import datetime
from StringIO import StringIO
from gplusfriends.models import Activity, Person
person2 = Person(
id='111',
name='Osoba 1',
url='http://plus.google.com/111',
type='person'
)
activity = Activity(
id='aaa',
title='Aktivita 1',
url='http://plus.google.com/aaa',
date=datetime(2013, 05, 05),
content='Aktivita 1 obsah',
publisher=person2
)
person = Person(
id='222',
name='Osoba 2',
url='http://plus.google.com/222',
type='person',
gender='male',
people=[person2],
activities=[activity]
)
a_dict = {
'id': 'aaa',
'title': 'Aktivita 1',
'url': 'http://plus.google.com/aaa',
'date': datetime(2013, 05, 05),
'content': 'Aktivita 1 obsah',
'publisher': person2,
}
a_json = """{
"id": "aaa",
"title": "Aktivita 1",
"url": "http://plus.google.com/aaa",
"date": "2013-05-05 00:00:00",
"content": "Aktivita 1 obsah",
"publisher": "111"
}"""
p_dict = {
'id': '222',
'name': 'Osoba 2',
'url': 'http://plus.google.com/222',
'gender': 'male',
'type': 'person',
'people': [person2],
'activities': [activity],
}
p_json = """{
"id": "222",
"name": "Osoba 2",
"url": "http://plus.google.com/222",
"gender": "male",
"type": "person",
"people": ["111"],
"activities": ["aaa"]
}"""
class TestPerson(unittest.TestCase):
def setUp(self):
self.person = person
self.dict_test = p_dict
self.json_test = json.loads(p_json)
self.xml_schema = 'gplusfriends/static/xsd/person.xsd'
def test_to_dict(self):
person_dict = self.person.to_dict()
self.assertEqual(person_dict, self.dict_test)
def test_to_json(self):
person_json = self.person.to_json()
self.assertEqual(json.loads(person_json), self.json_test)
def test_to_xml(self):
person_xml = etree.parse(StringIO(self.person.to_xml()))
with open(self.xml_schema) as f:
doc = etree.parse(f)
schema = etree.XMLSchema(doc)
self.assertTrue(schema.validate(person_xml))
class TestActivity(unittest.TestCase):
def setUp(self):
self.activity = activity
self.dict_test = a_dict
self.json_test = json.loads(a_json)
self.xml_schema = 'gplusfriends/static/xsd/activity.xsd'
def test_to_dict(self):
activity_dict = self.activity.to_dict()
self.assertEqual(activity_dict, self.dict_test)
def test_to_json(self):
activity_json = self.activity.to_json()
self.assertEqual(json.loads(activity_json), self.json_test)
def test_to_xml(self):
activity_xml = etree.parse(StringIO(self.activity.to_xml()))
with open(self.xml_schema) as f:
doc = etree.parse(f)
schema = etree.XMLSchema(doc)
self.assertTrue(schema.validate(activity_xml))
if __name__ == '__main__':
unittest.main()