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

Unit tests live markets (After isV3 flag is removed) #231

Open
wants to merge 32 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
748e6d9
Remove isV3 property
lotinasa Nov 18, 2024
edcecf4
Add unit tests for live markets
lotinasa Nov 20, 2024
36e2801
Merge branch 'isv3-flag-removal' into unit-tests-live-markets
lotinasa Nov 20, 2024
fb5b0e1
Update live markets test without streams
lotinasa Nov 20, 2024
0e97c23
Refactor mocks location in tests
lotinasa Nov 20, 2024
738660a
Add test coverage config
lotinasa Nov 20, 2024
9740add
Add tests for overtime index files and live markets streams
lotinasa Nov 20, 2024
bd8ba7c
Add test for live markets streams
lotinasa Nov 21, 2024
6198c09
Merge branch 'main' into unit-tests-live-markets
lotinasa Nov 21, 2024
4936b42
Merge branch 'fix-live-markets-streams' into unit-tests-live-markets
lotinasa Nov 21, 2024
f5abeb1
Add rounding to live markets odds tests
lotinasa Nov 21, 2024
cbc2ebb
Cover live markets stream locked odds and results
lotinasa Nov 22, 2024
6620bfa
Use mocked live trading utils
lotinasa Nov 22, 2024
041476e
Update test with fix from utils
lotinasa Nov 26, 2024
1c88a89
Add more error tests for live markets
lotinasa Nov 26, 2024
5deec78
Refactor live markets error checks
lotinasa Nov 26, 2024
6440672
Cover tennis with tests for live markets
lotinasa Nov 26, 2024
753f6e2
Add more utils live markets tests
lotinasa Nov 27, 2024
3cd64ee
Cover live markets utils with tests
lotinasa Nov 27, 2024
301e817
Merge branch 'fix-live-markets' into unit-tests-live-markets
lotinasa Nov 28, 2024
cf1ab0b
Merge remote-tracking branch 'origin/main' into unit-tests-live-markets
lotinasa Nov 28, 2024
a7377a2
Fix merge from main
lotinasa Nov 28, 2024
723f2b7
Merge remote-tracking branch 'origin/main' into unit-tests-live-markets
lotinasa Nov 28, 2024
ff3ae16
Merge branch 'fix-live-markets-streams' into unit-tests-live-markets
lotinasa Nov 29, 2024
6e9dff4
Update dockignore
lotinasa Nov 29, 2024
ab451bd
Add tests for optic odds fixtures utils
lotinasa Nov 29, 2024
3978d5e
Merge remote-tracking branch 'origin/main' into unit-tests-live-markets
lotinasa Nov 29, 2024
4331391
Merge branch 'fix-live-markets-streams' into unit-tests-live-markets
lotinasa Nov 29, 2024
27b3a6f
Update redis set command for mock v4
lotinasa Dec 2, 2024
c5286a2
Add tests for optic odds utils fixture odds
lotinasa Dec 3, 2024
6fe147b
Merge remote-tracking branch 'origin/main' into unit-tests-live-markets
lotinasa Dec 3, 2024
8214df3
Merge branch 'main' into unit-tests-live-markets
lotinasa Dec 18, 2024
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
7 changes: 6 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,9 @@ dist
/.idea/*
.vscode
.git
/*/test/

# Test folders
/*/test/
jest
jest.config.js
__mocks__
105 changes: 105 additions & 0 deletions __mocks__/overtime-live-trading-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// MOCKED functions

let __gameContraints = { allow: true, message: "" };
const __mockCheckGameContraints = (gameContraints) => (__gameContraints = gameContraints);
const checkGameContraints = () => __gameContraints;

// NOT MOCKED functions

const teamNamesMatching = (leagueId, marketHomeTeam, marketAwayTeam, apiHomeTeam, apiAwayTeam, teamsMap) =>
jest
.requireActual("overtime-live-trading-utils")
.teamNamesMatching(leagueId, marketHomeTeam, marketAwayTeam, apiHomeTeam, apiAwayTeam, teamsMap);

const gamesDatesMatching = (marketMaturityDate, apiStartDate, sportId, tennisDifferenceEnvVariable) =>
jest
.requireActual("overtime-live-trading-utils")
.gamesDatesMatching(marketMaturityDate, apiStartDate, sportId, tennisDifferenceEnvVariable);

const getLeagueOpticOddsName = (league) =>
jest.requireActual("overtime-live-trading-utils").getLeagueOpticOddsName(league);

const getBookmakersArray = (bookmakersData, sportId, backupLiveOddsProviders) =>
jest
.requireActual("overtime-live-trading-utils")
.getBookmakersArray(bookmakersData, sportId, backupLiveOddsProviders);

const getLiveSupportedLeagues = (leagueInfoArray) =>
jest.requireActual("overtime-live-trading-utils").getLiveSupportedLeagues(leagueInfoArray);

const getBetTypesForLeague = (league, leagueInfoArray) =>
jest.requireActual("overtime-live-trading-utils").getBetTypesForLeague(league, leagueInfoArray);

const getLeagueSport = (league) => jest.requireActual("overtime-live-trading-utils").getLeagueSport(league);

const getLeagueLabel = (league) => jest.requireActual("overtime-live-trading-utils").getLeagueLabel(league);

const getLeagueProvider = (league) => jest.requireActual("overtime-live-trading-utils").getLeagueProvider(league);

const getLeagueIsDrawAvailable = (league) =>
jest.requireActual("overtime-live-trading-utils").getLeagueIsDrawAvailable(league);

const fetchResultInCurrentSet = (currentSet, opticOddsScoresApiResponse) =>
jest.requireActual("overtime-live-trading-utils").fetchResultInCurrentSet(currentSet, opticOddsScoresApiResponse);

const League = jest.requireActual("overtime-live-trading-utils").League;
const Sport = jest.requireActual("overtime-live-trading-utils").Sport;
const MoneylineTypes = jest.requireActual("overtime-live-trading-utils").MoneylineTypes;
const Provider = jest.requireActual("overtime-live-trading-utils").Provider;
const PeriodType = jest.requireActual("overtime-live-trading-utils").PeriodType;
const TotalTypes = jest.requireActual("overtime-live-trading-utils").TotalTypes;

const UFC_LEAGUE_IDS = jest.requireActual("overtime-live-trading-utils").UFC_LEAGUE_IDS;
const LeagueMap = jest.requireActual("overtime-live-trading-utils").LeagueMap;
const LeagueIdMapEnetpulse = jest.requireActual("overtime-live-trading-utils").LeagueIdMapEnetpulse;
const LeagueIdMapOpticOdds = jest.requireActual("overtime-live-trading-utils").LeagueIdMapOpticOdds;

const processMarket = (
market,
apiResponseWithOdds,
liveOddsProviders,
spreadData,
isDrawAvailable,
defaultSpreadForLiveMarkets,
maxPercentageDiffBetwenOdds,
leagueMap,
) =>
jest
.requireActual("overtime-live-trading-utils")
.processMarket(
market,
apiResponseWithOdds,
liveOddsProviders,
spreadData,
isDrawAvailable,
defaultSpreadForLiveMarkets,
maxPercentageDiffBetwenOdds,
leagueMap,
);

module.exports = {
checkGameContraints,
__mockCheckGameContraints,
teamNamesMatching,
gamesDatesMatching,
getLeagueOpticOddsName,
getBookmakersArray,
getLiveSupportedLeagues,
getBetTypesForLeague,
getLeagueSport,
getLeagueLabel,
getLeagueProvider,
getLeagueIsDrawAvailable,
fetchResultInCurrentSet,
processMarket,
League,
Sport,
MoneylineTypes,
Provider,
PeriodType,
TotalTypes,
UFC_LEAGUE_IDS,
LeagueMap,
LeagueIdMapEnetpulse,
LeagueIdMapOpticOdds,
};
3 changes: 3 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import globals from "globals";
export default [
{
files: ["**/*.js"],
env: {
jest: true,
},
languageOptions: { sourceType: "commonjs" },
rules: {
"prefer-const": "error",
Expand Down
7 changes: 6 additions & 1 deletion help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,9 @@ docker-compose up -d overtime-v2-api-testnet
- Access SERVER
- Run: docker ps
- Copy "CONTAINER ID" of redis:alpine
- Run: docker exec -it "CONTAINER ID" redis-cli
- Run: docker exec -it "CONTAINER ID" redis-cli

### RUN TESTS ###
npm test
npm test -- liveMarkets.test.js
npm test -- --testNamePattern 'checks number of live markets' --no-silent
7 changes: 7 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
verbose: true,
setupFilesAfterEnv: ["./jest/jestInitialSetup.js"],
collectCoverage: true,
collectCoverageFrom: ["overtimeV2Api/**/*.js"],
coveragePathIgnorePatterns: ["contracts", "test"],
};
2 changes: 2 additions & 0 deletions jest/jestInitialSetup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
jest.mock("redis", () => jest.requireActual("./redisV4Mock"));
// jest.mock("redis", () => jest.requireActual("redis-mock")); // redis V4 is not supported by redis-mock
31 changes: 31 additions & 0 deletions jest/redisV4Mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// redis-mock has not been updated for node-redis v4 yet, but the main changes
// in the API are camelCase names and promises instead of callback, so we can work around it.
// https://github.com/yeahoffline/redis-mock/issues/195

const redis = require("redis-mock");
const { promisify } = require("util");

const client = redis.createClient();
const set = promisify(client.set).bind(client);
const setEx = promisify(client.setex).bind(client);

const v4Client = {
isOpen: false,
connect: () => (v4Client.isOpen = true),
get: promisify(client.get).bind(client),
set: (key, value) => set(key, value),
del: promisify(client.del).bind(client),
hSet: promisify(client.hset).bind(client),
hGet: promisify(client.hget).bind(client),
hDel: promisify(client.hdel).bind(client),
flushAll: promisify(client.flushall).bind(client),
setEx: promisify(client.setex).bind(client),
expire: promisify(client.expire).bind(client),
mGet: promisify(client.mget).bind(client),
mSet: promisify(client.mset).bind(client),
pSetEx: (key, ms, value) => setEx(key, ms / 1000, value),
on: () => v4Client,
// Add additional functions as needed...
};

module.exports = { ...redis, createClient: () => v4Client };
1 change: 0 additions & 1 deletion overtimeV2Api/constants/markets.js
Original file line number Diff line number Diff line change
Expand Up @@ -1637,7 +1637,6 @@ const PARENT_MARKET_PROPERTIES_TO_EXCLUDE = [
"isPlayerPropsMarket",
"isOneSidePlayerPropsMarket",
"isYesNoPlayerPropsMarket",
"isV3",
];

const CHILD_MARKET_PROPERTIES_TO_EXCLUDE = [
Expand Down
9 changes: 4 additions & 5 deletions overtimeV2Api/source/liveMarkets.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ async function processLiveMarkets() {
const startTime = new Date().getTime();
logAllInfo(`Live markets ${network}: process live markets`);

await processAllMarkets(
// call function from the same file with module.exports for unit tests mocking purpose
await module.exports.processAllMarkets(
oddsStreamsInfoByLeagueMap,
oddsInitializedByLeagueMap,
resultsInitializedByLeagueMap,
Expand Down Expand Up @@ -110,9 +111,7 @@ async function processAllMarkets(
const openMarketsMap = new Map(JSON.parse(openMarkets));

const supportedLiveMarkets = Array.from(openMarketsMap.values())
.filter(
(market) => !!market.isV3 && market.statusCode === "ongoing" && !market.isWholeGameResolved && !market.noTickets,
)
.filter((market) => market.statusCode === "ongoing" && !market.isWholeGameResolved && !market.noTickets)
.filter((market) => supportedLiveLeagueIds.includes(market.leagueId));
const uniqueLiveLeagueIds = uniq(supportedLiveMarkets.map((market) => market.leagueId));

Expand Down Expand Up @@ -450,7 +449,6 @@ async function processMarketsByLeague(
market.proof = [];
market.homeScoreByPeriod = gamesHomeScoreByPeriod;
market.awayScoreByPeriod = gamesAwayScoreByPeriod;
market.isV3 = true;

if (!errorMessage) {
const processedMarket = processMarket(
Expand Down Expand Up @@ -503,4 +501,5 @@ async function processMarketsByLeague(

module.exports = {
processLiveMarkets,
processAllMarkets,
};
2 changes: 1 addition & 1 deletion overtimeV2Api/source/liveScores.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ async function processAllLiveResults(resultsInitialization, isTestnet) {
const leagueProvider = getLeagueProvider(leagueId);
const gameInfo = gamesInfoMap.get(market.gameId);

if (leagueProvider === Provider.OPTICODDS && market.isV3) {
if (leagueProvider === Provider.OPTICODDS) {
opticOddsGameIdsWithLeagueID.push({ gameId: market.gameId, leagueId, gameInfo });
}
}
Expand Down
4 changes: 2 additions & 2 deletions overtimeV2Api/source/markets.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,10 @@ const loadMarkets = async (isTestnet, shouldDoFullUpdate) => {
};

const mapMarket = (market) => {
const packedMarket = packMarket(market, false);
const packedMarket = packMarket(market);
packedMarket.childMarkets = [];
market.childMarkets.forEach((childMarket) => {
const packedChildMarket = packMarket(childMarket, true);
const packedChildMarket = packMarket(childMarket);
packedMarket.childMarkets.push(packedChildMarket);
});

Expand Down
7 changes: 7 additions & 0 deletions overtimeV2Api/test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
describe("Check OvertimeV2 initialization", () => {
it("checks redis client is connected", async () => {
require("../index");
const { redisClient } = require("../../redis/client");
expect(redisClient.isOpen).toBe(true);
});
});
7 changes: 7 additions & 0 deletions overtimeV2Api/test/indexTestnet.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
describe("Check OvertimeV2 testnet initialization", () => {
it("checks redis client is connected", async () => {
require("../indexTestnet");
const { redisClient } = require("../../redis/client");
expect(redisClient.isOpen).toBe(true);
});
});
97 changes: 0 additions & 97 deletions overtimeV2Api/test/liveMarkets/checkConstraints.test.js

This file was deleted.

Loading