|
1 |
| -"""Test script for the dbm.open function based on testdumbdbm.py""" |
2 |
| - |
3 | 1 | import unittest
|
4 | 2 | import dbm
|
5 | 3 | import os
|
| 4 | +import contextlib |
| 5 | +import io |
| 6 | +import sys |
| 7 | + |
6 | 8 | from test.support import import_helper
|
7 | 9 | from test.support import os_helper
|
| 10 | +from test.support.script_helper import assert_python_ok, assert_python_failure |
8 | 11 |
|
| 12 | +from dbm.__main__ import main as dbm_main |
9 | 13 |
|
10 | 14 | try:
|
11 | 15 | from dbm import sqlite3 as dbm_sqlite3
|
@@ -309,6 +313,99 @@ def setUp(self):
|
309 | 313 | self.dbm = import_helper.import_fresh_module('dbm')
|
310 | 314 |
|
311 | 315 |
|
| 316 | +class DBMCommandLineTestCase(unittest.TestCase): |
| 317 | + |
| 318 | + def setUp(self): |
| 319 | + self.addCleanup(cleaunup_test_dir) |
| 320 | + setup_test_dir() |
| 321 | + self.test_db = os.path.join(dirname, 'test.db') |
| 322 | + with dbm.open(self.test_db, 'c') as db: |
| 323 | + db[b'key1'] = b'value1' |
| 324 | + db[b'key2'] = b'value2' |
| 325 | + self.empty_db = os.path.join(dirname, 'empty.db') |
| 326 | + with dbm.open(self.empty_db, 'c'): |
| 327 | + pass |
| 328 | + self.dbm = import_helper.import_fresh_module('dbm') |
| 329 | + |
| 330 | + def run_cmd_ok(self, *args): |
| 331 | + return assert_python_ok('-m', 'dbm', *args).out |
| 332 | + |
| 333 | + def run_cmd_error(self, *args): |
| 334 | + return assert_python_failure('-m', 'dbm', *args) |
| 335 | + |
| 336 | + def test_help(self): |
| 337 | + output = self.run_cmd_ok('-h') |
| 338 | + self.assertIn(b'usage:', output) |
| 339 | + self.assertIn(b'python -m dbm', output) |
| 340 | + self.assertIn(b'--help', output) |
| 341 | + self.assertIn(b'whichdb', output) |
| 342 | + self.assertIn(b'dump', output) |
| 343 | + self.assertIn(b'reorganize', output) |
| 344 | + |
| 345 | + def test_whichdb_command(self): |
| 346 | + output = self.run_cmd_ok('--whichdb', self.test_db) |
| 347 | + self.assertIn(self.test_db.encode(), output) |
| 348 | + output = self.run_cmd_ok('--whichdb', self.test_db, self.empty_db) |
| 349 | + self.assertIn(self.test_db.encode(), output) |
| 350 | + self.assertIn(self.empty_db.encode(), output) |
| 351 | + |
| 352 | + def test_whichdb_nonexistent_file(self): |
| 353 | + rc, _, stderr = self.run_cmd_error('--whichdb', "nonexistent_db") |
| 354 | + self.assertEqual(rc, 1) |
| 355 | + self.assertIn(b'not found', stderr) |
| 356 | + |
| 357 | + def test_whichdb_unknown_format(self): |
| 358 | + text_file = os.path.join(dirname, 'text.txt') |
| 359 | + with open(text_file, 'w') as f: |
| 360 | + f.write('This is not a database file') |
| 361 | + output = self.run_cmd_ok('--whichdb', text_file) |
| 362 | + self.assertIn(b'UNKNOWN', output) |
| 363 | + self.assertIn(text_file.encode(), output) |
| 364 | + |
| 365 | + def test_whichdb_output_format(self): |
| 366 | + output = self.run_cmd_ok('--whichdb', self.test_db) |
| 367 | + output_str = output.decode('utf-8', errors='replace').strip() |
| 368 | + # Should be "TYPE - FILENAME" format |
| 369 | + self.assertIn(' - ', output_str) |
| 370 | + parts = output_str.split(' - ', 1) |
| 371 | + self.assertEqual(len(parts), 2) |
| 372 | + self.assertEqual(parts[1], self.test_db) |
| 373 | + |
| 374 | + def test_dump_command(self): |
| 375 | + output = self.run_cmd_ok('--dump', self.test_db) |
| 376 | + self.assertIn(b'key1: value1', output) |
| 377 | + self.assertIn(b'key2: value2', output) |
| 378 | + |
| 379 | + def test_dump_empty_database(self): |
| 380 | + output = self.run_cmd_ok('--dump', self.empty_db) |
| 381 | + self.assertEqual(output.strip(), b'') |
| 382 | + |
| 383 | + def test_dump_nonexistent_database(self): |
| 384 | + rc, _, stderr = self.run_cmd_error('--dump', "nonexistent_db") |
| 385 | + self.assertEqual(rc, 1) |
| 386 | + self.assertIn(b'not found', stderr) |
| 387 | + |
| 388 | + def test_reorganize_command(self): |
| 389 | + self.addCleanup(setattr, dbm, '_defaultmod', dbm._defaultmod) |
| 390 | + for module in dbm_iterator(): |
| 391 | + setup_test_dir() |
| 392 | + dbm._defaultmod = module |
| 393 | + with module.open(_fname, 'c') as f: |
| 394 | + f[b"1"] = b"1" |
| 395 | + if hasattr(module, 'reorganize'): |
| 396 | + with module.open(_fname, 'c') as db: |
| 397 | + output = self.run_cmd_ok('--reorganize', db) |
| 398 | + self.assertIn(b'Reorganized', output) |
| 399 | + |
| 400 | + def test_output_format_consistency(self): |
| 401 | + output = self.run_cmd_ok('--dump', self.test_db) |
| 402 | + lines = output.decode('utf-8', errors='replace').strip().split('\n') |
| 403 | + for line in lines: |
| 404 | + if line.strip(): # Skip empty lines |
| 405 | + self.assertIn(':', line) |
| 406 | + parts = line.split(':', 1) |
| 407 | + self.assertEqual(len(parts), 2) |
| 408 | + |
312 | 409 | for mod in dbm_iterator():
|
313 | 410 | assert mod.__name__.startswith('dbm.')
|
314 | 411 | suffix = mod.__name__[4:]
|
|
0 commit comments