aboutsummaryrefslogtreecommitdiff
path: root/tests/test_collection.py
blob: 1bacfd6b4a2a36960e039602525b80a12a20fb45 (plain)
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
import logging
import os.path
import shutil
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.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']), 4)

        self.collection.update_cache()
        self.assertIsNotNone(self.collection.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.cache)
        self.assertIsNotNone(self.collection.stemmer)

    def test_unsafe(self):
        self.collection = lesana.Collection('tests/data/simple')
        self.collection.safe = False
        self.collection.update_cache()


class testEntries(unittest.TestCase):
    def setUp(self):
        self.collection = lesana.Collection('tests/data/simple')
        self.basepath = 'tests/data/simple/items'

    def test_simple(self):
        fname = '085682ed-6792-499d-a3ab-9aebd683c011.yaml'
        with open(os.path.join(self.basepath, fname)) as fp:
            data = ruamel.yaml.load(fp)
        entry = lesana.Entry(self.collection, data=data, fname=fname)
        self.assertEqual(entry.idterm, 'Q'+data['uid'])
        self.assertEqual(len(entry.indexed_fields), 4)
        fname = '11189ee47ddf4796b718a483b379f976.yaml'
        uid = '11189ee47ddf4796b718a483b379f976'
        with open(os.path.join(self.basepath, fname)) as fp:
            data = ruamel.yaml.load(fp)
        entry = lesana.Entry(self.collection, data=data, fname=fname)
        self.assertEqual(entry.idterm, 'Q'+uid)
        self.assertEqual(len(entry.indexed_fields), 3)

    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
        with open(entry_fname) as fp:
            written = ruamel.yaml.load(fp)
        self.assertIsInstance(written['quantity'], int)
        self.assertIsInstance(written['name'], str)
        os.remove(entry_fname)


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']), 3)
        self.assertIsNotNone(self.collection.stemmer)

    def test_index(self):
        self.collection.update_cache()
        self.assertIsNotNone(self.collection.cache)