API and Database Testing

Back

Loading concept...

๐Ÿงช 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! ๐Ÿงชโœจ

Loading story...

Story - Premium Content

Please sign in to view this story and start learning.

Upgrade to Premium to unlock full access to all stories.

Stay Tuned!

Story is coming soon.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.