Skip to content
This repository has been archived by the owner on Aug 15, 2023. It is now read-only.

Commit

Permalink
fix(yt:shared): defined metadata filters based on nature type
Browse files Browse the repository at this point in the history
  • Loading branch information
ascariandrea committed Nov 22, 2022
1 parent 5d33bb8 commit 73759f3
Show file tree
Hide file tree
Showing 11 changed files with 240 additions and 78 deletions.
13 changes: 10 additions & 3 deletions platforms/storybook/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,21 @@
"moduleResolution": "node",
"isolatedModules": true,
"paths": {
"@shared/*": [
"../../../packages/shared/src/*"
]
"@shared/*": ["../../../packages/shared/src/*"]
}
},
"references": [
{
"path": "../../packages/shared"
},
{
"path": "../../packages/taboule"
},
{
"path": "../yttrex/shared"
},
{
"path": "../tktrex/shared"
}
],
"include": ["./src"],
Expand Down
2 changes: 1 addition & 1 deletion platforms/yttrex/backend/lib/automo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,7 @@ async function registerSteps(steps: Step[]): Promise<any> {
status: 'exist',
experimentId: exist.experimentId,
since: exist.when,
steps: [],
steps: exist.steps,
};
}

Expand Down
60 changes: 30 additions & 30 deletions platforms/yttrex/backend/routes/__tests__/ads.e2e.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/* eslint-disable import/first */
// mock curly module
jest.mock("../../lib/curly");
jest.mock("fetch-opengraph");

import { VideoArb } from "@shared/arbitraries/Video.arb";
import { AdArb } from "../../tests/arbitraries/Ad.arb";
import { VideoMetadataArb } from "../../tests/arbitraries/Metadata.arb";
import { fc } from "@shared/test";
import { v4 as uuid } from "uuid";
import { GetTest, Test } from "../../tests/Test";
import { sub } from "date-fns";

describe("The ADS API", () => {
jest.mock('../../lib/curly');
jest.mock('fetch-opengraph');

import { VideoArb } from '@shared/arbitraries/Video.arb';
import { AdArb } from '../../tests/arbitraries/Ad.arb';
import { VideoMetadataArb } from '@yttrex/shared/arbitraries/Metadata.arb';
import { fc } from '@shared/test';
import { v4 as uuid } from 'uuid';
import { GetTest, Test } from '../../tests/Test';
import { sub } from 'date-fns';

describe('The ADS API', () => {
const channelId = uuid();
let test: Test;

Expand All @@ -23,8 +23,8 @@ describe("The ADS API", () => {
await test.mongo.close();
});

describe("Get by channel", () => {
it("fails using channelId without date range", async () => {
describe('Get by channel', () => {
it('fails using channelId without date range', async () => {
const { body } = await test.app
.get(`/api/v2/ad/channel/${channelId}`)
.query({})
Expand All @@ -41,7 +41,7 @@ describe("The ADS API", () => {
const { body } = await test.app
.get(`/api/v2/ad/channel/${channelId}`)
.query({
till: "2022-01-01",
till: '2022-01-01',
})
.expect(500);

Expand All @@ -56,7 +56,7 @@ describe("The ADS API", () => {
const { body } = await test.app
.get(`/api/v2/ad/channel/${channelId}`)
.query({
since: "2022-01-01",
since: '2022-01-01',
})
.expect(500);

Expand All @@ -67,47 +67,47 @@ describe("The ADS API", () => {
});
});

it("succeeds using channelId", async () => {
it('succeeds using channelId', async () => {
const { body } = await test.app
.get(`/api/v2/ad/channel/${channelId}`)
.query({
since: "2020-01-01",
till: "2021-01-01",
since: '2020-01-01',
till: '2021-01-01',
})
.expect(200);

expect(body).toEqual([]);
});
});

describe("Get by video", () => {
describe('Get by video', () => {
const video = fc.sample(VideoArb, 1)[0];

it("fails using wrong videoId", async () => {
it('fails using wrong videoId', async () => {
const { body } = await test.app
.get(`/api/v2/ad/video/fake-id`)
.query({
since: "2020-01-01",
till: "2021-01-01",
since: '2020-01-01',
till: '2021-01-01',
})
.expect(500);

expect(body).toEqual({});
});

it("fails using videoId", async () => {
it('fails using videoId', async () => {
const { body } = await test.app
.get(`/api/v2/ad/video/${video.videoId}`)
.query({
since: "2020-01-01",
till: "2021-01-01",
since: '2020-01-01',
till: '2021-01-01',
})
.expect(200);

expect(body).toEqual([]);
});

it("succeeds using videoId", async () => {
it('succeeds using videoId', async () => {
const videoId = fc.sample(fc.uuid(), 1)[0];
const [metadata] = fc.sample(VideoMetadataArb, 1).map((meta) => ({
...meta,
Expand All @@ -124,7 +124,7 @@ describe("The ADS API", () => {
: ad.sponsoredSite.concat(`/`),
metadataId: metadata.id,
nature: {
type: "video",
type: 'video',
videoId,
},
savingTime: sub(new Date(), { weeks: 3 }),
Expand All @@ -133,13 +133,13 @@ describe("The ADS API", () => {
// insert video
await test.mongo3.insertMany(
test.mongo,
test.config.get("schema").metadata,
test.config.get('schema').metadata,
[metadata]
);
// insert ads
await test.mongo3.insertMany(
test.mongo,
test.config.get("schema").ads,
test.config.get('schema').ads,
ads
);

Expand Down
78 changes: 76 additions & 2 deletions platforms/yttrex/backend/routes/__tests__/metadata.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ jest.mock('fetch-opengraph');
import { fc } from '@shared/test';
import moment from 'moment';
import _ from 'lodash';
import { VideoMetadataArb } from '../../tests/arbitraries/Metadata.arb';
import {
HomeMetadataArb,
VideoMetadataArb,
} from '@yttrex/shared/arbitraries/Metadata.arb';
import { GetTest, Test } from '../../tests/Test';
import * as utils from '../../lib/utils';

Expand Down Expand Up @@ -55,7 +58,7 @@ describe('Metadata API', () => {

expect(body).toMatchObject({
name: 'APIError',
message: 'Request validation failed.'
message: 'Request validation failed.',
});
});

Expand Down Expand Up @@ -117,6 +120,77 @@ describe('Metadata API', () => {
);
});

it('succeeds with "video" metadata when filtering for "authorName"', async () => {
const total = 100;
const amount = 10;
const authorName = 'trex';

const videoMetadata = fc
.sample(VideoMetadataArb, total / 2)
.map((m, i) => ({
...m,
authorName: i % 2 === 0 ? authorName : m.authorName,
savingTime: new Date(),
}));

const homeMetadata = fc.sample(HomeMetadataArb, total / 2).map((m) => ({
...m,
savingTime: new Date(),
}));

const metadata = [...videoMetadata, ...homeMetadata];

await test.mongo3.insertMany(
test.mongo,
test.config.get('schema').metadata,
metadata
);

const expectedMetadata = videoMetadata
.filter(
(a, i) =>
a.savingTime.getTime() >
new Date(moment().startOf('day').toISOString()).getTime() &&
i % 2 === 0
)
.sort((a, b) => b.savingTime.getTime() - a.savingTime.getTime())
.slice(0, amount)
.map(({ publicKey, _id, experimentId, researchTag, ...m }: any) => {
return {
...m,
id: m.id.substring(0, 20),
blang: m.blang ?? null,
supporter: utils.string2Food(publicKey),
clientTime: m.clientTime.toISOString(),
publicationTime: m.publicationTime.toISOString(),
savingTime: m.savingTime.toISOString(),
related: m.related.map(toRelated),
};
});

const { body } = await test.app
.get(`/api/v2/metadata`)
.query({
nature: 'video',
authorName,
amount,
})
.expect(200);

expect(body.data.length).toBe(expectedMetadata.length);
expect(body.data).toMatchObject(expectedMetadata);

await test.mongo3.deleteMany(
test.mongo,
test.config.get('schema').metadata,
{
id: {
$in: metadata.map((m) => m.id),
},
}
);
});

it('succeeds with metadata', async () => {
const total = 100;
const experimentId = fc.sample(fc.uuid(), 1)[0];
Expand Down
27 changes: 23 additions & 4 deletions platforms/yttrex/backend/routes/__tests__/public.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,40 @@ jest.mock('fetch-opengraph');
import { fc } from '@shared/test';
import _ from 'lodash';
import { v4 as uuid } from 'uuid';
import {
ParsedInfoArb,
VideoMetadataArb,
} from '../../tests/arbitraries/Metadata.arb';
import { HomeMetadataArb } from '@yttrex/shared/arbitraries/Metadata.arb';
import { GetTest, Test } from '../../tests/Test';
import { HomeMetadataDB } from '../../models/metadata/HomeMetadata';

describe('The Public API', () => {
const channelId = uuid();
let test: Test;
let homes: HomeMetadataDB[];
const publicKey = 'public-key';

beforeAll(async () => {
test = await GetTest();
homes = fc.sample(HomeMetadataArb, 10).map((h) => ({
...h,
publicKey,
savingTime: new Date(),
selected: h.selected as any[],
}));

await test.mongo3.insertMany(
test.mongo,
test.config.get('schema').metadata,
homes
);
});

afterAll(async () => {
await test.mongo3.deleteMany(
test.mongo,
test.config.get('schema').metadata,
{
publicKey: { $eq: publicKey },
}
);
await test.mongo.close();
});

Expand Down
Loading

0 comments on commit 73759f3

Please sign in to comment.