Skip to content

Commit

Permalink
format write #7
Browse files Browse the repository at this point in the history
  • Loading branch information
georgeenciu committed Jul 8, 2023
1 parent 3659706 commit 879fd32
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 74 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: Build and deploy
on:
pull_request:
types:
- "closed"
- 'closed'
branches:
- "master"
- 'master'

jobs:
main:
Expand All @@ -16,6 +16,9 @@ jobs:
- uses: nrwl/nx-set-shas@v3
with:
main-branch-name: 'master'
- uses: actions/setup-node@v3
with:
node-version: 18
- run: npm ci

- run: npx nx format:check
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ name: Run on PR
on:
pull_request:


jobs:
main:
runs-on: ubuntu-latest
Expand All @@ -13,6 +12,9 @@ jobs:
- uses: nrwl/nx-set-shas@v3
with:
main-branch-name: 'master'
- uses: actions/setup-node@v3
with:
node-version: 20
- run: npm ci

- run: npx nx format:check
Expand Down
8 changes: 5 additions & 3 deletions libs/test-helpers/src/firestore/delete-collection.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
export async function deleteCollection(collectionPath: string) {
const endpoint = process.env['FIRESTORE_EMULATOR_HOST'] as string
const project = process.env['GCLOUD_PROJECT'] as string
const endpoint = process.env['FIRESTORE_EMULATOR_HOST'] as string;
const project = process.env['GCLOUD_PROJECT'] as string;

console.log(
`http://${endpoint}/emulator/v1/projects/${project}/databases/(default)/documents/${collectionPath}`
);
const response = await fetch(
`http://${endpoint}/emulator/v1/projects/${project}/databases/(default)/documents/${collectionPath}`,
{
Expand All @@ -12,4 +15,3 @@ export async function deleteCollection(collectionPath: string) {
throw new Error('Trouble clearing Emulator: ' + (await response.text()));
}
}

111 changes: 65 additions & 46 deletions libs/training-events/src/actions/emit-training-event.test.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,58 @@
import { afterEach, beforeEach, describe, it, expect } from "vitest";
import { buildTrainingIntentEvent } from '@rovacc/test-helpers'
import { afterEach, beforeEach, describe, it, expect } from 'vitest';
import { buildTrainingIntentEvent } from '@rovacc/test-helpers';

import { emitTrainingEvent } from "./emit-training-event";
import { deleteCollection } from "@rovacc/test-helpers";
import { getDatabaseCollection } from "@rovacc/clients";
import { Timestamp } from "firebase-admin/firestore";
import { emitTrainingEvent } from './emit-training-event';
import { deleteCollection } from '@rovacc/test-helpers';
import { getDatabaseCollection } from '@rovacc/clients';
import { Timestamp } from 'firebase-admin/firestore';


const TRAINING_ID = 'trainingId'
const EVENT_ID = 'eventId'
const DATE = new Date()
const TRAINING_ID = 'trainingId';
const EVENT_ID = 'eventId';
const DATE = new Date();

describe('emitTrainingEvent', () => {

beforeEach(async () => {
await deleteCollection('training')
await deleteCollection('training');
vi.mock('uuid', () => ({
v4: () => EVENT_ID
}))
v4: () => EVENT_ID,
}));
vi.mock('../helpers/get-date', () => ({
getDate: () => DATE
}))
})
getDate: () => DATE,
}));
});

afterEach(async () => {
vi.restoreAllMocks()
})
vi.restoreAllMocks();
});

it('should emit the event correctly and create the empty training object', async () => {
const eventData = buildTrainingIntentEvent({
trainingId: TRAINING_ID,
emittedAt: DATE,
eventId: EVENT_ID
})
eventId: EVENT_ID,
});

const reducedTraining = await emitTrainingEvent(eventData, null, 'correlationId')
const reducedTraining = await emitTrainingEvent(
eventData,
null,
'correlationId'
);
console.log({ reducedTraining });

expect(reducedTraining).toEqual({
trainingId: TRAINING_ID,
status: 'QUEUED',
purpose: 'acquire_rating',
rating: 2,
student: 123123123,
requestedAt: DATE
})
const trainingCollection = getDatabaseCollection('training')
const event = await trainingCollection.doc(TRAINING_ID).collection('events').doc(EVENT_ID).get()
requestedAt: DATE,
});
const trainingCollection = getDatabaseCollection('training');
const event = await trainingCollection
.doc(TRAINING_ID)
.collection('events')
.doc(EVENT_ID)
.get();

expect(event.data()).toEqual({
eventId: EVENT_ID,
Expand All @@ -54,29 +61,37 @@ describe('emitTrainingEvent', () => {
trainingId: TRAINING_ID,
payload: { student: 123123123, purpose: 'acquire_rating', rating: 2 },
name: 'training-intent',
correlationId: 'correlationId'
})
})
correlationId: 'correlationId',
});
});

it('should not emit the same event twice', async () => {
const eventData1 = buildTrainingIntentEvent({
trainingId: TRAINING_ID,
emittedAt: DATE,
eventId: EVENT_ID
})
eventId: EVENT_ID,
});

const reducedTraining1 = await emitTrainingEvent(eventData1, null, 'correlationId')
const reducedTraining1 = await emitTrainingEvent(
eventData1,
null,
'correlationId'
);

expect(reducedTraining1).toEqual({
trainingId: TRAINING_ID,
status: 'QUEUED',
purpose: 'acquire_rating',
rating: 2,
student: 123123123,
requestedAt: DATE
})
const trainingCollection = getDatabaseCollection('training')
const event1 = await trainingCollection.doc(TRAINING_ID).collection('events').doc(EVENT_ID).get()
requestedAt: DATE,
});
const trainingCollection = getDatabaseCollection('training');
const event1 = await trainingCollection
.doc(TRAINING_ID)
.collection('events')
.doc(EVENT_ID)
.get();

expect(event1.data()).toEqual({
eventId: EVENT_ID,
Expand All @@ -85,17 +100,21 @@ describe('emitTrainingEvent', () => {
trainingId: TRAINING_ID,
payload: { student: 123123123, purpose: 'acquire_rating', rating: 2 },
name: 'training-intent',
correlationId: 'correlationId'
})
correlationId: 'correlationId',
});

const eventData2 = buildTrainingIntentEvent({
trainingId: TRAINING_ID,
emittedAt: DATE,
eventId: 'eventId2'
})
await emitTrainingEvent(eventData2, reducedTraining1, 'correlationId2')
const eventCount = await trainingCollection.doc(TRAINING_ID).collection('events').count().get()

expect(eventCount.data().count).toEqual(1)
})
})
eventId: 'eventId2',
});
await emitTrainingEvent(eventData2, reducedTraining1, 'correlationId2');
const eventCount = await trainingCollection
.doc(TRAINING_ID)
.collection('events')
.count()
.get();

expect(eventCount.data().count).toEqual(1);
});
});
9 changes: 5 additions & 4 deletions libs/training-events/test/setup-test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { initializeApp } from "firebase-admin";
import { beforeAll } from "vitest";
import { initializeApp } from 'firebase-admin';
import { beforeAll } from 'vitest';

beforeAll(() => {
initializeApp()
})
console.log('initializing app');
initializeApp();
});
6 changes: 5 additions & 1 deletion nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@
"inputs": ["default", "^production"]
},
"lint": {
"inputs": ["default", "{workspaceRoot}/.eslintrc.json", "{workspaceRoot}/.eslintignore"]
"inputs": [
"default",
"{workspaceRoot}/.eslintrc.json",
"{workspaceRoot}/.eslintignore"
]
}
},
"namedInputs": {
Expand Down
22 changes: 5 additions & 17 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,18 @@
"importHelpers": true,
"target": "es2015",
"module": "esnext",
"lib": [
"es2020",
"dom"
],
"lib": ["es2020", "dom"],
"skipLibCheck": true,
"skipDefaultLibCheck": true,
"baseUrl": ".",
"paths": {
"@rovacc/clients": [
"libs/clients/src/index.ts"
],
"@rovacc/test-helpers": [
"libs/test-helpers/src/index.ts"
],
"@rovacc/training-events": [
"libs/training-events/src/index.ts"
],
"@rovacc/clients": ["libs/clients/src/index.ts"],
"@rovacc/test-helpers": ["libs/test-helpers/src/index.ts"],
"@rovacc/training-events": ["libs/training-events/src/index.ts"],
"@rovacc/training-events-types": [
"libs/training-events-types/src/index.ts"
]
}
},
"exclude": [
"node_modules",
"tmp"
]
"exclude": ["node_modules", "tmp"]
}

0 comments on commit 879fd32

Please sign in to comment.