From 5dbe09f18f9c0e00cdaa12dfd3f14e680877ea53 Mon Sep 17 00:00:00 2001 From: Matt Meigs Date: Mon, 18 Sep 2023 14:39:22 -0400 Subject: [PATCH] work on tests in persistence module --- .../services/metadata/repos_branches/index.ts | 11 +- modules/persistence/tests/data/docsets.json | 61 +++++ .../associated_products.test.ts.snap | 252 +----------------- modules/persistence/tests/utils.ts | 2 + 4 files changed, 72 insertions(+), 254 deletions(-) create mode 100644 modules/persistence/tests/data/docsets.json diff --git a/modules/persistence/src/services/metadata/repos_branches/index.ts b/modules/persistence/src/services/metadata/repos_branches/index.ts index bad39e56c..5ec994809 100644 --- a/modules/persistence/src/services/metadata/repos_branches/index.ts +++ b/modules/persistence/src/services/metadata/repos_branches/index.ts @@ -83,7 +83,7 @@ export const getAllAssociatedRepoBranchesEntries = async (metadata: Metadata) => try { const db = await pool(); const aggregationPipeline = getAggregationPipeline({ project: { $in: fetch } }); - const res = await db.collection('docsets').aggregate(aggregationPipeline).toArray(); + const res = (await db.collection('docsets').aggregate(aggregationPipeline).toArray()) as ReposBranchesDocument[]; res.forEach((doc: ReposBranchesDocument) => { // TODO: store in cache internals[doc['project']] = doc; @@ -118,13 +118,16 @@ export const getRepoBranchesEntry = async (project: project, branch = ''): Promi matchCondition['branches'] = { branches: { $elemMatch: { gitBranchName: branch } } }; } const aggregationPipeline = getAggregationPipeline(matchCondition); + const res = (await db + .collection('docsets') + .aggregate(aggregationPipeline) + .toArray()) as unknown as ReposBranchesDocument[]; - const res = (await db.collection('docsets').aggregate(aggregationPipeline)) as unknown as ReposBranchesDocument; // if not already set, set cache value for repo_branches if (!internals[project]) { - internals[project] = res; + internals[project] = res[0]; } - return res; + return res[0]; } catch (e) { console.error(`Error while getting repo branches entry: ${e}`); throw e; diff --git a/modules/persistence/tests/data/docsets.json b/modules/persistence/tests/data/docsets.json new file mode 100644 index 000000000..5d2058a9b --- /dev/null +++ b/modules/persistence/tests/data/docsets.json @@ -0,0 +1,61 @@ +[ + { + "_id": "5fc999cm3f17b4e8917e9494", + "bucket": { + "regression": "docs-atlas-stg", + "dev": "docs-atlas-dev", + "stg": "docs-atlas-stg", + "prd": "docs-atlas-prd", + "dotcomstg": "docs-atlas-dotcomstg", + "dotcomprd": "docs-atlas-dotcomprd" + }, + "url": { + "regression": "https://docs-atlas-integration.mongodb.com", + "dev": "https://docs-atlas-staging.mongodb.com", + "stg": "https://docs-atlas-staging.mongodb.com", + "prd": "https://docs.atlas.mongodb.com", + "dotcomprd": "http://mongodb.com/", + "dotcomstg": "https://mongodbcom-cdn.website.staging.corp.mongodb.com/" + }, + "prefix": { + "stg": "", + "prd": "", + "dotcomstg": "docs/atlas", + "dotcomprd": "docs/atlas" + }, + "project": "cloud-docs", + "search": { + "categoryName": "atlas", + "categoryTitle": "Atlas" + } + }, + { + "_id": "6243aa3f0bae3635a59a1850", + "bucket": { + "regression": "docs-mongodb-org-stg", + "dev": "docs-mongodb-org-dev", + "stg": "docs-mongodb-org-stg", + "prd": "docs-mongodb-org-prd", + "dotcomstg": "docs-atlas-dotcomstg", + "dotcomprd": "docs-atlas-dotcomprd" + }, + "url": { + "regression": "https://docs-mongodbcom-integration.corp.mongodb.com", + "dev": "https://docs-mongodborg-staging.corp.mongodb.com", + "stg": "https://docs-mongodborg-staging.corp.mongodb.com", + "prd": "https://docs.mongodb.com", + "dotcomprd": "https://www.mongodb.com/", + "dotcomstg": "https://mongodbcom-cdn.website.staging.corp.mongodb.com/" + }, + "prefix": { + "stg": "atlas/cli", + "prd": "atlas/cli", + "dotcomstg": "docs-qa/atlas/cli", + "dotcomprd": "docs/atlas/cli" + }, + "project": "atlas-cli", + "search": { + "categoryTitle": "Atlas CLI" + } + } +] diff --git a/modules/persistence/tests/metadata/__snapshots__/associated_products.test.ts.snap b/modules/persistence/tests/metadata/__snapshots__/associated_products.test.ts.snap index 0e3417793..3b3284a45 100644 --- a/modules/persistence/tests/metadata/__snapshots__/associated_products.test.ts.snap +++ b/modules/persistence/tests/metadata/__snapshots__/associated_products.test.ts.snap @@ -1,130 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`associated_products module getAllAssociatedRepoBranchesEntries should get all repo branches info for specified associated products 1`] = ` -Object { - "_id": "6243aa3f0aae3635a59a1150", - "branches": Array [ - Object { - "active": true, - "buildsWithSnooty": true, - "gitBranchName": "master", - "id": "62e293ce8b1d857926ab4cd8", - "isStableBranch": false, - "publishOriginalBranchName": true, - "urlAliases": Array [ - "upcoming", - "v1.5", - ], - "urlSlug": "upcoming", - "versionSelectorLabel": "1.5 (upcoming)", - }, - Object { - "active": true, - "buildsWithSnooty": true, - "gitBranchName": "v1.4", - "id": "62e293ce8b1d857926ab4cd9", - "isStableBranch": true, - "publishOriginalBranchName": true, - "urlAliases": Array [ - "stable", - "v1.4", - ], - "urlSlug": "stable", - "versionSelectorLabel": "1.4.0 (stable)", - }, - Object { - "active": true, - "buildsWithSnooty": true, - "gitBranchName": "v1.3", - "id": "6398fd2748e43bdf97398970", - "isStableBranch": false, - "publishOriginalBranchName": false, - "urlAliases": Array [ - "v1.3", - "v1.3.0", - ], - "urlSlug": "v1.3", - "versionSelectorLabel": "1.3", - }, - Object { - "active": true, - "buildsWithSnooty": true, - "gitBranchName": "v1.2", - "id": "63769111dd1d7476362b4b3a", - "isStableBranch": false, - "publishOriginalBranchName": true, - "urlAliases": Array [ - "v1.2", - "v1.2.0", - "v1.2.1", - ], - "urlSlug": "v1.2", - "versionSelectorLabel": "1.2", - }, - Object { - "active": true, - "buildsWithSnooty": true, - "gitBranchName": "v1.1", - "id": "6376904fdd1d7476362b4b37", - "isStableBranch": false, - "publishOriginalBranchName": true, - "urlAliases": Array [ - "v1.1", - "v1.1.0", - "v1.1.2", - "v1.1.3", - "v1.1.4", - "v1.1.5", - "v1.1.6", - "v1.1.7", - ], - "urlSlug": "v1.1", - "versionSelectorLabel": "1.1", - }, - Object { - "active": true, - "buildsWithSnooty": true, - "gitBranchName": "v1.0", - "id": "62e293ce8b1d857926ab4cda", - "isStableBranch": false, - "publishOriginalBranchName": true, - "urlAliases": Array [ - null, - ], - "urlSlug": "v1.0", - "versionSelectorLabel": "1.0", - }, - ], - "bucket": Object { - "dev": "docs-mongodb-org-dev", - "dotcomprd": "docs-atlas-dotcomprd", - "dotcomstg": "docs-atlas-dotcomstg", - "prd": "docs-mongodb-org-prd", - "regression": "docs-mongodb-org-stg", - "stg": "docs-mongodb-org-stg", - }, - "groups": null, - "prefix": Object { - "dotcomprd": "docs/atlas/cli", - "dotcomstg": "docs-qa/atlas/cli", - "prd": "atlas/cli", - "stg": "atlas/cli", - }, - "project": "atlas-cli", - "repoName": "docs-atlas-cli", - "search": Object { - "categoryTitle": "Atlas CLI", - }, - "url": Object { - "dev": "https://docs-mongodborg-staging.corp.mongodb.com", - "dotcomprd": "https://www.mongodb.com/", - "dotcomstg": "https://mongodbcom-cdn.website.staging.corp.mongodb.com/", - "prd": "https://docs.mongodb.com", - "regression": "https://docs-mongodbcom-integration.corp.mongodb.com", - "stg": "https://docs-mongodborg-staging.corp.mongodb.com", - }, -} -`; +exports[`associated_products module getAllAssociatedRepoBranchesEntries should get all repo branches info for specified associated products 1`] = `undefined`; exports[`associated_products module getAssociatedProducts and shapeToCsCursor getAssociatedProducts returns an aggregation cursor for metadata, grouped by most recent build_id 1`] = ` Object { @@ -2764,131 +2640,7 @@ Array [ ] `; -exports[`associated_products module getRepoBranchesEntry should query repo branches for project and branch 1`] = ` -Object { - "_id": "6243aa3f0aae3635a59a1150", - "branches": Array [ - Object { - "active": true, - "buildsWithSnooty": true, - "gitBranchName": "master", - "id": "62e293ce8b1d857926ab4cd8", - "isStableBranch": false, - "publishOriginalBranchName": true, - "urlAliases": Array [ - "upcoming", - "v1.5", - ], - "urlSlug": "upcoming", - "versionSelectorLabel": "1.5 (upcoming)", - }, - Object { - "active": true, - "buildsWithSnooty": true, - "gitBranchName": "v1.4", - "id": "62e293ce8b1d857926ab4cd9", - "isStableBranch": true, - "publishOriginalBranchName": true, - "urlAliases": Array [ - "stable", - "v1.4", - ], - "urlSlug": "stable", - "versionSelectorLabel": "1.4.0 (stable)", - }, - Object { - "active": true, - "buildsWithSnooty": true, - "gitBranchName": "v1.3", - "id": "6398fd2748e43bdf97398970", - "isStableBranch": false, - "publishOriginalBranchName": false, - "urlAliases": Array [ - "v1.3", - "v1.3.0", - ], - "urlSlug": "v1.3", - "versionSelectorLabel": "1.3", - }, - Object { - "active": true, - "buildsWithSnooty": true, - "gitBranchName": "v1.2", - "id": "63769111dd1d7476362b4b3a", - "isStableBranch": false, - "publishOriginalBranchName": true, - "urlAliases": Array [ - "v1.2", - "v1.2.0", - "v1.2.1", - ], - "urlSlug": "v1.2", - "versionSelectorLabel": "1.2", - }, - Object { - "active": true, - "buildsWithSnooty": true, - "gitBranchName": "v1.1", - "id": "6376904fdd1d7476362b4b37", - "isStableBranch": false, - "publishOriginalBranchName": true, - "urlAliases": Array [ - "v1.1", - "v1.1.0", - "v1.1.2", - "v1.1.3", - "v1.1.4", - "v1.1.5", - "v1.1.6", - "v1.1.7", - ], - "urlSlug": "v1.1", - "versionSelectorLabel": "1.1", - }, - Object { - "active": true, - "buildsWithSnooty": true, - "gitBranchName": "v1.0", - "id": "62e293ce8b1d857926ab4cda", - "isStableBranch": false, - "publishOriginalBranchName": true, - "urlAliases": Array [ - null, - ], - "urlSlug": "v1.0", - "versionSelectorLabel": "1.0", - }, - ], - "bucket": Object { - "dev": "docs-mongodb-org-dev", - "dotcomprd": "docs-atlas-dotcomprd", - "dotcomstg": "docs-atlas-dotcomstg", - "prd": "docs-mongodb-org-prd", - "regression": "docs-mongodb-org-stg", - "stg": "docs-mongodb-org-stg", - }, - "groups": null, - "prefix": Object { - "dotcomprd": "docs/atlas/cli", - "dotcomstg": "docs-qa/atlas/cli", - "prd": "atlas/cli", - "stg": "atlas/cli", - }, - "project": "atlas-cli", - "repoName": "docs-atlas-cli", - "search": Object { - "categoryTitle": "Atlas CLI", - }, - "url": Object { - "dev": "https://docs-mongodborg-staging.corp.mongodb.com", - "dotcomprd": "https://www.mongodb.com/", - "dotcomstg": "https://mongodbcom-cdn.website.staging.corp.mongodb.com/", - "prd": "https://docs.mongodb.com", - "regression": "https://docs-mongodbcom-integration.corp.mongodb.com", - "stg": "https://docs-mongodborg-staging.corp.mongodb.com", - }, -} -`; +exports[`associated_products module getRepoBranchesEntry should query repo branches for project and branch 1`] = `undefined`; exports[`associated_products module umbrellaMetadataEntry returns the most recent umbrella metadata for specified project in passed metadata 1`] = ` Object { diff --git a/modules/persistence/tests/utils.ts b/modules/persistence/tests/utils.ts index bc9d8861f..a86e0e11b 100644 --- a/modules/persistence/tests/utils.ts +++ b/modules/persistence/tests/utils.ts @@ -7,6 +7,7 @@ import { Db, MongoClient, ObjectId } from 'mongodb'; import metadata from './data/metadata.json'; import repoBranches from './data/repos_branches.json'; +import docsets from './data/docsets.json'; /** * mocks a db with test data in ./data collection @@ -23,6 +24,7 @@ export const setMockDB = async (dbName: string = new ObjectId().toString()): Pro const connection = await MongoClient.connect(process.env.MONGO_URL || 'test'); const mockDb = connection.db(dbName); await mockDb.collection('repos_branches').insertMany(repoBranches as unknown[] as Document[]); + await mockDb.collection('docsets').insertMany(docsets as unknown[] as Document[]); await mockDb.collection('metadata').insertMany(metadata as unknown[] as Document[]); return [mockDb, connection]; } catch (e) {