/
tmtorres-TestWCDB3.py
executable file
·813 lines (675 loc) · 32.6 KB
/
tmtorres-TestWCDB3.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
"""
This file demonstrates writing tests using the unittest module. These will pass
when you run "manage.py test".
Replace this with more appropriate tests for your application.
"""
import os, sys
from django.test import TestCase
from django.test.client import RequestFactory
from models import *
from upload import *
from download import *
from views import *
from datetime import datetime
from crisix.views import people, organizations, crises, display, display_more, get_datetime, get_contact, convert_li, is_stub
import xml.etree.ElementTree as ET
from xml.etree.ElementTree import tostring, fromstring
from xml.etree.ElementTree import ElementTree
class SimpleTest(TestCase, RequestFactory):
fixtures = ['crisix_fixture.json']
def setUp(self):
self.c = Crisis(id='CRI_HAIEAR',name='2010 Haiti Earthquake',kind='Natural Disaster',location='Haiti',created=datetime.now(),summary='Haitian Earthquake')
self.c.save()
self.p = Person(id='PER_BROBMA',name='Barack Obama',kind='President',location='Washington, D.C., USA',created=datetime.now(),summary="President of the USA")
self.p.save()
self.o = Organization(id='ORG_WHORGN',name='World Health Organization',kind='Public Health',location='Geneva, Switzerland',created=datetime.now(),summary='UN WHO')
self.o.save()
self.c.organizations.add(self.o)
self.c.people.add(self.p)
self.o.crises.add(self.c)
self.o.people.add(self.p)
# ------------
# Simple Tests
# ------------
def test_basic_addition(self):
"""
Tests that 1 + 1 always equals 2.
"""
self.assertEqual(1 + 1, 2)
def test_index(self):
# Test if page can be accessed properly
resp = self.client.get('/')
self.assertEqual(resp.status_code, 200)
# Test if database is properly populated
self.assertEqual(self.p.name,'Barack Obama')
def test_datetime_1(self):
# Test if date and time match for given crisis
root = fromstring(open(os.path.join(settings.BASE_DIR, 'crisix/database/TestXML/TestSHESSG.xml')).read())
insert(root)
a = Crisis.objects.get(id='CRI_SHESSG')
res = get_datetime(a)
self.assertEqual(res, '2012-12-14, 09:35:00')
def test_datetime_2(self):
# Test if date and time match for given crisis
root = fromstring(open(os.path.join(settings.BASE_DIR, 'crisix/database/TestXML/TestLYCLWR.xml')).read())
insert(root)
a = Crisis.objects.get(id='CRI_LYCLWR')
res = get_datetime(a)
self.assertEqual(res, '2011-02-15')
def test_datetime_3(self):
# Test if date and time are applicable to a non-crisis
root = fromstring(open(os.path.join(settings.BASE_DIR, 'crisix/database/TestXML/TestBROBMA.xml')).read())
insert(root)
a = Person.objects.get(id='PER_BROBMA')
res = get_datetime(a)
self.assertEqual(res, '')
def test_contact_1(self):
# Test if contact info is applicable to non-organization
root = fromstring(open(os.path.join(settings.BASE_DIR, 'crisix/database/TestXML/TestBROBMA.xml')).read())
insert(root)
a = Person.objects.get(id='PER_BROBMA')
res = get_contact(a)
self.assertEqual(res, '')
def test_contact_2(self):
# Test if contact info matches for given organization
root = fromstring(open(os.path.join(settings.BASE_DIR, 'crisix/database/TestXML/TestUNDWAY.xml')).read())
insert(root)
a = Organization.objects.get(id='ORG_UNDWAY')
res = get_contact(a)
self.assertEqual(res, 'http://apps.unitedway.org/contact/')
def test_contact_3(self):
# Test if contact info matches for given organization
root = fromstring(open(os.path.join(settings.BASE_DIR, 'crisix/database/TestXML/TestSNQKRF.xml')).read())
insert(root)
a = Organization.objects.get(id='ORG_SNQKRF')
res = get_contact(a)
self.assertEqual(res, 'http://sichuan-quake-relief.org/contact-us/')
def test_display_1(self):
# Test if person name is on page in format designated in template
request_factory = RequestFactory()
request = request_factory.get('/people')
response = display(request, 'people')
htmlstring = response.content
self.assertNotEqual(htmlstring.find('President'),-1)
def test_display_2(self):
# Test if person name is on page in format designated in template
request_factory = RequestFactory()
request = request_factory.get('/organizations')
response = display(request, 'organizations')
htmlstring = response.content
self.assertNotEqual(htmlstring.find('Public Health'),-1)
def test_display_3(self):
# Test if person name is on page in format designated in template
request_factory = RequestFactory()
request = request_factory.get('/crises')
response = display(request, 'crises')
htmlstring = response.content
self.assertNotEqual(htmlstring.find('Natural Disaster'),-1)
def test_display_more_1(self):
# Test if person name is on page in format designated in template
request_factory = RequestFactory()
request = request_factory.get('/people/brobma/videos')
response = display_more(request, 'people', 'brobma', 'videos')
htmlstring = response.content
self.assertNotEqual(htmlstring.find('Barack Obama'),-1)
def test_display_more_2(self):
# Test if organization name is on page in format designated in template
request_factory = RequestFactory()
request = request_factory.get('/organizations/whorgn/videos')
response = display_more(request, 'organizations', 'whorgn', 'videos')
htmlstring = response.content
self.assertNotEqual(htmlstring.find('World Health Organization'),-1)
def test_display_more_3(self):
# Test if crisis name is on page in format designated in template
request_factory = RequestFactory()
request = request_factory.get('/crises/haiear/videos')
response = display_more(request, 'crises', 'haiear', 'videos')
htmlstring = response.content
self.assertNotEqual(htmlstring.find('Haiti Earthquake'),-1)
def test_convertli_1(self):
text = "<li>World Health Organization</li>"
res = convert_li(text)
self.assertEqual(res, 'World Health Organization')
def test_convertli_2(self):
text = "<li>Barack Obama</li><li>Adam Lanza</li>"
res = convert_li(text)
self.assertEqual(res, 'Barack Obama Adam Lanza')
def test_convertli_3(self):
text = "<li>This is a sentence. Remove all li tags.</li>"
res = convert_li(text)
self.assertEqual(res, 'This is a sentence. Remove all li tags.')
def test_stub_1(self):
root = fromstring(open(os.path.join(settings.BASE_DIR, 'crisix/database/TestXML/TestBROBMA.xml')).read())
insert(root)
a = Person.objects.get(id='PER_BROBMA')
res = is_stub(a)
self.assertFalse(res)
def test_stub_2(self):
root = fromstring(open(os.path.join(settings.BASE_DIR, 'crisix/database/TestXML/TestSHESSG.xml')).read())
insert(root)
a = Crisis.objects.get(id='CRI_SHESSG')
res = is_stub(a)
self.assertFalse(res)
def test_stub_3(self):
root = fromstring(open(os.path.join(settings.BASE_DIR, 'crisix/database/TestXML/TestNLTLCL.xml')).read())
insert(root)
a = Organization.objects.get(id='ORG_NLTLCL')
res = is_stub(a)
self.assertFalse(res)
def test_search_1(self):
# Test functionality of search function in returning correct information
root = fromstring(open(os.path.join(settings.BASE_DIR, 'crisix/database/TestXML/TestBROBMA.xml')).read())
insert(root)
request_factory = RequestFactory()
request = request_factory.get('/search')
response = search(request)
htmlstring = response.content
self.assertNotEqual(htmlstring.find('Barack Obama'), -1)
def test_search_2(self):
# Test functionality of search function in returning correct information
root = fromstring(open(os.path.join(settings.BASE_DIR, 'crisix/database/TestXML/TestSHESSG.xml')).read())
insert(root)
request_factory = RequestFactory()
request = request_factory.get('/search')
response = search(request)
htmlstring = response.content
self.assertNotEqual(htmlstring.find('Sandy Hook'), -1)
def test_search_3(self):
# Test functionality of search function in returning correct information
root = fromstring(open(os.path.join(settings.BASE_DIR, 'crisix/database/TestXML/TestNLTLCL.xml')).read())
insert(root)
request_factory = RequestFactory()
request = request_factory.get('/search')
response = search(request)
htmlstring = response.content
self.assertNotEqual(htmlstring.find('National Transitional Council'), -1)
def test_query_1(self):
# Test if the given string exists inside the query
request_factory = RequestFactory()
request = request_factory.get('/query/?q=1')
response = query(request)
htmlstring = response.content
self.assertNotEqual(htmlstring.find('Magic Johnson'), -1)
def test_query_2(self):
# Test if the given string exists inside the query
request_factory = RequestFactory()
request = request_factory.get('/query/?q=4')
response = query(request)
htmlstring = response.content
self.assertNotEqual(htmlstring.find('Naoto Kan'), -1)
def test_query_3(self):
# Test if the given string exists inside the query
request_factory = RequestFactory()
request = request_factory.get('/query/?q=7')
response = query(request)
htmlstring = response.content
self.assertNotEqual(htmlstring.find('Sandy Hook Elementary School Shooting'), -1)
# -----------
# People View
# -----------
def test_people1(self):
# Test if the instance page is accessible
request_factory = RequestFactory()
request = request_factory.get('/crisix/people/brobma')
response = people(request, 'brobma')
self.assertEqual(response.status_code, 200)
def test_people2(self):
# Test if person kind is on page in format designated in template
request_factory = RequestFactory()
request = request_factory.get('/crisix/people/brobma')
response = people(request, 'brobma')
htmlstring = response.content
self.assertNotEqual(htmlstring.find('<li>President</li>'),-1)
def test_people3(self):
# Test if person name is on page in format designated in template
request_factory = RequestFactory()
request = request_factory.get('/crisix/people/brobma')
response = people(request, 'brobma')
htmlstring = response.content
self.assertNotEqual(htmlstring.find('<h1 class="main-focus;">Barack Obama</h1>'),-1)
def test_people4(self):
# Test if related objects are on page in format designated in template
request_factory = RequestFactory()
request = request_factory.get('/crisix/people/brobma')
response = people(request, 'brobma')
htmlstring = response.content
self.assertNotEqual(htmlstring.find('<li><a href="/crises/haiear/">2010 Haiti Earthquake</a></li>'),-1)
self.assertNotEqual(htmlstring.find('<li><a href="/organizations/whorgn/">World Health Organization</a></li>'),-1)
# -----------------
# Organization View
# -----------------
def test_organization1(self):
# Test if the instance page is accessible
request_factory = RequestFactory()
request = request_factory.get('/crisix/organization/whorgn')
response = organizations(request, 'whorgn')
self.assertEqual(response.status_code, 200)
def test_organization2(self):
# Test if person kind is on page in format designated in template
request_factory = RequestFactory()
request = request_factory.get('/crisix/organization/whorgn')
response = organizations(request, 'whorgn')
htmlstring = response.content
self.assertNotEqual(htmlstring.find('<li>Public Health</li>'),-1)
def test_organization3(self):
# Test if person name is on page in format designated in template
request_factory = RequestFactory()
request = request_factory.get('/crisix/organization/whorgn')
response = organizations(request, 'whorgn')
htmlstring = response.content
self.assertNotEqual(htmlstring.find('<h1 class="main-focus;">World Health Organization</h1>'),-1)
def test_organization4(self):
# Test if related objects are on page in format designated in template
request_factory = RequestFactory()
request = request_factory.get('/crisix/organization/whorgn')
response = organizations(request, 'whorgn')
htmlstring = response.content
self.assertNotEqual(htmlstring.find('<li><a href="/crises/haiear/">2010 Haiti Earthquake</a></li>'),-1)
self.assertNotEqual(htmlstring.find('<li><a href="/people/brobma/">Barack Obama</a></li>'),-1)
# -----------
# Crisis View
# -----------
def test_crisis1(self):
# Test if the instance page is accessible
request_factory = RequestFactory()
request = request_factory.get('/crisix/crises/haiear')
response = crises(request, 'haiear')
self.assertEqual(response.status_code, 200)
def test_crisis2(self):
# Test if crisis kind is on page in format designated in template
request_factory = RequestFactory()
request = request_factory.get('/crisix/crises/haiear')
response = crises(request, 'haiear')
htmlstring = response.content
self.assertNotEqual(htmlstring.find('<li>Natural Disaster</li>'),-1)
def test_crisis3(self):
# Test if crisis name is on page in format designated in template
request_factory = RequestFactory()
request = request_factory.get('/crisix/crises/haiear')
response = crises(request, 'haiear')
htmlstring = response.content
self.assertNotEqual(htmlstring.find('<h1 class="main-focus;">2010 Haiti Earthquake</h1>'),-1)
def test_crisis4(self):
# Test if related objects are on page in format designated in template
request_factory = RequestFactory()
request = request_factory.get('/crises/haiear')
response = crises(request, 'haiear')
htmlstring = response.content
self.assertNotEqual(htmlstring.find('<li><a href="/organizations/whorgn/">World Health Organization</a></li>'),-1)
self.assertNotEqual(htmlstring.find('<li><a href="/people/brobma/">Barack Obama</a></li>'),-1)
class TestUpload(TestCase):
def test_insert_1(self):
root = fromstring(open(os.path.join(settings.BASE_DIR, 'crisix/database/TestXML/TestSHESSG.xml')).read())
insert(root)
c = None
try:
c = Crisis.objects.get(id='CRI_SHESSG')
except Crisis.DoesNotExist:
self.assertTrue(False)
self.assertTrue(c is not None)
self.assertEqual(str(c.kind), 'Spree Shooting')
self.assertEqual(str(c.date), '2012-12-14')
self.assertEqual(str(c.time), '09:35:00')
self.assertEqual(str(c.location), '<li>Newtown, Connecticut</li>')
for c in Crisis.objects.all():
c.delete()
def test_insert_2(self):
root = fromstring(open(os.path.join(settings.BASE_DIR, 'crisix/database/TestXML/TestBROBMA.xml')).read())
insert(root)
p = None
try:
p = Person.objects.get(id='PER_BROBMA')
except Person.DoesNotExist:
self.assertTrue(False)
self.assertEqual(str(p.kind), 'President')
self.assertEqual(str(p.location), 'Washington, D.C, United States Of America')
for c in Crisis.objects.all():
c.delete()
def test_insert_3(self):
root = fromstring(open(os.path.join(settings.BASE_DIR, 'crisix/database/TestXML/TestUNDWAY.xml')).read())
insert(root)
o = None
try:
o = Organization.objects.get(id='ORG_UNDWAY')
except Organization.DoesNotExist:
self.assertTrue(False)
self.assertEqual(str(o.kind), 'Non-Profit Organization')
self.assertEqual(str(o.location), 'Worldwide')
# self.assertTrue('<li>In 1887, a Denver woman, a priest, two ministers' in o.history)
self.assertEqual(str(o.contact), '<li href="http://apps.unitedway.org/contact/">Contact Form</li>')
for c in Crisis.objects.all():
c.delete()
def test_get_entity_1(self):
root = fromstring(open(os.path.join(settings.BASE_DIR, 'crisix/database/TestXML/TestSHESSG.xml')).read())
insert(root)
a = get_entity(Crisis, 'CRI_SHESSG')
self.assertEqual(str(a.kind), 'Spree Shooting')
self.assertEqual(str(a.date), '2012-12-14')
self.assertEqual(str(a.time), '09:35:00')
self.assertEqual(str(a.location), '<li>Newtown, Connecticut</li>')
for c in Crisis.objects.all():
c.delete()
def test_get_entity_2(self):
root2 = fromstring(open(os.path.join(settings.BASE_DIR, 'crisix/database/TestXML/TestBROBMA.xml')).read())
insert(root2)
b = get_entity(Person, 'PER_BROBMA')
self.assertEqual(str(b.kind), 'President')
self.assertEqual(str(b.location), 'Washington, D.C, United States Of America')
for c in Crisis.objects.all():
c.delete()
def test_get_entity_3(self):
root3 = fromstring(open(os.path.join(settings.BASE_DIR, 'crisix/database/TestXML/TestUNDWAY.xml')).read())
insert(root3)
c = get_entity(Organization, 'ORG_UNDWAY')
self.assertEqual(str(c.kind), 'Non-Profit Organization')
self.assertEqual(str(c.location), 'Worldwide')
self.assertEqual(str(c.contact), '<li href="http://apps.unitedway.org/contact/">Contact Form</li>')
for c in Crisis.objects.all():
c.delete()
def test_cri_handler_1(self):
root = fromstring(open(os.path.join(settings.BASE_DIR, 'crisix/database/TestXML/TestSHESSG.xml')).read())
cri_handler(root[0])
c = Crisis.objects.get(id='CRI_SHESSG')
self.assertEqual(str(c.kind), 'Spree Shooting')
self.assertEqual(str(c.date), '2012-12-14')
self.assertEqual(str(c.time), '09:35:00')
self.assertEqual(str(c.location), '<li>Newtown, Connecticut</li>')
for c in Crisis.objects.all():
c.delete()
def test_cri_handler_2(self):
root = fromstring(open(os.path.join(settings.BASE_DIR, 'crisix/database/TestXML/TestSCHERQ.xml')).read())
cri_handler(root[0])
c = Crisis.objects.get(id='CRI_SCHERQ')
self.assertEqual(str(c.kind), 'Earthquake')
self.assertEqual(str(c.date), '2008-05-12')
self.assertEqual(str(c.time), '14:28:01')
self.assertEqual(str(c.location), '<li>Wenchuan County, Sichuan</li><li>Sichuan Province</li>')
for c in Crisis.objects.all():
c.delete()
def test_cri_handler_3(self):
root = fromstring(open(os.path.join(settings.BASE_DIR, 'crisix/database/TestXML/TestLYCLWR.xml')).read())
cri_handler(root[0])
c = Crisis.objects.get(id='CRI_LYCLWR')
self.assertEqual(str(c.kind), 'Civil War')
self.assertEqual(str(c.date), '2011-02-15')
self.assertEqual(str(c.location), '<li>Libya</li>')
for c in Crisis.objects.all():
c.delete()
def test_org_handler_1(self):
root =fromstring(open(os.path.join(settings.BASE_DIR, 'crisix/database/TestXML/TestUNDWAY.xml')).read())
org_handler(root[0])
o = Organization.objects.get(id='ORG_UNDWAY')
self.assertEqual(str(o.kind), 'Non-Profit Organization')
self.assertEqual(str(o.location), 'Worldwide')
self.assertEqual(str(o.contact), '<li href="http://apps.unitedway.org/contact/">Contact Form</li>')
for c in Crisis.objects.all():
c.delete()
def test_org_handler_2(self):
root = fromstring(open(os.path.join(settings.BASE_DIR, 'crisix/database/TestXML/TestSNQKRF.xml')).read())
org_handler(root[0])
o = Organization.objects.get(id='ORG_SNQKRF')
self.assertEqual(str(o.kind), 'Non-Profit, Humanitarian Organization')
self.assertEqual(str(o.location), 'Chengdu, China')
self.assertTrue('<li>Volunteer: volunteer@sichuan-quake-relief.org</li>' in str(o.contact))
for c in Crisis.objects.all():
c.delete()
def test_org_handler_3(self):
root = fromstring(open(os.path.join(settings.BASE_DIR, 'crisix/database/TestXML/TestNLTLCL.xml')).read())
org_handler(root[0])
o = Organization.objects.get(id='ORG_NLTLCL')
self.assertEqual(str(o.kind), 'De Facto Government')
self.assertEqual(str(o.location), 'Benghazi, Libya')
self.assertEqual(str(o.contact), '<li> Organization is no longer active. </li>')
for c in Crisis.objects.all():
c.delete()
def test_per_handler_1(self):
root = fromstring(open(os.path.join(settings.BASE_DIR, 'crisix/database/TestXML/TestBROBMA.xml')).read())
per_handler(root[0])
p = Person.objects.get(id='PER_BROBMA')
self.assertEqual(str(p.kind), 'President')
self.assertEqual(str(p.location), 'Washington, D.C, United States Of America')
for c in Crisis.objects.all():
c.delete()
def test_per_handler_2(self):
root = fromstring(open(os.path.join(settings.BASE_DIR, 'crisix/database/TestXML/TestADMLNZ.xml')).read())
per_handler(root[0])
p = Person.objects.get(id='PER_ADMLNZ')
self.assertEqual(str(p.kind), 'Murderer')
self.assertEqual(str(p.location), 'Newtown, Connecticut')
for c in Crisis.objects.all():
c.delete()
def test_per_handler_3(self):
root = fromstring(open(os.path.join(settings.BASE_DIR, 'crisix/database/TestXML/TestHUJNTO.xml')).read())
per_handler(root[0])
p = Person.objects.get(id='PER_HUJNTO')
self.assertEqual(str(p.kind), 'President')
self.assertEqual(str(p.location), 'Communist Party Of China')
for c in Crisis.objects.all():
c.delete()
def test_insert_elem_1(self):
root = fromstring(open(os.path.join(settings.BASE_DIR, 'crisix/database/TestXML/TestBROBMA.xml')).read())
per_handler(root[0])
p = Person.objects.get(id='PER_BROBMA')
# Test is proper amount of Videos are in DB
element = p.elements.filter(ctype='VID')
self.assertEqual(len(element), 2)
# Test is insertion works without prior functions
insert_elem({'href':'www.youtube.com/videos'}, {'entity':p, 'ctype':'VID', 'text': 'Test Youtube Video'})
element = str(p.elements.filter(ctype='VID'))
self.assertNotEqual(element.find('www.youtube.com/videos'), -1)
element = p.elements.filter(ctype='VID')
self.assertEqual(len(element), 3)
element = p.elements.filter(ctype='CITE')
self.assertEqual(len(element), 2)
for c in Crisis.objects.all():
c.delete()
def test_insert_elem_2(self):
root = fromstring(open(os.path.join(settings.BASE_DIR, 'crisix/database/TestXML/TestSHESSG.xml')).read())
cri_handler(root[0])
c = Crisis.objects.get(id='CRI_SHESSG')
# Test is proper amount of Videos are in DB
element = c.elements.filter(ctype='VID')
self.assertEqual(len(element), 1)
# Test is insertion works without prior functions
insert_elem({'href':'www.youtube.com/videos'}, {'entity':c, 'ctype':'VID', 'text': 'Test Youtube Video'})
element = str(c.elements.filter(ctype='VID'))
self.assertNotEqual(element.find('www.youtube.com/videos'), -1)
element = c.elements.filter(ctype='VID')
self.assertEqual(len(element), 2)
element = c.elements.filter(ctype='CITE')
self.assertEqual(len(element), 3)
for c in Crisis.objects.all():
c.delete()
def test_insert_elem_3(self):
root = fromstring(open(os.path.join(settings.BASE_DIR, 'crisix/database/TestXML/TestUNDWAY.xml')).read())
org_handler(root[0])
o = Organization.objects.get(id='ORG_UNDWAY')
# Test is proper amount of Videos are in DB
element = o.elements.filter(ctype='VID')
self.assertEqual(len(element), 1)
# Test is insertion works without prior functions
insert_elem({'href':'www.youtube.com/videos'}, {'entity':o, 'ctype':'VID', 'text': 'Test Youtube Video'})
element = str(o.elements.filter(ctype='VID'))
self.assertNotEqual(element.find('www.youtube.com/videos'), -1)
element = o.elements.filter(ctype='VID')
self.assertEqual(len(element), 2)
element = o.elements.filter(ctype='MAP')
self.assertEqual(len(element), 1)
for c in Crisis.objects.all():
c.delete()
def test_com_handler_1(self):
root = fromstring(open(os.path.join(settings.BASE_DIR, 'crisix/database/TestXML/TestBROBMA.xml')).read())
per_handler(root[0])
p = Person.objects.get(id='PER_BROBMA')
com_handler(root[0], p)
p.save()
summary = p.summary
self.assertNotEqual(summary.find('Barack Obama is the 44th President of the United States'), -1)
for c in Crisis.objects.all():
c.delete()
def test_com_handler_2(self):
root = fromstring(open(os.path.join(settings.BASE_DIR, 'crisix/database/TestXML/TestSHESSG.xml')).read())
cri_handler(root[0])
c = Crisis.objects.get(id='CRI_SHESSG')
com_handler(root[0], c)
c.save()
summary = c.summary
self.assertNotEqual(summary.find('Adam Lanza is believed to have shot his mother'), -1)
for c in Crisis.objects.all():
c.delete()
def test_com_handler_3(self):
root = fromstring(open(os.path.join(settings.BASE_DIR, 'crisix/database/TestXML/TestUNDWAY.xml')).read())
org_handler(root[0])
o = Organization.objects.get(id='ORG_UNDWAY')
com_handler(root[0], o)
o.save()
summary = o.summary
self.assertNotEqual(summary.find('The Sandy Hook School SupportFund (SHSSF) was created by United Way of Western Connecticut'), -1)
for c in Crisis.objects.all():
c.delete()
def test_valid_map_1(self):
"""
testing a valid Google Map
"""
s = 'https://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=zhongnanhai,+Beijing,+China&aq=1&oq=Zhongnanhai&sll=30.307761,-97.753401&sspn=1.046869,2.113495&ie=UTF8&hq=zhongnanhai,&hnear=Beijing,+China&t=m&z=15&output=embed" text="Zhongnanhai, Beijing, China'
assert (valid_map(s) is not None)
def test_valid_map_2(self):
"""
testing an invalid Google Map
"""
s = 'https://maps.google/maps?client=ubuntu&channel=fs&q=white+house&oe=utf-8&ie=UTF-8&ei=5xgAUsqdFIGMyQHknYCoDQ&ved=0CAoQ_AUoAg'
assert (valid_map(s) is None)
def test_valid_map_3(self):
"""
testing a valid Google map with a co.uk extension
"""
s = 'https://maps.google.co.uk/maps/ms?ie=UTF8&oe=UTF8&msa=0&msid=200809166078445189642.00049f7a79747b94b2b24&t=m&ll=37.300275,12.65625&spn=41.526391,52.734375&z=3&output=embed'
assert (valid_map(s) is not None)
def test_valid_map_4(self):
"""
testing a valid Bing Map
"""
s = 'http://www.bing.com/maps/embed/?v=2&cp=38.897610~-77.036720&lvl=18&dir=0&sty=r&q=white%20house&form=LMLTEW&emid=85759cd1-e41e-4e69-9d3b-dacd3d688a9c'
t = 'http://www.bing.com/maps/embed/?v=2&cp=17.573495~-3.998823&lvl=6&dir=0&sty=r&q=mali&form=LMLTEW&emid=e012f5fb-e4f7-1293-6e86-377c384ee67a'
assert (valid_map(s) is not None)
assert (valid_map(t) is not None)
class TestDownload(TestCase):
def test_get_crises_1(self):
test = fromstring(open(os.path.join(settings.BASE_DIR, 'crisix/database/TestXML/TestSHESSG.xml')).read())
insert(test)
root = ET.Element('WorldCrises')
get_crises(root)
self.assertEqual(root[0][2].text, 'Spree Shooting')
self.assertEqual(root[0][3].text, '2012-12-14')
self.assertEqual(root[0][4].text, '09:35:00')
for c in Crisis.objects.all():
c.delete()
def test_get_crises_2(self):
test = fromstring(open(os.path.join(settings.BASE_DIR, 'crisix/database/TestXML/TestSCHERQ.xml')).read())
insert(test)
root = ET.Element('WorldCrises')
get_crises(root)
self.assertEqual(root[0][2].text, 'Earthquake')
self.assertEqual(root[0][3].text, '2008-05-12')
self.assertEqual(root[0][4].text, '14:28:01')
for c in Crisis.objects.all():
c.delete()
def test_get_crises_3(self):
test = fromstring(open(os.path.join(settings.BASE_DIR, 'crisix/database/TestXML/TestLYCLWR.xml')).read())
insert(test)
root = ET.Element('WorldCrises')
get_crises(root)
self.assertEqual(root[0][2].text, 'Civil War')
self.assertEqual(root[0][3].text, '2011-02-15')
for c in Crisis.objects.all():
c.delete()
def test_get_organizations_1(self):
test = fromstring(open(os.path.join(settings.BASE_DIR, 'crisix/database/TestXML/TestUNDWAY.xml')).read())
insert(test)
root = ET.Element('WorldCrises')
get_organizations(root)
self.assertEqual(root[0][2].text, 'Non-Profit Organization')
self.assertEqual(root[0][3].text, 'Worldwide')
for c in Crisis.objects.all():
c.delete()
def test_get_organizations_2(self):
test = fromstring(open(os.path.join(settings.BASE_DIR, 'crisix/database/TestXML/TestSNQKRF.xml')).read())
insert(test)
root = ET.Element('WorldCrises')
get_organizations(root)
self.assertEqual(root[0][2].text, 'Non-Profit, Humanitarian Organization')
self.assertEqual(root[0][3].text, 'Chengdu, China')
for c in Crisis.objects.all():
c.delete()
def test_get_organizations_3(self):
test = fromstring(open(os.path.join(settings.BASE_DIR, 'crisix/database/TestXML/TestNLTLCL.xml')).read())
insert(test)
root = ET.Element('WorldCrises')
get_organizations(root)
self.assertEqual(root[0][2].text, 'De Facto Government')
self.assertEqual(root[0][3].text, 'Benghazi, Libya')
for c in Crisis.objects.all():
c.delete()
def test_get_people_1(self):
test = fromstring(open(os.path.join(settings.BASE_DIR, 'crisix/database/TestXML/TestADMLNZ.xml')).read())
insert(test)
root = ET.Element('WorldCrises')
get_people(root)
self.assertEqual(root[0][2].text, 'Murderer')
self.assertEqual(root[0][3].text, 'Newtown, Connecticut')
for c in Crisis.objects.all():
c.delete()
def test_get_people_2(self):
test = fromstring(open(os.path.join(settings.BASE_DIR, 'crisix/database/TestXML/TestBROBMA.xml')).read())
insert(test)
root = ET.Element('WorldCrises')
get_people(root)
self.assertEqual(root[0][2].text, 'President')
self.assertEqual(root[0][3].text, 'Washington, D.C, United States Of America')
for c in Crisis.objects.all():
c.delete()
def test_get_people_3(self):
test = fromstring(open(os.path.join(settings.BASE_DIR, 'crisix/database/TestXML/TestHUJNTO.xml')).read())
insert(test)
root = ET.Element('WorldCrises')
get_people(root)
self.assertEqual(root[0][2].text, 'President')
self.assertEqual(root[0][3].text, 'Communist Party Of China')
for c in Crisis.objects.all():
c.delete()
def test_get_common_1(self):
test = fromstring(open(os.path.join(settings.BASE_DIR, 'crisix/database/TestXML/TestSHESSG.xml')).read())
insert(test)
root = ET.Element('WorldCrises')
c = Crisis.objects.get(id='CRI_SHESSG')
x = ET.SubElement(root, 'Crisis')
get_common(x, c)
c.save()
summary= c.summary
self.assertNotEqual(summary.find('According to reports, most of the shooting'), -1)
for c in Crisis.objects.all():
c.delete()
def test_get_common_2(self):
test = fromstring(open(os.path.join(settings.BASE_DIR, 'crisix/database/TestXML/TestUNDWAY.xml')).read())
insert(test)
root = ET.Element('WorldCrises')
c = Organization.objects.get(id='ORG_UNDWAY')
x = ET.SubElement(root, 'Organization')
get_common(x, c)
c.save()
summary= c.summary
self.assertNotEqual(summary.find('Immediately following the tragedy on December 14, 2012,'), -1)
for c in Crisis.objects.all():
c.delete()
def test_get_common_3(self):
test = fromstring(open(os.path.join(settings.BASE_DIR, 'crisix/database/TestXML/TestBROBMA.xml')).read())
insert(test)
root = ET.Element('WorldCrises')
c = Person.objects.get(id='PER_BROBMA')
x = ET.SubElement(root, 'Person')
get_common(x, c)
c.save()
summary= c.summary
self.assertNotEqual(summary.find('He was re-elected president in November 2012,'), -1)
for c in Crisis.objects.all():
c.delete()