File: unit_tests.py

package info (click to toggle)
go2 1.20121210-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 224 kB
  • ctags: 296
  • sloc: python: 1,222; makefile: 34; sh: 9
file content (478 lines) | stat: -rw-r--r-- 11,650 bytes parent folder | download | duplicates (2)
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
#!/usr/bin/atheist
# -*- mode:python; coding:utf-8 -*-

import sys
import os
import time
import multiprocessing as mp

from unittest import TestCase

import fs
from fs.memoryfs import MemoryFS
from fs import ResourceNotFoundError

from pyDoubles.framework import *
import gobject

import go2


HOME = '/home/user'


def hello_task():
    return ["hi"]


def foo_filter(value):
    return value == 'foo'


class StubFS(object):
    def setUp(self):
        self.fs = MemoryFS()
        self.mkdir('lab')

        config = go2.get_config([])
        config.fs = self.fs
        go2.config = config

    def mkdir(self, relpath):
        self.fs.makedir(fs.join(HOME, relpath), recursive=True)


class TestPool(TestCase):
    def setUp(self):
        self.pool = go2.ProcessPool()

    def test_create(self):
        self.pool.terminate()

    # Esta prueba no funciona con atheist
    def test_add_task_hello(self):
        self.pool.add_task(hello_task)

        self.assertEquals(self.pool.output_queue.get(timeout=1), "hi")

        self.pool.join()


class Test_QueueReactor(TestCase):
    def setUp(self):
        self.sut = go2.QueueReactor()

    def test_queue_add_watch(self):
        # given:
        queue = mp.Queue()
        queue.put('foo')

        handler = empty_mock()
        expect_call(handler.handle).with_args(ANY_ARG, gobject.IO_IN, queue).then_return(False)

        self.sut.queue_add_watch(queue, handler.handle)

        # when
        self.sut.process_pending()

        # then
        handler.assert_that_is_satisfied()

    def test_quit(self):
        # given
        gobject.timeout_add(1, self.sut.quit)

        # when
        self.sut.run()

        # then
        # reach this

    def test_quit_on_conditiion(self):
        def quit(reactor):
            reactor.quit()

        # given
        gobject.timeout_add(1, quit, self.sut)

        # when
        self.sut.run()

        # then
        # reach this

    def test_exception_break_reactor_and_re_raises_exception(self):
        def raise_exception():
            raise go2.CancelException()

        # given
        self.sut.timeout_add(1, raise_exception)

        # when/then
        with self.assertRaises(go2.CancelException):
            self.sut.run()


class Test_PathBuffer(TestCase):
    def setUp(self):
        self.path = '/usr/share'

    def test_add_level0(self):
        # given
        sut = go2.PathBuffer(empty_stub())

        # when
        sut.add(go2.PathItem(self.path, 0))

        # then
        self.assertIn(self.path, sut.groups[0])

    def test_add_level0_ignore_repeat(self):
        # given
        sut = go2.PathBuffer(empty_stub())

        # when
        sut.add(go2.PathItem(self.path, 0))
        sut.add(go2.PathItem(self.path, 0))

        # then
        self.assertEquals(len(sut.groups[0]), 1)

    def test_add_level0_goes_to_menu(self):
        # given
        menu = empty_mock()
        expect_call(menu.add_entry)

        sut = go2.PathBuffer(menu)

        # when
        sut.add(go2.PathItem(self.path, 0))

        # then
        menu.assert_that_is_satisfied()

    def test_add_level_not0_does_not_go_to_menu(self):
        # given
        menu = empty_mock()
        sut = go2.PathBuffer(menu)

        # when
        sut.add(go2.PathItem(self.path, 1))

        # then
        menu.assert_that_is_satisfied()

    def test_add_alternate(self):
        # given
        menu = empty_mock()
        expect_call(menu.next_group)
        expect_call(menu.add_entry).with_args(self.path)
        sut = go2.PathBuffer(menu)

        # when
        sut.add(go2.PathItem(self.path, 1))
        sut.flush_alternate()

        # then
        menu.assert_that_is_satisfied()

    def test_add_filtered(self):
        # given
        menu = empty_mock()
        sut = go2.PathBuffer(menu)
        sut.add_filter(foo_filter)

        # when
        sut.add(go2.PathItem('foo', 0))

        # then
        menu.assert_that_is_satisfied()


class PathFileHelper(StubFS):
    def PathFileStore_with_content(self, store, more=''):
        self.fs.createfile(store, u'''\
1: /home/user/lab
2: /home/user/lab/media
''' + more)
        return go2.PathFileStore(store)


class Test_PathFileStore(TestCase, PathFileHelper):
    def setUp(self):
        StubFS.setUp(self)
        self.store_fpath = '/paths'

    def assert_visits(self, store_path, path, visits):
        self.assertIn("{0}:{1}\n".format(visits, path),
                      self.fs.getcontents(self.store_fpath))

    def store_create_and_insert(self, store_path, path, size=None, visits=None):
        sut = self.PathFileStore_with_content(store_path)
        sut.add_visit(path).save()
        if size:
            self.assertEquals(len(sut.data), size)
        if visits:
            self.assert_visits(store_path, path, visits)
        return sut

    def test_load_ok(self):
        sut = self.PathFileStore_with_content(store=self.store_fpath)
        self.assertItemsEqual(
            sut.data.keys(),
            [u'/home/user/lab', u'/home/user/lab/media'])

    def test_load_missing(self):
        with self.assertRaises(ResourceNotFoundError):
            go2.PathFileStore('missing')

    def test_insert_new(self):
        self.store_create_and_insert(self.store_fpath, u'/XXX', 3, 1)

    def test_insert_old_most_visited(self):
        path = u'/home/user/lab/media'
        sut = self.store_create_and_insert(
            self.store_fpath, path, size=2, visits=3)
        self.assertEquals(list(sut)[0], path)

    def test_insert_old_less_visited(self):
        path = u'/home/user/lab'
        sut = self.store_create_and_insert(
            self.store_fpath, path, size=2, visits=2)
        sut.add_visit(path).save()
        self.assert_visits(self.store_fpath, path, 3)
        self.assertEquals(list(sut)[0], path)

    def test_deprecated_file_format(self):
        # given
        self.fs.createfile(self.store_fpath, u'''\
/home/user/lab
/home/user/lab/media
''')
        # when
        go2.PathFileStore(self.store_fpath).save()

        # then
        self.assertItemsEqual(
            self.fs.getcontents(self.store_fpath).split(),
            u'''\
1:/home/user/lab
1:/home/user/lab/media
'''.split())

    def test_wrong_file_encoding_lines_are_pruned(self):
        # given
        go2.config.encoding = 'utf-8'
        self.fs.createfile(self.store_fpath, u'''\
1:/foo
1:/home/ñandú
'''.encode('latin1'))

        # when
        go2.PathFileStore(self.store_fpath).save()

        # then
        self.assertEquals(
            self.fs.getcontents(self.store_fpath).strip(),
            "1:/foo")


class Test_from_file_provider(TestCase, PathFileHelper):
    def setUp(self):
        StubFS.setUp(self)

    def test_search_existing(self):
        # given
        go2.config.matcher = go2.PathMatcher(['la'])
        store = self.PathFileStore_with_content('paths')

        # whem
        paths = list(go2.from_file_provider(store))

        # then
        self.assertIn(go2.PathItem(u'/home/user/lab'), paths)

    def test_search_missing(self):
        "from_file_provider remove checked missing paths"
        sut = self.PathFileStore_with_content('paths')
        go2.config.pattern = ['med']

        paths = list(go2.from_file_provider(sut))

        self.assertFalse(paths)


class Test_Menu(TestCase):
    def setUp(self):
        self.reactor_spy = empty_spy()
        self.fd_stub = empty_stub()

    def new_menu(self, **kargs):
        params = dict(reactor=self.reactor_spy, out=self.fd_stub)
        params.update(kargs)
        return go2.Menu(**params)

    def test_add_entry(self):
        sut = self.new_menu()

        sut.add_entry('foo')

        self.assertIn('foo', sut.entries)

    def test_FullSinkException(self):
        # given
        sut = self.new_menu(max_size=1)

        # when/then
        sut.add_entry('foo')
        with self.assertRaises(go2.Menu.FullSinkException):
            sut.add_entry('bar')

        self.assert_(sut.is_full)

    def test_print_ENTER_for_first(self):
        # given
        fd = empty_spy()
        sut = self.new_menu(out=fd)

        # when
        sut.add_entry('foo')

        # then
        assert_that_was_called(fd.write).with_args('a: foo')

    def test_print_second(self):
         # given
        fd = empty_spy()
        sut = self.new_menu(out=fd)

        # when
        sut.add_entry('foo')
        sut.add_entry('bar')

        # then
        assert_that_was_called(fd.write).with_args(ANY_ARG)
        assert_that_was_called(fd.write).with_args('b: bar')

    def test_select_first_with_empty_menu(self):
        # given
        sut = self.new_menu()

        # when/then
        with self.assertRaises(go2.Sink.InvalidChoiceException):
            sut.perform_choice(0)

    def test_valid_key(self):
        # given
        sut = self.new_menu()
        sut.add_entry('foo')

        # when
        sut.perform_choice(0)

        # then
        self.assertEquals(sut.target, 'foo')
        assert_that_was_called(self.reactor_spy.quit)

    def test_wrong_key_raises_invalid(self):
        # given
        sut = self.new_menu()
        sut.add_entry('foo')

        # when/then
        with self.assertRaises(go2.Sink.InvalidChoiceException):
            sut.perform_choice(1)

    def FIXME_test_groups(self):
        pass


class Test_UserChoiceHanlder(TestCase):
    def input_stub(self, key):
        retval = empty_stub()
        when(retval.read).then_return(key)
        return retval

    def test_ESC_quit_reactor(self):
       # given
        sut = go2.UserInputHandler()
        fd = self.input_stub(chr(go2.ESC))

        # when/then
        with self.assertRaises(go2.CancelException):
            sut(fd)

        # then
#        assert_that_was_called(self.reactor_spy.quit)


class TestIgnore(TestCase):
    def create_manager(self, content):
        return go2.IgnoreManager(content)

    def test_ignored_path_with_1_pattern(self):
        sut = self.create_manager("foo")

        self.assert_(
            sut.is_ignored("/home/user/repo/foo"))

    def test_ignored_path_with_2_patterns(self):
        # given
        sut = self.create_manager('''
foo
bar''')

        self.assert_(sut.is_ignored("/home/foo"))
        self.assert_(sut.is_ignored("/home/bar"))

    def test_not_matching_path(self):
        sut = self.create_manager("foo")

        self.assertFalse(
            sut.is_ignored('/usr/share'))

    def test_path_match_in_any_place(self):
        sut = self.create_manager("foo")

        self.assert_(
            sut.is_ignored('/usr/foo/share'))

    def test_path_not_match_if_not_full_name(self):
        sut = self.create_manager("foo")

        self.assertFalse(
            sut.is_ignored('/usr/xxfoo'))

        self.assertFalse(
            sut.is_ignored('/usr/fooxx'))

    def test_absolute_dir(self):
        sut = self.create_manager('/usr/local/*')

        self.assert_(sut.is_ignored('/usr/local/foo'))
        self.assert_(sut.is_ignored('/usr/local'))

    def test_from_file__missing_file(self):
        # given
        fs = MemoryFS()
        config = go2.get_config()
        config.fs = fs
        go2.config = config

        # when
        with self.assertRaises(ResourceNotFoundError):
            go2.IgnoreManager.from_file('ignore')

    def test_from_file(self):
        # given
        fs = MemoryFS()
        fs.createfile('ignore', 'foo')
        config = go2.get_config()
        config.fs = fs
        go2.config = config

        # when
        sut = go2.IgnoreManager.from_file('ignore')

        # then
        self.assert_(sut.is_ignored('/home/foo'))