|
| 1 | +import unittest |
| 2 | +import json |
| 3 | +import tempfile |
| 4 | +import os |
| 5 | +import sys |
| 6 | +from dotenv import load_dotenv |
| 7 | + |
| 8 | +load_dotenv() |
| 9 | +sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/..") |
| 10 | + |
| 11 | +from src.file_store import read, write |
| 12 | + |
| 13 | +class TestExample(unittest.TestCase): |
| 14 | + |
| 15 | + def setUp(self): |
| 16 | + self.test_user_id = 'test_user_id' |
| 17 | + self.test_data = {'key': 'value'} |
| 18 | + |
| 19 | + def test_write(self): |
| 20 | + # Use temporary file for testing |
| 21 | + with tempfile.NamedTemporaryFile() as f: |
| 22 | + |
| 23 | + # Write to file |
| 24 | + write_status, write_code = write(self.test_data, self.test_user_id) |
| 25 | + |
| 26 | + # Test status and code |
| 27 | + self.assertEqual(write_status, 'ok') |
| 28 | + self.assertEqual(write_code, 200) |
| 29 | + |
| 30 | + # Read from file |
| 31 | + read_data, read_code = read(self.test_user_id) |
| 32 | + |
| 33 | + # Test read data and code |
| 34 | + self.assertEqual(read_data, self.test_data) |
| 35 | + self.assertEqual(read_code, 200) |
| 36 | + |
| 37 | + def test_read_not_found(self): |
| 38 | + # Use temporary file for testing |
| 39 | + with tempfile.TemporaryDirectory() as tempdir: |
| 40 | + os.environ['FILE_STORE_PATH'] = tempdir |
| 41 | + |
| 42 | + # Read from unexisting file |
| 43 | + read_data, read_code = read(self.test_user_id + '0000') |
| 44 | + |
| 45 | + # Test not found data and code |
| 46 | + self.assertEqual(read_data, 'File not found') |
| 47 | + self.assertEqual(read_code, 404) |
| 48 | + |
| 49 | + def test_read(self): |
| 50 | + # Use temporary file for testing |
| 51 | + with tempfile.TemporaryDirectory() as tempdir: |
| 52 | + # Read from unexisting file |
| 53 | + read_data, read_code = read(self.test_user_id) |
| 54 | + |
| 55 | + # Test not found data and code |
| 56 | + self.assertEqual(read_data, self.test_data) |
| 57 | + self.assertEqual(read_code, 200) |
| 58 | + |
| 59 | +if __name__ == '__main__': |
| 60 | + unittest.main() |
0 commit comments