Skip to content

Commit

Permalink
Delete tasks with datasource
Browse files Browse the repository at this point in the history
  • Loading branch information
mawandm committed Apr 19, 2024
1 parent 4c7237a commit 18241db
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
2 changes: 1 addition & 1 deletion nesis/api/core/services/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def get_enabled_datasources():
return session.query(Datasource).filter(Datasource.enabled.is_(True)).all()

def get_enabled_tasks():
return session.query(Task).filter(Datasource.enabled.is_(True)).all()
return session.query(Task).filter(Task.enabled.is_(True)).all()

if session_user.get("root") or False:
match resource_type:
Expand Down
12 changes: 12 additions & 0 deletions nesis/api/core/services/datasources.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,18 @@ def delete(self, **kwargs):

session.delete(datasource)
session.commit()

tasks = self._task_service.get(
token=kwargs["token"], parent_id=datasource_id
)
for task in tasks:
try:
self._task_service.delete(
token=kwargs["token"], task_id=task.uuid
)
except:
self._LOG.exception(f"Failed to delete task {task.uuid}")

except:
self._LOG.exception(f"Error when deleting setting")
raise
Expand Down
62 changes: 62 additions & 0 deletions nesis/api/tests/core/controllers/test_datasources.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,3 +332,65 @@ def test_update_datasource_schedule(client, tc):
assert 200 == response.status_code, response.json
assert 1 == len(response.json["items"])
assert response.json["items"][0]["schedule"] == datasource["schedule"]


def test_delete_datasource_schedule(client, tc):
# This test focuses on the delete functionality. We want to ensure that when a datasource is deleted, all
# corresponding tasks are delete too
payload = {
"type": "minio",
"name": "finance6",
"connection": {
"user": "caikuodda",
"password": "some.password",
"host": "localhost",
"port": "5432",
"database": "initdb",
},
"schedule": "*/5 * * * *",
}

admin_session = get_admin_session(client=client)

response = client.post(
f"/v1/datasources",
headers=tests.get_header(token=admin_session["token"]),
data=json.dumps(payload),
)
assert 200 == response.status_code, response.text
datasource = response.json

# Test that we have only the one datasource
response = client.get(
"/v1/datasources", headers=tests.get_header(token=admin_session["token"])
)
assert 200 == response.status_code, response.json
assert 1 == len(response.json["items"])

# Test that we have only the one task
response = client.get(
"/v1/tasks", headers=tests.get_header(token=admin_session["token"])
)
assert 200 == response.status_code, response.json
assert 1 == len(response.json["items"])

#
# Delete the datasource
response = client.delete(
f"/v1/datasources/{datasource['id']}",
headers=tests.get_header(token=admin_session["token"]),
)
assert 200 == response.status_code, response.json

response = client.get(
"/v1/datasources", headers=tests.get_header(token=admin_session["token"])
)
assert 200 == response.status_code, response.json
assert 0 == len(response.json["items"])

# Test that we have only the one task
response = client.get(
"/v1/tasks", headers=tests.get_header(token=admin_session["token"])
)
assert 200 == response.status_code, response.json
assert 0 == len(response.json["items"])

0 comments on commit 18241db

Please sign in to comment.