From 34db54511702e7b09bbfb07934f0cb73414c9394 Mon Sep 17 00:00:00 2001 From: Kai Hillenbrand Date: Thu, 5 Oct 2023 16:43:04 +0200 Subject: [PATCH] - automatic feature file generation - removed old hardcoded test files - renamed test mode to "test-scenario" --- packages/be/features/add_car_to_fleet.feature | 6 -- .../step_definitions/AddCarToFleetSteps.ts | 85 ------------------- .../features/__feature__.feature__tmpl__ | 5 ++ packages/cody/src/lib/hooks/on-feature.ts | 14 ++- packages/messaging/src/lib/helpers.ts | 2 +- 5 files changed, 12 insertions(+), 100 deletions(-) delete mode 100644 packages/be/features/add_car_to_fleet.feature delete mode 100644 packages/be/features/step_definitions/AddCarToFleetSteps.ts create mode 100644 packages/cody/src/lib/hooks/behaviour-test-files/features/__feature__.feature__tmpl__ diff --git a/packages/be/features/add_car_to_fleet.feature b/packages/be/features/add_car_to_fleet.feature deleted file mode 100644 index f6758b18..00000000 --- a/packages/be/features/add_car_to_fleet.feature +++ /dev/null @@ -1,6 +0,0 @@ -Feature: Add Car To Fleet - Scenario: Add Car To Fleet - Given Car Added - When Add Car To Fleet - Then Incomplete Car Added - diff --git a/packages/be/features/step_definitions/AddCarToFleetSteps.ts b/packages/be/features/step_definitions/AddCarToFleetSteps.ts deleted file mode 100644 index 75202509..00000000 --- a/packages/be/features/step_definitions/AddCarToFleetSteps.ts +++ /dev/null @@ -1,85 +0,0 @@ -import { before, binding, given, then, when } from "cucumber-tsflow"; -import {addCarToFleet} from "@app/shared/commands/fleet-management/add-car-to-fleet"; -import { Event } from "@event-engine/messaging/event"; -import { getConfiguredMessageBox } from "@server/infrastructure/configuredMessageBox"; -import { getConfiguredEventStore } from "@server/infrastructure/configuredEventStore"; -import {carAdded} from "@app/shared/events/fleet-management/car/car-added"; -import {incompleteCarAdded} from "@app/shared/events/fleet-management/car/incomplete-car-added"; -import expect from "expect"; - - -@binding() -// eslint-disable-next-line @typescript-eslint/no-unused-vars -class AddCarToFleetSteps { - private messageBox = getConfiguredMessageBox(); - private eventStore = getConfiguredEventStore(); - private events: Event[] = []; - - @before() - public setup(): void { - const listener = (streamName: string, events: Event[]) => { - this.events.push(...events); - }; - this.eventStore.attachAppendToListener(listener); - } - - @given('Car Added') - public async givenCarAdded(): Promise { - const payload = { - 'productionYear': 1998, -'vehicleId': '6a76bead-46ce-4651-bea0-d8a387b2e9d0', -'brand': 'Ford', -'model': 'Focus' - }; - - const event = carAdded(payload); - - await this.messageBox.dispatch(event.name, event.payload, event.meta); - } - -/* multiple givens via iterator like above - @given('Car Added To Fleet') - public async givenCarAddedToFleet(): Promise { - const payload = { - 'vehicleId': '6a76bead-46ce-4651-bea0-d8a387b2e9d0', - }; - - const event = carAddedToFleet(payload); - - await this.messageBox.dispatch(event.name, event.payload, event.meta); - } -*/ - - @when('Add Car To Fleet') - public async whenAddCarToFleet(): Promise { - const payload = { - 'vehicleId': '6a76bead-46ce-4651-bea0-d8a387b2e9d0', -'brand': 'BMW', -'model': '1er' - }; - - const command = addCarToFleet(payload); - - await this.messageBox.dispatch(command.name, command.payload, command.meta); - } - - @then('Incomplete Car Added') - public async thenIncompleteCarAdded(): Promise { - const identifier = '6a76bead-46ce-4651-bea0-d8a387b2e9d0'; - const expectedPayload = { - 'vehicleId': '6a76bead-46ce-4651-bea0-d8a387b2e9d0', 'brand': 'BMW', -'model': '1er' - }; - const expectedEvent = incompleteCarAdded(expectedPayload); - - const newestEventToCompare = this.events. - filter((event: Event) => { - return event.name === expectedEvent.name && event.meta.aggregateId === identifier; - }).sort((event1, event2) => { - return event2.createdAt.getTime() - event1.createdAt.getTime(); - }).pop(); - - expect(newestEventToCompare).toBeDefined(); - expect(newestEventToCompare?.payload).toEqual(expectedPayload); - } -} diff --git a/packages/cody/src/lib/hooks/behaviour-test-files/features/__feature__.feature__tmpl__ b/packages/cody/src/lib/hooks/behaviour-test-files/features/__feature__.feature__tmpl__ new file mode 100644 index 00000000..60d604ff --- /dev/null +++ b/packages/cody/src/lib/hooks/behaviour-test-files/features/__feature__.feature__tmpl__ @@ -0,0 +1,5 @@ +Feature: <%= featureNames.name %> + Scenario: <%= featureNames.name %> + Given <%= givenEvent.name %> + When <%= whenEvent.name %> + Then <%= thenEvent.name %> \ No newline at end of file diff --git a/packages/cody/src/lib/hooks/on-feature.ts b/packages/cody/src/lib/hooks/on-feature.ts index b506afca..14702e1a 100644 --- a/packages/cody/src/lib/hooks/on-feature.ts +++ b/packages/cody/src/lib/hooks/on-feature.ts @@ -10,7 +10,7 @@ import {flushChanges} from "nx/src/generators/tree"; import {listChangesForCodyResponse} from "./utils/fs-tree"; const modeKey = "mode"; -const modeValueTest = "test"; +const modeValueTest = "test-scenario"; export const onFeature: CodyHook = async (feature: Node, ctx: Context) => { try { @@ -64,7 +64,7 @@ export const onFeature: CodyHook = async (feature: Node, ctx: Context) } } - const changesForCodyResponse = await createTestFile(feature.getName(), givenNodes, whenCommand, thenNodes, ctx); + const changesForCodyResponse = await createTestFiles(feature.getName(), givenNodes, whenCommand, thenNodes, ctx); // for logging: const loggedNodes: Array = []; @@ -99,7 +99,8 @@ export const onFeature: CodyHook = async (feature: Node, ctx: Context) } } -async function createTestFile(featureName: string, givenNodes : Array, whenCommand : Node, thenNodes : Array, ctx: Context): Promise { +async function createTestFiles(featureName: string, givenNodes : Array, whenCommand : Node, thenNodes : Array, ctx: Context): Promise { + // if using a service from another board (e.g. Fleet Management), make sure to set this up in the test feature's metadata! const service = withErrorCheck(detectService, [whenCommand, ctx]); const aggregate = 'car'; @@ -119,16 +120,13 @@ async function createTestFile(featureName: string, givenNodes : Array, whe "aggregate": aggregate, "expectedIdentifier": "6a76bead-46ce-4651-bea0-d8a387b2e9d0" // TODO: read from "then" node payload (convert to json, read & remove "expectedIdentifier", convert back to string) } - // console.log(substitutions); + // generate test files const {tree} = ctx; - generateFiles(tree, __dirname + '/behaviour-test-files', ctx.beSrc+'/../', substitutions); // TODO: setup correct template/target folder - + generateFiles(tree, __dirname + '/behaviour-test-files', ctx.beSrc+'/../', substitutions); await formatFiles(tree); - const changes = tree.listChanges(); - flushChanges(ctx.projectRoot, changes); return listChangesForCodyResponse(tree); diff --git a/packages/messaging/src/lib/helpers.ts b/packages/messaging/src/lib/helpers.ts index f9fd6529..01fe3c91 100644 --- a/packages/messaging/src/lib/helpers.ts +++ b/packages/messaging/src/lib/helpers.ts @@ -2,7 +2,7 @@ import {camelCase, startCase, snakeCase, kebabCase} from 'lodash'; export const names = (str: string): {name: string, className: string, propertyName: string, constantName: string, fileName: string} => { return { - name: str, + name: str.trim(), className: startCase(camelCase(str)).replace(/ /g, ''), propertyName: camelCase(str), constantName: snakeCase(str).toUpperCase(),