Test Overview
This test verifies that data saved in a database remains accurate and unchanged after a save operation. It checks if the data retrieved matches the data input, ensuring data integrity.
This test verifies that data saved in a database remains accurate and unchanged after a save operation. It checks if the data retrieved matches the data input, ensuring data integrity.
import unittest class TestDataIntegrity(unittest.TestCase): def setUp(self): # Simulate a simple in-memory database as a dictionary self.database = {} def save_data(self, key, value): # Simulate saving data self.database[key] = value def get_data(self, key): # Simulate retrieving data return self.database.get(key, None) def test_data_integrity_after_save(self): input_data = {'id': 1, 'name': 'Alice', 'age': 30} self.save_data(input_data['id'], input_data) retrieved_data = self.get_data(input_data['id']) self.assertEqual(retrieved_data, input_data, "Data retrieved does not match data saved") if __name__ == '__main__': unittest.main()
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and setUp method initializes an empty in-memory database | Empty dictionary simulating database: {} | - | PASS |
| 2 | Test calls save_data with key=1 and value={'id':1, 'name':'Alice', 'age':30} | Database now contains: {1: {'id':1, 'name':'Alice', 'age':30}} | - | PASS |
| 3 | Test calls get_data with key=1 to retrieve saved data | Retrieved data is {'id':1, 'name':'Alice', 'age':30} | Check if retrieved data equals input data | PASS |
| 4 | Assertion compares retrieved data with input data | Both data sets match exactly | assertEqual passes confirming data integrity | PASS |
| 5 | Test ends successfully | No errors, test passed | - | PASS |