Skip to content

Commit

Permalink
Fix tests for single
Browse files Browse the repository at this point in the history
  • Loading branch information
j3lte committed Dec 6, 2023
1 parent 0402cd7 commit 287debe
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
18 changes: 18 additions & 0 deletions src/Query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,24 @@ export class SodaQuery<T> {
}));
}

single(
queryID?: string,
signal?: AbortSignal,
): DataResponse<T & ExtraDataFields> {
const currentLimit = this.#limit;
this.#limit = 1;
return this.execute(queryID, signal).then((res) => {
this.#limit = currentLimit;
return {
...res,
data: res.data?.[0] ?? null,
};
}).catch((err) => {
this.#limit = currentLimit;
return Promise.reject(err);
});
}

executeGeoJSON(
queryID?: string,
signal?: AbortSignal,
Expand Down
33 changes: 26 additions & 7 deletions test/Query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,22 +114,41 @@ Deno.test("SodaQuery QueryObj", () => {
Deno.test("SodaQuery (request)", async () => {
const query = createSampleQuery<{ test: number }>();

const res = new Response(JSON.stringify([{ test: 1 }]), {
status: 200,
headers: {
"Last-Modified": "Mon, 01 Jan 2001 00:00:00 GMT",
"ETag": "test",
},
});

await mockFetch(
createRequest(),
new Response(JSON.stringify([{ test: 1 }]), {
status: 200,
headers: {
"Last-Modified": "Mon, 01 Jan 2001 00:00:00 GMT",
"ETag": "test",
},
}),
res,
);
await mockFetch(
createRequest("https://test.example.com/resource/test.json?$limit=1"),
res,
);

const d = await query.execute();
const single = await query.single();
assertEquals(d.data[0]?.test, 1);
assertEquals(single.data.test, 1);
assertEquals(query.headers.lastModified, "Mon, 01 Jan 2001 00:00:00 GMT");
assertEquals(query.headers.etag, "test");

const emptyQuery = createSampleQuery<{ test: number }>();
await mockFetch(
createRequest("https://test.example.com/resource/test.json?$limit=1"),
new Response(JSON.stringify([]), {
status: 200,
}),
);

const emptySingle = await emptyQuery.single();
assertEquals(emptySingle.data, null);

// With Access Token
const query2 = createSampleQuery<{ test: number }>({
accessToken: "test",
Expand Down

0 comments on commit 287debe

Please sign in to comment.