|
| 1 | +--- |
| 2 | +id: express-postgresql-prisma |
| 3 | +title: Express + PostgreSQL + Prisma Sample Application |
| 4 | +sidebar_label: Express + PostgreSQL + Prisma |
| 5 | +description: The following sample app showcases how to use Express framework, PostgreSQL and Prisma ORM and the Keploy Platform. |
| 6 | +tags: |
| 7 | + - javascript |
| 8 | + - quickstart |
| 9 | + - samples |
| 10 | + - examples |
| 11 | + - tutorial |
| 12 | + - nodejs |
| 13 | +keyword: |
| 14 | + - Express Framework |
| 15 | + - PostgreSQL |
| 16 | + - Prisma ORM |
| 17 | + - API Test generator |
| 18 | + - Auto Testcase generation |
| 19 | +--- |
| 20 | + |
| 21 | +## Introduction |
| 22 | + |
| 23 | +A sample Task Management application and see how seamlessly Keploy integrates with [Express](https://expressjs.com/) , [PostgreSQL](https://www.postgresql.org/) and [Prisma ORM](https://www.prisma.io/). Buckle up, it's gonna be a fun ride! 🎢 |
| 24 | + |
| 25 | +import InstallationGuide from '../concepts/installation.md' |
| 26 | + |
| 27 | +<InstallationGuide/> |
| 28 | + |
| 29 | +## Get Started! 🎬 |
| 30 | + |
| 31 | +### Prerequisites |
| 32 | + |
| 33 | +Ensure you have the following installed: |
| 34 | + |
| 35 | +- Docker |
| 36 | +- Node.js and npm |
| 37 | +- Keploy CLI |
| 38 | + |
| 39 | +Clone the repository and move to express-postgresql-prisma folder |
| 40 | + |
| 41 | +```bash |
| 42 | +git clone https://github.com/keploy/samples-typescript.git |
| 43 | +cd samples-typescript/express-postgresql-prisma |
| 44 | +``` |
| 45 | + |
| 46 | +### Install the dependencies |
| 47 | + |
| 48 | +```bash |
| 49 | +npm install |
| 50 | +``` |
| 51 | + |
| 52 | +### Set up environment variables: |
| 53 | + |
| 54 | +```bash |
| 55 | +cp .env.example .env |
| 56 | +``` |
| 57 | + |
| 58 | +### Start PostgreSQL Container |
| 59 | + |
| 60 | +```bash |
| 61 | +docker run --name my-postgres -e POSTGRES_PASSWORD=mysecretpassword -p 5432:5432 -d postgres |
| 62 | +``` |
| 63 | + |
| 64 | +> Note: PostgreSQL Password is `mysecretpassword` |
| 65 | +
|
| 66 | +### Update the `.env` file with your PostgreSQL connection string: |
| 67 | + |
| 68 | +```bash |
| 69 | +PORT=3000 |
| 70 | +DATABASE_URL="postgresql://postgres:mysecretpassword@localhost:5432/postgres" |
| 71 | +``` |
| 72 | + |
| 73 | +### Migrate the database: |
| 74 | + |
| 75 | +```bash |
| 76 | +npm run generate |
| 77 | +npm run migrate init |
| 78 | +``` |
| 79 | + |
| 80 | +```bash |
| 81 | +root@Linus:~/samples-typescript/express-postgresql-prisma# npm run migrate init |
| 82 | + |
| 83 | + |
| 84 | +> prisma migrate dev --name init |
| 85 | + |
| 86 | +Environment variables loaded from .env |
| 87 | +Prisma schema loaded from prisma/schema.prisma |
| 88 | +Datasource "db": PostgreSQL database "postgres", schema "public" at "localhost:5432" |
| 89 | + |
| 90 | +Applying migration `20240730015533_initial_migration` |
| 91 | +Applying migration `20240731010434_due_date_type_change_to_string` |
| 92 | + |
| 93 | +The following migration(s) have been applied: |
| 94 | + |
| 95 | +migrations/ |
| 96 | + └─ 20240730015533_initial_migration/ |
| 97 | + └─ migration.sql |
| 98 | + └─ 20240731010434_due_date_type_change_to_string/ |
| 99 | + └─ migration.sql |
| 100 | + |
| 101 | +Your database is now in sync with your schema. |
| 102 | + |
| 103 | +✔ Generated Prisma Client (v5.17.0) to ./node_modules/@prisma/clie |
| 104 | +nt in 50ms |
| 105 | + |
| 106 | + |
| 107 | +┌─────────────────────────────────────────────────────────┐ |
| 108 | +│ Update available 5.17.0 -> 5.19.0 │ |
| 109 | +│ Run the following to update │ |
| 110 | +│ npm i --save-dev prisma@latest │ |
| 111 | +│ npm i @prisma/client@latest │ |
| 112 | +└─────────────────────────────────────────────────────────┘ |
| 113 | +``` |
| 114 | + |
| 115 | +### Start the application: |
| 116 | + |
| 117 | +```bash |
| 118 | +npm run dev |
| 119 | +``` |
| 120 | + |
| 121 | +```bash |
| 122 | +Server is listening at PORT 3000 |
| 123 | + |
| 124 | + Server: http://localhost:3000 |
| 125 | + API Docs: http://localhost:3000/api/docs |
| 126 | +``` |
| 127 | + |
| 128 | +> Note: The application will run on `http://localhost:3000`. |
| 129 | +
|
| 130 | +Now we walkthrough how to leverage Keploy to automatically generate test cases for the application, and later test the application using Keploy. |
| 131 | + |
| 132 | +### Generate Test Cases |
| 133 | + |
| 134 | +> Note: Build the application first using `npm run build` |
| 135 | +
|
| 136 | +```bash |
| 137 | +keploy record -c "npm start" |
| 138 | +``` |
| 139 | + |
| 140 | +```bash |
| 141 | +root@Linus:~/samples-typescript/express-postgresql-prisma# keploy record -c "npm start" |
| 142 | +🐰 Keploy: 2024-08-28T09:48:30+05:30 INFO config file not found; proceeding with flags only |
| 143 | + |
| 144 | + ▓██▓▄ |
| 145 | + ▓▓▓▓██▓█▓▄ |
| 146 | + ████████▓▒ |
| 147 | + ▀▓▓███▄ ▄▄ ▄ ▌ |
| 148 | + ▄▌▌▓▓████▄ ██ ▓█▀ ▄▌▀▄ ▓▓▌▄ ▓█ ▄▌▓▓▌▄ ▌▌ ▓ |
| 149 | + ▓█████████▌▓▓ ██▓█▄ ▓█▄▓▓ ▐█▌ ██ ▓█ █▌ ██ █▌ █▓ |
| 150 | + ▓▓▓▓▀▀▀▀▓▓▓▓▓▓▌ ██ █▓ ▓▌▄▄ ▐█▓▄▓█▀ █▓█ ▀█▄▄█▀ █▓█ |
| 151 | + ▓▌ ▐█▌ █▌ |
| 152 | + ▓ |
| 153 | + |
| 154 | +version: 2.3.0-beta14 |
| 155 | + |
| 156 | +🐰 Keploy: 2024-08-28T09:48:30+05:30 INFO Generated config file based on the flags that are used |
| 157 | +🐰 Keploy: 2024-08-28T09:48:32+05:30 INFO keploy initialized and probes added to the kernel. |
| 158 | +🐰 Keploy: 2024-08-28T09:48:32+05:30 INFO Keploy has taken control of the DNS resolution mechanism, your application may misbehave if you have provided wrong domain name in your application code. |
| 159 | +🐰 Keploy: 2024-08-28T09:48:32+05:30 INFO Proxy started at port:16789 |
| 160 | +🐰 Keploy: 2024-08-28T09:48:32+05:30 INFO starting TCP DNS server at addr :26789 |
| 161 | +🐰 Keploy: 2024-08-28T09:48:32+05:30 INFO starting UDP DNS server at addr :26789 |
| 162 | + |
| 163 | + |
| 164 | +> node dist/index.js |
| 165 | + |
| 166 | +Server is listening at PORT 3000 |
| 167 | + |
| 168 | + Server: http://localhost:3000 |
| 169 | + API Docs: http://localhost:3000/api/docs |
| 170 | +``` |
| 171 | +
|
| 172 | +The above command will start recording the API calls made to the application and will generate a test case in the `testcases/` directory. |
| 173 | +
|
| 174 | +> 💡 You can use Postman or any other API testing tool to test the API calls. Additionally, the application will run a swagger UI on `http://localhost:3000/api/docs` to visualize the API calls. |
| 175 | +
|
| 176 | +### Interact with Application |
| 177 | +
|
| 178 | +Make API Calls using [Hoppscotch](https://hoppscotch.io/), [Postman](https://www.postman.com/) or [cURL](https://curl.se/) command. Keploy with capture those calls to generate the test-suites containing testcases and data mocks. |
| 179 | +
|
| 180 | +### API Routes |
| 181 | +
|
| 182 | +#### Add Task |
| 183 | +
|
| 184 | +- **URL:** `/api/v1/task/add` |
| 185 | +- **Method:** `POST` |
| 186 | +- **Description:** Add a new task. |
| 187 | +- **Request Body:** |
| 188 | + ```json |
| 189 | + { |
| 190 | + "author": "John Doe", |
| 191 | + "title": "Complete the report", |
| 192 | + "description": "Complete the quarterly report by end of the week", |
| 193 | + "dueDate": "2024-08-01", |
| 194 | + "status": "Pending", |
| 195 | + "priority": 3 |
| 196 | + } |
| 197 | + ``` |
| 198 | +
|
| 199 | +Using `curl` |
| 200 | +
|
| 201 | +```bash |
| 202 | +curl -X 'POST' \ |
| 203 | + 'http://localhost:3000/api/v1/task/add' \ |
| 204 | + -H 'accept: application/json' \ |
| 205 | + -H 'Content-Type: application/json' \ |
| 206 | + -d '{ |
| 207 | + "author": "John Doe", |
| 208 | + "title": "Complete the report", |
| 209 | + "description": "Complete the quarterly report by end of the week", |
| 210 | + "dueDate": "2024-08-01", |
| 211 | + "status": "Pending", |
| 212 | + "priority": 3 |
| 213 | +}' |
| 214 | +``` |
| 215 | +
|
| 216 | +#### View All Tasks |
| 217 | +
|
| 218 | +Using `curl` |
| 219 | +
|
| 220 | +```bash |
| 221 | +curl -X 'GET' \ |
| 222 | + 'http://localhost:3000/api/v1/task/view' \ |
| 223 | + -H 'accept: application/json' |
| 224 | +``` |
| 225 | +
|
| 226 | +#### View Task by ID |
| 227 | +
|
| 228 | +- **URL:** `/api/v1/task/view/:id` |
| 229 | +- **Method:** `GET` |
| 230 | +- **Description:** Retrieve a specific task by its ID. |
| 231 | +- **Request Params:** `id` (task ID) |
| 232 | +
|
| 233 | +Using `curl` |
| 234 | +
|
| 235 | +```bash |
| 236 | +curl -X 'GET' \ |
| 237 | + 'http://localhost:3000/api/v1/task/view/1' \ |
| 238 | + -H 'accept: application/json' |
| 239 | +``` |
| 240 | +
|
| 241 | +#### Change Task Priority |
| 242 | +
|
| 243 | +- **URL:** `/api/v1/task/change-priority/:id` |
| 244 | +- **Method:** `PUT` |
| 245 | +- **Description:** Update the priority of a specific task. |
| 246 | +- **Request Params:** `id` (task ID) |
| 247 | +- **Request Body:** |
| 248 | + ```json |
| 249 | + { |
| 250 | + "priority": 3 |
| 251 | + } |
| 252 | + ``` |
| 253 | +
|
| 254 | +Using `curl` |
| 255 | +
|
| 256 | +```bash |
| 257 | +curl -X 'PUT' \ |
| 258 | + 'http://localhost:3000/api/v1/task/change-priority/1' \ |
| 259 | + -H 'accept: application/json' \ |
| 260 | + -H 'Content-Type: application/json' \ |
| 261 | + -d '{ |
| 262 | + "priority": 3 |
| 263 | +}' |
| 264 | +``` |
| 265 | +
|
| 266 | +#### Update Task |
| 267 | +
|
| 268 | +- **URL:** `/api/v1/task/update/:id` |
| 269 | +- **Method:** `PUT` |
| 270 | +- **Description:** Update details of a specific task. |
| 271 | +- **Request Params:** `id` (task ID) |
| 272 | +- **Request Body:** |
| 273 | + ```json |
| 274 | + { |
| 275 | + "author": "John Doe", |
| 276 | + "title": "Complete the report", |
| 277 | + "description": "Complete the quarterly report by end of the week", |
| 278 | + "dueDate": "2024-08-01", |
| 279 | + "status": "Pending", |
| 280 | + "priority": 3 |
| 281 | + } |
| 282 | + ``` |
| 283 | +
|
| 284 | +Using `curl` |
| 285 | +
|
| 286 | +```bash |
| 287 | +curl -X 'PUT' \ |
| 288 | + 'http://localhost:3000/api/v1/task/update/2' \ |
| 289 | + -H 'accept: application/json' \ |
| 290 | + -H 'Content-Type: application/json' \ |
| 291 | + -d '{ |
| 292 | + "author": "John Doe", |
| 293 | + "title": "Complete the report", |
| 294 | + "description": "Complete the quarterly report by end of the week", |
| 295 | + "dueDate": "2024-08-01", |
| 296 | + "status": "Pending", |
| 297 | + "priority": 3 |
| 298 | +}' |
| 299 | +``` |
| 300 | +
|
| 301 | +#### Delete Task |
| 302 | +
|
| 303 | +- **URL:** `/api/v1/task/delete/:id` |
| 304 | +- **Method:** `DELETE` |
| 305 | +- **Description:** Delete a specific task. |
| 306 | +- **Request Params:** `id` (task ID) |
| 307 | +
|
| 308 | +Using `curl` |
| 309 | +
|
| 310 | +```bash |
| 311 | +curl -X 'DELETE' \ |
| 312 | + 'http://localhost:3000/api/v1/task/delete/1' \ |
| 313 | + -H 'accept: application/json' |
| 314 | +``` |
| 315 | +
|
| 316 | +> 🐰 Test Data and Configuration: After recording the interactions, a `keploy` folder will be created containing the recorded test data. Additionally, a `keploy.yml` file will be created as the configuration file. |
| 317 | +
|
| 318 | +### Test the Application using Keploy |
| 319 | +
|
| 320 | +```bash |
| 321 | +keploy test -c "npm start" |
| 322 | +``` |
| 323 | +
|
| 324 | +Keploy will replay the recorded interactions and validate the responses against the expected results. |
| 325 | +
|
| 326 | +```bash |
| 327 | +Node.js v22.7.0 |
| 328 | +🐰 Keploy: 2024-08-28T10:14:14+05:30 WARN To enable storing mocks in cloud please use disableMockUpload flag/configuration |
| 329 | +
|
| 330 | + <=========================================> |
| 331 | + COMPLETE TESTRUN SUMMARY. |
| 332 | + Total tests: 7 |
| 333 | + Total test passed: 7 |
| 334 | + Total test failed: 0 |
| 335 | + Total time taken: "5.66 s" |
| 336 | +
|
| 337 | + Test Suite Name Total Test Passed Failed Time Taken |
| 338 | +
|
| 339 | + "test-set-0" 7 7 0 "5.66 s" |
| 340 | +<=========================================> |
| 341 | +
|
| 342 | +🐰 Keploy: 2024-08-28T10:14:14+05:30 INFO calculating coverage for the test run and inserting it into the report |
| 343 | +🐰 Keploy: 2024-08-28T10:14:14+05:30 INFO [Total Coverage Percentage: 86.16%] |
| 344 | +🐰 Keploy: 2024-08-28T10:14:14+05:30 INFO stopping Keploy {"reason": "replay completed successfully"} |
| 345 | +🐰 Keploy: 2024-08-28T10:14:14+05:30 INFO proxy stopped... |
| 346 | +🐰 Keploy: 2024-08-28T10:14:17+05:30 INFO eBPF resources released successfully... |
| 347 | +``` |
| 348 | +
|
| 349 | +Voila! 🎉 You have successfully tested the application using Keploy. Keploy also generates coverage reports for the test-suites. |
0 commit comments