๐งช Specialized Testing: API and Database Testing
Break It Before Users Do!
๐ญ The Restaurant Kitchen Story
Imagine a busy restaurant. The kitchen (backend) makes food. The waiters (APIs) take orders and bring food to tables. The pantry (database) stores all ingredients.
If waiters forget orders, or the pantry has spoiled food, customers get angry!
API and Database Testing = Making sure waiters never mess up orders, and the pantry always has fresh, organized ingredients.
๐ก API Testing Overview
What is an API?
Think of an API like a waiter in a restaurant.
- You (the app) tell the waiter: โI want pizza!โ
- The waiter goes to the kitchen (server)
- The kitchen makes pizza
- The waiter brings it back to you
API = The messenger between you and the kitchen
Why Test APIs?
What could go wrong?
โโโโโโโโโโโโโโโโโโโโ
โ Waiter forgets your order
โ Waiter brings wrong food
โ Waiter takes too long
โ Kitchen is closed
API Testing checks:
- โ Does the waiter understand my order?
- โ Does the waiter bring the right food?
- โ Does the waiter reply fast enough?
- โ What happens if I order something weird?
Simple Example
You send: GET /users/123
You expect: { "name": "Alice", "age": 25 }
Test checks:
โ Status code = 200 (success)
โ Response has "name" field
โ Response time < 2 seconds
๐ API Protocol Testing
Whatโs a Protocol?
A protocol = Rules for how to talk.
Like different languages:
- REST = Simple English (most common)
- GraphQL = Custom order form (ask exactly what you want)
- SOAP = Formal letter (old-fashioned, strict)
- gRPC = Super fast walkie-talkie
REST API Testing
REST uses simple words like:
- GET = โGive me somethingโ
- POST = โHereโs something newโ
- PUT = โUpdate this thingโ
- DELETE = โRemove this thingโ
Testing a REST API:
โโโโโโโโโโโโโโโโโโโ
GET /toys โ Should list all toys
POST /toys โ Should create new toy
PUT /toys/5 โ Should update toy #5
DELETE /toys/5 โ Should remove toy #5
GraphQL Testing
GraphQL is like a custom menu. You ask for exactly what you want!
Query: "Give me user name and email only"
{
user(id: 1) {
name
email
}
}
Response: Only name and email (nothing extra!)
Test checks: Did I get exactly what I asked for?
๐ Contract Testing
The Promise Between Friends
Imagine you and your friend have a promise:
โWhen I say โhigh-fiveโ, you raise your hand.โ
Contract = A written promise of how APIs will behave.
Why Contract Testing Matters
graph TD A["Team A: Makes API"] -->|Promise| C["Contract"] B["Team B: Uses API"] -->|Expects| C C -->|Both agree on| D["Request Format"] C -->|Both agree on| E["Response Format"]
Problem without contracts:
- Team A changes the API
- Team Bโs app breaks
- Everyone is surprised and angry!
With contract testing:
- Both teams agree on the โpromiseโ
- If anyone breaks the promise, tests fail immediately
- No surprises!
Simple Contract Example
Contract: GET /user/1 must return:
{
"id": number (required),
"name": string (required),
"email": string (required)
}
If API returns something different โ TEST FAILS!
๐๏ธ Microservices Testing
What Are Microservices?
Instead of one giant robot doing everything, you have many small robots, each doing one job.
Monolith (One Robot):
๐ค Does: Login + Orders + Payments + Emails
Microservices (Team of Robots):
๐ค Login Robot
๐ฆ Orders Robot
๐ณ Payments Robot
โ๏ธ Email Robot
Why Test Microservices?
The small robots need to talk to each other.
graph TD A["๐ค Login"] -->|Token| B["๐ฆ Orders"] B -->|Order Info| C["๐ณ Payments"] C -->|Success| D["โ๏ธ Email"]
What could go wrong?
- One robot is sleeping (service down)
- Robots speak different languages (format mismatch)
- Message gets lost in the middle
Testing Types for Microservices
| Test Type | What It Checks |
|---|---|
| Unit | One robot works alone |
| Integration | Two robots talk correctly |
| End-to-End | Whole team works together |
Example Test
Scenario: User places an order
1. Login service โ returns token โ
2. Order service โ creates order โ
3. Payment service โ charges card โ
4. Email service โ sends confirmation โ
All robots worked together = TEST PASSED!
๐๏ธ Database Testing
The Pantry of Your App
The database is like a giant pantry that stores everything:
- User information (names, passwords)
- Orders
- Products
- Messages
Why Test the Database?
What if the pantry:
โ Loses ingredients (data loss)
โ Mixes up labels (wrong data)
โ Is too slow to find things
โ Lets anyone in (security hole)
What Database Testing Checks
โ
Can we save data correctly?
โ
Can we find data quickly?
โ
Is data protected from strangers?
โ
Does data stay safe when things crash?
Example: CRUD Testing
CRUD = Create, Read, Update, Delete (the 4 basic actions)
-- CREATE: Add new toy
INSERT INTO toys (name) VALUES ('Teddy');
โ Check: Did toy appear in database?
-- READ: Find the toy
SELECT * FROM toys WHERE name='Teddy';
โ Check: Did we get the right toy?
-- UPDATE: Rename the toy
UPDATE toys SET name='Mr. Teddy' WHERE id=1;
โ Check: Is the name changed?
-- DELETE: Remove the toy
DELETE FROM toys WHERE id=1;
โ Check: Is the toy gone?
๐ Data Integrity Testing
Keeping Data Clean and Honest
Data Integrity = Data is correct, complete, and trustworthy.
Like making sure:
- Your piggy bank count is accurate
- No one secretly added or removed coins
- The total always makes sense
Types of Data Integrity
๐ Entity Integrity
Every row has a unique ID
Example: No two users have same ID
๐ Referential Integrity
Relationships make sense
Example: Order can't belong to
a user that doesn't exist
โ
Domain Integrity
Data follows rules
Example: Age can't be negative
๐งฎ User-Defined Integrity
Your custom rules
Example: Discount can't exceed 50%
Testing Data Integrity
Test 1: Unique IDs
โ Try to create two users with same ID
โ Database should say NO!
Test 2: Required Fields
โ Try to save user without email
โ Database should say NO!
Test 3: Valid Values
โ Try to set age = -5
โ Database should say NO!
Example
-- This should FAIL (no duplicate emails!)
INSERT INTO users (email) VALUES ('bob@mail.com');
INSERT INTO users (email) VALUES ('bob@mail.com');
โ Error: Duplicate entry! โ
-- This should FAIL (age must be positive!)
INSERT INTO users (age) VALUES (-10);
โ Error: Check constraint failed! โ
๐ Data Migration Testing
Moving to a New House
Data Migration = Moving data from old place to new place.
Like moving from an old house to a new house:
- Did all furniture arrive?
- Is everything in the right room?
- Did anything break during the move?
When Do We Migrate Data?
๐ฆ Upgrading database version
๐ฆ Switching to new database system
๐ฆ Merging two companies' data
๐ฆ Moving to cloud
๐ฆ Splitting one database into many
What Migration Testing Checks
graph TD A["Old Database"] -->|Migrate| B["New Database"] C["Test: Count Records"] --> D{Same Count?} D -->|Yes| E["โ Pass"] D -->|No| F["โ Fail"] G["Test: Compare Data"] --> H{Same Values?} H -->|Yes| E H -->|No| F
Testing Checklist
| Check | Question |
|---|---|
| Count | Same number of records? |
| Values | All data matches exactly? |
| Format | Data in correct new format? |
| Links | Relationships still work? |
| Speed | New database fast enough? |
Example Migration Test
OLD DATABASE (MySQL):
users table: 10,000 records
NEW DATABASE (PostgreSQL):
users table: ??? records
MIGRATION TEST:
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Count: 10,000 = 10,000 โ
โ Sample user #1 data matches โ
โ Sample user #5000 data matches โ
โ All emails still unique โ
โ Foreign keys still work โ
MIGRATION SUCCESSFUL! ๐
๐ฏ Quick Summary
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ API & DATABASE TESTING CHEAT SHEET โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ ๐ฝ๏ธ API = Waiter between you & kitchen โ
โ โ
โ ๐ก Protocol = Language APIs speak โ
โ (REST, GraphQL, SOAP, gRPC) โ
โ โ
โ ๐ Contract = Promise between teams โ
โ โ
โ ๐๏ธ Microservices = Team of small robotsโ
โ โ
โ ๐๏ธ Database = The pantry of your app โ
โ โ
โ ๐ Data Integrity = Keep data clean โ
โ โ
โ ๐ Migration = Moving to new house โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
๐ Youโve Got This!
Remember:
- APIs are just messengers - test that messages arrive correctly
- Databases are just storage - test that nothing gets lost or corrupted
- Always test before users find problems!
Happy Testing! ๐งชโจ
