|
| 1 | +"""Test the read-only properties in the SqliterDB class.""" |
| 2 | + |
| 3 | +import tempfile |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from sqliter.exceptions import DatabaseConnectionError |
| 8 | +from sqliter.model.model import BaseDBModel |
| 9 | +from sqliter.sqliter import SqliterDB |
| 10 | + |
| 11 | + |
| 12 | +class TestSqliterDBProperties: |
| 13 | + """Test suite for the read-only properties in the SqliterDB class.""" |
| 14 | + |
| 15 | + def test_filename_property_memory_db(self) -> None: |
| 16 | + """Test the 'filename' property for an in-memory database.""" |
| 17 | + db = SqliterDB(memory=True) |
| 18 | + assert db.filename is None, "Expected None for in-memory database" |
| 19 | + |
| 20 | + def test_filename_property_file_db(self) -> None: |
| 21 | + """Test the 'filename' property for a file-based database.""" |
| 22 | + db = SqliterDB(db_filename="test.db") |
| 23 | + assert db.filename == "test.db", "Expected 'test.db' as filename" |
| 24 | + |
| 25 | + def test_is_memory_property_true(self) -> None: |
| 26 | + """Test the 'is_memory' property returns True for in-memory database.""" |
| 27 | + db = SqliterDB(memory=True) |
| 28 | + assert db.is_memory is True, "Expected True for in-memory database" |
| 29 | + |
| 30 | + def test_is_memory_property_false(self) -> None: |
| 31 | + """Test 'is_memory' property returns False for file-based database.""" |
| 32 | + db = SqliterDB(db_filename="test.db") |
| 33 | + assert db.is_memory is False, "Expected False for file-based database" |
| 34 | + |
| 35 | + def test_is_autocommit_property_true(self) -> None: |
| 36 | + """Test 'is_autocommit' prop returns True when auto-commit enabled.""" |
| 37 | + db = SqliterDB(memory=True, auto_commit=True) |
| 38 | + assert db.is_autocommit is True, "Expected True for auto-commit enabled" |
| 39 | + |
| 40 | + def test_is_autocommit_property_false(self) -> None: |
| 41 | + """Test 'is_autocommit' prop returns False when auto-commit disabled.""" |
| 42 | + db = SqliterDB(memory=True, auto_commit=False) |
| 43 | + assert ( |
| 44 | + db.is_autocommit is False |
| 45 | + ), "Expected False for auto-commit disabled" |
| 46 | + |
| 47 | + def test_is_connected_property_when_connected(self) -> None: |
| 48 | + """Test the 'is_connected' property when the database is connected.""" |
| 49 | + db = SqliterDB(memory=True) |
| 50 | + with db.connect(): |
| 51 | + assert db.is_connected is True, "Expected True when connected" |
| 52 | + |
| 53 | + def test_is_connected_property_when_disconnected(self) -> None: |
| 54 | + """Test 'is_connected' property when the database is disconnected.""" |
| 55 | + db = SqliterDB(memory=True) |
| 56 | + assert db.is_connected is False, "Expected False when not connected" |
| 57 | + |
| 58 | + def test_table_names_property(self) -> None: |
| 59 | + """Test the 'table_names' property returns correct tables.""" |
| 60 | + |
| 61 | + # Define a simple model for the test |
| 62 | + class TestTableModel(BaseDBModel): |
| 63 | + id: int |
| 64 | + |
| 65 | + class Meta: |
| 66 | + table_name = "test_table" |
| 67 | + |
| 68 | + # Create the database without using the context manager |
| 69 | + db = SqliterDB(memory=True) |
| 70 | + db.create_table(TestTableModel) # ORM-based table creation |
| 71 | + |
| 72 | + # Verify that the table exists while the connection is still open |
| 73 | + table_names = db.table_names |
| 74 | + assert ( |
| 75 | + "test_table" in table_names |
| 76 | + ), f"Expected 'test_table', got {table_names}" |
| 77 | + |
| 78 | + # Explicitly close the connection afterwards |
| 79 | + db.close() |
| 80 | + |
| 81 | + def test_table_names_property_when_disconnected(self) -> None: |
| 82 | + """Test the 'table_names' property with no active connection.""" |
| 83 | + |
| 84 | + # Define a simple model for the test |
| 85 | + class AnotherTableModel(BaseDBModel): |
| 86 | + id: int |
| 87 | + |
| 88 | + class Meta: |
| 89 | + table_name = "another_table" |
| 90 | + |
| 91 | + # Create the database without the context manager |
| 92 | + db = SqliterDB(memory=True) |
| 93 | + db.create_table(AnotherTableModel) # ORM-based table creation |
| 94 | + |
| 95 | + # Check the table names while the connection is still open |
| 96 | + table_names = db.table_names |
| 97 | + assert ( |
| 98 | + "another_table" in table_names |
| 99 | + ), f"Expected 'another_table', got {table_names}" |
| 100 | + |
| 101 | + # Close the connection explicitly after the check |
| 102 | + db.close() |
| 103 | + |
| 104 | + def test_table_names_property_no_connection_error(self) -> None: |
| 105 | + """Test the 'table_names' property reconnects after disconnection. |
| 106 | +
|
| 107 | + This test uses a real temp database file since sqlite3 seems to bypass |
| 108 | + the 'pyfakefs' filesystem. |
| 109 | + """ |
| 110 | + with tempfile.NamedTemporaryFile(suffix=".sqlite") as temp_db: |
| 111 | + db_filename = temp_db.name |
| 112 | + |
| 113 | + # Define a simple model for the test |
| 114 | + class TestTableModel(BaseDBModel): |
| 115 | + id: int |
| 116 | + |
| 117 | + class Meta: |
| 118 | + table_name = "test_table" |
| 119 | + |
| 120 | + # Create the database using the temporary file |
| 121 | + db = SqliterDB(db_filename=db_filename) |
| 122 | + db.create_table(TestTableModel) |
| 123 | + |
| 124 | + # Close the connection |
| 125 | + db.close() |
| 126 | + |
| 127 | + # Ensure that accessing table_names does NOT raise an error Since |
| 128 | + # it's file-based, the table should still exist after reconnecting |
| 129 | + table_names = db.table_names |
| 130 | + assert ( |
| 131 | + "test_table" in table_names |
| 132 | + ), f"Expected 'test_table', got {table_names}" |
| 133 | + |
| 134 | + def test_table_names_connection_failure(self, mocker) -> None: |
| 135 | + """Test 'table_names' raises exception if the connection fails.""" |
| 136 | + # Create an instance of the database |
| 137 | + db = SqliterDB(memory=True) |
| 138 | + |
| 139 | + # Mock the connect method to simulate a failed connection |
| 140 | + mocker.patch.object(db, "connect", return_value=None) |
| 141 | + |
| 142 | + # Close any existing connection to ensure db.conn is None |
| 143 | + db.close() |
| 144 | + |
| 145 | + # Attempt to access table_names and expect DatabaseConnectionError |
| 146 | + with pytest.raises(DatabaseConnectionError): |
| 147 | + _ = db.table_names |
0 commit comments