test_caching.py (2741B)
1 from ratchets.caching import CachingDatabase, BlameRecord 2 from ratchets.abstracted_tests import find_project_root 3 import os 4 from datetime import datetime 5 6 CACHING_FILENAME = "tests/test_files/temp_ratchet_blame.db" 7 8 9 def test_create_new_db(): 10 """Ensure DB creation when one does not exist works as expected.""" 11 repo_root = find_project_root() 12 db_path = os.path.join(str(repo_root), CACHING_FILENAME) 13 try: 14 os.remove(db_path) 15 except FileNotFoundError: 16 pass 17 db = CachingDatabase(db_path) 18 19 20 def test_create_multi_connections(): 21 """Ensure multiple DB connections can be created concurrently.""" 22 for i in range(0, 10): 23 repo_root = find_project_root() 24 db_path = os.path.join(str(repo_root), CACHING_FILENAME) 25 os.remove(db_path) 26 db = CachingDatabase(db_path) 27 28 29 def test_record_updating(): 30 """Ensure records are updated correctly when line numbers and file names match.""" 31 32 repo_root = find_project_root() 33 db_path = os.path.join(str(repo_root), CACHING_FILENAME) 34 if os.path.exists(db_path): 35 os.remove(db_path) 36 db = CachingDatabase(db_path) 37 38 file_name = "example.py" 39 line_number = 42 40 41 # create record 42 record1 = BlameRecord( 43 line_content="print('Hello, world!')", 44 line_number=line_number, 45 timestamp=datetime(2020, 1, 1, 12, 0, 0), 46 file_name=file_name, 47 author="Author1", 48 ) 49 db.create_or_update_blame(record1) 50 51 # update with new author/timestamp/content 52 record2 = BlameRecord( 53 line_content="print('Updated!')", 54 line_number=line_number, 55 timestamp=datetime(2021, 1, 1, 12, 0, 0), 56 file_name=file_name, 57 author="Author2", 58 ) 59 db.create_or_update_blame(record2) 60 61 updated = db.get_blame(line_number, file_name) 62 63 assert updated is not None, "We inserted this record, it should not be 'None'" 64 65 assert updated.author == "Author2", "Single-record update failed" 66 assert updated.line_content == "print('Updated!')" 67 68 # test batch updating 69 record3 = BlameRecord( 70 line_content="print('Batch update!')", 71 line_number=line_number, 72 timestamp=datetime(2022, 1, 1, 12, 0, 0), 73 file_name=file_name, 74 author="Author3", 75 ) 76 db.create_or_update_blames([record3]) 77 78 updated = db.get_blame(line_number, file_name) 79 80 assert updated is not None, "We inserted this record, it should not be 'None'" 81 assert updated.author == "Author3", "Batch-record update failed" 82 assert updated.line_content == "print('Batch update!')" 83 84 print("test_record_updating passed") 85 86 87 if __name__ == "__main__": 88 test_create_new_db() 89 test_create_multi_connections() 90 test_record_updating()