askrobots / dbbasic-tsv
Public
A database in TSV text files: data you can read, fast inserts, and an optional Rust speed-up.
test_edge_cases.py · 8.8 KB · 240 lines
| 1 | #!/usr/bin/env python3 |
| 2 | """ |
| 3 | Edge case tests for dbbasic-tsv |
| 4 | """ |
| 5 | |
| 6 | import unittest |
| 7 | import tempfile |
| 8 | import shutil |
| 9 | from pathlib import Path |
| 10 | from dbbasic import TSV |
| 11 | |
| 12 | |
| 13 | class TestEdgeCases(unittest.TestCase): |
| 14 | """Test edge cases and boundary conditions""" |
| 15 | |
| 16 | def setUp(self): |
| 17 | """Create temporary directory and test table""" |
| 18 | self.test_dir = Path(tempfile.mkdtemp(prefix="dbbasic_test_")) |
| 19 | self.db = TSV("test_edge", ["id", "data", "value"], data_dir=self.test_dir) |
| 20 | |
| 21 | def tearDown(self): |
| 22 | """Clean up test directory""" |
| 23 | shutil.rmtree(self.test_dir, ignore_errors=True) |
| 24 | |
| 25 | def test_empty_strings(self): |
| 26 | """Test handling of empty string values""" |
| 27 | self.db.insert({"id": "1", "data": "", "value": ""}) |
| 28 | result = self.db.query_one(id="1") |
| 29 | self.assertIsNotNone(result) |
| 30 | self.assertEqual(result["data"], "") |
| 31 | self.assertEqual(result["value"], "") |
| 32 | |
| 33 | def test_special_characters(self): |
| 34 | """Test handling of special characters""" |
| 35 | special_data = "Hello\nWorld\tTest\"Quote'Single" |
| 36 | self.db.insert({"id": "1", "data": special_data, "value": "test"}) |
| 37 | result = self.db.query_one(id="1") |
| 38 | self.assertIsNotNone(result) |
| 39 | # Data should be preserved (though newlines/tabs may be escaped) |
| 40 | self.assertIn("Hello", result["data"]) |
| 41 | |
| 42 | def test_unicode_characters(self): |
| 43 | """Test handling of Unicode/emoji characters""" |
| 44 | unicode_data = "Hello 世界 🎉 café" |
| 45 | self.db.insert({"id": "1", "data": unicode_data, "value": "test"}) |
| 46 | result = self.db.query_one(id="1") |
| 47 | self.assertIsNotNone(result) |
| 48 | self.assertEqual(result["data"], unicode_data) |
| 49 | |
| 50 | def test_very_long_strings(self): |
| 51 | """Test handling of very long strings""" |
| 52 | long_data = "x" * 10000 |
| 53 | self.db.insert({"id": "1", "data": long_data, "value": "test"}) |
| 54 | result = self.db.query_one(id="1") |
| 55 | self.assertIsNotNone(result) |
| 56 | self.assertEqual(len(result["data"]), 10000) |
| 57 | |
| 58 | def test_missing_columns(self): |
| 59 | """Test inserting record with missing columns""" |
| 60 | self.db.insert({"id": "1", "data": "test"}) # missing 'value' |
| 61 | result = self.db.query_one(id="1") |
| 62 | self.assertIsNotNone(result) |
| 63 | self.assertEqual(result["data"], "test") |
| 64 | self.assertEqual(result.get("value", ""), "") |
| 65 | |
| 66 | def test_extra_columns(self): |
| 67 | """Test inserting record with extra columns""" |
| 68 | self.db.insert({"id": "1", "data": "test", "value": "val", "extra": "ignored"}) |
| 69 | result = self.db.query_one(id="1") |
| 70 | self.assertIsNotNone(result) |
| 71 | # Extra column should be ignored |
| 72 | self.assertNotIn("extra", result) |
| 73 | |
| 74 | def test_duplicate_ids(self): |
| 75 | """Test handling of duplicate IDs""" |
| 76 | self.db.insert({"id": "1", "data": "first", "value": "100"}) |
| 77 | self.db.insert({"id": "1", "data": "second", "value": "200"}) |
| 78 | |
| 79 | results = self.db.query(id="1") |
| 80 | # Both records should exist (TSV doesn't enforce uniqueness by default) |
| 81 | self.assertEqual(len(results), 2) |
| 82 | |
| 83 | def test_query_nonexistent_field(self): |
| 84 | """Test querying with non-existent field""" |
| 85 | self.db.insert({"id": "1", "data": "test", "value": "100"}) |
| 86 | results = self.db.query(nonexistent="value") |
| 87 | # Should return empty results, not error |
| 88 | self.assertEqual(len(results), 0) |
| 89 | |
| 90 | def test_update_nonexistent_record(self): |
| 91 | """Test updating non-existent record""" |
| 92 | updated = self.db.update({"id": "999"}, {"data": "new"}) |
| 93 | self.assertEqual(updated, 0) |
| 94 | |
| 95 | def test_delete_nonexistent_record(self): |
| 96 | """Test deleting non-existent record""" |
| 97 | deleted = self.db.delete(id="999") |
| 98 | self.assertEqual(deleted, 0) |
| 99 | |
| 100 | def test_empty_database_operations(self): |
| 101 | """Test operations on empty database""" |
| 102 | self.assertEqual(self.db.count(), 0) |
| 103 | self.assertEqual(len(self.db.query(id="1")), 0) |
| 104 | self.assertIsNone(self.db.query_one(id="1")) |
| 105 | self.assertEqual(list(self.db.all()), []) |
| 106 | |
| 107 | def test_large_batch_insert(self): |
| 108 | """Test inserting large batch of records""" |
| 109 | records = [ |
| 110 | {"id": str(i), "data": f"data_{i}", "value": str(i * 10)} |
| 111 | for i in range(1000) |
| 112 | ] |
| 113 | count = self.db.insert_many(records) |
| 114 | self.assertEqual(count, 1000) |
| 115 | self.assertEqual(self.db.count(), 1000) |
| 116 | |
| 117 | def test_multiple_queries_same_condition(self): |
| 118 | """Test multiple queries with same condition""" |
| 119 | for i in range(10): |
| 120 | self.db.insert({"id": str(i), "data": "active", "value": str(i)}) |
| 121 | |
| 122 | results = self.db.query(data="active") |
| 123 | self.assertEqual(len(results), 10) |
| 124 | |
| 125 | def test_update_multiple_records(self): |
| 126 | """Test updating multiple records at once""" |
| 127 | for i in range(5): |
| 128 | self.db.insert({"id": str(i), "data": "old", "value": str(i)}) |
| 129 | |
| 130 | updated = self.db.update({"data": "old"}, {"data": "new"}) |
| 131 | self.assertEqual(updated, 5) |
| 132 | |
| 133 | results = self.db.query(data="new") |
| 134 | self.assertEqual(len(results), 5) |
| 135 | |
| 136 | def test_delete_multiple_records(self): |
| 137 | """Test deleting multiple records at once""" |
| 138 | for i in range(5): |
| 139 | self.db.insert({"id": str(i), "data": "delete_me", "value": str(i)}) |
| 140 | |
| 141 | deleted = self.db.delete(data="delete_me") |
| 142 | self.assertEqual(deleted, 5) |
| 143 | self.assertEqual(self.db.count(), 0) |
| 144 | |
| 145 | |
| 146 | class TestDataIntegrity(unittest.TestCase): |
| 147 | """Test data integrity and persistence""" |
| 148 | |
| 149 | def setUp(self): |
| 150 | self.test_dir = Path(tempfile.mkdtemp(prefix="dbbasic_test_")) |
| 151 | |
| 152 | def tearDown(self): |
| 153 | shutil.rmtree(self.test_dir, ignore_errors=True) |
| 154 | |
| 155 | def test_persistence_across_instances(self): |
| 156 | """Test that data persists when reopening database""" |
| 157 | # Create database and insert data |
| 158 | db1 = TSV("persist_test", ["id", "name"], data_dir=self.test_dir) |
| 159 | db1.insert({"id": "1", "name": "Alice"}) |
| 160 | db1.insert({"id": "2", "name": "Bob"}) |
| 161 | del db1 |
| 162 | |
| 163 | # Reopen database in same directory |
| 164 | db2 = TSV("persist_test", ["id", "name"], data_dir=self.test_dir) |
| 165 | self.assertEqual(db2.count(), 2) |
| 166 | result = db2.query_one(id="1") |
| 167 | self.assertEqual(result["name"], "Alice") |
| 168 | |
| 169 | def test_index_rebuild_on_load(self): |
| 170 | """Test that indexes are rebuilt when loading existing data""" |
| 171 | # Create database with data |
| 172 | db1 = TSV("index_test", ["id", "email"], data_dir=self.test_dir) |
| 173 | for i in range(100): |
| 174 | db1.insert({"id": str(i), "email": f"user{i}@example.com"}) |
| 175 | del db1 |
| 176 | |
| 177 | # Reopen and verify index works |
| 178 | db2 = TSV("index_test", ["id", "email"], data_dir=self.test_dir) |
| 179 | result = db2.query_one(id="50") |
| 180 | self.assertIsNotNone(result) |
| 181 | self.assertEqual(result["email"], "user50@example.com") |
| 182 | |
| 183 | def test_data_file_format(self): |
| 184 | """Test that data file is valid TSV format""" |
| 185 | db = TSV("format_test", ["id", "name", "email"], data_dir=self.test_dir) |
| 186 | db.insert({"id": "1", "name": "Alice", "email": "alice@example.com"}) |
| 187 | db.insert({"id": "2", "name": "Bob", "email": "bob@example.com"}) |
| 188 | |
| 189 | # Read file directly and verify TSV format |
| 190 | data_file = self.test_dir / "format_test.tsv" |
| 191 | with open(data_file) as f: |
| 192 | lines = f.readlines() |
| 193 | |
| 194 | # First line should be header |
| 195 | self.assertEqual(lines[0].strip(), "id\tname\temail") |
| 196 | |
| 197 | # Data lines should have tab separators |
| 198 | self.assertIn("\t", lines[1]) |
| 199 | self.assertIn("\t", lines[2]) |
| 200 | |
| 201 | |
| 202 | class TestErrorHandling(unittest.TestCase): |
| 203 | """Test error handling""" |
| 204 | |
| 205 | def setUp(self): |
| 206 | self.test_dir = Path(tempfile.mkdtemp(prefix="dbbasic_test_")) |
| 207 | |
| 208 | def tearDown(self): |
| 209 | shutil.rmtree(self.test_dir, ignore_errors=True) |
| 210 | |
| 211 | def test_create_index_invalid_column(self): |
| 212 | """Test creating index on non-existent column raises error""" |
| 213 | db = TSV("error_test", ["id", "name"], data_dir=self.test_dir) |
| 214 | with self.assertRaises(ValueError): |
| 215 | db.create_index("nonexistent_column") |
| 216 | |
| 217 | def test_transaction_preserves_data_on_error(self): |
| 218 | """Test that transaction errors don't corrupt data""" |
| 219 | db = TSV("tx_error_test", ["id", "value"], data_dir=self.test_dir) |
| 220 | db.insert({"id": "1", "value": "100"}) |
| 221 | db.insert({"id": "2", "value": "200"}) |
| 222 | |
| 223 | original_count = db.count() |
| 224 | |
| 225 | try: |
| 226 | with db.transaction() as tx: |
| 227 | tx.update({"id": "1"}, {"value": "999"}) |
| 228 | tx.delete(id="2") |
| 229 | raise Exception("Test error") |
| 230 | except: |
| 231 | pass |
| 232 | |
| 233 | # Database should still have original records |
| 234 | self.assertEqual(db.count(), original_count) |
| 235 | self.assertEqual(db.query_one(id="1")["value"], "100") |
| 236 | self.assertIsNotNone(db.query_one(id="2")) |
| 237 | |
| 238 | |
| 239 | if __name__ == "__main__": |
| 240 | unittest.main() |