Test Overview
This test checks that data saved in the database matches what was input through the application. It verifies that no data is lost or changed incorrectly, ensuring data integrity.
This test checks that data saved in the database matches what was input through the application. It verifies that no data is lost or changed incorrectly, ensuring data integrity.
import sqlite3 import unittest class TestDatabaseIntegrity(unittest.TestCase): def setUp(self): self.conn = sqlite3.connect(':memory:') self.cursor = self.conn.cursor() self.cursor.execute('''CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)''') def test_insert_and_retrieve(self): # Insert data self.cursor.execute('INSERT INTO users (name, age) VALUES (?, ?)', ('Alice', 30)) self.conn.commit() # Retrieve data self.cursor.execute('SELECT name, age FROM users WHERE name = ?', ('Alice',)) row = self.cursor.fetchone() # Assert data integrity self.assertIsNotNone(row) self.assertEqual(row[0], 'Alice') self.assertEqual(row[1], 30) def tearDown(self): self.conn.close() if __name__ == '__main__': unittest.main()
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and in-memory database is created with users table | Empty database with users table ready | - | PASS |
| 2 | Insert user data ('Alice', 30) into users table | Database contains one user record with name 'Alice' and age 30 | - | PASS |
| 3 | Retrieve user data where name is 'Alice' | Query returns one record with name 'Alice' and age 30 | Check that retrieved data is not None | PASS |
| 4 | Assert retrieved name equals 'Alice' | Retrieved data name field is 'Alice' | AssertEqual(row[0], 'Alice') | PASS |
| 5 | Assert retrieved age equals 30 | Retrieved data age field is 30 | AssertEqual(row[1], 30) | PASS |
| 6 | Test ends and database connection is closed | Database connection closed | - | PASS |