-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtests.py
178 lines (125 loc) · 4.43 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
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
import os
import shutil
import unittest
import indexer
TEST_SNIPPET = 'test_snippet.md'
TEST_DIR = 'test'
SAMPLE_TEXT_PART1 = '''# Overview
TODO: Write a brief description of the problem being solved by this code. Or what new functionality does this provide?
## Examples
- URL(s) to see this working in your site
'''
SAMPLE_TEXT_PART2 = '''# Solution
TODO: Write a brief description of the solution. This could include:
- How does it work?
- Prerequisites or dependencies?
- URL(s) to see source code in context in your repository
## Installation instructions
TODO: Write steps for users to follow to implement this. Skip the standard download/modify/zip/upload Primo view instructions.
'''
SAMPLE_INDEX_CONTENT = '''# Keyword Index
## --- A ---
## --- B ---
**Bugs:**
- [ucla/boolean_search.md](ucla/boolean_search.md)
- [uci/boolean_search.md](uci/boolean_search.md)
## --- C ---
## --- D ---
## --- E ---
## --- F ---
## --- G ---
## --- H ---
**HathiTrust:**
- [ucla/hathitrust.md](ucla/hathitrust.md)
- [ucsb/hathitrust2.md](ucsb/hathitrust2.md)
## --- I ---
## --- J ---
## --- K ---
## --- L ---
## --- M ---
## --- N ---
## --- O ---
## --- P ---
## --- Q ---
## --- R ---
## --- S ---
**Something:**
- [ucla/something.md](ucla/something.md)
**Something else:**
- [ucla/something_else.md](ucla/something_else.md)
## --- T ---
## --- U ---
## --- V ---
## --- W ---
## --- X ---
## --- Y ---
## --- Z ---
'''
class TestIndexer(unittest.TestCase):
def create_one_test_file(self, keywords='', path=None):
text = f'{SAMPLE_TEXT_PART1}{keywords}{SAMPLE_TEXT_PART2}'
path = TEST_SNIPPET if path is None else path
with open(path, 'w') as file:
file.write(text)
def create_test_dirs(self):
dirs = []
os.mkdir(TEST_DIR)
for campus in indexer.CAMPUS_DIRS:
dir = (os.path.join(TEST_DIR, campus))
os.mkdir(dir)
dirs.append(dir)
return dirs
def create_multiple_test_files(self, dirs):
paths = []
for dir in dirs:
path = os.path.join(dir, TEST_SNIPPET)
paths.append(path)
keywords = f'## Keywords\n\n{os.path.split(dir)[1]}\n\n'
self.create_one_test_file(keywords=keywords, path=path)
return paths
def tearDown(self):
if os.path.exists(TEST_SNIPPET):
os.remove(TEST_SNIPPET)
if os.path.exists(TEST_DIR):
shutil.rmtree(TEST_DIR)
def test_extract_keywords_normal(self):
text = '## Keywords\n\nranking, HathiTrust, ILL, "welcome screen", \'nav bar\', "advanced search" \n\n'
self.create_one_test_file(text)
result = indexer.extract_keywords(TEST_SNIPPET, indexer.PATTERN)
expected = ['Ranking', 'HathiTrust', 'ILL', 'Welcome screen', 'Nav bar', 'Advanced search']
self.assertEqual(result, expected)
def test_extract_keywords_empty(self):
self.create_one_test_file('')
with self.assertRaises(Exception):
indexer.extract_keywords(TEST_SNIPPET, indexer.PATTERN)
def test_get_snippet_filepaths(self):
dirs = self.create_test_dirs()
paths = self.create_multiple_test_files(dirs)
result = indexer.get_snippet_filepaths(dirs)
self.assertEqual(result, paths)
def test_build_index(self):
dirs = self.create_test_dirs()
paths = self.create_multiple_test_files(dirs)
result, errors = indexer.build_index(paths, indexer.PATTERN)
expected = {
'Ucb': ['test/ucb/test_snippet.md'],
'Ucd': ['test/ucd/test_snippet.md'],
'Uci': ['test/uci/test_snippet.md'],
'Ucla': ['test/ucla/test_snippet.md'],
'Ucm': ['test/ucm/test_snippet.md'],
'Ucr': ['test/ucr/test_snippet.md'],
'Ucsb': ['test/ucsb/test_snippet.md'],
'Ucsc': ['test/ucsc/test_snippet.md'],
'Ucsd': ['test/ucsd/test_snippet.md'],
'Ucsf': ['test/ucsf/test_snippet.md']
}
self.assertEqual(result, expected)
def test_draft_index_content(self):
index = {
'Bugs': ['ucla/boolean_search.md', 'uci/boolean_search.md'],
'HathiTrust': ['ucla/hathitrust.md', 'ucsb/hathitrust2.md'],
'Something': ['ucla/something.md'],
'Something else': ['ucla/something_else.md']
}
result = indexer.draft_index_content(index)
self.assertEqual(result, SAMPLE_INDEX_CONTENT)