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
|
import logging
import os.path
import shutil
import tempfile
import unittest
import ruamel.yaml
import lesana
class testCollectionLoading(unittest.TestCase):
def tearDown(self):
shutil.rmtree(os.path.join(self.collection.basedir, '.lesana'))
def test_empty(self):
self.collection = lesana.Collection('tests/data/empty')
self.assertEqual(self.collection.settings, {})
self.collection.update_cache()
self.assertIsNotNone(self.collection.stemmer)
def test_simple(self):
self.collection = lesana.Collection('tests/data/simple')
self.assertIsNotNone(self.collection.settings)
self.assertEqual(
self.collection.settings['name'],
"Simple lesana collection"
)
self.assertEqual(len(self.collection.settings['fields']), 5)
self.assertEqual(len(self.collection.indexed_fields), 2)
self.collection.update_cache()
self.assertIsNotNone(self.collection.stemmer)
def test_wrong_language(self):
# This loads a collection with an invalid value in lang
with self.assertLogs(level=logging.WARNING) as cm:
self.collection = lesana.Collection('tests/data/wrong')
self.assertEqual(len(cm.output), 1)
self.assertIn("Invalid language", cm.output[0])
# The collection will default to english, but should still work.
self.collection.update_cache()
self.assertIsNotNone(self.collection.settings)
self.assertIsNotNone(self.collection.stemmer)
def test_no_index_for_one_entry(self):
# This loads a collection where some of the entries have no
# "index" field
with self.assertLogs(level=logging.WARNING):
self.collection = lesana.Collection('tests/data/wrong')
self.collection.update_cache()
self.assertIsNotNone(self.collection.settings)
self.assertIsNotNone(self.collection.stemmer)
# Fields with no "index" entry are not indexed
self.assertEqual(len(self.collection.settings['fields']), 3)
self.assertEqual(len(self.collection.indexed_fields), 1)
def test_unsafe(self):
self.collection = lesana.Collection('tests/data/simple')
self.collection.safe = False
self.collection.update_cache()
def test_full_search(self):
self.collection = lesana.Collection('tests/data/simple')
self.collection.start_search('Item')
res = self.collection.get_all_search_results()
matches = list(res)
self.assertEqual(len(matches), 2)
for m in matches:
self.assertIsInstance(m, lesana.Entry)
def test_search(self):
self.collection = lesana.Collection('tests/data/simple')
self.collection.start_search('Item')
res = self.collection.get_search_results()
matches = list(res)
self.assertEqual(len(matches), 2)
for m in matches:
self.assertIsInstance(m, lesana.Entry)
def test_search_non_init(self):
self.collection = lesana.Collection('tests/data/simple')
matches = list(self.collection.get_search_results())
self.assertEqual(matches, [])
matches = list(self.collection.get_all_search_results())
self.assertEqual(matches, [])
def test_entry_from_uid(self):
self.collection = lesana.Collection('tests/data/simple')
entry = self.collection.entry_from_uid(
'11189ee47ddf4796b718a483b379f976'
)
self.assertEqual(entry.uid, '11189ee47ddf4796b718a483b379f976')
class testEntries(unittest.TestCase):
def setUp(self):
self.collection = lesana.Collection('tests/data/simple')
self.basepath = 'tests/data/simple/items'
self.filenames = []
def tearDown(self):
for fname in self.filenames:
os.remove(fname)
shutil.rmtree(os.path.join(self.collection.basedir, '.lesana'))
def test_simple(self):
fname = '085682ed-6792-499d-a3ab-9aebd683c011.yaml'
with open(os.path.join(self.basepath, fname)) as fp:
data = ruamel.yaml.safe_load(fp)
entry = lesana.Entry(self.collection, data=data, fname=fname)
self.assertEqual(entry.idterm, 'Q'+data['uid'])
fname = '11189ee47ddf4796b718a483b379f976.yaml'
uid = '11189ee47ddf4796b718a483b379f976'
with open(os.path.join(self.basepath, fname)) as fp:
data = ruamel.yaml.safe_load(fp)
entry = lesana.Entry(self.collection, data=data, fname=fname)
self.assertEqual(entry.idterm, 'Q'+uid)
def test_write_new(self):
new_entry = lesana.Entry(self.collection)
self.collection.save_entries(entries=[new_entry])
entry_fname = 'tests/data/simple/items/' + new_entry.fname
self.filenames.append(entry_fname)
with open(entry_fname) as fp:
text = fp.read()
self.assertIn('quantity (integer): how many items are there', text)
self.assertIn('other (yaml):', text)
self.assertNotIn('position (string)', text)
written = ruamel.yaml.safe_load(text)
self.assertIsInstance(written['quantity'], int)
self.assertIsInstance(written['name'], str)
def test_entry_representation(self):
uid = '11189ee47ddf4796b718a483b379f976'
entry = self.collection.entry_from_uid(uid)
self.assertEqual(
str(entry),
uid
)
label = '{{ uid }}: {{ name }}'
self.collection.settings['entry_label'] = label
self.assertEqual(
str(entry),
'{uid}: {name}'.format(uid=uid, name='Another item')
)
class testComplexCollection(unittest.TestCase):
@classmethod
def setUpClass(self):
self.collection = lesana.Collection('tests/data/complex')
@classmethod
def tearDownClass(self):
shutil.rmtree(os.path.join(self.collection.basedir, '.lesana'))
def test_init(self):
self.assertIsNotNone(self.collection.settings)
self.assertEqual(
self.collection.settings['name'],
"Fully featured lesana collection"
)
self.assertEqual(len(self.collection.settings['fields']), 4)
self.assertIsNotNone(self.collection.stemmer)
self.assertEqual(len(self.collection.indexed_fields), 2)
def test_index(self):
self.collection.update_cache()
class testCollectionCreation(unittest.TestCase):
def test_init(self):
tmpdir = tempfile.mkdtemp()
collection = lesana.Collection.init(tmpdir)
self.assertIsInstance(collection, lesana.Collection)
self.assertTrue(os.path.isdir(os.path.join(tmpdir, '.git')))
self.assertTrue(os.path.isdir(os.path.join(tmpdir, '.lesana')))
self.assertTrue(os.path.isfile(os.path.join(tmpdir, 'settings.yaml')))
self.assertTrue(os.path.isfile(os.path.join(tmpdir, '.gitignore')))
# and then run it twice on the same directory, nothing should break
collection = lesana.Collection.init(tmpdir)
self.assertIsInstance(collection, lesana.Collection)
self.assertTrue(os.path.isdir(os.path.join(tmpdir, '.git')))
self.assertTrue(os.path.isdir(os.path.join(tmpdir, '.lesana')))
self.assertTrue(os.path.isfile(os.path.join(tmpdir, 'settings.yaml')))
self.assertTrue(os.path.isfile(os.path.join(tmpdir, '.gitignore')))
shutil.rmtree(tmpdir)
def do_nothing(*args, **kwargs):
pass
def test_init_edit_file(self):
tmpdir = tempfile.mkdtemp()
collection = lesana.Collection.init(tmpdir, edit_file=self.do_nothing)
self.assertIsInstance(collection, lesana.Collection)
self.assertTrue(os.path.isdir(os.path.join(tmpdir, '.git')))
self.assertTrue(os.path.isdir(os.path.join(tmpdir, '.lesana')))
self.assertTrue(os.path.isfile(os.path.join(tmpdir, 'settings.yaml')))
self.assertTrue(os.path.isfile(os.path.join(tmpdir, '.gitignore')))
shutil.rmtree(tmpdir)
def test_init_no_git(self):
tmpdir = tempfile.mkdtemp()
collection = lesana.Collection.init(tmpdir, git_enabled=False)
self.assertIsInstance(collection, lesana.Collection)
self.assertFalse(os.path.isdir(os.path.join(tmpdir, '.git')))
self.assertTrue(os.path.isdir(os.path.join(tmpdir, '.lesana')))
self.assertTrue(os.path.isfile(os.path.join(tmpdir, 'settings.yaml')))
self.assertFalse(os.path.isfile(os.path.join(tmpdir, '.gitignore')))
# and then run it twice on the same directory, nothing should break
collection = lesana.Collection.init(tmpdir, git_enabled=False)
self.assertIsInstance(collection, lesana.Collection)
self.assertFalse(os.path.isdir(os.path.join(tmpdir, '.git')))
self.assertTrue(os.path.isdir(os.path.join(tmpdir, '.lesana')))
self.assertTrue(os.path.isfile(os.path.join(tmpdir, 'settings.yaml')))
self.assertFalse(os.path.isfile(os.path.join(tmpdir, '.gitignore')))
shutil.rmtree(tmpdir)
if __name__ == '__main__':
unittest.main()
|