1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
import unittest |
3
|
|
|
import os |
4
|
|
|
from datetime import date |
5
|
|
|
from shutil import copyfile |
6
|
|
|
|
7
|
|
|
from oe_utils.utils.actor_utils import actor_uri |
8
|
|
|
from oe_utils.utils.file_utils import ( |
9
|
|
|
get_file_size, |
10
|
|
|
get_last_modified_date, |
11
|
|
|
convert_size |
12
|
|
|
) |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
class ActorUtilsTest(unittest.TestCase): |
16
|
|
|
def setUp(self): |
17
|
|
|
self.user = {"userid": "jalapjo", "actor": {"uri": "https://actor.test.org.test/a/123", "id": 123, |
18
|
|
|
"omschrijving": "The Jalapeno, Jose"}} |
19
|
|
|
|
20
|
|
|
def tearDown(self): |
21
|
|
|
pass |
22
|
|
|
|
23
|
|
|
def test_actor_uri(self): |
24
|
|
|
uri = actor_uri(self.user) |
25
|
|
|
self.assertEqual("https://actor.test.org.test/a/123", uri) |
26
|
|
|
|
27
|
|
|
def test_actor_uri_na(self): |
28
|
|
|
uri = actor_uri({}) |
29
|
|
|
self.assertEqual("NA", uri) |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
class FileUtilsTest(unittest.TestCase): |
33
|
|
|
def setUp(self): |
34
|
|
|
self.fixtures_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'fixtures') |
35
|
|
|
self.test_file = os.path.join(self.fixtures_path, 'Test_document.pdf') |
36
|
|
|
self.non_existing_file = os.path.join(self.fixtures_path, 'I_do_not_exist.pdf') |
37
|
|
|
|
38
|
|
|
def tearDown(self): |
39
|
|
|
pass |
40
|
|
|
|
41
|
|
|
def test_get_file_size(self): |
42
|
|
|
filesize = get_file_size(self.test_file) |
43
|
|
|
self.assertEqual('7.94 KB', filesize) |
44
|
|
|
|
45
|
|
|
def test_get_file_size_non_existing_file(self): |
46
|
|
|
self.assertIsNone(get_file_size(self.non_existing_file)) |
47
|
|
|
|
48
|
|
|
def test_get_last_modified_date(self): |
49
|
|
|
today = date.today().strftime('%d/%m/%Y') |
50
|
|
|
# create new file to guarantee the last modified date is today |
51
|
|
|
copyfile(self.test_file, |
52
|
|
|
os.path.join(self.fixtures_path, 'test_utils_temp_document.pdf')) |
53
|
|
|
self.new_file = os.path.join(self.fixtures_path, 'test_utils_temp_document.pdf') |
54
|
|
|
lm_date = get_last_modified_date(self.new_file) |
55
|
|
|
self.assertEqual(today, lm_date) |
56
|
|
|
os.remove(self.new_file) |
57
|
|
|
|
58
|
|
|
def test_get_last_modified_date_no_file(self): |
59
|
|
|
self.assertIsNone(get_last_modified_date('testststs')) |
60
|
|
|
|
61
|
|
|
def test_get_last_modified_date_non_existing_file(self): |
62
|
|
|
self.assertIsNone(get_file_size(self.non_existing_file)) |
63
|
|
|
|
64
|
|
|
def test_convert_size(self): |
65
|
|
|
self.assertEqual('2.0 KB', convert_size(2048)) |
66
|
|
|
self.assertEqual('20.0 MB', convert_size(20971520)) |
67
|
|
|
self.assertEqual('1.0 GB', convert_size(1073741824)) |
68
|
|
|
self.assertEqual('0B', convert_size(0)) |
69
|
|
|
|