-
Notifications
You must be signed in to change notification settings - Fork 0
/
djc977-TestWCDB1.py
executable file
·162 lines (131 loc) · 5.11 KB
/
djc977-TestWCDB1.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
import sys
import os
import datetime, json
from os.path import join
from subprocess import call, Popen, PIPE
from crisis_app import views
from views import make_parent, add_child, make_json
from django.test import TestCase
from django.core import management
from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login, logout
from crisis_app.converters import to_xml, to_db
from crisis_app.models import Event, Organization, Person, Embed
XML_FIXTURE_PATH = 'crisis_app/fixtures/xml'
XML = dict((f.split('.')[0], open(join(XML_FIXTURE_PATH, f)).read().strip())
for f in os.listdir(XML_FIXTURE_PATH))
class UsefulTestCase(TestCase):
def _clear_db(self):
management.call_command('flush', load_initial_data=False,
interactive=False)
def _reset_db(self):
management.call_command('flush', interactive=False)
class ToJsonTestCase(TestCase):
def test_make_parent_1(self):
p = make_parent("test", "color")
self.assertEqual(p['id'], "test")
self.assertEqual(p['data']['$color'], "color")
def test_make_parent_2(self):
p = make_parent(1, "color")
self.assertEqual(p['id'], 1)
self.assertEqual(p['data']['$color'], "color")
def test_make_parent_3(self):
p = make_parent((1, 2), [1, 2, 3])
self.assertEqual(p['id'], (1, 2))
self.assertEqual(p['data']['$color'], [1, 2, 3])
def test_add_child_1(self):
p = make_parent("test", "color")
add_child(p, 'child', 'color', 0, [], 'desc')
self.assertEqual(p['children'][0]['id'], 'child')
self.assertEqual(p['children'][0]['data']['$color'], 'color')
self.assertEqual(p['children'][0]['data']['$area'], 0)
self.assertEqual(p['children'][0]['data']['popularity'], 0)
self.assertEqual(p['children'][0]['data']['image'], 'None')
self.assertEqual(p['children'][0]['data']['description'], 'desc')
def test_add_child_2(self):
p = make_parent("test", "color")
add_child(p, 'child', 'color', 0, [], 'desc')
add_child(p, 'child', 'color', 0, [1, 2, 3], 'desc')
self.assertEqual(p['children'][1]['id'], 'child')
self.assertEqual(p['children'][1]['data']['$color'], 'color')
self.assertEqual(p['children'][1]['data']['$area'], 0)
self.assertEqual(p['children'][1]['data']['popularity'], 0)
self.assertEqual(p['children'][1]['data']['image'], '1')
self.assertEqual(p['children'][1]['data']['description'], 'desc')
def test_make_json_1(self):
j = make_json(3)
p = json.loads(j)
self.assertEqual(p["id"], "Crisis")
self.assertEqual(p['children'][0]['id'], "Events")
self.assertEqual(p['children'][1]['id'], "People")
self.assertEqual(p['children'][2]['id'], "Organizations")
class ToXmlTestCase(TestCase):
def test_export(self):
# just a sanity check to make sure we're working w/ the correct data
self.assertEqual(len(Event.objects.all()), 1)
# the real test
self.assertEqual(to_xml.convert().strip(), XML['initial_data'])
def test_export_validation(self):
xml = Popen('./manage.py xml'.split(), stdout=PIPE)
devnull = open(os.devnull, 'w')
self.assertEqual(call('./manage.py validate'.split(),
stdin=xml.stdout, stdout=devnull, stderr=devnull), 0)
try:
devnull.close()
except:
pass
xml.kill()
class ToDbTestCase(UsefulTestCase):
def test_import(self):
self._clear_db()
self.assertEqual(len(Event.objects.all()), 0)
to_db.convert(XML['initial_data'])
events = Event.objects.all()
people = Person.objects.all()
orgs = Organization.objects.all()
self.assertEqual(len(events), 1)
self.assertEqual(len(events[0].organization_set.all()), 1)
self.assertEqual(len(events[0].person_set.all()), 1)
self.assertEqual(len(events[0].embed_set.all()), 8)
self.assertEqual(len(people), 1)
self.assertEqual(len(people[0].event.all()), 1)
self.assertEqual(len(people[0].organization_set.all()), 1)
self.assertEqual(len(people[0].embed_set.all()), 10)
self.assertEqual(len(orgs), 1)
self.assertEqual(len(orgs[0].event.all()), 1)
self.assertEqual(len(orgs[0].person.all()), 1)
self.assertEqual(len(orgs[0].embed_set.all()), 10)
self._reset_db()
class ImportExportTest(UsefulTestCase):
PWD = '123'
def setUp(self):
self._clear_db()
self.xml = open(join(XML_FIXTURE_PATH, 'initial_data.xml'))
self.user = User.objects.create_user('admin', 'dont@talk.to.me',
self.PWD)
def tearDown(self):
self.client.logout()
self.user.delete()
self.xml.close()
self._reset_db()
def test_xml_redirect_to_login(self):
response = self.client.get('/upload_xml')
self.assertEqual(response.status_code, 302)
def test_xml_submission(self):
self.client.login(username=self.user.username, password=self.PWD)
response = self.client.get('/upload_xml')
self.assertEqual(response.status_code, 200)
response = self.client.post('/upload_xml', {
'xml': self.xml,
'Submit': 'Submit'
})
self.assertEqual(response.status_code, 302)
self.assertEqual(response['location'], 'http://testserver/data.xml')
response = self.client.get('/xml')
try:
self.assertEqual(response.content.strip(),
XML['initial_data'].strip())
except AssertionError as e:
# write the wrong file out to disk for inspection from the cmd line
open('invalid_response.xml', 'w').write(response.content)
raise e