-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
206 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { runCommand } from "@oclif/test"; | ||
import { fail } from "assert"; | ||
|
||
export type ShellResult = { stdout: string; stderr: string; ok: boolean }; | ||
|
||
const TEST_PREFIX = "fauna_shell_integ_test_"; | ||
|
||
export const newDB = async (secret?: string): Promise<string> => { | ||
const name = TEST_PREFIX + Math.floor(Math.random() * 1000000000); | ||
|
||
return evalOk<string>( | ||
stripMargin( | ||
`|if (Database.byName('${name}').exists()) { | ||
| Database.byName('${name}').delete() | ||
|} | ||
|Database.create({ name: '${name}', typechecked: true }) | ||
|Key.create({ role: 'admin', database: '${name}' }).secret | ||
|` | ||
), | ||
secret | ||
); | ||
}; | ||
|
||
// Cleanup after ourselves. | ||
after(async () => { | ||
evalOk( | ||
stripMargin( | ||
`|Database.all().forEach((db) => { | ||
| if (db.name.startsWith('${TEST_PREFIX}')) { | ||
| db.delete() | ||
| } | ||
|}) | ||
|` | ||
) | ||
); | ||
}); | ||
|
||
export const evalOk = async <T>(code: string, secret?: string): Promise<T> => { | ||
const res = JSON.parse( | ||
await shellOk(`fauna eval "${code}" --format json`, secret) | ||
); | ||
// FIXME: This should really fail `shellOk`, but error handling is hard. | ||
if (res?.error) { | ||
fail(`Eval failed: ${res.summary}`); | ||
} | ||
|
||
return res; | ||
}; | ||
|
||
export const shellOk = async ( | ||
cmd: string, | ||
secret?: string | ||
): Promise<string> => { | ||
const res = await shell(cmd, secret); | ||
if (!res.ok) { | ||
fail(`Command unexpectedly failed:\n${res.stderr}`); | ||
} | ||
|
||
return res.stdout; | ||
}; | ||
|
||
export const shellErr = async (cmd: string): Promise<string> => { | ||
const res = await shell(cmd); | ||
if (res.ok) { | ||
fail(`Command should not have exitted succesfully:\n${res.stdout}`); | ||
} | ||
|
||
return res.stderr; | ||
}; | ||
|
||
export const stripMargin = (str: string): string => { | ||
return str | ||
.split("\n") | ||
.map((line) => { | ||
const trimmed = line.trimStart(); | ||
if (trimmed.startsWith("|")) { | ||
return trimmed.slice(1); | ||
} else { | ||
return trimmed; | ||
} | ||
}) | ||
.join("\n"); | ||
}; | ||
|
||
export const shell = async ( | ||
cmd: string, | ||
secret?: string | ||
): Promise<ShellResult> => { | ||
const parts = cmd.split(" "); | ||
if (parts[0] !== "fauna") { | ||
fail("Command must start with fauna"); | ||
} | ||
|
||
const opts = [ | ||
parts.slice(1).join(" "), | ||
"--url http://127.0.0.1:8443", | ||
`--secret ${secret ?? "secret"}`, | ||
]; | ||
|
||
const out = await runCommand(opts); | ||
|
||
return { | ||
stdout: out.stdout, | ||
stderr: out.stderr + out.error?.message, | ||
ok: out.error === undefined, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { expect } from "chai"; | ||
import { evalOk, newDB, shellOk, stripMargin } from "./base"; | ||
|
||
it("fauna schema push --stage --force works", async () => { | ||
const secret = await newDB(); | ||
|
||
await shellOk( | ||
"fauna schema push --dir test/integ/schema/start --force", | ||
secret | ||
); | ||
|
||
expect( | ||
await evalOk("Collection.all().map(.name).toArray()", secret) | ||
).to.deep.equal(["User"]); | ||
|
||
await shellOk( | ||
"fauna schema push --dir test/integ/schema/staged_index --force --stage", | ||
secret | ||
); | ||
|
||
// Index should be in the FQL definition. | ||
expect( | ||
await evalOk("Collection.byName('User')!.indexes.byName", secret) | ||
).to.deep.equal({ | ||
terms: [ | ||
{ | ||
field: ".name", | ||
mva: false, | ||
}, | ||
], | ||
queryable: true, | ||
status: "complete", | ||
}); | ||
|
||
// But, the index should not be visible on the companion object. | ||
expect( | ||
await evalOk( | ||
stripMargin( | ||
`|let user: Any = User | ||
|user.byName` | ||
), | ||
secret | ||
) | ||
).to.deep.equal(null); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
collection User { | ||
name: String | ||
email: String | ||
|
||
index byName { | ||
terms [.name] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
collection User { | ||
name: String | ||
email: String | ||
} |