Skip to content

Commit a22a03e

Browse files
committed
Added tests for storage utility
1 parent b9135f4 commit a22a03e

File tree

2 files changed

+196
-1
lines changed

2 files changed

+196
-1
lines changed

tests/test_db_schema.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,4 @@ def test_get_views(self):
6969
def test_table_index(self):
7070
schema = Schema(db)
7171
table = schema.get_table('t1')
72-
assert ['t1_pkey'] == table.get_indexes()
72+
assert ['t1_pkey'] == table.get_indexes()

tests/test_storage.py

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
import pytest
2+
import shutil
3+
import pandas as pd
4+
import requests
5+
from pathlib import Path
6+
from toolkit import StoragePath
7+
from toolkit.storage import _get_aws_settings
8+
9+
aws_test_settings = _get_aws_settings(
10+
"TEST_AWS_ACCESS_KEY_ID",
11+
"TEST_AWS_SECRET_ACCESS_KEY",
12+
"TEST_AWS_ENDPOINT_URL",
13+
"TEST_AWS_DEFAULT_REGION"
14+
)
15+
16+
@pytest.fixture
17+
def storage_root():
18+
root = StoragePath("test", "storage", aws_settings=aws_test_settings)
19+
root.rmdir() # Make sure that storage root is clean if exists
20+
return root
21+
22+
class TestStoragePath:
23+
def test_file_exists(self, storage_root):
24+
test_file = storage_root / "test_file.txt"
25+
assert test_file.exists() == False
26+
test_file.write_text("Hello, World!")
27+
assert test_file.exists() == True
28+
29+
def test_remove_file(self, storage_root):
30+
test_file = storage_root / "test_file.txt"
31+
test_file.write_text("Hello, World!")
32+
assert test_file.exists() == True
33+
test_file.unlink()
34+
assert test_file.exists() == False
35+
36+
def test_remove_directory(self, storage_root):
37+
dir_path = storage_root / "dir"
38+
test_file1 = storage_root / "dir" / "test_file1.txt"
39+
test_file2 = storage_root / "dir" / "test_file2.txt"
40+
test_file1.write_text("Hello, World!")
41+
test_file2.write_text("Hello, World!")
42+
43+
assert dir_path.dir_exists() == True
44+
dir_path.rmdir()
45+
assert dir_path.dir_exists() == False
46+
47+
def test_download(self, storage_root):
48+
file_content = "Hello, World!"
49+
download_loc = "/tmp/test_file.txt"
50+
51+
storage_path = storage_root / "test_file.txt"
52+
storage_path.write_text(file_content)
53+
54+
storage_path.download(download_loc)
55+
assert open(download_loc).read() == file_content
56+
57+
def test_upload(self, storage_root):
58+
file_content = "Hello, World!"
59+
local_path = "/tmp/upload.txt"
60+
open(local_path, "w").write(file_content)
61+
62+
storage_path = storage_root / "upload.txt"
63+
storage_path.upload(local_path)
64+
assert storage_path.read_text() == file_content
65+
66+
def test_read(self, storage_root):
67+
file_content = "Hello, World!"
68+
storage_path = storage_root / "test_file.txt"
69+
storage_path.write_text(file_content)
70+
71+
assert storage_path.read_text() == file_content
72+
assert storage_path.read_bytes() == bytes(file_content, "utf-8")
73+
74+
def test_read_write_csv(self, storage_root):
75+
df = pd.DataFrame({"a": [10], "b": [20]})
76+
storage_path = storage_root / "test_file.csv"
77+
storage_path.write_csv(df)
78+
storage_df = storage_path.read_csv()
79+
assert storage_df.equals(df)
80+
81+
def test_read_write_parquet(self, storage_root):
82+
df = pd.DataFrame({"a": [10], "b": [20]})
83+
storage_path = storage_root / "test_file.parq"
84+
storage_path.write_parquet(df)
85+
storage_df = storage_path.read_parquet()
86+
assert storage_df.equals(df)
87+
88+
def test_iterdir(self, storage_root):
89+
file_content = "Hello, World!"
90+
storage_path = storage_root / "test_file.txt"
91+
storage_path.write_text(file_content)
92+
93+
storage_files = [each.path for each in storage_root.iterdir()]
94+
assert storage_files == [storage_path.path]
95+
96+
def test_sync_to(self, storage_root):
97+
"""test sync to localpath.
98+
"""
99+
file_content = "Hello, World!"
100+
local_root = "/tmp/root"
101+
storage_path = storage_root / "test_file.txt"
102+
storage_path.write_text(file_content)
103+
104+
storage_root.sync_to(local_root)
105+
assert [each.name for each in Path(local_root).glob("*")] == ["test_file.txt"]
106+
107+
def test_sync_from(self, storage_root):
108+
"""Test sync from localpath.
109+
"""
110+
file_content = "Hello, World!"
111+
local_root = "/tmp/root"
112+
113+
root = Path(local_root)
114+
if root.exists():
115+
shutil.rmtree(local_root)
116+
root.mkdir()
117+
open(root / "test_file.txt", "w").write(file_content)
118+
119+
storage_root.sync_from(local_root)
120+
storage_files = [each.name for each in storage_root.iterdir()]
121+
assert storage_files == ["test_file.txt"]
122+
123+
def test_copy(self, storage_root):
124+
"""Copy from one storage path to other.
125+
"""
126+
file_content = "Hello, World!"
127+
source_path = storage_root / "orig.txt"
128+
dest_path = storage_root / "copy.txt"
129+
source_path.write_text(file_content)
130+
131+
dest_path.copy(source_path)
132+
assert dest_path.read_text() == file_content
133+
134+
def test_copy_dir(self, storage_root):
135+
"""Copy from one storage path to other.
136+
"""
137+
file_content = "Hello, World!"
138+
source_dir = storage_root / "orig"
139+
source_path = source_dir /"orig.txt"
140+
141+
dest_dir = storage_root / "copy"
142+
dest_path = dest_dir / "orig.txt"
143+
source_path.write_text(file_content)
144+
145+
assert dest_path.exists() == False
146+
dest_dir.copy_dir(source_dir)
147+
assert dest_path.exists() == True
148+
assert dest_path.read_text() == file_content
149+
150+
def test_move(self, storage_root):
151+
"""move from one storage path to other.
152+
"""
153+
file_content = "Hello, World!"
154+
source_path = storage_root / "orig.txt"
155+
dest_path = storage_root / "copy.txt"
156+
source_path.write_text(file_content)
157+
158+
dest_path.move(source_path)
159+
assert dest_path.read_text() == file_content
160+
assert source_path.exists() == False
161+
162+
def test_move_dir(self, storage_root):
163+
"""move from one storage dir to other.
164+
"""
165+
file_content = "Hello, World!"
166+
source_dir = storage_root / "orig"
167+
source_path = source_dir /"orig.txt"
168+
169+
dest_dir = storage_root / "copy"
170+
dest_path = dest_dir / "orig.txt"
171+
source_path.write_text(file_content)
172+
173+
assert dest_path.exists() == False
174+
dest_dir.move_dir(source_dir)
175+
assert dest_path.exists() == True
176+
assert dest_path.read_text() == file_content
177+
assert source_dir.dir_exists() == False
178+
179+
def test_presigned_url_for_download(self, storage_root):
180+
file_content = "Hello, World!"
181+
source_file = storage_root / "test_file.txt"
182+
source_file.write_text(file_content)
183+
184+
url = source_file.get_presigned_url_for_download()
185+
assert requests.get(url).text == file_content
186+
187+
def test_presigned_url_for_upload(self, storage_root):
188+
file_content = "Hello, World!"
189+
dest_file = storage_root / "test_file.txt"
190+
assert dest_file.exists() == False
191+
192+
url = dest_file.get_presigned_url_for_upload(content_type="text/html")
193+
requests.put(url, data=file_content, headers={"Content-type": "text/html"})
194+
assert dest_file.exists() == True
195+
assert dest_file.read_text() == file_content

0 commit comments

Comments
 (0)