Skip to content

Commit

Permalink
trying to create test for api:move_publish,small fixes to make it work
Browse files Browse the repository at this point in the history
  • Loading branch information
wingechr committed Aug 30, 2024
1 parent ebab2bb commit ef77b8c
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 14 deletions.
4 changes: 2 additions & 2 deletions api/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1556,7 +1556,7 @@ def move(from_schema, table, to_schema):
except DBTable.DoesNotExist:
raise APIError("Table for schema movement not found")
try:
to_schema_reg = DBSchema.objects.get(name=to_schema)
to_schema_reg, _ = DBSchema.objects.get_or_create(name=to_schema)
except DBSchema.DoesNotExist:
raise APIError("Target schema not found")
if from_schema == to_schema:
Expand Down Expand Up @@ -1637,7 +1637,7 @@ def move_publish(from_schema, table_name, to_schema, embargo_period):

try:
t = DBTable.objects.get(name=table_name, schema__name=from_schema)
to_schema_reg = DBSchema.objects.get(name=to_schema)
to_schema_reg, _ = DBSchema.objects.get_or_create(name=to_schema)

if from_schema == to_schema:
raise APIError("Target schema same as current schema")
Expand Down
33 changes: 21 additions & 12 deletions api/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ def assertDictEqualKeywise(self, d1, d2, excluded=None):

def api_req(
self,
method,
table=None,
schema=None,
path=None,
data=None,
method: str,
table: str = None,
schema: str = None,
path: str = None,
data: dict = None,
auth=None,
exp_code=None,
exp_code: int = None,
exp_res=None,
):
path = path or ""
Expand Down Expand Up @@ -116,7 +116,14 @@ def api_req(
exp_code = 201
else:
exp_code = 200
self.assertEqualJson(resp.status_code, exp_code, msg=json_resp)

if not isinstance(exp_code, (list, tuple)):
exp_code = [exp_code]

self.assertTrue(
resp.status_code in exp_code,
f"Status {resp.status_code} not in {exp_code}: {json_resp}",
)

if exp_res:
if json_resp and "data" in json_resp:
Expand All @@ -125,7 +132,9 @@ def api_req(

return json_resp

def create_table(self, structure=None, data=None, schema=None, table=None):
def create_table(
self, structure=None, data=None, schema=None, table=None, exp_code=201
):
# default structure
structure = structure or {"columns": [{"name": "id", "data_type": "bigint"}]}
self.api_req("put", table, schema, data={"query": structure})
Expand All @@ -136,11 +145,11 @@ def create_table(self, structure=None, data=None, schema=None, table=None):
schema,
"rows/new",
data={"query": data},
exp_code=201,
exp_code=exp_code,
)

def drop_table(self, schema=None, table=None):
self.api_req("delete", table, schema)
def drop_table(self, schema=None, table=None, exp_code=200):
self.api_req("delete", table, schema, exp_code=exp_code)


class APITestCaseWithTable(APITestCase):
Expand Down Expand Up @@ -183,5 +192,5 @@ def setUp(self) -> None:
self.create_table(structure=self.test_structure, data=self.test_data)

def tearDown(self) -> None:
super().setUp()
super().tearDown()
self.drop_table()
84 changes: 84 additions & 0 deletions api/tests/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,87 @@ def test_simple(self):
self.api_req("get", exp_code=404)
# create table again so that teardown works
self.create_table(self.test_structure)


class TestMove(APITestCaseWithTable):
test_table = "test_table_move"
test_schema = "model_draft" # cannot move from "test" schema
target_schema = "scenario"

def test_move(self):
self.api_req(
"post",
path=f"move/{self.target_schema}/",
exp_code=200,
)

# check that target table exists, and source does not exist anymore
self.api_req("get", exp_code=404)
self.api_req("get", schema=self.target_schema, exp_code=200)

# move back
self.api_req(
"post",
schema=self.target_schema,
path=f"move/{self.test_schema}/",
exp_code=200,
)


class TestMovePublish(APITestCaseWithTable):
test_table = "test_table_move_publish"
test_schema = "model_draft" # cannot move from "test" schema
target_schema = "scenario"

def test_move_publish(self):
# this will fail, because the licenses check fails
self.api_req(
"post", path=f"move_publish/{self.target_schema}/", exp_code=400, exp_res={}
)

# so we set the metadata ...
self.api_req(
"post",
path="meta/",
data={"id": self.test_table, "licenses": [{"name": "CC-BY-4.0"}]},
exp_code=200,
)

# .. now we do it
embargo_duration = "6_months"
self.api_req(
"post",
path=f"move_publish/{self.target_schema}/",
data={"embargo": {"duration": embargo_duration}},
exp_code=200,
)

# check that target table exists, and source does not exist anymore
self.api_req("get", exp_code=404)
self.api_req("get", schema=self.target_schema, exp_code=200)

# move back so we can publish again
self.api_req(
"post",
schema=self.target_schema,
path=f"move/{self.test_schema}/",
exp_code=200,
)

# publish again with valid embargo
# .. now we do it, but without (valid) setting embargo
embargo_duration = "none"
self.api_req(
"post",
path=f"move_publish/{self.target_schema}/",
data={"embargo": {"duration": embargo_duration}},
exp_code=200,
)

# move back so cleanup works
self.api_req(
"post",
schema=self.target_schema,
path=f"move/{self.test_schema}/",
exp_code=200,
)
2 changes: 2 additions & 0 deletions api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ def wrapper(*args, **kwargs):
return JsonResponse({"reason": e.message}, status=e.status)
except KeyError as e:
return JsonResponse({"reason": e}, status=400)
except DBTable.DoesNotExist:
return JsonResponse({"reason": "table does not exist"}, status=404)

return wrapper

Expand Down

0 comments on commit ef77b8c

Please sign in to comment.