Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

product module api test cases and code issue resolved #172

Merged
merged 3 commits into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion modules/chat/tests/api/chat-routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ describe.only('chat api endpoints', () => {
describe('GET /chat/random', () => {
it('should return a random chat snapshot with 1000 messages', async () => {
const response = await request(app).get(`/chat/random`);

expect(response.body.messages.length).toBe(1000);
expect(response.body.messages[0]).toHaveProperty('id');
expect(response.body.messages[0]).toHaveProperty('createdAt');
Expand Down
1 change: 0 additions & 1 deletion modules/countries/test/countries-routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ describe('country api endpoints', () => {
it('should return gpb_usd ticker data', async () => {
const filter = 'Afghanistan';
const response = await request(app).get(`/countries/${filter}`);

expect(response.body.countries[0]).toEqual(filter);
});
});
Expand Down
31 changes: 0 additions & 31 deletions modules/images/tests/Api/images-routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ describe('image api endpoints', () => {
const width = 200;
const height = 200;
const qty = 2;

const response = await request(app).get(`/images/abstract/${width}/${height}/${qty}}`);
expect(response.body.length).toBe(qty);
const width_height = get_width_height(response.body[0]);

expect(width_height[0]).toEqual(width.toString());
expect(width_height[1]).toEqual(height.toString());
});
Expand All @@ -29,11 +27,9 @@ describe('image api endpoints', () => {
const width = 200;
const height = 200;
const qty = 2;

const response = await request(app).get(`/images/animals/${width}/${height}/${qty}}`);
expect(response.body.length).toBe(qty);
const width_height = get_width_height(response.body[0]);

expect(width_height[0]).toEqual(width.toString());
expect(width_height[1]).toEqual(height.toString());
});
Expand All @@ -45,14 +41,9 @@ describe('image api endpoints', () => {
const width = 200;
const height = 200;
const qty = 2;

const response = await request(app).get(`/images/avatar/${width}/${height}/${qty}}`);

console.log(response.body);

expect(response.body.length).toBe(qty);
const width_height = get_width_height(response.body[0]);

expect(width_height[0]).toEqual(width.toString());
expect(width_height[1]).toEqual(height.toString());
});
Expand All @@ -63,11 +54,9 @@ describe('image api endpoints', () => {
const width = 200;
const height = 200;
const qty = 2;

const response = await request(app).get(`/images/business/${width}/${height}/${qty}}`);
expect(response.body.length).toBe(qty);
const width_height = get_width_height(response.body[0]);

expect(width_height[0]).toEqual(width.toString());
expect(width_height[1]).toEqual(height.toString());
});
Expand All @@ -78,11 +67,9 @@ describe('image api endpoints', () => {
const width = 200;
const height = 200;
const qty = 2;

const response = await request(app).get(`/images/cats/${width}/${height}/${qty}}`);
expect(response.body.length).toBe(qty);
const width_height = get_width_height(response.body[0]);

expect(width_height[0]).toEqual(width.toString());
expect(width_height[1]).toEqual(height.toString());
});
Expand All @@ -93,11 +80,9 @@ describe('image api endpoints', () => {
const width = 200;
const height = 200;
const qty = 2;

const response = await request(app).get(`/images/city/${width}/${height}/${qty}}`);
expect(response.body.length).toBe(qty);
const width_height = get_width_height(response.body[0]);

expect(width_height[0]).toEqual(width.toString());
expect(width_height[1]).toEqual(height.toString());
});
Expand All @@ -108,11 +93,9 @@ describe('image api endpoints', () => {
const width = 200;
const height = 200;
const qty = 2;

const response = await request(app).get(`/images/fashion/${width}/${height}/${qty}}`);
expect(response.body.length).toBe(qty);
const width_height = get_width_height(response.body[0]);

expect(width_height[0]).toEqual(width.toString());
expect(width_height[1]).toEqual(height.toString());
});
Expand All @@ -123,11 +106,9 @@ describe('image api endpoints', () => {
const width = 200;
const height = 200;
const qty = 2;

const response = await request(app).get(`/images/food/${width}/${height}/${qty}}`);
expect(response.body.length).toBe(qty);
const width_height = get_width_height(response.body[0]);

expect(width_height[0]).toEqual(width.toString());
expect(width_height[1]).toEqual(height.toString());
});
Expand All @@ -138,11 +119,9 @@ describe('image api endpoints', () => {
const width = 200;
const height = 200;
const qty = 2;

const response = await request(app).get(`/images/nature/${width}/${height}/${qty}}`);
expect(response.body.length).toBe(qty);
const width_height = get_width_height(response.body[0]);

expect(width_height[0]).toEqual(width.toString());
expect(width_height[1]).toEqual(height.toString());
});
Expand All @@ -153,11 +132,9 @@ describe('image api endpoints', () => {
const width = 200;
const height = 200;
const qty = 2;

const response = await request(app).get(`/images/nightlife/${width}/${height}/${qty}}`);
expect(response.body.length).toBe(qty);
const width_height = get_width_height(response.body[0]);

expect(width_height[0]).toEqual(width.toString());
expect(width_height[1]).toEqual(height.toString());
});
Expand All @@ -168,11 +145,9 @@ describe('image api endpoints', () => {
const width = 200;
const height = 200;
const qty = 2;

const response = await request(app).get(`/images/people/${width}/${height}/${qty}}`);
expect(response.body.length).toBe(qty);
const width_height = get_width_height(response.body[0]);

expect(width_height[0]).toEqual(width.toString());
expect(width_height[1]).toEqual(height.toString());
});
Expand All @@ -183,11 +158,9 @@ describe('image api endpoints', () => {
const width = 200;
const height = 200;
const qty = 2;

const response = await request(app).get(`/images/sports/${width}/${height}/${qty}}`);
expect(response.body.length).toBe(qty);
const width_height = get_width_height(response.body[0]);

expect(width_height[0]).toEqual(width.toString());
expect(width_height[1]).toEqual(height.toString());
});
Expand All @@ -198,11 +171,9 @@ describe('image api endpoints', () => {
const width = 200;
const height = 200;
const qty = 2;

const response = await request(app).get(`/images/technics/${width}/${height}/${qty}}`);
expect(response.body.length).toBe(qty);
const width_height = get_width_height(response.body[0]);

expect(width_height[0]).toEqual(width.toString());
expect(width_height[1]).toEqual(height.toString());
});
Expand All @@ -213,11 +184,9 @@ describe('image api endpoints', () => {
const width = 200;
const height = 200;
const qty = 2;

const response = await request(app).get(`/images/transport/${width}/${height}/${qty}}`);
expect(response.body.length).toBe(qty);
const width_height = get_width_height(response.body[0]);

expect(width_height[0]).toEqual(width.toString());
expect(width_height[1]).toEqual(height.toString());
});
Expand Down
4 changes: 2 additions & 2 deletions modules/ip/api/ip-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = function (app: core.Express) {
* '/ip/{qty}':
* get:
* tags:
* - Products
* - IPs
* summary: Returns a random list of ip addresses
* parameters:
* - in: path
Expand Down Expand Up @@ -75,7 +75,7 @@ module.exports = function (app: core.Express) {
* '/mac/{qty}':
* get:
* tags:
* - Products
* - IPs
* summary: Returns a random list of mac addresses
* parameters:
* - in: path
Expand Down
20 changes: 11 additions & 9 deletions modules/products/api/products-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as core from 'express-serve-static-core';
import productReviews from '../data/product-reviews';
import { getQtyFromRequest } from '../../../utils/route-utils';
import getProducts from '../utils/getProducts';
import { randomRating } from '../../../utils/numbers';

/**
* @openapi
Expand Down Expand Up @@ -102,7 +103,7 @@ module.exports = function (app: core.Express) {

/**
* @openapi
* '/product/{department}':
* '/product/department/{department}':
* get:
* tags:
* - Products
Expand All @@ -117,7 +118,7 @@ module.exports = function (app: core.Express) {
* schema:
* $ref: '#/definitions/MockProduct'
*/
app.get('/product/:department', (req: Request, res: Response) => {
app.get('/product/department/:department', (req: Request, res: Response) => {
const department = req.params.department ? req.params.department.toString() : 'General';
res.json(getProducts(1, department));
});
Expand Down Expand Up @@ -204,12 +205,13 @@ module.exports = function (app: core.Express) {
* schema:
* $ref: '#/definitions/MockProductReview'
*/
app.get('/products/reviews/rating/:rating', (req: Request, res: Response) => {
let reviews = productReviews;
const rating = parseInt(req.params.rating);
reviews.forEach((element) => {
element.rating = rating;
});
res.json(reviews);
app.get('/products/reviews/ratings/:rating', (req: Request, res: Response) => {
const rating = req.params.rating ?
(parseInt(req.params.rating) <= 5 ?
parseInt(req.params.rating)
: 5)
: randomRating();
const filtereReview = productReviews.filter((element) => element.rating = rating);
res.json(filtereReview);
});
};
67 changes: 63 additions & 4 deletions modules/products/test/api/products-routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,71 @@ describe('products api endpoints', () => {
});
});

describe('GET /products/reviews/rating/:rating', () => {
describe('GET /products/reviews/ratings/:rating', () => {
const rating = 2;

it('should return a list of users ', async () => {
const response = await request(app).get(`/products/reviews/rating/${rating}`);
expect(response.body[0].rating).toBeLessThanOrEqual(rating);
it('should return a list of reviews ', async () => {
const response = await request(baseURL).get(`/products/reviews/ratings/${rating}`);
expect(response.body[0].rating).toEqual(rating);
expect(response.body[0]).toHaveProperty('productName');
expect(response.body[0]).toHaveProperty('productId');
expect(response.body[0]).toHaveProperty('message');
expect(response.body[0]).toHaveProperty('dateTime');
expect(response.body[0]).toHaveProperty('rating');
expect(response.body[0]).toHaveProperty('userName');
expect(response.body[0]).toHaveProperty('categories');
});
});

describe('GET /products/:qty/:department', () => {
const quantity = 4;
const department = "Computer";

it('should return a list of products ', async () => {
const response = await request(baseURL).get(`/products/${quantity}/${department}`);
expect(response.body.length).toEqual(quantity);
expect(response.body[0].department).toEqual(department);
expect(response.body[0]).toHaveProperty('department');
expect(response.body[0]).toHaveProperty('type');
expect(response.body[0]).toHaveProperty('name');
expect(response.body[0]).toHaveProperty('adjective');
expect(response.body[0]).toHaveProperty('description');
expect(response.body[0]).toHaveProperty('material');
expect(response.body[0]).toHaveProperty('sku');
});
});

describe('GET /products/quantity/:qty', () => {
const quantity = 4;

it('should return a list of products ', async () => {
const response = await request(baseURL).get(`/products/quantity/${quantity}`);
expect(response.body.length).toEqual(quantity);

});
});

describe('GET /products/department/:qty', () => {
const department = "Computer";

it('should return a list of products', async () => {
const response = await request(baseURL).get(`/products/department/${department}`);
expect(response.body[0].department).toEqual(department);

});
});

describe('GET /product', () => {
it('should return a random product ', async () => {
const response = await request(baseURL).get(`/product`);
expect(response.body.length).toEqual(1);
expect(response.body[0]).toHaveProperty('department');
expect(response.body[0]).toHaveProperty('type');
expect(response.body[0]).toHaveProperty('name');
expect(response.body[0]).toHaveProperty('adjective');
expect(response.body[0]).toHaveProperty('description');
expect(response.body[0]).toHaveProperty('material');
expect(response.body[0]).toHaveProperty('sku');
});
});
});
2 changes: 1 addition & 1 deletion modules/time_zones/api/timezones-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Request, Response } from 'express';
import * as core from 'express-serve-static-core';
import time_zones from '../data/time_zones';

module.exports = function(app : core.Express){
module.exports = function (app: core.Express) {

/**
* @openapi
Expand Down
6 changes: 3 additions & 3 deletions modules/time_zones/data/time_zones.ts
Original file line number Diff line number Diff line change
Expand Up @@ -901,7 +901,7 @@ const time_zones = [
"isdst": false,
"text": "(UTC+03:00) Moscow, St. Petersburg, Volgograd, Minsk",
"utc": [
"Europe/Kirov",
"Europe/Kirov",
"Europe/Moscow",
"Europe/Simferopol",
"Europe/Volgograd",
Expand All @@ -915,9 +915,9 @@ const time_zones = [
"isdst": false,
"text": "(UTC+04:00) Samara, Ulyanovsk, Saratov",
"utc": [
"Europe/Astrakhan",
"Europe/Astrakhan",
"Europe/Samara",
"Europe/Ulyanovsk"
"Europe/Ulyanovsk"
]
},
{
Expand Down
Loading