From 2607e83420e9fdfa5f324a314f74864b4f7aafc9 Mon Sep 17 00:00:00 2001 From: Marty Byrde <45905689+Marty-Byrde@users.noreply.github.com> Date: Wed, 26 Feb 2025 17:04:19 +0100 Subject: [PATCH 1/5] ref: added ZodEffects to schemaDefaults switch The reason for this change is that when a schema property uses the .transform utility method the type of this property includes ZodEffects. Without handling it in the schemaDefault method it will throw an error. --- schemas/utils/schemaDefaults.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/schemas/utils/schemaDefaults.ts b/schemas/utils/schemaDefaults.ts index 93a676b..1513f26 100644 --- a/schemas/utils/schemaDefaults.ts +++ b/schemas/utils/schemaDefaults.ts @@ -79,6 +79,9 @@ export default function schemaDefaults Date: Wed, 26 Feb 2025 17:05:12 +0100 Subject: [PATCH 2/5] fix: transformed article_category_id to handle null values The reason for this change is that the value of this property can be of type number or null. Given the current definition of number or undefined, when the value is null it is transformed to undefined. --- schemas/article/RawArticle.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/schemas/article/RawArticle.ts b/schemas/article/RawArticle.ts index 6c956e5..99eae03 100644 --- a/schemas/article/RawArticle.ts +++ b/schemas/article/RawArticle.ts @@ -8,7 +8,10 @@ import { getRandomNumberAsString } from '@/functions/utils/randomDefaultValues' */ export const RawArticleSchema = z.object({ article_id: z.string().default(getRandomNumberAsString()).optional(), - article_category_id: z.number().optional(), + article_category_id: z + .number() + .transform((val) => (val === null ? undefined : val)) + .optional(), article_name: z.string().default('Article-XY'), article_code: z.string().default(getRandomNumberAsString()).optional(), article_eanCode: z.string().default(getRandomNumberAsString()).optional(), From 61b45a1559fb6eabdfadc2a5a39b57c278d214a1 Mon Sep 17 00:00:00 2001 From: Marty Byrde <45905689+Marty-Byrde@users.noreply.github.com> Date: Wed, 26 Feb 2025 17:08:43 +0100 Subject: [PATCH 3/5] test: added test-case to check validity of customers schema --- tests/api/Get.test.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/api/Get.test.ts b/tests/api/Get.test.ts index c6ab996..2210c3b 100644 --- a/tests/api/Get.test.ts +++ b/tests/api/Get.test.ts @@ -3,6 +3,7 @@ import GET from '@/api/GET' import { setAuthorization } from '@/config/authorization' import { RawInvoices, safeParseRawInvoices } from '@/schemas/invoice/RawInvoices' import { RawArticles, safeParseRawArticles } from '@/schemas/article/RawArticles' +import { RawCustomers, safeParseRawCustomers } from '@/schemas/customer/RawCustomers' beforeEach(() => { setAuthorization(process.env.AUTH_TOKEN!) @@ -15,7 +16,7 @@ describe('Testing #GET function: ', () => { expect(safeParseRawInvoices(response).success).toBe(true) }) - test('articles endpoint should return RawInvoices object', async () => { + test('articles endpoint should return RawArticles object', async () => { const response = await GET('articles') expect(safeParseRawArticles(response).success).toBe(true) @@ -25,4 +26,9 @@ describe('Testing #GET function: ', () => { const articles = await GET('articles') expect(articles.articles.length).toBeGreaterThan(0) }) + + test('validity of customer schema against response of /users endpoint', async () => { + const customers = await GET('users') + expect(safeParseRawCustomers(customers).success).toBe(true) + }) }) From ce9a44ef846459b9a667676040ee28053c6abe92 Mon Sep 17 00:00:00 2001 From: Marty Byrde <45905689+Marty-Byrde@users.noreply.github.com> Date: Wed, 26 Feb 2025 17:10:53 +0100 Subject: [PATCH 4/5] test: updated test-case by adding filters --- tests/api/Get.test.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/api/Get.test.ts b/tests/api/Get.test.ts index 2210c3b..d999516 100644 --- a/tests/api/Get.test.ts +++ b/tests/api/Get.test.ts @@ -17,9 +17,10 @@ describe('Testing #GET function: ', () => { }) test('articles endpoint should return RawArticles object', async () => { - const response = await GET('articles') + const response = await GET('articles', [`limit=${5}`]) expect(safeParseRawArticles(response).success).toBe(true) + expect(response.articles.length).toBeGreaterThanOrEqual(5) }) test('calling without filter params should yield results anyway', async () => { From c6725a34e0986633f148aab068019be1a3ca4ab1 Mon Sep 17 00:00:00 2001 From: Marty Byrde <45905689+Marty-Byrde@users.noreply.github.com> Date: Wed, 26 Feb 2025 17:47:23 +0100 Subject: [PATCH 5/5] test: updated test-case assertions The reason for this change is that fetching invoices may return an empty array, hence the toBeGreaterThan assertions were updated to include equal and greaterThan. --- tests/invoices/getInvoices.test.ts | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/tests/invoices/getInvoices.test.ts b/tests/invoices/getInvoices.test.ts index ab734b6..cc9d21a 100644 --- a/tests/invoices/getInvoices.test.ts +++ b/tests/invoices/getInvoices.test.ts @@ -13,25 +13,20 @@ test('getInvoices - limit of 10, Expect 10 invoices', async () => { expect(invoices.length).toBeLessThanOrEqual(10) }) -test('getInvoices - limit of 0, Expect 0 invoices', async () => { - const invoices = await getInvoices(0) - expect(invoices).toHaveLength(0) -}) - test( - 'getInvoices - limit of 1000, Expect at least 0 invoices', + 'getInvoices - limit of 1000 expect at least 0 invoices', async () => { const invoices = await getInvoices(1000) expect(Array.isArray(invoices)).toBe(true) - expect(invoices.length).toBeGreaterThan(0) + expect(invoices.length).toBeGreaterThanOrEqual(0) }, 10 * 1000, ) -test('getInvoices - limit of -1, Expect at least 0 invoices', async () => { - const invoices = await getInvoices(-1) +test('getInvoices - default limit expect array of invoices', async () => { + const invoices = await getInvoices() expect(Array.isArray(invoices)).toBe(true) - expect(invoices.length).toBeGreaterThan(0) + expect(invoices.length).toBeGreaterThanOrEqual(0) }) test('getInvoices - No Authentication set; Expect Authentication Failure', async () => {