Introduction
Imagine sending a message or saving important information, but it gets changed or corrupted without you knowing. Data integrity checks help catch these mistakes to keep information accurate and trustworthy.
Think of sending a sealed letter with a wax stamp. The stamp shows if the letter was opened or changed during delivery. If the stamp is broken, you know the letter might not be safe.
┌─────────────────────────────┐
│ Original Data │
└─────────────┬───────────────┘
│
▼
┌─────────────────────────────┐
│ Generate Integrity Code │
│ (checksum, hash, parity) │
└─────────────┬───────────────┘
│
▼
┌─────────────────────────────┐
│ Store or Transmit Data + │
│ Integrity Code │
└─────────────┬───────────────┘
│
▼
┌─────────────────────────────┐
│ Receive or Retrieve Data │
└─────────────┬───────────────┘
│
▼
┌─────────────────────────────┐
│ Recalculate Integrity Code │
└─────────────┬───────────────┘
│
▼
┌──────────────┬──────────────┐
│ │ │
│ Codes Match │ Codes Differ│
│ (Pass) │ (Fail) │
│ │ │
└──────┬───────┴──────┬───────┘
│ │
▼ ▼
Use Data Reject or Request
Data Againimport hashlib def calculate_hash(data: str) -> str: return hashlib.sha256(data.encode()).hexdigest() original_data = "Hello, world!" hash_original = calculate_hash(original_data) # Simulate data transmission received_data = "Hello, world!" hash_received = calculate_hash(received_data) if hash_original == hash_received: print("Data integrity verified: no changes detected.") else: print("Data integrity check failed: data was altered.")
data integrity checks in software testing?expected_data = {'id': 101, 'name': 'Alice'}
actual_data = {'id': 101, 'name': 'Alice'}
assert expected_data == actual_data, 'Data mismatch!'expected = {'user': 'bob', 'age': 30}
actual = {'user': 'bob', 'age': '30'}
assert expected == actual, 'Mismatch found!'users = [
{'id': 1, 'name': 'Anna'},
{'id': 2, 'name': 'Ben'},
{'id': 1, 'name': 'Anna'},
{'id': 3, 'name': 'Cara'}
]Pass the test and this pattern moves unknown