Skip to content

Commit

Permalink
fix: fix singleton specs
Browse files Browse the repository at this point in the history
  • Loading branch information
pviti committed Nov 21, 2024
1 parent 36c5a6b commit 2a2b4fd
Show file tree
Hide file tree
Showing 129 changed files with 3,455 additions and 3,323 deletions.
9 changes: 8 additions & 1 deletion gen/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const CONFIG = {
}
/**** **** **** **** **** **** **** **** ****/


const RESOURCE_COMMON_FIELDS = ['type', 'id', 'reference', 'reference_origin', 'metadata', 'created_at', 'updated_at']


Expand Down Expand Up @@ -460,10 +461,15 @@ const generateSpec = (type: string, name: string, resource: Resource): string =>

const allOperations = ['list', 'create', 'retrieve', 'update', 'delete', 'singleton']

let singleton = false

// Generate CRUD operations specs
allOperations.forEach(op => {
if (!Object.values(resource.operations).map(o => {
if ((o.name === 'list') && o.singleton) return 'singleton'
if ((o.name === 'list') && o.singleton) {
singleton = true
return 'singleton'
}
else return o.name
}).includes(op)) {
const opStartIdx = findLine(`spec.${op}.start`, lines).index - 2
Expand Down Expand Up @@ -519,6 +525,7 @@ const generateSpec = (type: string, name: string, resource: Resource): string =>

spec = spec.replace(/##__RESOURCE_CLASS__##/g, name)
spec = spec.replace(/##__RESOURCE_TYPE__##/g, type)
spec = spec.replace(/##__RESOURCE_PATH__##/g, singleton? Inflector.singularize(type) : type)
// Clear unused placeholders
spec = spec.replace(/##__RELATIONSHIP_SPECS__##/g, '')
spec = spec.replace(/##__TRIGGER_SPECS__##/g, '')
Expand Down
37 changes: 19 additions & 18 deletions gen/templates/spec.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ beforeAll(async () => { cl = await getClient() })
describe('##__RESOURCE_CLASS__## resource', () => {
const resourceType = '##__RESOURCE_TYPE__##'
const resourcePath = '##__RESOURCE_PATH__##'
/* spec.create.start */
Expand All @@ -32,13 +33,13 @@ describe('##__RESOURCE_CLASS__## resource', () => {
cl.addRequestInterceptor((request) => {
const data = JSON.parse(String(request.options.body))
expect(request.options.method).toBe('POST')
checkCommon(request, resourceType)
checkCommon(request, resourcePath)
checkCommonData(data, resourceType, attributes)
expect(cl[resourceType].is##__RESOURCE_MODEL__##(data.data)).toBeTruthy()
expect(cl[resourcePath].is##__RESOURCE_MODEL__##(data.data)).toBeTruthy()
return interceptRequest()
})

await cl[resourceType].create(resData, params, CommonData.options)
await cl[resourcePath].create(resData, params, CommonData.options)
.then((res: ##__RESOURCE_MODEL__##) => expect(res).not.toBeNull())
.catch(handleError)
.finally(() => cl.removeInterceptor('request'))
Expand All @@ -55,12 +56,12 @@ describe('##__RESOURCE_CLASS__## resource', () => {

cl.addRequestInterceptor((request) => {
expect(request.options.method).toBe('GET')
checkCommon(request, resourceType, id, currentAccessToken)
checkCommon(request, resourcePath, id, currentAccessToken)
checkCommonParams(request, params)
return interceptRequest()
})

await cl[resourceType].retrieve(id, params, CommonData.options)
await cl[resourcePath].retrieve(id, params, CommonData.options)
.then((res: ##__RESOURCE_MODEL__##) => expect(res).not.toBeNull())
.catch(handleError)
.finally(() => cl.removeInterceptor('request'))
Expand All @@ -79,12 +80,12 @@ describe('##__RESOURCE_CLASS__## resource', () => {
cl.addRequestInterceptor((request) => {
const data = JSON.parse(String(request.options.body))
expect(request.options.method).toBe('PATCH')
checkCommon(request, resourceType, resData.id, currentAccessToken)
checkCommon(request, resourcePath, resData.id, currentAccessToken)
checkCommonData(data, resourceType, attributes, resData.id)
return interceptRequest()
})

await cl[resourceType].update(resData, params, CommonData.options)
await cl[resourcePath].update(resData, params, CommonData.options)
.then((res: ##__RESOURCE_MODEL__##) => expect(res).not.toBeNull())
.catch(handleError)
.finally(() => cl.removeInterceptor('request'))
Expand All @@ -100,11 +101,11 @@ describe('##__RESOURCE_CLASS__## resource', () => {
cl.addRequestInterceptor((request) => {
expect(request.options.method).toBe('DELETE')
checkCommon(request, resourceType, id, currentAccessToken)
checkCommon(request, resourcePath, id, currentAccessToken)
return interceptRequest()
})

await cl[resourceType].delete(id, CommonData.options)
await cl[resourcePath].delete(id, CommonData.options)
.catch(handleError)
.finally(() => cl.removeInterceptor('request'))

Expand All @@ -119,12 +120,12 @@ describe('##__RESOURCE_CLASS__## resource', () => {
cl.addRequestInterceptor((request) => {
expect(request.options.method).toBe('GET')
checkCommon(request, resourceType)
checkCommon(request, resourcePath)
checkCommonParamsList(request, params)
return interceptRequest()
})

await cl[resourceType].list(params, CommonData.options)
await cl[resourcePath].list(params, CommonData.options)
.catch(handleError)
.finally(() => cl.removeInterceptor('request'))

Expand All @@ -139,12 +140,12 @@ describe('##__RESOURCE_CLASS__## resource', () => {

cl.addRequestInterceptor((request) => {
expect(request.options.method).toBe('GET')
checkCommon(request, resourceType)
checkCommon(request, resourcePath)
checkCommonParams(request, params)
return interceptRequest()
})

await cl[resourceType].retrieve(params, CommonData.options)
await cl[resourcePath].retrieve(params, CommonData.options)
.catch(handleError)
.finally(() => cl.removeInterceptor('request'))

Expand All @@ -156,9 +157,9 @@ describe('##__RESOURCE_CLASS__## resource', () => {
it(resourceType + '.type', async () => {
const resource = { id: TestData.id, type: resourceType }
expect(cl[resourceType].is##__RESOURCE_MODEL__##(resource)).toBeTruthy()
expect(cl[resourcePath].is##__RESOURCE_MODEL__##(resource)).toBeTruthy()

const type = cl[resourceType].type()
const type = cl[resourcePath].type()
expect(type).toBe(resourceType)

})
Expand All @@ -168,10 +169,10 @@ describe('##__RESOURCE_CLASS__## resource', () => {
/* spec.relationship.start */
it(resourceType + '.relationship', async () => {
const relId = cl[resourceType].relationship(TestData.id)
const relId = cl[resourcePath].relationship(TestData.id)
expect(isEqual(relId, { id: TestData.id, type: resourceType}))

const relResId = cl[resourceType].relationship({ id: TestData.id, type: resourceType })
const relResId = cl[resourcePath].relationship({ id: TestData.id, type: resourceType })
expect(isEqual(relResId, { id: TestData.id, type: resourceType}))

})
Expand Down Expand Up @@ -206,7 +207,7 @@ describe('##__RESOURCE_CLASS__## resource', () => {
}
`

const res = cl[resourceType].parse(payload) as ##__RESOURCE_MODEL__##
const res = cl[resourcePath].parse(payload) as ##__RESOURCE_MODEL__##

expect(res.type).toBe(resourceType)
expect(res.reference).toBe(reference)
Expand Down
4 changes: 2 additions & 2 deletions gen/templates/spec_relationship.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ it(resourceType + '.##__OPERATION_NAME__##', async () => {

const intId = cl.addRequestInterceptor((request) => {
expect(request.options.method).toBe('GET')
checkCommon(request, resourceType, id, currentAccessToken, '##__OPERATION_NAME__##')
checkCommon(request, resourcePath, id, currentAccessToken, '##__OPERATION_NAME__##')
checkCommonParams(request, params)
return interceptRequest()
})

await cl[resourceType].##__OPERATION_NAME__##(id, params, CommonData.options)
await cl[resourcePath].##__OPERATION_NAME__##(id, params, CommonData.options)
.catch(handleError)
.finally(() => cl.removeInterceptor('request'))

Expand Down
4 changes: 2 additions & 2 deletions gen/templates/spec_trigger.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ it(resourceType + '.##__OPERATION_NAME__##', async () => {
const intId = cl.addRequestInterceptor((request) => {
const data = JSON.parse(String(request.options.body))
expect(request.options.method).toBe('PATCH')
checkCommon(request, resourceType, id, currentAccessToken)
checkCommon(request, resourcePath, id, currentAccessToken)
checkCommonData(data, resourceType, attributes, id)
return interceptRequest()
})

await cl[resourceType].##__OPERATION_NAME__##(##__TRIGGER_PARAMS__##, {}, CommonData.options)
await cl[resourcePath].##__OPERATION_NAME__##(##__TRIGGER_PARAMS__##, {}, CommonData.options)
.catch(handleError)
.finally(() => cl.removeInterceptor('request'))

Expand Down
49 changes: 25 additions & 24 deletions specs/resources/addresses.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ beforeAll(async () => { cl = await getClient() })
describe('Addresses resource', () => {

const resourceType = 'addresses'
const resourcePath = 'addresses'


/* spec.create.start */
Expand All @@ -41,13 +42,13 @@ describe('Addresses resource', () => {
cl.addRequestInterceptor((request) => {
const data = JSON.parse(String(request.options.body))
expect(request.options.method).toBe('POST')
checkCommon(request, resourceType)
checkCommon(request, resourcePath)
checkCommonData(data, resourceType, attributes)
expect(cl[resourceType].isAddress(data.data)).toBeTruthy()
expect(cl[resourcePath].isAddress(data.data)).toBeTruthy()
return interceptRequest()
})

await cl[resourceType].create(resData, params, CommonData.options)
await cl[resourcePath].create(resData, params, CommonData.options)
.then((res: Address) => expect(res).not.toBeNull())
.catch(handleError)
.finally(() => cl.removeInterceptor('request'))
Expand All @@ -64,12 +65,12 @@ describe('Addresses resource', () => {

cl.addRequestInterceptor((request) => {
expect(request.options.method).toBe('GET')
checkCommon(request, resourceType, id, currentAccessToken)
checkCommon(request, resourcePath, id, currentAccessToken)
checkCommonParams(request, params)
return interceptRequest()
})

await cl[resourceType].retrieve(id, params, CommonData.options)
await cl[resourcePath].retrieve(id, params, CommonData.options)
.then((res: Address) => expect(res).not.toBeNull())
.catch(handleError)
.finally(() => cl.removeInterceptor('request'))
Expand All @@ -88,12 +89,12 @@ describe('Addresses resource', () => {
cl.addRequestInterceptor((request) => {
const data = JSON.parse(String(request.options.body))
expect(request.options.method).toBe('PATCH')
checkCommon(request, resourceType, resData.id, currentAccessToken)
checkCommon(request, resourcePath, resData.id, currentAccessToken)
checkCommonData(data, resourceType, attributes, resData.id)
return interceptRequest()
})

await cl[resourceType].update(resData, params, CommonData.options)
await cl[resourcePath].update(resData, params, CommonData.options)
.then((res: Address) => expect(res).not.toBeNull())
.catch(handleError)
.finally(() => cl.removeInterceptor('request'))
Expand All @@ -109,11 +110,11 @@ describe('Addresses resource', () => {

cl.addRequestInterceptor((request) => {
expect(request.options.method).toBe('DELETE')
checkCommon(request, resourceType, id, currentAccessToken)
checkCommon(request, resourcePath, id, currentAccessToken)
return interceptRequest()
})

await cl[resourceType].delete(id, CommonData.options)
await cl[resourcePath].delete(id, CommonData.options)
.catch(handleError)
.finally(() => cl.removeInterceptor('request'))

Expand All @@ -128,12 +129,12 @@ describe('Addresses resource', () => {

cl.addRequestInterceptor((request) => {
expect(request.options.method).toBe('GET')
checkCommon(request, resourceType)
checkCommon(request, resourcePath)
checkCommonParamsList(request, params)
return interceptRequest()
})

await cl[resourceType].list(params, CommonData.options)
await cl[resourcePath].list(params, CommonData.options)
.catch(handleError)
.finally(() => cl.removeInterceptor('request'))

Expand All @@ -145,9 +146,9 @@ describe('Addresses resource', () => {
it(resourceType + '.type', async () => {

const resource = { id: TestData.id, type: resourceType }
expect(cl[resourceType].isAddress(resource)).toBeTruthy()
expect(cl[resourcePath].isAddress(resource)).toBeTruthy()

const type = cl[resourceType].type()
const type = cl[resourcePath].type()
expect(type).toBe(resourceType)

})
Expand All @@ -157,10 +158,10 @@ describe('Addresses resource', () => {
/* spec.relationship.start */
it(resourceType + '.relationship', async () => {

const relId = cl[resourceType].relationship(TestData.id)
const relId = cl[resourcePath].relationship(TestData.id)
expect(isEqual(relId, { id: TestData.id, type: resourceType}))

const relResId = cl[resourceType].relationship({ id: TestData.id, type: resourceType })
const relResId = cl[resourcePath].relationship({ id: TestData.id, type: resourceType })
expect(isEqual(relResId, { id: TestData.id, type: resourceType}))

})
Expand Down Expand Up @@ -195,7 +196,7 @@ describe('Addresses resource', () => {
}
`
const res = cl[resourceType].parse(payload) as Address
const res = cl[resourcePath].parse(payload) as Address
expect(res.type).toBe(resourceType)
expect(res.reference).toBe(reference)
Expand All @@ -214,12 +215,12 @@ describe('Addresses resource', () => {

const intId = cl.addRequestInterceptor((request) => {
expect(request.options.method).toBe('GET')
checkCommon(request, resourceType, id, currentAccessToken, 'geocoder')
checkCommon(request, resourcePath, id, currentAccessToken, 'geocoder')
checkCommonParams(request, params)
return interceptRequest()
})

await cl[resourceType].geocoder(id, params, CommonData.options)
await cl[resourcePath].geocoder(id, params, CommonData.options)
.catch(handleError)
.finally(() => cl.removeInterceptor('request'))

Expand All @@ -235,12 +236,12 @@ describe('Addresses resource', () => {

const intId = cl.addRequestInterceptor((request) => {
expect(request.options.method).toBe('GET')
checkCommon(request, resourceType, id, currentAccessToken, 'events')
checkCommon(request, resourcePath, id, currentAccessToken, 'events')
checkCommonParams(request, params)
return interceptRequest()
})

await cl[resourceType].events(id, params, CommonData.options)
await cl[resourcePath].events(id, params, CommonData.options)
.catch(handleError)
.finally(() => cl.removeInterceptor('request'))

Expand All @@ -256,12 +257,12 @@ describe('Addresses resource', () => {

const intId = cl.addRequestInterceptor((request) => {
expect(request.options.method).toBe('GET')
checkCommon(request, resourceType, id, currentAccessToken, 'tags')
checkCommon(request, resourcePath, id, currentAccessToken, 'tags')
checkCommonParams(request, params)
return interceptRequest()
})

await cl[resourceType].tags(id, params, CommonData.options)
await cl[resourcePath].tags(id, params, CommonData.options)
.catch(handleError)
.finally(() => cl.removeInterceptor('request'))

Expand All @@ -277,12 +278,12 @@ describe('Addresses resource', () => {

const intId = cl.addRequestInterceptor((request) => {
expect(request.options.method).toBe('GET')
checkCommon(request, resourceType, id, currentAccessToken, 'versions')
checkCommon(request, resourcePath, id, currentAccessToken, 'versions')
checkCommonParams(request, params)
return interceptRequest()
})

await cl[resourceType].versions(id, params, CommonData.options)
await cl[resourcePath].versions(id, params, CommonData.options)
.catch(handleError)
.finally(() => cl.removeInterceptor('request'))

Expand Down
Loading

0 comments on commit 2a2b4fd

Please sign in to comment.