aboutsummaryrefslogtreecommitdiff
path: root/tests/test_commands.py
blob: 92b603f94319e001d7a160c04e6f0b9255879f5f (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
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
import contextlib
import io
import os
import tempfile
import unittest

from lesana import command
from . import utils


class Args:
    def __init__(self, args):
        self.args = args

    def __getattribute__(self, k):
        try:
            return super().__getattribute__(k)
        except AttributeError:
            try:
                return self.args[k]
            except KeyError as e:
                raise AttributeError(e)


class testCommands(unittest.TestCase):
    def setUp(self):
        self.tmpdir = tempfile.TemporaryDirectory()
        utils.copytree(
            'tests/data/simple',
            self.tmpdir.name,
            dirs_exist_ok=True,
        )

    def tearDown(self):
        pass

    def _edit_file(self, filepath):
        return True

    def _run_command(self, cmd, args):
        stream = {
            'stdout': io.StringIO(),
            'stderr': io.StringIO(),
        }
        cmd.edit_file_in_external_editor = self._edit_file
        cmd.args = Args(args)
        with contextlib.redirect_stdout(stream['stdout']):
            with contextlib.redirect_stderr(stream['stderr']):
                cmd.main()
        return stream

    def test_init(self):
        args = {
            'collection': self.tmpdir.name,
            'git': True,
        }
        streams = self._run_command(command.Init(), args)
        self.assertEqual(streams['stdout'].getvalue(), '')
        self.assertEqual(streams['stderr'].getvalue(), '')

    def test_new(self):
        args = {
            'collection': self.tmpdir.name,
            'git': True,
        }
        streams = self._run_command(command.New(), args)
        self.assertEqual(len(streams['stdout'].getvalue()), 33)
        self.assertEqual(streams['stderr'].getvalue(), '')

    def test_edit(self):
        args = {
            'collection': self.tmpdir.name,
            'git': True,
            'eid': '11189ee4',
        }
        streams = self._run_command(command.Edit(), args)
        self.assertTrue(args['eid'] in streams['stdout'].getvalue())
        self.assertEqual(streams['stderr'].getvalue(), '')

    def test_show(self):
        args = {
            'collection': self.tmpdir.name,
            'git': True,
            'eid': '11189ee4',
            'template': False,
        }
        streams = self._run_command(command.Show(), args)
        self.assertTrue(
            'name: Another item' in streams['stdout'].getvalue()
        )
        self.assertEqual(streams['stderr'].getvalue(), '')

    def test_index(self):
        args = {
            'collection': self.tmpdir.name,
            'git': True,
            'files': None,
            'reset': True,
        }
        streams = self._run_command(command.Index(), args)
        self.assertEqual(
            streams['stdout'].getvalue(),
            'Found and indexed 3 entries\n',
        )
        self.assertEqual(streams['stderr'].getvalue(), '')

    def test_search(self):
        args = {
            'collection': self.tmpdir.name,
            'git': True,
            'template': False,
            'query': 'Another',
            'offset': None,
            'pagesize': None,
            'sort': None,
            'all': False,
        }
        streams = self._run_command(command.Search(), args)
        self.assertTrue(
            '11189ee4' in streams['stdout'].getvalue()
        )
        self.assertEqual(streams['stderr'].getvalue(), '')

    def test_export(self):
        dest_tmpdir = tempfile.TemporaryDirectory()
        utils.copytree(
            'tests/data/simple',
            dest_tmpdir.name,
            dirs_exist_ok=True,
        )
        # TODO: make finding the templates less prone to breaking and
        # then remove the cwd change from here
        old_cwd = os.getcwd()
        os.chdir(self.tmpdir.name)
        args = {
            'collection': self.tmpdir.name,
            'git': True,
            'template': 'templates/from_self.yaml',
            'query': 'Another',
            'destination': dest_tmpdir.name,
        }
        streams = self._run_command(command.Export(), args)
        os.chdir(old_cwd)
        self.assertEqual(streams['stdout'].getvalue(), '')
        self.assertEqual(streams['stderr'].getvalue(), '')

    def test_remove(self):
        args = {
            'collection': self.tmpdir.name,
            'git': True,
            'entries': '11189ee4',
        }
        streams = self._run_command(command.Remove(), args)
        self.assertEqual(streams['stdout'].getvalue(), '')
        self.assertEqual(streams['stderr'].getvalue(), '')

    def test_update(self):
        args = {
            'collection': self.tmpdir.name,
            'git': True,
            'query': 'Another',
            'field': 'position',
            'value': 'here',
        }
        streams = self._run_command(command.Update(), args)
        self.assertEqual(streams['stdout'].getvalue(), '')
        self.assertEqual(streams['stderr'].getvalue(), '')


if __name__ == '__main__':
    unittest.main()