-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
test_package.py
131 lines (121 loc) · 4.21 KB
/
test_package.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
#!/usr/bin/env python3
# coding=utf-8
import json
import pytest
import subprocess
import os
import io
try:
to_unicode = unicode
except NameError:
to_unicode = str
# 1. Is it a good json format? (Maybe someone let it as unformatted json file.)
# 2. Varify the json file is used 2 spaces per indent level.
# 3. Check every object
class GetFiles():
def get_group_files(self):
cmd = "find ~/ -name TGmeetup | sed -n '1p'"
output = subprocess.check_output(cmd, shell=True)
try:
mydir = str(output.splitlines()).split("'")[1]
except BaseException:
mydir = str(output.splitlines())
cmd = "du -a " + mydir + "/community " + mydir + \
"/conference | grep package.json | awk '{print $2}'"
output = subprocess.check_output(cmd, shell=True)
gf_all = []
for gf in output.splitlines():
try:
gf_all.append(str(gf).split("'")[1])
except BaseException:
gf_all.append(str(gf))
return(gf_all)
class TestFileFormat:
def test_isformat(self):
message = "TEST_MESSAGE"
try:
get_files = GetFiles()
all_files = get_files.get_group_files()
for org_file in all_files:
json.load(open(org_file))
except pytest.raises.Exception as e:
assert e.msg == message
else:
assert True, "Expected pytest.raises.Exception"
def test_indent(self):
get_files = GetFiles()
all_files = get_files.get_group_files()
for org_file in all_files:
data = json.load(open(org_file))
with io.open("tmplate.json", 'w', encoding='utf8') as outfile:
str_ = json.dumps(data,
indent=2, sort_keys=False,
separators=(',', ': '), ensure_ascii=False)
outfile.write(to_unicode(str_))
f1 = open(org_file)
f2 = open("tmplate.json")
with f1, f2, open('outfile.txt', 'w') as outfile:
for line1, line2 in zip(f1, f2):
if line2 != "}":
assert line1 == line2, org_file
try:
os.remove("tmplate.json")
except BaseException:
pass
class TestObjects:
# 1. Test require fields
# 2. Test other fields
def test_necessary_field(self):
get_files = GetFiles()
all_files = get_files.get_group_files()
for org_file in all_files:
data = json.load(open(org_file))
# Name
assert isinstance(data["name"], str)
assert len(data["name"].split()) == 1
# Title
assert isinstance(data["title"], str)
# countrycode
assert isinstance(data["countrycode"], str)
assert len(data["countrycode"]) == 2
# city
assert isinstance(data["city"], str)
# keywords
assert isinstance(data["keywords"], list)
# registration
assert isinstance(data["registration"], dict)
def test_not_necessary_field(self):
get_files = GetFiles()
all_files = get_files.get_group_files()
for org_file in all_files:
data = json.load(open(org_file))
# description
try:
assert isinstance(data["description"], str)
except BaseException:
continue
# homepage
try:
assert isinstance(data["homepage"], str)
except BaseException:
continue
# contact
try:
assert isinstance(data["contact"], str)
except BaseException:
continue
# contributors
try:
assert isinstance(data["contributors"], str)
except BaseException:
continue
# chat
try:
assert isinstance(data["chat"], list)
except BaseException:
continue
# social-media
try:
assert isinstance(data["social_media"], list)
except BaseException:
continue