Skip to content

Commit

Permalink
add test to ensure del_msar_schema works as intended
Browse files Browse the repository at this point in the history
  • Loading branch information
Anish9901 committed Jan 4, 2024
1 parent 7461fdc commit 4b138eb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion mathesar/api/db/viewsets/databases.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def get_queryset(self):

def destroy(self, request, pk=None):
db_object = self.get_object()
if request.query_params.get('del_msar_schemas').lower()=='true':
if request.query_params.get('del_msar_schemas').lower() == 'true':
engine = db_object._sa_engine
uninstall_mathesar_from_database(engine)
db_object.delete()
Expand Down
29 changes: 29 additions & 0 deletions mathesar/tests/api/test_database_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from mathesar.state.django import reflect_db_objects
from mathesar.models.base import Table, Schema, Database
from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist
from db.install import install_mathesar
from db.engine import create_future_engine_with_custom_types


Expand Down Expand Up @@ -143,6 +145,33 @@ def test_database_list_deleted(client, db_dj_model):
check_database(db_dj_model, response_data['results'][0])


def test_delete_dbconn_with_msar_schemas(client, db_dj_model):
# install mathesar specific schemas
install_mathesar(
db_dj_model.name,
db_dj_model.username,
db_dj_model.password,
db_dj_model.host,
db_dj_model.port,
True
)
engine = db_dj_model._sa_engine
check_schema_exists = text(
"SELECT schema_name FROM information_schema.schemata \
WHERE schema_name LIKE '%msar' OR schema_name = 'mathesar_types';"
)
with engine.connect() as conn:
before_deletion = conn.execute(check_schema_exists)
response = client.delete(f'/api/db/v0/connections/{db_dj_model.id}/?del_msar_schemas=true')
after_deletion = conn.execute(check_schema_exists)

with pytest.raises(ObjectDoesNotExist):
Database.objects.get(id=db_dj_model.id)
assert response.status_code == 204
assert before_deletion.rowcount == 3
assert after_deletion.rowcount == 0


def test_database_detail(client, db_dj_model):
response = client.get(f'/api/db/v0/connections/{db_dj_model.id}/')
response_database = response.json()
Expand Down

0 comments on commit 4b138eb

Please sign in to comment.