Skip to content

Commit

Permalink
Fixed PR reviews, updated transformation typing and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
punith300i committed Nov 17, 2023
1 parent 9d4858f commit 10e3a4c
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 55 deletions.
20 changes: 7 additions & 13 deletions sand/controllers/transformation.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class TransformRequestPayload:
mode: str
datapath: Union[str, List[str]]
code: str
tolerance: int
rows: Optional[int] = None
outputpath: Optional[Union[str, List[str]]] = None


Expand Down Expand Up @@ -257,11 +259,7 @@ def transform():
[table_row.row[col_index] for col_index in col_index_list],
Context(index=table_row.index, row=table_row.row),
)
for table_row in table_rows[
: int(request.args["rows"])
if request.args["rows"] not in ["null", "undefined"]
else None
]
for table_row in table_rows[: request_data.rows]
)

transformed_data = None
Expand All @@ -271,31 +269,27 @@ def transform():
raise BadRequest(
"For transform type map the outputpath should be a single column"
)
transformed_data = transform_map(
transform_func, data, int(request.args["tolerance"])
)
transformed_data = transform_map(transform_func, data, request_data.tolerance)

elif request_data.type == "filter":
if request_data.outputpath and len(request_data.outputpath) != 1:
raise BadRequest(
"For transform type map the outputpath should be a single column"
)
transformed_data = transform_filter(
transform_func, data, int(request.args["tolerance"])
transform_func, data, request_data.tolerance
)

elif request_data.type == "split":
if request_data.outputpath is None:
raise BadRequest(
"transform type split needs to have outputpath defined in the request body"
)
transformed_data = transform_split(
transform_func, data, int(request.args["tolerance"])
)
transformed_data = transform_split(transform_func, data, request_data.tolerance)

elif request_data.type == "concatenate":
transformed_data = transform_concatenate(
transform_func, data, int(request.args["tolerance"])
transform_func, data, request_data.tolerance
)

return jsonify(transformed_data)
5 changes: 3 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from sand.helpers.dependency_injection import use_auto_inject
from sand.models import all_tables
from sand.models.base import StoreWrapper, db, init_db
from flask.testing import FlaskClient


def get_entity_db(dbfile, proxy=False) -> StoreWrapper:
Expand Down Expand Up @@ -92,7 +93,7 @@ def client():


@pytest.fixture
def example_db(client):
def example_db(client: FlaskClient):
try:
from sand.config import _ROOT_DIR

Expand Down Expand Up @@ -137,7 +138,7 @@ def example_db(client):
"order": 1,
"insert_after": None,
}
res = client.post("/api/transformation", json=transformation_data)
resp = client.post("/api/transformation", json=transformation_data)
yield None
finally:
for table in all_tables:
Expand Down
39 changes: 21 additions & 18 deletions tests/test_transformation_api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
def test_api_transformation_map_single_line(client, example_db):
from flask.testing import FlaskClient


def test_api_transformation_map_single_line(client: FlaskClient, example_db):
resp = client.post(
"/api/transformation/test",
json={
Expand Down Expand Up @@ -45,7 +48,7 @@ def test_api_transformation_map_single_line(client, example_db):
assert response == transformed_data


def test_api_transformation_map_single_line_rows(client, example_db):
def test_api_transformation_map_single_line_rows(client: FlaskClient, example_db):
resp = client.post(
"/api/transformation/test",
json={
Expand Down Expand Up @@ -76,7 +79,7 @@ def test_api_transformation_map_single_line_rows(client, example_db):
assert response == transformed_data


def test_api_transformation_map_outputpath(client, example_db):
def test_api_transformation_map_outputpath(client: FlaskClient, example_db):
resp = client.post(
"/api/transformation/test",
json={
Expand All @@ -101,7 +104,7 @@ def test_api_transformation_map_outputpath(client, example_db):
assert response == transformed_data


def test_api_transformation_map_single_line_context(client, example_db):
def test_api_transformation_map_single_line_context(client: FlaskClient, example_db):
resp = client.post(
"/api/transformation/test",
json={
Expand Down Expand Up @@ -152,7 +155,7 @@ def test_api_transformation_map_single_line_context(client, example_db):
assert response == transformed_data


def test_api_transformation_map_single_line_fail(client, example_db):
def test_api_transformation_map_single_line_fail(client: FlaskClient, example_db):
resp = client.post(
"/api/transformation/test",
json={
Expand Down Expand Up @@ -194,7 +197,7 @@ def test_api_transformation_map_single_line_fail(client, example_db):
assert response == transformed_data


def test_api_transformation_map_multiline_error(client, example_db):
def test_api_transformation_map_multiline_error(client: FlaskClient, example_db):
resp = client.post(
"/api/transformation/test",
json={
Expand Down Expand Up @@ -246,7 +249,7 @@ def error_func():
assert response == transformed_data


def test_api_transformation_map_multiline_multi_error(client, example_db):
def test_api_transformation_map_multiline_multi_error(client: FlaskClient, example_db):
resp = client.post(
"/api/transformation/test",
json={
Expand Down Expand Up @@ -299,7 +302,7 @@ def error_func():
assert response == transformed_data


def test_api_transformation_map_multiline(client, example_db):
def test_api_transformation_map_multiline(client: FlaskClient, example_db):
resp = client.post(
"/api/transformation/test",
json={
Expand Down Expand Up @@ -346,7 +349,7 @@ def test_api_transformation_map_multiline(client, example_db):
assert response == transformed_data


def test_api_transformation_filter_multiline(client, example_db):
def test_api_transformation_filter_multiline(client: FlaskClient, example_db):
resp = client.post(
"/api/transformation/test",
json={
Expand Down Expand Up @@ -391,7 +394,7 @@ def test_api_transformation_filter_multiline(client, example_db):
assert response == transformed_data


def test_api_transformation_filter_single_line_fail(client, example_db):
def test_api_transformation_filter_single_line_fail(client: FlaskClient, example_db):
resp = client.post(
"/api/transformation/test",
json={
Expand Down Expand Up @@ -434,7 +437,7 @@ def test_api_transformation_filter_single_line_fail(client, example_db):
assert response == transformed_data


def test_api_transformation_split_single_line(client, example_db):
def test_api_transformation_split_single_line(client: FlaskClient, example_db):
resp = client.post(
"/api/transformation/test",
json={
Expand Down Expand Up @@ -490,7 +493,7 @@ def test_api_transformation_split_single_line(client, example_db):
assert response == transformed_data


def test_api_transformation_concatenate_single_line(client, example_db):
def test_api_transformation_concatenate_single_line(client: FlaskClient, example_db):
resp = client.post(
"/api/transformation/test",
json={
Expand Down Expand Up @@ -573,7 +576,7 @@ def test_api_transformation_concatenate_single_line(client, example_db):
assert response == transformed_data


def test_api_transformation_compilation_error(client, example_db):
def test_api_transformation_compilation_error(client: FlaskClient, example_db):
resp = client.post(
"/api/transformation/test",
json={
Expand Down Expand Up @@ -605,7 +608,7 @@ def error_func():
}


def test_api_transformation_map_single_line_str(client, example_db):
def test_api_transformation_map_single_line_str(client: FlaskClient, example_db):
resp = client.post(
"/api/transformation/test",
json={
Expand Down Expand Up @@ -683,7 +686,7 @@ def error_func():
]


def test_api_transformation_create(client, example_db):
def test_api_transformation_create(client: FlaskClient, example_db):
resp = client.post(
"/api/transformation",
json={
Expand Down Expand Up @@ -721,7 +724,7 @@ def test_api_transformation_create(client, example_db):
assert response == response_data


def test_api_transformation_get_one(client, example_db):
def test_api_transformation_get_one(client: FlaskClient, example_db):
resp = client.get("/api/transformation/1")
response_data = {
"code": "abort",
Expand All @@ -744,7 +747,7 @@ def test_api_transformation_get_one(client, example_db):
assert response == response_data


def test_api_transformation_get_all(client, example_db):
def test_api_transformation_get_all(client: FlaskClient, example_db):
resp = client.get("/api/transformation")
response_data = {
"items": [
Expand Down Expand Up @@ -772,7 +775,7 @@ def test_api_transformation_get_all(client, example_db):
assert response == response_data


def test_api_transformation_delete(client, example_db):
def test_api_transformation_delete(client: FlaskClient, example_db):
resp = client.delete("/api/transformation/1")
response_data = {"status": "success"}

Expand Down
41 changes: 24 additions & 17 deletions www/src/models/transformation/TransformationStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ export interface Transformation extends Record<number> {

export interface DraftCreateTransformation extends Omit<Transformation, "id"> {
draftID: string;
tolerance: number;
rows: number;
}

export interface DraftUpdateTransformation extends Transformation {
Expand All @@ -35,24 +33,33 @@ export class TransformationStore extends CRUDStore<
}

async testTransformation(
payload: DraftCreateTransformation
payload: DraftCreateTransformation | Transformation,
tolerance: number,
rows: number
): Promise<TransformationResult[] | undefined> {
let resp: any = await axios
.post(
`${SERVER}/api/transformation/test?tolerance=${
payload.tolerance
}&rows=${payload!.rows}`,
{
table_id: payload.tableId,
type: payload.type,
code: payload.code,
mode: payload.mode,
datapath: payload.datapath,
outputpath: payload.outputpath,
}
)
.post(`${SERVER}/api/transformation/test`, {
table_id: payload.tableId,
type: payload.type,
code: payload.code,
mode: payload.mode,
datapath: payload.datapath,
outputpath: payload.outputpath,
tolerance: tolerance,
rows: rows,
})
.then((res) => res.data)
.catch((error) => error.response.data.message);
.catch((error) => {
if (
axios.isAxiosError(error) &&
error.response &&
error.response.status === 400
) {
return error.response.data.message;
} else {
return Promise.reject(error);
}
});
return resp;
}
}
14 changes: 9 additions & 5 deletions www/src/pages/table/forms/TransformationForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
TransformationResult,
useStores,
DraftCreateTransformation,
Transformation,
Table as TableModel,
} from "../../../models";

Expand Down Expand Up @@ -83,21 +84,24 @@ export const TransformationForm = observer(
];

const onExecute = async () => {
const transformationPayload: DraftCreateTransformation = {
draftID: "-1",
const transformationPayload:
| DraftCreateTransformation
| Transformation = {
draftID: table.id.toString(),
id: -1,
tableId: table.id,
type: form.getFieldValue("type"),
code: form.getFieldValue("code"),
mode: "restrictedpython",
onError: form.getFieldValue("onerror"),
datapath: form.getFieldValue("datapath"),
outputpath: form.getFieldValue("outputpath"),
tolerance: form.getFieldValue("tolerance"),
rows: form.getFieldValue("rows"),
};

let response = await transformationStore.testTransformation(
transformationPayload
transformationPayload,
form.getFieldValue("tolerance"),
form.getFieldValue("rows")
);
setResult(response);
};
Expand Down

0 comments on commit 10e3a4c

Please sign in to comment.