Skip to content

Commit

Permalink
Fix build script and add isLive property to MatchData
Browse files Browse the repository at this point in the history
  • Loading branch information
deveshsangwan committed Jan 28, 2024
1 parent aaa6d46 commit ccbbb8b
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 16 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion app/src/LiveMatches/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions app/src/MatchStats/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions app/src/controller.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions app/ts_src/LiveMatches/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -71,7 +71,7 @@ export class LiveMatches {
const existingMatches: Record<string, MatchData> = {};
const newMatches: Record<string, MatchData> = {};

$('.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');

Expand Down
1 change: 1 addition & 0 deletions app/ts_src/MatchStats/MatchStatsInterfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface ITeamData {

export interface MatchData {
matchId: string;
isLive: boolean;
team1: ITeamData | {};
team2: ITeamData | {};
onBatting: {
Expand Down
9 changes: 5 additions & 4 deletions app/ts_src/MatchStats/index.ts
Original file line number Diff line number Diff line change
@@ -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');

Expand Down Expand Up @@ -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}`);
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion app/ts_src/Utils.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions app/ts_src/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
26 changes: 26 additions & 0 deletions unitTest/MatchStats.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
});
});
6 changes: 6 additions & 0 deletions unitTest/TestData/MatchStats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
},
};

0 comments on commit ccbbb8b

Please sign in to comment.