""" Test cases for deltalakeopsserver/logstats/objstore.py """ import os import sys import tempfile import shutil import unittest import gzip from pathlib import Path # Add project root to path sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../..'))) from deltalakeopsserver.logstats.objstore import Local, ObjectStore class TestObjectStore(unittest.TestCase): """Test base ObjectStore class""" def test_objectstore_is_abstract(self): """Test that ObjectStore is an abstract base class""" with self.assertRaises(NotImplementedError): store = ObjectStore() store.list("bucket", "prefix") with self.assertRaises(NotImplementedError): store = ObjectStore() store.object("bucket", "key") with self.assertRaises(NotImplementedError): store = ObjectStore() store.upload("bucket", "key", "filepath") with self.assertRaises(NotImplementedError): store = ObjectStore() store.download("bucket", "key", "filepath") with self.assertRaises(NotImplementedError): store = ObjectStore() store.existed("bucket", "key") class TestLocal(unittest.TestCase): """Test Local file system implementation""" def setUp(self): """Set up test fixtures""" self.temp_dir = tempfile.mkdtemp() self.store = Local(self.temp_dir) def tearDown(self): """Clean up test fixtures""" if os.path.exists(self.temp_dir): shutil.rmtree(self.temp_dir) def test_list_empty_directory(self): """Test listing files in empty directory""" keys = self.store.list("bucket", "prefix") self.assertEqual(keys, []) def test_list_files(self): """Test listing files with prefix""" # Create test files test_dir = os.path.join(self.temp_dir, "test") os.makedirs(test_dir, exist_ok=True) # Create files with open(os.path.join(test_dir, "file1.txt"), "w") as f: f.write("content1") with open(os.path.join(test_dir, "file2.txt"), "w") as f: f.write("content2") with open(os.path.join(test_dir, "other.txt"), "w") as f: f.write("content3") # List with prefix keys = self.store.list("bucket", "test/file") self.assertEqual(len(keys), 2) self.assertIn("test/file1.txt", keys) self.assertIn("test/file2.txt", keys) self.assertNotIn("test/other.txt", keys) def test_list_all_files(self): """Test listing all files with empty prefix""" # Create test files test_dir = os.path.join(self.temp_dir, "test") os.makedirs(test_dir, exist_ok=True) with open(os.path.join(test_dir, "file1.txt"), "w") as f: f.write("content1") with open(os.path.join(test_dir, "file2.txt"), "w") as f: f.write("content2") keys = self.store.list("bucket", "test/") self.assertEqual(len(keys), 2) def test_lists_multiple_prefixes(self): """Test listing files with multiple prefixes""" # Create test directories dir1 = os.path.join(self.temp_dir, "dir1") dir2 = os.path.join(self.temp_dir, "dir2") os.makedirs(dir1, exist_ok=True) os.makedirs(dir2, exist_ok=True) with open(os.path.join(dir1, "file1.txt"), "w") as f: f.write("content1") with open(os.path.join(dir2, "file2.txt"), "w") as f: f.write("content2") keys = self.store.lists("bucket", ["dir1/", "dir2/"]) self.assertEqual(len(keys), 2) def test_object_regular_file(self): """Test reading a regular file""" test_file = os.path.join(self.temp_dir, "test.txt") with open(test_file, "w") as f: f.write("test content") obj = self.store.object("bucket", "test.txt") self.assertIsNotNone(obj) content = obj.read() self.assertEqual(content, b"test content") obj.close() def test_object_gzip_file(self): """Test reading a gzip file""" test_file = os.path.join(self.temp_dir, "test.txt.gz") with gzip.open(test_file, "wb") as f: f.write(b"compressed content") obj = self.store.object("bucket", "test.txt.gz") self.assertIsNotNone(obj) content = obj.read() self.assertEqual(content, b"compressed content") obj.close() def test_object_nonexistent_file(self): """Test reading a non-existent file""" obj = self.store.object("bucket", "nonexistent.txt") self.assertIsNone(obj) def test_upload_regular_file(self): """Test uploading a regular file (should be gzipped)""" source_file = os.path.join(self.temp_dir, "source.txt") with open(source_file, "w") as f: f.write("test content") self.store.upload("bucket", "dest.txt", source_file) # Check that file was uploaded and gzipped dest_file = os.path.join(self.temp_dir, "dest.txt") self.assertTrue(os.path.exists(dest_file)) with gzip.open(dest_file, "rb") as f: content = f.read() self.assertEqual(content, b"test content") def test_upload_gzip_file(self): """Test uploading a gzip file (should remain gzipped)""" source_file = os.path.join(self.temp_dir, "source.txt.gz") with gzip.open(source_file, "wb") as f: f.write(b"compressed content") self.store.upload("bucket", "dest.txt.gz", source_file) dest_file = os.path.join(self.temp_dir, "dest.txt.gz") self.assertTrue(os.path.exists(dest_file)) with gzip.open(dest_file, "rb") as f: content = f.read() self.assertEqual(content, b"compressed content") def test_download_regular_file(self): """Test downloading a regular file""" source_file = os.path.join(self.temp_dir, "source.txt") with open(source_file, "w") as f: f.write("test content") dest_file = os.path.join(self.temp_dir, "dest.txt") result = self.store.download("bucket", "source.txt", dest_file) self.assertTrue(result) self.assertTrue(os.path.exists(dest_file)) with open(dest_file, "r") as f: content = f.read() self.assertEqual(content, "test content") def test_download_gzip_file(self): """Test downloading a gzip file (should be decompressed)""" source_file = os.path.join(self.temp_dir, "source.txt.gz") with gzip.open(source_file, "wb") as f: f.write(b"compressed content") dest_file = os.path.join(self.temp_dir, "dest.txt") result = self.store.download("bucket", "source.txt.gz", dest_file) self.assertTrue(result) self.assertTrue(os.path.exists(dest_file)) with open(dest_file, "r") as f: content = f.read() self.assertEqual(content, "compressed content") def test_download_nonexistent_file(self): """Test downloading a non-existent file""" dest_file = os.path.join(self.temp_dir, "dest.txt") result = self.store.download("bucket", "nonexistent.txt", dest_file) self.assertFalse(result) def test_existed_file_exists(self): """Test checking if an existing file exists""" test_file = os.path.join(self.temp_dir, "test.txt") with open(test_file, "w") as f: f.write("content") self.assertTrue(self.store.existed("bucket", "test.txt")) def test_existed_file_not_exists(self): """Test checking if a non-existent file exists""" self.assertFalse(self.store.existed("bucket", "nonexistent.txt")) def test_objects_multiple_files(self): """Test reading multiple files""" # Create test files with open(os.path.join(self.temp_dir, "file1.txt"), "w") as f: f.write("content1") with open(os.path.join(self.temp_dir, "file2.txt"), "w") as f: f.write("content2") objs = self.store.objects("bucket", ["file1.txt", "file2.txt"]) self.assertEqual(len(objs), 2) self.assertEqual(objs[0].read(), b"content1") self.assertEqual(objs[1].read(), b"content2") objs[0].close() objs[1].close() if __name__ == '__main__': unittest.main()