|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import os |
| 3 | +import io |
| 4 | +import unittest |
| 5 | +from src.lighthouseweb3 import Lighthouse |
| 6 | +from src.lighthouseweb3.functions.utils import NamedBufferedReader |
| 7 | +from .setup import parse_env |
| 8 | +import string |
| 9 | +import secrets |
| 10 | + |
| 11 | + |
| 12 | +def generate_random_string(length: int) -> str: |
| 13 | + characters = string.ascii_letters + string.digits |
| 14 | + return ''.join(secrets.choice(characters) for _ in range(length)) |
| 15 | + |
| 16 | + |
| 17 | +class TestDownload(unittest.TestCase): |
| 18 | + def setUp(self) -> None: |
| 19 | + """setup test environment""" |
| 20 | + parse_env() |
| 21 | + |
| 22 | + def test_env(self): |
| 23 | + """test env var""" |
| 24 | + self.assertNotEqual( |
| 25 | + os.environ.get("LIGHTHOUSE_TOKEN"), None, "token is not None" |
| 26 | + ) |
| 27 | + |
| 28 | + def test_download_file(self): |
| 29 | + """test Upload function""" |
| 30 | + l = Lighthouse() # will use env var |
| 31 | + res, _ = l.download( |
| 32 | + "Qmd5MBBScDUV3Ly8qahXtZFqyRRfYSmUwEcxpYcV4hzKfW") |
| 33 | + self.assertIsInstance(res, bytes, "type doesn't match") |
| 34 | + self.assertEqual(res, b'tests/testdir/', "data doesn't match") |
| 35 | + |
| 36 | + # def test_download_blob(self): |
| 37 | + # """test Upload function""" |
| 38 | + # l = Lighthouse(os.environ.get("LIGHTHOUSE_TOKEN")) |
| 39 | + # res = l.uploadBlob( |
| 40 | + # io.BytesIO(b"tests/testdir/"), f"{generate_random_string(16)}.txt") |
| 41 | + # self.assertNotEqual(res.get("data"), None, "data is None") |
| 42 | + # self.assertIsInstance(res.get("data"), dict, "data is a dict") |
| 43 | + # self.assertNotEqual(res.get("data").get("Hash"), None, "data is None") |
| 44 | + |
| 45 | + def test_download_blob_file(self): |
| 46 | + """test download_blob function""" |
| 47 | + l = Lighthouse(os.environ.get("LIGHTHOUSE_TOKEN")) |
| 48 | + with open("./image.png", "wb") as file: |
| 49 | + res = l.downloadBlob( |
| 50 | + file, "QmPT11PFFQQD3mT6BdwfSHQGHRdF8ngmRmcvxtSBiddWEa", chunk_size=1024*100) |
| 51 | + self.assertEqual(file.tell(), 123939, "File Size dont match") |
| 52 | + self.assertEqual(res, True, "data doesn't match") |
| 53 | + |
| 54 | + |
| 55 | +if __name__ == "__main__": |
| 56 | + unittest.main() |
0 commit comments