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

Send Schema body as FormData; Format schema test file #349

Merged
merged 2 commits into from
Jul 24, 2024
Merged
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
113 changes: 66 additions & 47 deletions fsl/test-script.mjs
Original file line number Diff line number Diff line change
@@ -1,41 +1,52 @@
#!/usr/bin/env zx

/**
* This is provided in the pipeline to pass in the built version of the
* fauna CLI to be tested.
*/
* This is provided in the pipeline to pass in the built version of the
* fauna CLI to be tested.
*/
let faunaCmd;
if (argv._.length === 1) {
faunaCmd = `${argv._[0]}`
faunaCmd = `${argv._[0]}`;
} else {
faunaCmd = "fauna"
faunaCmd = "fauna";
}

const expectedCollNames = ["bye", "hi"]
const expectedFuncNames = ["sayHello"]
const secretFlag = process.env.FAUNA_SECRET
const expectedCollNames = ["bye", "hi"];
const expectedFuncNames = ["sayHello"];
const secretFlag = process.env.FAUNA_SECRET;
if (secretFlag) {
$.verbose = false
console.log("A secret was found for FAUNA_SECRET, it will be used for all fauna CLI commands")
$.verbose = false;
console.log(
"A secret was found for FAUNA_SECRET, it will be used for all fauna CLI commands"
);
}


await ensureClean()
await ensureClean();

/**
* Test Push
*/
await execFaunaCmd(["schema", "push", "--force"])
const collNames = await execPaginated("Collection.all().map(.name).order()")
if (collNames.length != expectedCollNames.length || !collNames.every((elem, idx) => elem === expectedCollNames[idx])) {
throw new Error(`Schema collections do not match actual: ${collNames} expected: ${expectedCollNames}`)
* Test Push
*/
await execFaunaCmd(["schema", "push", "--force"]);
const collNames = await execPaginated("Collection.all().map(.name).order()");
if (
collNames.length != expectedCollNames.length ||
!collNames.every((elem, idx) => elem === expectedCollNames[idx])
) {
throw new Error(
`Schema collections do not match actual: ${collNames} expected: ${expectedCollNames}`
);
}
const funcNames = await execPaginated("Function.all().map(.name)")
if (funcNames.length != expectedFuncNames.length || !funcNames.every((elem, idx) => elem === expectedFuncNames[idx])) {
throw new Error(`Schema functions do not match actual: ${collNames} expected: ${expectedCollNames}`)
const funcNames = await execPaginated("Function.all().map(.name)");
if (
funcNames.length != expectedFuncNames.length ||
!funcNames.every((elem, idx) => elem === expectedFuncNames[idx])
) {
throw new Error(
`Schema functions do not match actual: ${collNames} expected: ${expectedCollNames}`
);
}

await $`rm schema/*`
await $`rm schema/*`;

/**
* Test Pull
Expand All @@ -44,56 +55,64 @@ await $`rm schema/*`
*/
let pullProcess;
if (secretFlag) {
pullProcess = $`${faunaCmd} schema pull --secret ${secretFlag}`
pullProcess = $`${faunaCmd} schema pull --secret ${secretFlag}`;
} else {
pullProcess = $`${faunaCmd} schema pull`
pullProcess = $`${faunaCmd} schema pull`;
}
pullProcess.stdin.write('yes\n')
pullProcess.stdin.end()
await sleep(10000)
const schemaFiles = (await $`ls schema`).stdout.trim().split('\n')
if (schemaFiles.length !== 2 || schemaFiles[0] !== "collections.fsl" || schemaFiles[1] !== "sayHelloFunction.fsl") {
throw new Error(`Schema files after pull did not equal expected. Expected: ['collections.fsl, 'sayHelloFunction.fsl'] Actual: ${schemaFiles}`)
pullProcess.stdin.write("yes\n");
pullProcess.stdin.end();
await sleep(10000);
const schemaFiles = (await $`ls schema`).stdout.trim().split("\n");
if (
schemaFiles.length !== 2 ||
schemaFiles[0] !== "collections.fsl" ||
schemaFiles[1] !== "sayHelloFunction.fsl"
) {
throw new Error(
`Schema files after pull did not equal expected. Expected: ['collections.fsl, 'sayHelloFunction.fsl'] Actual: ${schemaFiles}`
);
}
console.log("Schema tests run successfully!")
console.log("Schema tests run successfully!");

async function ensureClean() {
await execFQL(`
Collection.all().forEach(.delete())
Role.all().forEach(.delete())
Function.all().forEach(.delete())
`)
`);
// await execFQL("Collection.create({ name: 'hi' })")
const respColls = await execPaginated("Collection.all() { name }")
const respColls = await execPaginated("Collection.all() { name }");
if (!Array.isArray(respColls) || respColls.length != 0) {
throw new Error(`Expected empty collection set. ${respColls}`)
throw new Error(`Expected empty collection set. ${respColls}`);
}
const respFunctions = await execPaginated("Function.all() { name }")
const respFunctions = await execPaginated("Function.all() { name }");
if (!Array.isArray(respFunctions) || respFunctions.length != 0) {
throw new Error(`Expected empty function set. ${resp}`)
throw new Error(`Expected empty function set. ${resp}`);
}
}

/**
* The provided cmd must be an array where each string you want to show up
* in the shell is an element.
* ex: execFaunaCmd(["schema", "diff"])
*/
* The provided cmd must be an array where each string you want to show up
* in the shell is an element.
* ex: execFaunaCmd(["schema", "diff"])
*/
async function execFaunaCmd(cmd) {
if (secretFlag) {
return $`${faunaCmd} ${cmd} --secret ${secretFlag}`
return $`${faunaCmd} ${cmd} --secret ${secretFlag}`;
} else {
return $`${faunaCmd} ${cmd}`
return $`${faunaCmd} ${cmd}`;
}
}

async function execPaginated(fql) {
const resp = await execFQL(fql)
return resp?.data
const resp = await execFQL(fql);
return resp?.data;
}

async function execFQL(fql) {
const resp = secretFlag ? await $`${faunaCmd} eval ${fql} --format=json --secret ${secretFlag}` : await $`${faunaCmd} eval ${fql} --format=json`
const respParsed = JSON.parse(resp._stdout)
return respParsed
const resp = secretFlag
? await $`${faunaCmd} eval ${fql} --format=json --secret ${secretFlag}`
: await $`${faunaCmd} eval ${fql} --format=json`;
const respParsed = JSON.parse(resp._stdout);
return respParsed;
}
2 changes: 0 additions & 2 deletions src/commands/schema/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ export default class PushSchemaCommand extends SchemaCommand {
method: "POST",
headers: { AUTHORIZATION: `Bearer ${secret}` },
body: this.body(files),
// @ts-expect-error-next-line
duplex: "half",
});
const json = await res.json();
if (json.error) {
Expand Down
10 changes: 3 additions & 7 deletions src/lib/schema-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,11 @@ export default abstract class SchemaCommand extends FaunaCommand {
// Helper to construct form data for a collection of files, as
// returned by `gather`.
body(files: File[]) {
const fd: Record<string, string> = {};
const fd = new FormData();
for (const file of files) {
fd[file.name] = file.content.toString();
fd.set(file.name, new Blob([file.content]));
}
const Readable = require("stream").Readable;
const s = new Readable();
s.push(JSON.stringify(fd));
s.push(null);
return s;
return fd;
}

// Reads the files using their relative-to-`basedir` paths and returns their
Expand Down