Skip to content

Commit

Permalink
Migrate Copora and Projects + Omit projectId (#811)
Browse files Browse the repository at this point in the history
* fix(migration): add languages to corpora and types to projects

* feat(examples): omit projectId from returned examples

* chore: remove typo in integration file

* test: fix tests

* test: fix all test blocks
  • Loading branch information
ijemmao authored Oct 5, 2024
1 parent 2e4d1e8 commit 0a6c2fe
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
with:
node-version: ${{ matrix.node-version }}
- name: Start MongoDB
uses: supercharge/mongodbw[email protected]
uses: supercharge/mongodb[email protected]
with:
mongodb-version: ${{ matrix.mongodb-version }}
- name: Install Project Dependencies
Expand Down
25 changes: 25 additions & 0 deletions migrations/20240928023848-add-languages-to-corpus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module.exports = {
async up(db) {
const collections = ['corpus', 'corpussuggestions'];
return collections.map((collection) =>
db.collection(collection).updateMany(
{},
{
$set: { languages: [] },
}
)
);
},

async down(db) {
const collections = ['corpus', 'corpussuggestions'];
return collections.map((collection) =>
db.collection(collection).updateMany(
{},
{
$unset: { languages: null },
}
)
);
},
};
25 changes: 25 additions & 0 deletions migrations/20240928024053-add-types-to-projecs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module.exports = {
async up(db) {
const collections = ['projects'];
return collections.map((collection) =>
db.collection(collection).updateMany(
{},
{
$set: { types: ['TEXT_AUDIO_ANNOTATION'] },
}
)
);
},

async down(db) {
const collections = ['projects'];
return collections.map((collection) =>
db.collection(collection).updateMany(
{},
{
$unset: { types: null },
}
)
);
},
};
18 changes: 9 additions & 9 deletions src/controllers/utils/buildDocs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,19 @@ const removeKeysInNestedDoc = <T>(docs: T[], nestedDocsKey: keyof T) => {

const cleanExamples = ({ examples, version }: { examples: IncomingExample[], version: Version }) =>
examples.map((example) => {
const cleanedExample: Omit<IncomingExample, 'source' | 'translations'> & {
igbo: string,
english: string,
pronunciation?: string,
pronunciations?: string[],
} = omit(
const cleanedExample = omit(
assign({
...example,
igbo: '',
english: '',
}),
['source', 'translations']
);
['source', 'translations', 'projectId']
) as Omit<IncomingExample, 'source' | 'translations' | 'projectId'> & {
igbo: string,
english: string,
pronunciation?: string,
pronunciations?: string[],
};
if (version === Version.VERSION_1) {
cleanedExample.pronunciation = example.source.pronunciations?.[0]?.audio || '';
} else {
Expand Down Expand Up @@ -141,7 +141,7 @@ export const findWordsWithMatch = async ({
(word) => {
const updatedWord = assign(word);
// @ts-expect-error different versions
updatedWord.examples = cleanExamples({ examples: updatedWord.examples, version });
updatedWord.examples = cleanExamples({ examples: updatedWord.examples || [], version });
return updatedWord;
}
);
Expand Down
20 changes: 10 additions & 10 deletions src/middleware/helpers/__tests__/authorizeDeveloperUsage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jest.mock('../createDeveloperUsage');

describe('authorizeDeveloperUsage', () => {
it("authorizes the current developer's usage", async () => {
const route = 'speech-to-text';
const path = 'speech-to-text';
const developer = developerFixture({});
const developerUsage = developerUsageFixture({});
// @ts-expect-error mockReturnValue
Expand All @@ -25,12 +25,12 @@ describe('authorizeDeveloperUsage', () => {
});

// @ts-expect-error developer
const res = await authorizeDeveloperUsage({ route, developer });
const res = await authorizeDeveloperUsage({ path, developer });
expect(res).toEqual(developerUsage);
});

it("updates the current developer's usage", async () => {
const route = 'speech-to-text';
const path = 'speech-to-text';
const developer = developerFixture({});
const developerUsage = developerUsageFixture({});
const developerUsageDocument = {
Expand All @@ -42,12 +42,12 @@ describe('authorizeDeveloperUsage', () => {
findDeveloperUsage.mockReturnValue(developerUsageDocument);

// @ts-expect-error developer
await authorizeDeveloperUsage({ route, developer });
await authorizeDeveloperUsage({ path, developer });
expect(developerUsageDocument.usage.count).toEqual(1);
});

it('creates a fallback developer usage if none exist exclusively for Igbo API', async () => {
const route = 'igbo_api';
const path = 'igbo_api';
const developer = developerFixture({ id: documentId });
const developerUsage = developerUsageFixture({});
const developerUsageDocument = {
Expand All @@ -60,24 +60,24 @@ describe('authorizeDeveloperUsage', () => {
// @ts-expect-error mockReturnValue
createDeveloperUsage.mockReturnValue(developerUsageDocument);
// @ts-expect-error developer
await authorizeDeveloperUsage({ route, developer });
await authorizeDeveloperUsage({ path, developer });
expect(createDeveloperUsage).toHaveBeenCalled();
});

it('throws error unable finding developer usage', async () => {
const route = 'speech-to-text';
const path = 'speech-to-text';
const developer = developerFixture({});
// @ts-expect-error mockReturnValue
findDeveloperUsage.mockReturnValue(undefined);

// @ts-expect-error developer
authorizeDeveloperUsage({ route, developer }).catch((err) => {
authorizeDeveloperUsage({ path, developer }).catch((err) => {
expect(err.message).toEqual('No developer usage found');
});
});

it('throws error due to exceeding usage limit', async () => {
const route = 'speech-to-text';
const path = 'speech-to-text';
const developer = developerFixture({});
const developerUsage = developerUsageFixture({
usage: { date: new Date(), count: ApiUsageLimit[ApiType.SPEECH_TO_TEXT] + 1 },
Expand All @@ -90,7 +90,7 @@ describe('authorizeDeveloperUsage', () => {
});

// @ts-expect-error developer
authorizeDeveloperUsage({ route, developer }).catch((err) => {
authorizeDeveloperUsage({ path, developer }).catch((err) => {
expect(err.message).toEqual('You have exceeded your limit for this API for the day.');
});
});
Expand Down

0 comments on commit 0a6c2fe

Please sign in to comment.