Skip to content

Commit b27f6da

Browse files
committed
Add download tests
1 parent b310fd9 commit b27f6da

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# standard
2+
import pytest
3+
import mock
4+
from datetime import datetime as dt
5+
from datetime import timedelta
6+
7+
# first party
8+
from delphi_changehc.download_ftp_files import *
9+
10+
class TestDownloadFTPFiles:
11+
12+
class MockSFTP:
13+
14+
# Mocks an SFTP connection
15+
def __init__(self, attrs):
16+
self.attrs = attrs
17+
self.num_gets = 0
18+
19+
# Attrs are modified time and filename
20+
def listdir_attr(self):
21+
return self.attrs
22+
23+
# Don't download anything, just note that method was called
24+
def get(self, infile, outfile, callback=None):
25+
self.num_gets += 1
26+
return
27+
28+
29+
class FileAttr:
30+
31+
def __init__(self, time, name):
32+
self.st_mtime = time
33+
self.filename = name
34+
35+
36+
@mock.patch("os.path")
37+
def test_get_files(self, mock_path):
38+
39+
# When one new file is present, one file is downloaded
40+
one_new = self.MockSFTP([self.FileAttr(dt.timestamp(dt.now()-timedelta(minutes=1)),"foo")])
41+
get_files_from_dir(one_new, "")
42+
assert one_new.num_gets == 1
43+
44+
# When one new file and one old file are present, one file is downloaded
45+
one_new_one_old = self.MockSFTP([self.FileAttr(dt.timestamp(dt.now()-timedelta(minutes=1)),"foo"),
46+
self.FileAttr(dt.timestamp(dt.now()-timedelta(days=10)),"foo")])
47+
get_files_from_dir(one_new_one_old, "")
48+
assert one_new_one_old.num_gets == 1
49+
50+
# When three new files are present, AssertionError
51+
new_file1 = self.FileAttr(dt.timestamp(dt.now()-timedelta(minutes=1)),"foo1")
52+
new_file2 = self.FileAttr(dt.timestamp(dt.now()-timedelta(minutes=1)),"foo2")
53+
new_file3 = self.FileAttr(dt.timestamp(dt.now()-timedelta(minutes=1)),"foo3")
54+
three_new = self.MockSFTP([new_file1, new_file2, new_file3])
55+
with pytest.raises(AssertionError):
56+
get_files_from_dir(three_new,"")
57+
58+
# When the file already exists, no files are downloaded
59+
mock_path.exists.return_value = True
60+
one_exists = self.MockSFTP([new_file1])
61+
get_files_from_dir(one_new, "")
62+
assert one_exists.num_gets == 0
63+

0 commit comments

Comments
 (0)