-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_dump_events_to_json.py
104 lines (85 loc) · 3.14 KB
/
test_dump_events_to_json.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
import os
import codecs
import numpy as np
import ujson as json
from collections import defaultdict
from nose.tools import assert_equal, with_setup, assert_true
from .dump_events_to_json import run
from .dump_contexted_events_to_json import run_with_context
from .test_util import CURDIR, remove_tmp_data
def setup_func():
"set up test fixtures"
def teardown_func():
remove_tmp_data(os.path.join(CURDIR, 'test/data/tmp'))
@with_setup(setup_func, teardown_func)
def test_dump_events_to_json():
output_path = os.path.join(CURDIR, 'test/data/tmp/candidate_trees.json')
run(os.path.join(CURDIR, 'test/data/candidate_trees.pkl'),
k=5,
id2people=defaultdict(lambda: {'name': 'fake'}),
id2interaction=defaultdict(
lambda: {'body': 'fake', 'subject': 'fake'}
),
dirname=os.path.join(CURDIR, 'test/data/tmp'))
with codecs.open(output_path, 'r', 'utf8') as f:
obj = json.loads(f.read())
assert_equal(5, len(obj))
for o in obj:
for n in o['nodes']:
assert_true('r' in n)
assert_true('sender' in n)
assert_true('recipients' in n)
assert_true('subject' in n)
assert_true('body' in n)
assert_true(isinstance(n['cluster_label'], int))
@with_setup(setup_func, teardown_func)
def test_dump_events_to_json_to_original_graph():
output_path = os.path.join(CURDIR, 'test/data/tmp/candidate_trees.json')
run(os.path.join(CURDIR, 'test/data/candidate_trees.pkl'),
k=5,
id2people=defaultdict(lambda: {'name': 'fake'}),
id2interaction=defaultdict(
lambda: {'body': 'fake', 'subject': 'fake'}
),
dirname=os.path.join(CURDIR, 'test/data/tmp'),
to_original_graph=True)
with codecs.open(output_path, 'r', 'utf8') as f:
obj = json.loads(f.read())
assert_equal(5, len(obj))
for o in obj:
for n in o['nodes']:
assert_true('r' not in n)
def check(output_path):
with codecs.open(output_path, 'r', 'utf8') as f:
obj = json.loads(f.read())
for t in obj:
assert_true(
np.any([n.get('event', False)
for n in t['nodes']])
)
assert_equal(5, len(obj))
@with_setup(setup_func, teardown_func)
def test_dump_contexted_events_to_json():
output_path = os.path.join(
CURDIR,
'test/data/tmp/candidate_trees_decompose=False.json'
)
run_with_context(
os.path.join(CURDIR, 'test/data/enron-whole.json'),
os.path.join(CURDIR, 'test/data/candidate_trees_decompose=False.pkl'),
os.path.join(CURDIR, 'test/data/tmp')
)
check(output_path)
@with_setup(setup_func, teardown_func)
def test_dump_contexted_events_to_json_to_original_graph():
output_path = os.path.join(
CURDIR,
'test/data/tmp/candidate_trees_decompose=False.json'
)
run_with_context(
os.path.join(CURDIR, 'test/data/enron-whole.json'),
os.path.join(CURDIR, 'test/data/candidate_trees_decompose=False.pkl'),
os.path.join(CURDIR, 'test/data/tmp'),
to_original_graph=True
)
check(output_path)