From 38695c37fc9b46ae785a47a50fd1e0eced35a380 Mon Sep 17 00:00:00 2001 From: Max Kostow Date: Mon, 5 Jun 2023 16:49:10 -0700 Subject: [PATCH] fix maybeSelect w/ throwOnError w/ n > 1 results --- src/PostgrestBuilder.ts | 6 +++--- test/basic.ts | 31 ++++++++++++++++++++++++------- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/PostgrestBuilder.ts b/src/PostgrestBuilder.ts index e48d69f0..0b5fed83 100644 --- a/src/PostgrestBuilder.ts +++ b/src/PostgrestBuilder.ts @@ -153,10 +153,10 @@ export default abstract class PostgrestBuilder status = 200 statusText = 'OK' } + } - if (error && this.shouldThrowOnError) { - throw error - } + if (error && this.shouldThrowOnError) { + throw error } const postgrestResponse = { diff --git a/test/basic.ts b/test/basic.ts index 9e7951fd..bffe54cd 100644 --- a/test/basic.ts +++ b/test/basic.ts @@ -665,18 +665,35 @@ test('connection error w/ throwOnError', async () => { expect(isErrorCaught).toBe(true) }) -test('maybeSingle w/ throwOnError', async () => { - let passes = true - await postgrest +test('maybeSingle w/ throwOnError (0 results)', async () => { + const res = await postgrest .from('messages') .select() .eq('message', 'i do not exist') .throwOnError() .maybeSingle() - .then(undefined, () => { - passes = false - }) - expect(passes).toEqual(true) + expect(res).toMatchInlineSnapshot(` + Object { + "count": null, + "data": null, + "error": null, + "status": 200, + "statusText": "OK", + } + `) +}) + +test('maybeSingle w/ throwOnError (2 results)', async () => { + await expect(async () => { + await postgrest.from('messages').select().throwOnError().maybeSingle() + }).rejects.toMatchInlineSnapshot(` + Object { + "code": "PGRST116", + "details": "Results contain 2 rows, application/vnd.pgrst.object+json requires 1 row", + "hint": null, + "message": "JSON object requested, multiple (or no) rows returned", + } + `) }) test("don't mutate PostgrestClient.headers", async () => {