1 | from unittest import TestCase |
||
2 | from mock import Mock, sentinel, patch |
||
3 | import io, os, shutil, bson |
||
4 | |||
5 | |||
6 | class PictureCacheTests(TestCase): |
||
7 | |||
8 | def setUp(self): |
||
9 | picdir = os.path.expanduser('~/.niprov-snapshots') |
||
10 | if os.path.isdir(picdir): |
||
11 | shutil.rmtree(picdir) |
||
12 | import niprov.pictures |
||
13 | niprov.pictures._CACHE = {} # reset cache |
||
14 | |||
15 | def test_Serialize(self): |
||
16 | from niprov.pictures import PictureCache |
||
17 | pictures = PictureCache(sentinel.dependencies) |
||
18 | pictures.getFilepath = Mock() |
||
19 | fpath = pictures.serializeSingle(sentinel.img) |
||
20 | pictures.getFilepath.assert_called_with(for_=sentinel.img) |
||
21 | self.assertEqual(pictures.getFilepath(), fpath) |
||
22 | |||
23 | def test_Provides_new_picture_file_handle(self): |
||
24 | from niprov.pictures import PictureCache |
||
25 | pictures = PictureCache(sentinel.dependencies) |
||
26 | newPicture = pictures.new() |
||
27 | self.assertTrue(hasattr(newPicture, 'write')) |
||
28 | |||
29 | View Code Duplication | def test_Stored_picture_can_be_retrieved_as_filepath(self): |
|
0 ignored issues
–
show
Duplication
introduced
by
![]() |
|||
30 | from niprov.pictures import PictureCache |
||
31 | myImg = Mock() |
||
32 | myImg.provenance = {'id':'007'} |
||
33 | pictures = PictureCache(sentinel.dependencies) |
||
34 | self.assertIsNone(pictures.getFilepath(for_=myImg)) |
||
35 | pictures.keep(io.BytesIO('/x10/x05/x5f'), for_=myImg) |
||
36 | picfpath = os.path.expanduser('~/.niprov-snapshots/007.png') |
||
37 | self.assertEqual(picfpath, pictures.getFilepath(for_=myImg)) |
||
38 | |||
39 | def test_Stored_picture_can_be_retrieved_as_bytes(self): |
||
40 | from niprov.pictures import PictureCache |
||
41 | myImg = Mock() |
||
42 | myImg.provenance = {'id':'007'} |
||
43 | pictures = PictureCache(sentinel.dependencies) |
||
44 | newPicture = pictures.new() |
||
45 | newPicture.write('/x10/x05/x5f') |
||
46 | pictures.keep(newPicture, for_=myImg) |
||
47 | outBytes = pictures.getBytes(for_=myImg) |
||
48 | self.assertEqual('/x10/x05/x5f', outBytes) |
||
49 | |||
50 | View Code Duplication | def test_Can_be_told_to_persist_picture_to_disk_now(self): |
|
0 ignored issues
–
show
|
|||
51 | from niprov.pictures import PictureCache |
||
52 | myImg = Mock() |
||
53 | myImg.provenance = {'id':'007'} |
||
54 | pictures = PictureCache(sentinel.dependencies) |
||
55 | pictures.saveToDisk(for_=myImg) #shouldn't do anything |
||
56 | pictures.keep(io.BytesIO('/x10/x05/x5f'), for_=myImg) |
||
57 | pictures.saveToDisk(for_=myImg) |
||
58 | picfpath = os.path.expanduser('~/.niprov-snapshots/007.png') |
||
59 | self.assertTrue(os.path.isfile(picfpath)) |
||
60 | with open(picfpath) as picFile: |
||
61 | self.assertEqual(picFile.read(), '/x10/x05/x5f') |
||
62 | |||
63 | View Code Duplication | def test_If_pic_is_not_in_cache_but_on_filesystem_provides_that(self): |
|
0 ignored issues
–
show
|
|||
64 | import niprov.pictures |
||
65 | myImg = Mock() |
||
66 | myImg.provenance = {'id':'007'} |
||
67 | pictures = niprov.pictures.PictureCache(sentinel.dependencies) |
||
68 | pictures.keep(io.BytesIO('/x10/x05/x5f'), for_=myImg) |
||
69 | pictures.saveToDisk(for_=myImg) |
||
70 | niprov.pictures._CACHE = {} # reset cache |
||
71 | picfpath = os.path.expanduser('~/.niprov-snapshots/007.png') |
||
72 | self.assertEqual(picfpath, pictures.getFilepath(for_=myImg)) |
||
73 | |||
74 | def test_Keep_accepts_bsonBinary(self): |
||
75 | from niprov.pictures import PictureCache |
||
76 | myImg = Mock() |
||
77 | myImg.provenance = {'id':'007'} |
||
78 | pictures = PictureCache(sentinel.dependencies) |
||
79 | pictures.keep(bson.Binary('/x10/x05/x5f'), for_=myImg) |
||
80 | self.assertEqual('/x10/x05/x5f', pictures.getBytes(for_=myImg)) |
||
81 | |||
82 | |||
83 | |||
84 |