From ccbbb8b9c9b068de1287caa51f9bd7ec9f411755 Mon Sep 17 00:00:00 2001 From: deveshsangwan Date: Sun, 28 Jan 2024 22:36:48 +0530 Subject: [PATCH] Fix build script and add isLive property to MatchData --- .github/workflows/test.yml | 3 +++ README.md | 2 +- app/src/LiveMatches/index.js | 2 +- app/src/MatchStats/index.js | 5 ++-- app/src/controller.js | 4 +-- app/ts_src/LiveMatches/index.ts | 4 +-- app/ts_src/MatchStats/MatchStatsInterfaces.ts | 1 + app/ts_src/MatchStats/index.ts | 9 ++++--- app/ts_src/Utils.ts | 2 +- app/ts_src/controller.ts | 4 +-- package.json | 2 +- unitTest/MatchStats.test.ts | 26 +++++++++++++++++++ unitTest/TestData/MatchStats.ts | 6 +++++ 13 files changed, 54 insertions(+), 16 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 45fc702..86371dd 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -21,6 +21,9 @@ jobs: - name: Install Dependencies run: npm i + - name: Run Build + run: npm run build + - name: Run Tests run: npm run test:coverage diff --git a/README.md b/README.md index 3b8b631..17d4f5d 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ Welcome to the Cricket Score API! This project is designed to provide real-time Our aim is to maintain high code coverage to ensure the quality of the project. Here are our current stats: ![Functions](https://img.shields.io/badge/functions-96.07%25-brightgreen.svg?style=flat) -![Lines](https://img.shields.io/badge/lines-91%25-brightgreen.svg?style=flat) +![Lines](https://img.shields.io/badge/lines-91.66%25-brightgreen.svg?style=flat) ## Getting Started diff --git a/app/src/LiveMatches/index.js b/app/src/LiveMatches/index.js index ff04843..305c996 100644 --- a/app/src/LiveMatches/index.js +++ b/app/src/LiveMatches/index.js @@ -93,7 +93,7 @@ class LiveMatches { const MATCH_ID_LENGTH = 16; const existingMatches = {}; const newMatches = {}; - $('.cb-col-100 .cb-col .cb-schdl').each((i, el) => { + $('.cb-col-100 .cb-col .cb-schdl').each((_, el) => { const matchUrl = $(el).find('.cb-lv-scr-mtch-hdr a').attr('href'); const matchName = $(el).find('.cb-billing-plans-text a').attr('title'); if (matchUrl && matchName) { diff --git a/app/src/MatchStats/index.js b/app/src/MatchStats/index.js index 39170a6..108d3df 100644 --- a/app/src/MatchStats/index.js +++ b/app/src/MatchStats/index.js @@ -127,7 +127,7 @@ class MatchStats { if (elements.length === 0) { throw new Error('No elements found with the selector .cb-col.cb-col-100.cb-bg-white'); } - const tournamentNames = elements.map((i, el) => $(el).find('a').attr('title')).get(); + const tournamentNames = elements.map((_, el) => $(el).find('a').attr('title')).get(); return tournamentNames[0]; } catch (error) { @@ -148,7 +148,8 @@ class MatchStats { player1: (0, MatchUtils_1.getBatsmanData)($, 0), player2: (0, MatchUtils_1.getBatsmanData)($, 1) }, - summary: $('div.cb-text-stumps, div.cb-text-complete, div.cb-text-inprogress').text().trim() + summary: $('div.cb-text-stumps, div.cb-text-complete, div.cb-text-inprogress').text().trim(), + isLive: isLive }; resolve(matchData); } diff --git a/app/src/controller.js b/app/src/controller.js index e8ae06b..ea73f3d 100644 --- a/app/src/controller.js +++ b/app/src/controller.js @@ -2,7 +2,7 @@ Object.defineProperty(exports, "__esModule", { value: true }); const LiveMatches_1 = require("./LiveMatches"); const MatchStats_1 = require("./MatchStats"); -const live = async (req, res) => { +const live = async (_req, res) => { try { const liveMatchesObj = new LiveMatches_1.LiveMatches(); const liveMatchesResponse = await liveMatchesObj.getMatches(); @@ -39,7 +39,7 @@ const matchStats = async (req, res) => { }); } }; -const getMatchStats = async (req, res) => { +const getMatchStats = async (_req, res) => { try { const matchStatsObj = new MatchStats_1.MatchStats(); const matchStatsResponse = await matchStatsObj.getMatchStats("0"); diff --git a/app/ts_src/LiveMatches/index.ts b/app/ts_src/LiveMatches/index.ts index ee2bae7..e745c66 100644 --- a/app/ts_src/LiveMatches/index.ts +++ b/app/ts_src/LiveMatches/index.ts @@ -1,5 +1,5 @@ import { Utils } from '../Utils'; -import { writeLogInfo, writeLogError } from '../../core/logger'; +import { writeLogError } from '../../core/logger'; import { MatchData } from './LiveMatchesInterfaces'; import { insertDataToLiveMatchesTable } from './LiveMatchesUtility'; import { CustomError } from '../errors'; @@ -71,7 +71,7 @@ export class LiveMatches { const existingMatches: Record = {}; const newMatches: Record = {}; - $('.cb-col-100 .cb-col .cb-schdl').each((i, el) => { + $('.cb-col-100 .cb-col .cb-schdl').each((_, el) => { const matchUrl = $(el).find('.cb-lv-scr-mtch-hdr a').attr('href'); const matchName = $(el).find('.cb-billing-plans-text a').attr('title'); diff --git a/app/ts_src/MatchStats/MatchStatsInterfaces.ts b/app/ts_src/MatchStats/MatchStatsInterfaces.ts index 1d37ce9..804ef4b 100644 --- a/app/ts_src/MatchStats/MatchStatsInterfaces.ts +++ b/app/ts_src/MatchStats/MatchStatsInterfaces.ts @@ -20,6 +20,7 @@ export interface ITeamData { export interface MatchData { matchId: string; + isLive: boolean; team1: ITeamData | {}; team2: ITeamData | {}; onBatting: { diff --git a/app/ts_src/MatchStats/index.ts b/app/ts_src/MatchStats/index.ts index 767d372..1e010ed 100644 --- a/app/ts_src/MatchStats/index.ts +++ b/app/ts_src/MatchStats/index.ts @@ -1,9 +1,9 @@ import { LiveMatches } from '../LiveMatches'; import { Utils } from '../Utils'; import * as mongo from '../../core/baseModel'; -import { writeLogInfo, writeLogError } from '../../core/logger'; +import { writeLogError } from '../../core/logger'; import { InvalidMatchIdError, MatchIdRequriedError, NoMatchesFoundError } from '../errors'; -import { LiveMatchesResponse, ITeamData, MatchData } from './MatchStatsInterfaces'; +import { LiveMatchesResponse, MatchData } from './MatchStatsInterfaces'; import { getTeamScoreString, getTeamData, getBatsmanData } from './MatchUtils' const _ = require('underscore'); @@ -123,7 +123,7 @@ export class MatchStats { if (elements.length === 0) { throw new Error('No elements found with the selector .cb-col.cb-col-100.cb-bg-white'); } - const tournamentNames = elements.map((i, el) => $(el).find('a').attr('title')).get(); + const tournamentNames = elements.map((_, el) => $(el).find('a').attr('title')).get(); return tournamentNames[0]; } catch (error) { throw new Error(`Error while fetching tournament name: ${error.message}`); @@ -145,7 +145,8 @@ export class MatchStats { player1: getBatsmanData($, 0), player2: getBatsmanData($, 1) }, - summary: $('div.cb-text-stumps, div.cb-text-complete, div.cb-text-inprogress').text().trim() + summary: $('div.cb-text-stumps, div.cb-text-complete, div.cb-text-inprogress').text().trim(), + isLive: isLive }; resolve(matchData); diff --git a/app/ts_src/Utils.ts b/app/ts_src/Utils.ts index 1a6acf5..c76a1f6 100644 --- a/app/ts_src/Utils.ts +++ b/app/ts_src/Utils.ts @@ -1,6 +1,6 @@ const axios = require('axios'); const cheerio = require('cheerio'); -import { writeLogInfo, writeLogError } from '../core/logger'; +import { writeLogError } from '../core/logger'; import * as mongo from '../core/baseModel'; export class Utils { diff --git a/app/ts_src/controller.ts b/app/ts_src/controller.ts index 43b7b87..fca468d 100644 --- a/app/ts_src/controller.ts +++ b/app/ts_src/controller.ts @@ -2,7 +2,7 @@ import { Request, Response } from 'express'; import { LiveMatches } from "./LiveMatches"; import { MatchStats } from "./MatchStats"; -const live = async (req: Request, res: Response) => { +const live = async (_req: Request, res: Response) => { try { const liveMatchesObj = new LiveMatches(); const liveMatchesResponse = await liveMatchesObj.getMatches(); @@ -41,7 +41,7 @@ const matchStats = async (req: Request, res: Response) => { } } -const getMatchStats = async (req: Request, res: Response) => { +const getMatchStats = async (_req: Request, res: Response) => { try { const matchStatsObj = new MatchStats(); const matchStatsResponse = await matchStatsObj.getMatchStats("0"); diff --git a/package.json b/package.json index 3887a69..f78bf00 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "scripts": { "dev": "tsc -p . -w & export NODE_ENV=development && nodemon build/index.js", "start": "NODE_ENV=production node build/index.js", - "build": "tsc -p . -w & export NODE_ENV=production", + "build": "tsc -p . & export NODE_ENV=production", "test": "NODE_ENV=test mocha", "test:coverage": "NODE_ENV=test nyc mocha && npm run posttest", "posttest": "istanbul-badges-readme" diff --git a/unitTest/MatchStats.test.ts b/unitTest/MatchStats.test.ts index 5cde01d..0ceccf3 100644 --- a/unitTest/MatchStats.test.ts +++ b/unitTest/MatchStats.test.ts @@ -3,6 +3,7 @@ import { MatchStats } from '../app/src/MatchStats'; import { getTeamData } from '../app/src/MatchStats/MatchUtils'; import { testData } from './TestData/MatchStats'; import { assert } from 'chai'; +import sinon from 'sinon'; const ErrorMessage = 'Test failed due to error:'; const TIMEOUT = 20000; @@ -131,4 +132,29 @@ describe('MatchStats | getTeamData function', function () { const inputString = ''; runTestWithDeepEqual(inputString, {}); }); +}); + +describe('MatchStats | getTournamentName function', function () { + let $; + let matchStatsObj: MatchStats = new MatchStats(); + + beforeEach(() => { + $ = sinon.stub(); + }); + + afterEach(() => { + sinon.restore(); + }); + + it('throws an error if no elements found', async () => { + const { input, expectedOutput } = testData.getTournamentNameErrorHandling; + $.returns(input); + try { + await matchStatsObj.getTournamentName($); + assert.fail('Expected getTournamentName to throw an error'); + } catch (error) { + assert.isTrue($.calledWith('.cb-col.cb-col-100.cb-bg-white')); + assert.equal(error.message, expectedOutput); + } + }); }); \ No newline at end of file diff --git a/unitTest/TestData/MatchStats.ts b/unitTest/TestData/MatchStats.ts index 75cfe73..f358702 100644 --- a/unitTest/TestData/MatchStats.ts +++ b/unitTest/TestData/MatchStats.ts @@ -53,4 +53,10 @@ export const testData = { } } }, + getTournamentNameErrorHandling: { + input: { + length: 0 + }, + expectedOutput: 'Error while fetching tournament name: No elements found with the selector .cb-col.cb-col-100.cb-bg-white' + }, }; \ No newline at end of file