Skip to content

Commit e5b2171

Browse files
committed
test: refactored tests to inherit from python unittest module
1 parent 4a94136 commit e5b2171

File tree

8 files changed

+72
-34
lines changed

8 files changed

+72
-34
lines changed

lighthouse/axios.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,11 @@ def post_files(
4949
files = utils.read_files_for_upload(file)
5050
r = req.post(self.url, headers=headers, files=files)
5151
r.raise_for_status()
52+
utils.close_files_after_upload(files)
5253
try:
5354
return r.json()
5455
except Exception:
5556
return r.text
5657
except Exception as e:
58+
utils.close_files_after_upload(files)
5759
raise e

lighthouse/utils.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ def extract_file_name_with_source(file: str, source: str) -> str:
2828
return base + file.split(base)[-1]
2929

3030

31-
def read_files_for_upload(files: t.FileDict) -> List[Tuple[BufferedReader]]:
31+
def read_files_for_upload(
32+
files: t.FileDict,
33+
) -> List[Tuple[str, Tuple[str, BufferedReader, str]]]:
3234
file_list = []
3335
for file in files["files"]:
3436
if files["is_dir"]:
@@ -54,3 +56,10 @@ def read_files_for_upload(files: t.FileDict) -> List[Tuple[BufferedReader]]:
5456
),
5557
)
5658
return file_list
59+
60+
61+
def close_files_after_upload(
62+
files: List[Tuple[str, Tuple[str, BufferedReader, str]]]
63+
) -> None:
64+
for file in files:
65+
file[1][1].close()

readme.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,25 @@ Lighthouse is a permanent decentralized file storage protocol that allows the ab
77
```
88
pip install lighthouse.py
99
```
10+
11+
# Testing
12+
13+
The tests are written with inheritance from the unittest module. To run the tests, run the following command:
14+
15+
```
16+
pip install requirements.txt && python -m unittest discover
17+
```
18+
19+
or using nose2
20+
21+
```
22+
pip install requirements.txt && python -m nose2
23+
```
24+
25+
# Bundling
26+
27+
To bundle the library, run the following command:
28+
29+
```
30+
python setup.py
31+
```

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ charset-normalizer==2.1.1
44
click==8.1.3
55
idna==3.4
66
mypy-extensions==0.4.3
7+
nose2==0.12.0
78
pathspec==0.10.1
89
platformdirs==2.5.2
910
requests==2.28.1

setup.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env python3
2+
3+
4+
def pip_setup():
5+
6+
pass

tests/deploy.py

Lines changed: 0 additions & 21 deletions
This file was deleted.

tests/setup.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#!/usr/bin/env python3
22

33
import os
4-
from . import deploy as dt
54

65

76
def parse_env():
@@ -16,14 +15,3 @@ def parse_env():
1615
except FileNotFoundError:
1716
print("No .env file found")
1817
print("Defaulting to preset environment variables...")
19-
20-
21-
def run_test():
22-
"""setup test environment and run tests"""
23-
parse_env()
24-
dt.test_deploy_file()
25-
dt.test_deploy_dir()
26-
27-
28-
if __name__ == "__main__":
29-
run_test()

tests/test_deploy.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env python3
2+
3+
import os
4+
import unittest
5+
from main import Lighthouse
6+
from .setup import parse_env
7+
8+
9+
class TestDeploy(unittest.TestCase):
10+
def setUp(self) -> None:
11+
"""setup test environment"""
12+
parse_env()
13+
14+
def test_deploy_file(self):
15+
"""test deploy function"""
16+
l = Lighthouse(os.environ.get("LH_TOKEN", ""))
17+
res = l.deploy("tests/testdir/testfile.txt")
18+
self.assertNotEqual(res.get("data"), None, "data is None")
19+
self.assertNotEqual(res.get("data").get("Hash"), None, "data is None")
20+
21+
def test_deploy_dir(self):
22+
"""test deploy function"""
23+
l = Lighthouse(os.environ["LH_TOKEN"])
24+
res = l.deploy("tests/testdir")
25+
self.assertNotEqual(res.get("data"), None, "data is None")
26+
self.assertIsInstance(res.get("data"), str, "data is a string")
27+
self.assertIn("Hash", res.get("data"), "Hash is in data")
28+
29+
30+
if __name__ == "__main__":
31+
unittest.main()

0 commit comments

Comments
 (0)