Skip to content

Commit

Permalink
1.1.1 release (#67)
Browse files Browse the repository at this point in the history
* cleanup whose injecting

* update snaps to reflect variation labels

* update changelog

* revise changelog version number
  • Loading branch information
nicklloyd authored Jun 30, 2021
1 parent 384010a commit ef073e0
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 46 deletions.
4 changes: 4 additions & 0 deletions CHANEGLOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## v1.1.1 - (2021-06-30)

### Variation Tests

- Fix to ensure overwrites for variations are not overwritten when running globals

### CLI

- Fix incomplete Postman collection when using --localPostman CLI options
Expand Down
24 changes: 23 additions & 1 deletion src/Portman.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import fs from 'fs-extra'
import emoji from 'node-emoji'
import path from 'path'
import { Collection, CollectionDefinition } from 'postman-collection'
import { CollectionWriter, runNewmanWith, TestSuite, writeNewmanEnv } from './application'
import {
CollectionWriter,
runNewmanWith,
TestSuite,
VariationWriter,
writeNewmanEnv
} from './application'
import { clearTmpDirectory, execShellCommand, getConfig } from './lib'
import { OpenApiParser } from './oas'
import { PostmanParser } from './postman'
Expand All @@ -25,6 +31,7 @@ export class Portman {
postmanCollection: Collection
portmanCollection: CollectionDefinition
testSuite: TestSuite
variationWriter: VariationWriter
consoleLine: string

public collectionFile: string
Expand All @@ -42,6 +49,7 @@ export class Portman {
await this.injectTestSuite()
await this.injectVariationTests()
await this.runPortmanOverrides()
await this.injectVariationOverwrites()
await this.writePortmanCollectionToFile()
await this.runNewmanSuite()
await this.syncCollectionToPostman()
Expand Down Expand Up @@ -258,6 +266,8 @@ export class Portman {

if (includeTests && testSuite) {
// Inject variations
this.variationWriter = new VariationWriter({ testSuite: testSuite })
testSuite.variationWriter = this.variationWriter
testSuite.generateVariationTests()

this.portmanCollection = testSuite.collection.toJSON()
Expand All @@ -273,6 +283,18 @@ export class Portman {
this.portmanCollection = collectionWriter.collection
}

async injectVariationOverwrites(): Promise<void> {
const { testSuite, variationWriter } = this
if (!variationWriter || !testSuite || !testSuite.variationTests) return

Object.entries(variationWriter.overwriteMap).map(([id, overwrites]) => {
const pmOperation = this.postmanParser.getOperationById(id)
pmOperation && testSuite.injectOverwrites([pmOperation], overwrites)
})

this.portmanCollection = testSuite.collection.toJSON()
}

async writePortmanCollectionToFile(): Promise<void> {
// --- Portman - Write Postman collection to file
const { output } = this.options
Expand Down
35 changes: 18 additions & 17 deletions src/application/TestSuite.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'fs-extra'
import { TestSuite } from '../application'
import { TestSuite, VariationWriter } from '../application'
import { getConfig } from '../lib'
import { OpenApiParser } from '../oas'
import { PostmanParser } from '../postman'
Expand All @@ -8,7 +8,7 @@ import { omitKeys } from '../utils'
describe('TestSuite', () => {
let postmanParser: PostmanParser
let oasParser: OpenApiParser
let testSuiteService: TestSuite
let testSuite: TestSuite

const postmanJson = '__tests__/fixtures/crm.postman.json'
const oasYml = '__tests__/fixtures/crm.yml'
Expand All @@ -21,54 +21,55 @@ describe('TestSuite', () => {
const config = await getConfig(postmanConfigFile)
postmanParser = new PostmanParser({ postmanObj: postmanObj, oasParser: oasParser })

testSuiteService = new TestSuite({ oasParser, postmanParser, config })
testSuite = new TestSuite({ oasParser, postmanParser, config })
testSuite.variationWriter = new VariationWriter({ testSuite: testSuite })
})

it('should generateContractTests', () => {
testSuiteService.generateContractTests()
expect(omitKeys(testSuiteService.collection.toJSON(), ['id', '_postman_id'])).toMatchSnapshot()
testSuite.generateContractTests()
expect(omitKeys(testSuite.collection.toJSON(), ['id', '_postman_id'])).toMatchSnapshot()
})

it('should injectContentTests', () => {
testSuiteService.injectContentTests()
expect(omitKeys(testSuiteService.collection.toJSON(), ['id', '_postman_id'])).toMatchSnapshot()
testSuite.injectContentTests()
expect(omitKeys(testSuite.collection.toJSON(), ['id', '_postman_id'])).toMatchSnapshot()
})

it('should injectAssignVariables', () => {
testSuiteService.injectAssignVariables()
expect(omitKeys(testSuiteService.collection.toJSON(), ['id', '_postman_id'])).toMatchSnapshot()
testSuite.injectAssignVariables()
expect(omitKeys(testSuite.collection.toJSON(), ['id', '_postman_id'])).toMatchSnapshot()
})

it('should injectExtendedTests', () => {
testSuiteService.injectExtendedTests()
expect(omitKeys(testSuiteService.collection.toJSON(), ['id', '_postman_id'])).toMatchSnapshot()
testSuite.injectExtendedTests()
expect(omitKeys(testSuite.collection.toJSON(), ['id', '_postman_id'])).toMatchSnapshot()
})

it('should generateVariationTests', () => {
testSuiteService.generateVariationTests()
expect(omitKeys(testSuiteService.collection.toJSON(), ['id', '_postman_id'])).toMatchSnapshot()
testSuite.generateVariationTests()
expect(omitKeys(testSuite.collection.toJSON(), ['id', '_postman_id'])).toMatchSnapshot()
})

describe('injectOverwrites', () => {
it('should overwriteRequestBody', () => {
testSuiteService.injectOverwrites()
testSuite.injectOverwrites()
expect(postmanParser.mappedOperations[1].item.request.body).toMatchSnapshot()
})

it('should overwriteRequestQueryParams', () => {
testSuiteService.injectOverwrites()
testSuite.injectOverwrites()

expect(postmanParser.mappedOperations[0].item.request.url.query).toMatchSnapshot()
})

it('should overwriteRequestPathVariables', () => {
testSuiteService.injectOverwrites()
testSuite.injectOverwrites()

expect(postmanParser.mappedOperations[3].item.request.url.variables).toMatchSnapshot()
})

it('should overwriteRequestHeaders', () => {
testSuiteService.injectOverwrites()
testSuite.injectOverwrites()

expect(postmanParser.mappedOperations[3].item.request.headers).toMatchSnapshot()
})
Expand Down
9 changes: 4 additions & 5 deletions src/application/TestSuite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class TestSuite {
oasParser: OpenApiParser
postmanParser: PostmanParser
config: PortmanConfig
variationWriter: VariationWriter

contractTests?: ContractTestConfig[]
contentTests?: ContentTestConfig[]
Expand Down Expand Up @@ -92,23 +93,21 @@ export class TestSuite {
}

public generateVariationTests = (): void => {
const variationTests = this.variationTests
const { variationTests } = this
if (!variationTests) return

const variationWriter = new VariationWriter({ testSuite: this })

variationTests.map(variationTest => {
//Get Postman operations to inject variation test for
const pmOperations = this.getOperationsFromSetting(variationTest)

pmOperations.map(pmOperation => {
// Get OpenApi responses
const oaOperation = this.oasParser.getOperationByPath(pmOperation.pathRef)
variationWriter.add(pmOperation, oaOperation, variationTest.variations)
this.variationWriter.add(pmOperation, oaOperation, variationTest.variations)
})
})

this.collection = variationWriter.mergeToCollection(this.collection)
this.collection = this.variationWriter.mergeToCollection(this.collection)
}

public getOperationsFromSetting(
Expand Down
11 changes: 6 additions & 5 deletions src/application/TestSuiteVariations.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'fs-extra'
import { TestSuite } from '../application'
import { TestSuite, VariationWriter } from '../application'
import { getConfig } from '../lib'
import { OpenApiParser } from '../oas'
import { PostmanParser } from '../postman'
Expand All @@ -8,7 +8,7 @@ import { omitKeys } from '../utils'
describe('TestSuite Variations', () => {
let postmanParser: PostmanParser
let oasParser: OpenApiParser
let testSuiteService: TestSuite
let testSuite: TestSuite

const postmanJson = '__tests__/fixtures/crm.postman.json'
const oasYml = '__tests__/fixtures/crm.yml'
Expand All @@ -21,11 +21,12 @@ describe('TestSuite Variations', () => {
const config = await getConfig(postmanConfigFile)
postmanParser = new PostmanParser({ postmanObj: postmanObj, oasParser: oasParser })

testSuiteService = new TestSuite({ oasParser, postmanParser, config })
testSuite = new TestSuite({ oasParser, postmanParser, config })
testSuite.variationWriter = new VariationWriter({ testSuite: testSuite })
})

it('should generateVariationTests for variations', () => {
testSuiteService.generateVariationTests()
expect(omitKeys(testSuiteService.collection.toJSON(), ['id', '_postman_id'])).toMatchSnapshot()
testSuite.generateVariationTests()
expect(omitKeys(testSuite.collection.toJSON(), ['id', '_postman_id'])).toMatchSnapshot()
})
})
5 changes: 5 additions & 0 deletions src/application/VariationWriter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,9 @@ describe('VariationWriter', () => {
variationWriter.mergeToCollection(testSuite.collection)
expect(omitKeys(testSuite.collection.toJSON(), ['id', '_postman_id'])).toMatchSnapshot()
})

it(`should keep a reference to it's variations`, () => {
variationWriter.mergeToCollection(testSuite.collection)
expect(variationWriter.overwriteMap).toMatchSnapshot()
})
})
19 changes: 11 additions & 8 deletions src/application/VariationWriter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Collection, Item, ItemGroup } from 'postman-collection'
import { OasMappedOperation } from 'src/oas'
import { PostmanMappedOperation } from '../postman'
import { VariationConfig } from '../types'
import { OverwriteRequestConfig, VariationConfig } from '../types'
import { TestSuite } from './'

export type VariationWriterOptions = {
Expand All @@ -10,14 +10,20 @@ export type VariationWriterOptions = {

export class VariationWriter {
testSuite: TestSuite
public variationFolder: ItemGroup<Item>
public variationCollection: Collection
public operationFolders: Record<string, string>
public overwriteMap: Record<string, OverwriteRequestConfig[]>

constructor(options: VariationWriterOptions) {
const { testSuite } = options
this.testSuite = testSuite
this.operationFolders = {}
this.overwriteMap = {}
this.variationCollection = new Collection()
this.variationFolder = new ItemGroup<Item>({
name: 'Variation Testing'
})
}

public add(
Expand All @@ -28,7 +34,7 @@ export class VariationWriter {
variations.map(variation => {
const folderId = pmOperation.getParentFolderId()
const folderName = pmOperation.getParentFolderName()
const variationName = `${pmOperation.item.name}-${variation.name}`
const variationName = `${pmOperation.item.name}[${variation.name}]`
const operationVariation = pmOperation.clone(variationName)

this.injectVariations(operationVariation, oaOperation, variation)
Expand All @@ -38,16 +44,12 @@ export class VariationWriter {
}

public mergeToCollection(collection: Collection): Collection {
const variationFolder = new ItemGroup<Item>({
name: 'Variation Testing'
})

// eslint-disable-next-line @typescript-eslint/no-explicit-any
this.variationCollection.items.map((item: any) => {
variationFolder.items.add(item)
this.variationFolder.items.add(item)
})

collection.items.add(variationFolder)
collection.items.add(this.variationFolder)
return collection
}

Expand Down Expand Up @@ -89,6 +91,7 @@ export class VariationWriter {
const { overwrites, tests, assignVariables } = variation

if (overwrites) {
this.overwriteMap[pmOperation.id as string] = overwrites
this.testSuite.injectOverwrites([pmOperation], overwrites)
}

Expand Down
4 changes: 2 additions & 2 deletions src/application/__snapshots__/TestSuite.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -76012,7 +76012,7 @@ if (typeof jsonData.data[0].id !== \\"undefined\\") {
},
},
],
"name": "Create lead-missingParams",
"name": "Create lead[missingParams]",
"request": Object {
"auth": Object {
"bearer": Array [
Expand Down Expand Up @@ -76170,7 +76170,7 @@ if (typeof jsonData.data[0].id !== \\"undefined\\") {
},
],
"method": "POST",
"name": "Create lead-missingParams",
"name": "Create lead[missingParams]",
"url": Object {
"host": Array [
"{{baseUrl}}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37193,7 +37193,7 @@ if (typeof jsonData.data[0].id !== \\"undefined\\") {
},
},
],
"name": "Create lead-missingParams",
"name": "Create lead[missingParams]",
"request": Object {
"auth": Object {
"bearer": Array [
Expand Down Expand Up @@ -37351,7 +37351,7 @@ if (typeof jsonData.data[0].id !== \\"undefined\\") {
},
],
"method": "POST",
"name": "Create lead-missingParams",
"name": "Create lead[missingParams]",
"url": Object {
"host": Array [
"{{baseUrl}}",
Expand Down Expand Up @@ -38696,7 +38696,7 @@ pm.test(\\"[POST]::/crm/leads - Schema is valid\\", function() {
},
},
],
"name": "Create lead-400",
"name": "Create lead[400]",
"request": Object {
"auth": Object {
"bearer": Array [
Expand Down Expand Up @@ -38854,7 +38854,7 @@ pm.test(\\"[POST]::/crm/leads - Schema is valid\\", function() {
},
],
"method": "POST",
"name": "Create lead-400",
"name": "Create lead[400]",
"url": Object {
"host": Array [
"{{baseUrl}}",
Expand Down
Loading

0 comments on commit ef073e0

Please sign in to comment.