Skip to content

Commit

Permalink
feat: cypress tests for custom schema
Browse files Browse the repository at this point in the history
  • Loading branch information
sanjibansg committed Nov 2, 2023
1 parent c8299d8 commit 5db9fb7
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 11 deletions.
5 changes: 2 additions & 3 deletions api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def add_schema(
json_data = json.loads(schema["schema"])
table_name = json_data["table"] + "_" + user_id

query = "CREATE TABLE IF NOT EXISTS "
query = "CREATE TABLE "
query += table_name + "("
for field in json_data["fields"]:
query += field["name"] + " "
Expand All @@ -224,7 +224,6 @@ def add_schema(
query += ", "
query = query[:-2]
query += ");"

response = execute_duckdb(query, db_conn)
app.state.schema_cache[table_name] = None
return response
Expand All @@ -237,6 +236,7 @@ def add_schema(
@router.post("/parse/", status_code=status.HTTP_200_OK)
def parse_to_substrait(
data: dict,
headers: dict = Depends(verify_token),
db_conn: DuckDBPyConnection = Depends(get_duck_conn),
):
'''
Expand All @@ -252,7 +252,6 @@ def parse_to_substrait(
response (dict): Response JSON for translated
Substrait plan
'''
logger.info(data)
response = parse_from_duckDB(data.get("query"), db_conn)
return response

Expand Down
126 changes: 126 additions & 0 deletions client/cypress/e2e/schema.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/// <reference types="cypress" />

describe("Substrait Fiddle Custom Schema test", () => {

it("add custom schema", { browser: 'electron' }, () => {
cy.visit("/")

cy.get("select").select("sql");

cy.get("button").contains("Show/Add Schema").click();

cy.get("#addSchema").click();

cy.get("#schemaModal")
.focused()
.contains("New Schema")

cy.get("#schemaTextArea")
.type(`
{
"table": "test",
"fields": [
{
"name": "field_1",
"type": "INTEGER",
"properties": [
"NOT NULL"
]
}
]
}
`);

cy.get("button").contains("Validate").click();
cy.on('window:alert', (str) => {
expect(str).to.equal(`Schema validated!`);
});

cy.get("button").contains("Save changes").click();

cy.get("#editor")
.click()
.focused()
.type("{ctrl}a")
.clear()
.type("select * from test;");

cy.get("button").contains("Generate").click();

cy.get("#status").should("contain", "Plan generation successful!");

cy.get("svg").should("not.be.empty");

});

it("schema for lineitem table", { browser: 'electron' }, () => {
cy.visit("/")

cy.get("select").select("sql");

cy.get("button").contains("Show/Add Schema").click();

cy.get("#addSchema").click();

cy.get("#schemaModal")
.focused()
.contains("New Schema")

cy.get("#schemaTextArea")
.type(`
{
"table": "lineitem",
"fields": [
{
"name": "field_1",
"type": "INTEGER",
"properties": [
"NOT NULL"
]
}
]
}
`);

cy.get("button").contains("Validate").click();
cy.on('window:alert', (str) => {
expect(str).to.equal(`Invalid schema: Table name cannot be "lineitem"`);
});

});

it("schema with invalid format", { browser: 'electron' }, () => {
cy.visit("/")

cy.get("select").select("sql");

cy.get("button").contains("Show/Add Schema").click();

cy.get("#addSchema").click();

cy.get("#schemaModal")
.focused()
.contains("New Schema")

cy.get("#schemaTextArea")
.type(`
{
"table": "lineitem",
"field": [
{
"name": "field_1",
"type": "INTEGER",
}
]
}
`);

cy.get("button").contains("Validate").click();
cy.on('window:alert', (str) => {
expect(str).to.contain(`Invalid JSON: SyntaxError:`);
});

});

});

8 changes: 7 additions & 1 deletion client/src/components/SqlSchema.vue
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
class="form-control"
rows="4"
placeholder="Enter new schema"
id="schemaTextArea"
></textarea>
<div class="d-flex justify-content-end">
<button
Expand Down Expand Up @@ -106,7 +107,12 @@
>
Close
</button>
<button type="button" class="btn btn-success" @click="addNewSchema">
<button
type="button"
class="btn btn-success"
@click="addNewSchema"
id="addSchema"
>
Add Schema
</button>
<button
Expand Down
6 changes: 3 additions & 3 deletions client/src/components/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export const store = reactive({
this.schemas.push(schema_name);
},
async set_token() {
const sessionToken = uuidv4();
this.user = sessionToken.replace(/-/g, "_");
this.token = await generateToken(store.user_id);
const user_id = uuidv4();
this.set_user(user_id.replace(/-/g, "_"));
this.sessionToken = await generateToken(store.user_id);
},
});
16 changes: 12 additions & 4 deletions client/src/views/CodeView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,18 @@ export default {
(match) => match + "_" + store.user_id,
);
}
const duckDbRsp = await axios.post("/api/route/parse/", {
query: this.code,
});
const duckDbRsp = await axios.post(
"/api/route/parse/",
{
query: this.code,
},
{
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${store.sessionToken}`,
},
},
);
if (store.schemas.length) {
const pattern = new RegExp("_" + store.user_id, "g");
duckDbRsp.data = duckDbRsp.data.replace(pattern, "");
Expand Down

0 comments on commit 5db9fb7

Please sign in to comment.