Skip to content

Commit

Permalink
Introduce layers (#40)
Browse files Browse the repository at this point in the history
* Introduce layers

* Bump version
  • Loading branch information
abdala authored Dec 9, 2024
1 parent 4da8fa9 commit 78001c9
Show file tree
Hide file tree
Showing 17 changed files with 140 additions and 28 deletions.
2 changes: 1 addition & 1 deletion assert/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cgauge/assert",
"version": "0.10.7",
"version": "0.11.0",
"description": "Extra assert library",
"type": "module",
"repository": {
Expand Down
10 changes: 5 additions & 5 deletions dtc-aws-plugin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cgauge/dtc-aws-plugin",
"version": "0.10.7",
"version": "0.11.0",
"description": "AWS plugin for Declarative TestCases",
"repository": {
"type": "git",
Expand All @@ -22,10 +22,10 @@
"@aws-sdk/client-sns": "^3.645.0",
"@aws-sdk/lib-dynamodb": "^3.645.0",
"@aws-sdk/util-dynamodb": "^3.645.0",
"@cgauge/assert": "^0.10.0",
"@cgauge/dtc": "^0.10.0",
"@cgauge/nock-aws": "^0.10.0",
"@cgauge/type-guard": "^0.10.0"
"@cgauge/assert": "^0.11.0",
"@cgauge/dtc": "^0.11.0",
"@cgauge/nock-aws": "^0.11.0",
"@cgauge/type-guard": "^0.11.0"
},
"scripts": {
"build": "rm -rf dist && tsc",
Expand Down
8 changes: 4 additions & 4 deletions dtc-graphql-plugin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cgauge/dtc-graphql-plugin",
"version": "0.10.7",
"version": "0.11.0",
"description": "GraphQl plugin for Declarative TestCases",
"repository": {
"type": "git",
Expand All @@ -15,9 +15,9 @@
"author": "Salam Suleymanov",
"license": "LGPL-3.0-or-later",
"dependencies": {
"@cgauge/assert": "^0.10.0",
"@cgauge/dtc": "^0.10.0",
"@cgauge/type-guard": "^0.10.0",
"@cgauge/assert": "^0.11.0",
"@cgauge/dtc": "^0.11.0",
"@cgauge/type-guard": "^0.11.0",
"graphql-request": "^7.1.2"
},
"scripts": {
Expand Down
8 changes: 4 additions & 4 deletions dtc-mysql-plugin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cgauge/dtc-mysql-plugin",
"version": "0.10.7",
"version": "0.11.0",
"description": "MySQL plugin for Declarative TestCases",
"repository": {
"type": "git",
Expand All @@ -15,9 +15,9 @@
"author": "Abdala Cerqueira",
"license": "LGPL-3.0-or-later",
"dependencies": {
"@cgauge/assert": "^0.10.0",
"@cgauge/dtc": "^0.10.0",
"@cgauge/type-guard": "^0.10.0",
"@cgauge/assert": "^0.11.0",
"@cgauge/dtc": "^0.11.0",
"@cgauge/type-guard": "^0.11.0",
"mysql2": "^3.11.0",
"node-sql-parser": "^5.1.0",
"type-assurance": "^1.5.1"
Expand Down
8 changes: 4 additions & 4 deletions dtc-playwright-plugin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cgauge/dtc-playwright-plugin",
"version": "0.10.7",
"version": "0.11.0",
"description": "Playwright plugin for Declarative TestCases",
"repository": {
"type": "git",
Expand All @@ -15,9 +15,9 @@
"author": "Abdala Cerqueira",
"license": "LGPL-3.0-or-later",
"dependencies": {
"@cgauge/assert": "^0.10.0",
"@cgauge/dtc": "^0.10.0",
"@cgauge/type-guard": "^0.10.0",
"@cgauge/assert": "^0.11.0",
"@cgauge/dtc": "^0.11.0",
"@cgauge/type-guard": "^0.11.0",
"@playwright/test": "^1.47.0"
},
"scripts": {
Expand Down
6 changes: 3 additions & 3 deletions dtc/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cgauge/dtc",
"version": "0.10.7",
"version": "0.11.0",
"description": "Declarative TestCases",
"repository": {
"type": "git",
Expand All @@ -18,8 +18,8 @@
"author": "Abdala Cerqueira",
"license": "LGPL-3.0-or-later",
"dependencies": {
"@cgauge/assert": "^0.10.0",
"@cgauge/type-guard": "^0.10.0",
"@cgauge/assert": "^0.11.0",
"@cgauge/type-guard": "^0.11.0",
"cleye": "^1.3.2",
"nock": "^14.0.0-beta.15"
},
Expand Down
10 changes: 10 additions & 0 deletions dtc/src/domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,22 @@ type GenericAttributes = TypeFromSchema<typeof GenericAttributes>

const UnionRecordRecordArray = union(record(String, unknown), [record(String, unknown)])

const Layer = {
retry: optional(Number),
delay: optional(Number),
parameters: optional(record(String, unknown)),
arrange: optional(UnionRecordRecordArray),
// assert: optional(union(String, intersection({exception: optional({name: String})}, UnionRecordRecordArray))),
clean: optional(UnionRecordRecordArray),
}

export const TestCase = intersection(GenericAttributes, {
name: String,
debug: optional(Boolean),
retry: optional(Number),
delay: optional(Number),
timeout: optional(Number),
layers: optional([Layer]),
parameters: optional(UnionRecordRecordArray),
arrange: optional(UnionRecordRecordArray),
act: optional(record(String, unknown)),
Expand Down
8 changes: 8 additions & 0 deletions dtc/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ export const executeTestCase = async (
const loadedPlugins = await Promise.all(plugins.map((plugin) => import(plugin)))
const executePluginFunction = preparePluginFunction(loadedPlugins, basePath, testRunnerArgs)

if (testCase.layers) {
await Promise.all(testCase.layers.filter((v) => v.arrange).map((v) => executePluginFunction('arrange', v.arrange)))
}

if (testCase.arrange) {
await executePluginFunction('arrange', testCase.arrange)
}
Expand All @@ -61,6 +65,10 @@ export const executeTestCase = async (
await retry(() => executePluginFunction('assert', testCase.assert), testCase.retry, testCase.delay)
}

if (testCase.layers) {
await Promise.all(testCase.layers.filter((v) => v.clean).map((v) => executePluginFunction('clean', v.clean)))
}

if (testCase.clean) {
await executePluginFunction('clean', testCase.clean)
}
Expand Down
19 changes: 18 additions & 1 deletion dtc/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {TestCaseExecution, Loader, TestCase} from './domain.js'
import {readdir, stat} from 'node:fs/promises'
import {join} from 'path'
import {assert} from '@cgauge/type-guard'
import {merge} from './utils.js'

const generateTestCaseExecution = async (filePath: string, loader: Loader): Promise<TestCaseExecution> => {
const testCase = await loader(filePath)
Expand Down Expand Up @@ -81,10 +82,18 @@ const replacePlaceholders = (template: unknown, params: Record<string, unknown>)

const resolveParameters = (testCaseExecution: TestCaseExecution): TestCaseExecution[] => {
if (testCaseExecution.testCase.parameters) {
const params = testCaseExecution.testCase.parameters
let params = testCaseExecution.testCase.parameters

if (Array.isArray(params)) {
return params.map((param, i) => {
if (testCaseExecution.testCase.layers) {
testCaseExecution.testCase.layers
.filter((v) => v.parameters)
.forEach((v) => {
params = merge(v.parameters, params)
})
}

const resolvedParams = replacePlaceholders(param, param)

return {
Expand All @@ -97,6 +106,14 @@ const resolveParameters = (testCaseExecution: TestCaseExecution): TestCaseExecut
})
}

if (testCaseExecution.testCase.layers) {
testCaseExecution.testCase.layers
.filter((v) => v.parameters)
.forEach((v) => {
params = merge(v.parameters, params)
})
}

const resolvedParams = replacePlaceholders(params, params)

return [
Expand Down
14 changes: 13 additions & 1 deletion dtc/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,16 @@ export const retry = async <T>(fn: () => Promise<T>, times = 2, seconds = 1): Pr
debug(`Errors ${JSON.stringify(errors)}`)

throw new Error(`Failed retrying ${times} times`)
}
}

export const merge = (to: any, from: any) => {
to = {...to}
for (const n in from) {
if (typeof to[n] != 'object') {
to[n] = from[n]
} else if (typeof from[n] == 'object') {
to[n] = merge(to[n], from[n])
}
}
return to
}
6 changes: 6 additions & 0 deletions dtc/test/fixtures/layers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {mock} from 'node:test'

export const arrange = mock.fn()
export const act = mock.fn()
export const assert = mock.fn()
export const clean = mock.fn()
24 changes: 24 additions & 0 deletions dtc/test/fixtures/t4.dtc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export default {
parameters: {
a: 'b',
},
layers: [{
parameters: {
b: '${a}',
c: {
d: 'e'
},
},
}],
name: 'Test 4',
act: {
import: 'syncFunction',
from: 'functions.js',
arguments: [
{a: 'content ${a} more ${b}', b: '${b} content', c: '${c}', d: '${c.d}'},
],
},
assert: {
a: 'content ${a} more ${b}'
},
}
29 changes: 27 additions & 2 deletions dtc/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ import {
assert as assertArray,
clean as cleanArray,
} from './fixtures/plugin-array.ts'
import {
arrange as arrangeLayers,
act as actLayers,
assert as assertLayers,
clean as cleanLayers,
} from './fixtures/layers.ts'

test('It runs plugins methods', async () => {
await executeTestCase(
Expand All @@ -28,7 +34,7 @@ test('It runs plugins methods', async () => {
nodeAssert.equal(clean.mock.callCount(), 1)
})

test('It pass parameters to plugins functions', async () => {
test('It passes parameters to plugins functions', async () => {
const testCase = {
name: 'Test',
arrange: {a: 'b'},
Expand All @@ -48,7 +54,7 @@ test('It pass parameters to plugins functions', async () => {
nodeAssert.deepEqual(cleanArgs.mock.calls[0].arguments, [testCase.clean, basePath, testArgs])
})

test('It ensure plugins methods are optional', async () => {
test('It ensures plugins methods are optional', async () => {
await executeTestCase({name: 'Test'}, ['../test/fixtures/blank-plugin.ts'], '', {})

nodeAssert.ok(true)
Expand All @@ -66,3 +72,22 @@ test('It supports array on arrange, assert and clean', async () => {
nodeAssert.equal(assertArray.mock.callCount(), 2)
nodeAssert.equal(cleanArray.mock.callCount(), 2)
})

test('It executes layers', async () => {
await executeTestCase(
{
name: 'Test',
act: {},
layers: [
{arrange: {}, clean: {}},
{arrange: [{}, {}], clean: [{}, {}]}
]
},
['../test/fixtures/layers.ts'],
'./filePath.js',
)

nodeAssert.equal(arrangeLayers.mock.callCount(), 3)
nodeAssert.equal(actLayers.mock.callCount(), 1)
nodeAssert.equal(cleanLayers.mock.callCount(), 3)
})
10 changes: 10 additions & 0 deletions dtc/test/loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,13 @@ test('It replaces multiple parameters placeholders', async () => {
nodeAssert.deepStrictEqual(testCaseExecutions[1].testCase.act?.arguments, [{a: 'c'}])
nodeAssert.deepStrictEqual(testCaseExecutions[1].testCase.assert, {a: 'c'})
})

test('It replaces parameters placeholders defined in the layers', async () => {
const testCaseExecutions = await loadTestCases(__dirname, defaultLoader, /.*\.dtc\.[jt]s?$/, `./fixtures/t4.dtc.ts`)

nodeAssert.equal(`${__dirname}/./fixtures/t4.dtc.ts`, testCaseExecutions[0].filePath)
nodeAssert.equal('Test 4', testCaseExecutions[0].testCase.name)

nodeAssert.deepStrictEqual(testCaseExecutions[0].testCase.act?.arguments, [{a: 'content b more b', b: 'b content', c: {d: 'e'}, d: 'e'}])
nodeAssert.deepStrictEqual(testCaseExecutions[0].testCase.assert, {a: 'content b more b'})
})
2 changes: 1 addition & 1 deletion nock-aws/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cgauge/nock-aws",
"version": "0.10.7",
"version": "0.11.0",
"description": "AWS Request mocker based on Nock",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion type-guard/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cgauge/type-guard",
"version": "0.10.7",
"version": "0.11.0",
"description": "Typescript type guards",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion yaml/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cgauge/yaml",
"version": "0.10.7",
"version": "0.11.0",
"description": "YAML parser with extra tags",
"repository": {
"type": "git",
Expand Down

0 comments on commit 78001c9

Please sign in to comment.