forked from hamishgibbs/pull_facebook_data_for_good
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_utils.py
executable file
·115 lines (68 loc) · 4.01 KB
/
test_utils.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 5 10:03:05 2020
@author: hamishgibbs
"""
import os
import shutil
import unittest
import pandas as pd
from datetime import datetime
from utils import try_mkdir_silent, rename_and_move, move_most_recent_files, get_home_dir, get_new_file_name, get_update_date, get_config, origin_to_datetime, get_download_variables, remove_empty_files
class TestUtils(unittest.TestCase):
def setUp(self):
try_mkdir_silent('./tmp1')
try_mkdir_silent('./tmp2')
def test_get_home_dir(self):
self.assertTrue('~' not in get_home_dir())
def test_try_mkdir_silent(self):
self.assertTrue(os.path.exists('./tmp1'))
self.assertTrue(os.path.exists('./tmp2'))
def test_rename_and_move(self):
self.data = pd.DataFrame({'data':[1, 2, 3, 4, 5]})
self.data.to_csv('./tmp1/test.csv')
self.assertTrue(os.path.exists('./tmp1/test.csv'))
rename_and_move('test.csv', './tmp1', 'test_move.csv', './tmp2')
self.assertTrue(os.path.exists('./tmp2/test_move.csv'))
def test_move_most_recent_files(self):
self.data1 = pd.DataFrame({'data':[1, 2, 3, 4, 5]})
self.data1.to_csv(get_home_dir() + '/Downloads/test1.csv')
download_start = datetime.now().timestamp()
self.data2 = pd.DataFrame({'data':[1, 2, 3, 4, 5]})
self.data2.to_csv(get_home_dir() + '/Downloads/Britain Coronavirus Disease Prevention Map Mar 06 2020 Id Id Colocation Map_2020-03-31.csv')
self.assertTrue(os.path.exists(get_home_dir() + '/Downloads/test1.csv'))
self.assertTrue(os.path.exists(get_home_dir() + '/Downloads/Britain Coronavirus Disease Prevention Map Mar 06 2020 Id Id Colocation Map_2020-03-31.csv'))
move_most_recent_files('./tmp1', ['url'], download_start)
self.assertTrue(os.path.exists(get_home_dir() + '/Downloads/test1.csv'))
self.assertTrue(os.path.exists('./tmp1/Britain_2020_03_31.csv'))
os.remove(get_home_dir() + '/Downloads/test1.csv')
def test_get_new_file_name(self):
self.colocation_fn = 'Britain Coronavirus Disease Prevention Map Mar 06 2020 Id Id Colocation Map_2020-03-31.csv'
self.mobility_fn = 'Britain Coronavirus Disease Prevention Map Mar 06 2020 Id Id Movement between Tiles_2020-03-10 0000.csv'
self.assertEqual(get_new_file_name(self.colocation_fn), 'Britain_2020_03_31.csv')
self.assertEqual(get_new_file_name(self.mobility_fn), 'Britain_2020_03_10_0000.csv')
def test_get_update_date(self):
self.data = pd.DataFrame({'data':[1, 2, 3, 4, 5]})
self.assertRaises(ValueError, get_update_date, './tmp1')
self.data.to_csv('./tmp1/test_2020_01_01.csv')
self.assertIsInstance(get_update_date('./tmp1'), datetime)
def test_get_config(self):
config = get_config()
self.assertIsInstance(config, dict)
def test_origin_to_datetime(self):
self.assertEqual(origin_to_datetime('2020_02_02'), datetime(2020, 2, 2))
self.assertEqual(origin_to_datetime('2020_02_02_08'), datetime(2020, 2, 2, 8))
self.assertRaises(ValueError, origin_to_datetime, 'anything_else')
def test_get_download_variables(self):
dl_variables = get_download_variables('Britain', 'Colocation')
self.assertIsInstance(dl_variables, dict)
self.assertEqual(list(dl_variables.keys()), ['id', 'origin'])
def test_remove_empty_files(self):
open('./tmp1/empty.csv', 'w').close()
self.assertTrue(os.path.exists('./tmp1/empty.csv'))
remove_empty_files('./tmp1')
self.assertFalse(os.path.exists('./tmp1/empty.csv'))
def tearDown(self):
shutil.rmtree('./tmp1')
shutil.rmtree('./tmp2')