Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/use fetch #40

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 22 additions & 41 deletions __tests__/functional/client-configuration.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import fetch from "jest-fetch-mock";
import { Client } from "../../src/client";
import { endpoints } from "../../src/client-configuration";

jest.mock("../../src/wire-protocol");

beforeEach(() => {
delete process.env["FAUNA_SECRET"];
});
Expand Down Expand Up @@ -57,47 +60,25 @@ an environmental variable named FAUNA_SECRET or pass it to the Client constructo
secret: "secret",
timeout_ms: 60_000,
});
expect(client.client.defaults.baseURL).toEqual("http://localhost:7443/");
const result = await client.query<number>({ query: '"taco".length' });
expect(result.txn_time).not.toBeUndefined();
expect(result).toEqual({ data: 4, txn_time: result.txn_time });
});

type HeaderTestInput = {
fieldName: "linearized" | "max_contention_retries" | "tags" | "traceparent";
fieldValue: any;
expectedHeader: string;
};
expect(client.clientConfiguration.endpoint.href).toEqual(
"http://localhost:7443/"
);

it.each`
fieldName | fieldValue | expectedHeader
${"linearized"} | ${true} | ${"x-linearized: true"}
${"max_contention_retries"} | ${3} | ${"x-max-contention-retries: 3"}
${"tags"} | ${{ t1: "v1", t2: "v2" }} | ${"x-fauna-tags: t1=v1,t2=v2"}
${"traceparent"} | ${"00-750efa5fb6a131eb2cf4db39f28366cb-5669e71839eca76b-00"} | ${"traceparent: 00-750efa5fb6a131eb2cf4db39f28366cb-5669e71839eca76b-00"}
`(
"Setting clientConfiguration $fieldName leads to it being sent in headers",
async ({ fieldName, fieldValue, expectedHeader }: HeaderTestInput) => {
const client = new Client({
endpoint: endpoints.local,
max_conns: 5,
secret: "secret",
timeout_ms: 5000,
[fieldName]: fieldValue,
});
client.client.interceptors.response.use(function (response) {
expect(response.request?._header).not.toBeUndefined();
if (response.request?._header) {
expect(response.request._header).toEqual(
expect.stringContaining("x-timeout-ms: 5000")
);
expect(response.request._header).toEqual(
expect.stringContaining(`\n${expectedHeader}`)
);
}
return response;
});
await client.query<number>({ query: '"taco".length' });
}
);
fetch.mockResponseOnce(
JSON.stringify({ data: { length: 4, txn_time: Date.now() } })
);

const result = await client.query({ query: '"taco".length' });
expect(result.txn_time).not.toBeUndefined();
expect(result).toEqual({
data: {
data: {
...result.data.data,
length: 4,
},
},
txn_time: result.txn_time,
});
});
});
73 changes: 32 additions & 41 deletions __tests__/integration/client-last-txn-tracking.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fetch from "jest-fetch-mock";
import { Client } from "../../src/client";
import { endpoints } from "../../src/client-configuration";
import { env } from "process";
Expand All @@ -11,38 +12,33 @@ describe("last_txn tracking in client", () => {
secret: env["secret"] || "secret",
timeout_ms: 60_000,
});
let expectedLastTxn: string | undefined = undefined;
myClient.client.interceptors.response.use(function (response) {
expect(response.request?._header).not.toBeUndefined();
if (expectedLastTxn === undefined) {
expect(response.request?._header).not.toEqual(
expect.stringContaining("x-last-txn")
);
} else {
expect(response.request?._header).toEqual(
expect.stringContaining(`\nx-last-txn: ${expectedLastTxn}`)
);
}
return response;
});

fetch.mockResponseOnce(JSON.stringify({ data: { txn_time: Date.now() } }));

const resultOne = await myClient.query({
query:
"\
if (Collection.byName('Customers') == null) {\
Collection.create({ name: 'Customers' })\
}",
});

expect(resultOne.txn_time).not.toBeUndefined();
expectedLastTxn = resultOne.txn_time;

fetch.mockResponseOnce(JSON.stringify({ data: { txn_time: Date.now() } }));

const resultTwo = await myClient.query(
fql`
if (Collection.byName('Orders') == null) {
Collection.create({ name: 'Orders' })
}`
);

expect(resultTwo.txn_time).not.toBeUndefined();
expect(resultTwo.txn_time).not.toEqual(resultOne.txn_time);
expectedLastTxn = resultTwo.txn_time;

fetch.mockResponseOnce(JSON.stringify({ data: { txn_time: Date.now() } }));

const resultThree = await myClient.query({
query:
"\
Expand All @@ -61,48 +57,43 @@ if (Collection.byName('Products') == null) {\
secret: env["secret"] || "secret",
timeout_ms: 60_000,
});
let expectedLastTxn: string | undefined = undefined;
myClient.client.interceptors.response.use(function (response) {
expect(response.request?._header).not.toBeUndefined();
if (expectedLastTxn === undefined) {
expect(response.request?._header).not.toEqual(
expect.stringContaining("x-last-txn")
);
} else {
expect(response.request?._header).toEqual(
expect.stringContaining(`\nx-last-txn: ${expectedLastTxn}`)
);
}
return response;
});

fetch.mockResponseOnce(JSON.stringify({ data: { txn_time: Date.now() } }));

const resultOne = await myClient.query({
query:
"\
if (Collection.byName('Customers') == null) {\
Collection.create({ name: 'Customers' })\
}",
if (Collection.byName('Customers') == null) {\
Collection.create({ name: 'Customers' })\
}",
});

expect(resultOne.txn_time).not.toBeUndefined();
expectedLastTxn = resultOne.txn_time;

fetch.mockResponseOnce(JSON.stringify({ data: { txn_time: Date.now() } }));

const resultTwo = await myClient.query(
fql`
if (Collection.byName('Orders') == null) {\
Collection.create({ name: 'Orders' })\
}
`,
if (Collection.byName('Orders') == null) {\
Collection.create({ name: 'Orders' })\
}
`,
{
last_txn: resultOne.txn_time,
}
);
expect(resultTwo.txn_time).not.toBeUndefined();
expect(resultTwo.txn_time).not.toEqual(resultOne.txn_time);

fetch.mockResponseOnce(JSON.stringify({ data: { txn_time: Date.now() } }));

const resultThree = await myClient.query({
last_txn: resultOne.txn_time,
query:
"\
if (Collection.byName('Products') == null) {\
Collection.create({ name: 'Products' })\
}",
if (Collection.byName('Products') == null) {\
Collection.create({ name: 'Products' })\
}",
});
expect(resultThree.txn_time).not.toBeUndefined();
expect(resultThree.txn_time).not.toEqual(resultTwo.txn_time);
Expand Down
178 changes: 0 additions & 178 deletions __tests__/integration/connection-pool.test.ts

This file was deleted.

Loading