Skip to content

Commit

Permalink
delete orphans from many-to-many relationship tables when deleting di…
Browse files Browse the repository at this point in the history
…scoursemes
  • Loading branch information
ausgerechnet committed Sep 8, 2024
1 parent c68ba59 commit ea5cde5
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 24 deletions.
2 changes: 1 addition & 1 deletion cads/mmda/constellation.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ class ConstellationOut(Schema):
class ConstellationDescriptionOut(Schema):

id = Integer(required=True)
discourseme_ids = List(Integer(), required=True, dump_default=[])
# discourseme_ids = List(Integer(), required=True, dump_default=[])
corpus_id = Integer(required=True)
subcorpus_id = Integer(required=True, dump_default=None, metadata={'nullable': True})
s = String(required=True)
Expand Down
10 changes: 5 additions & 5 deletions cads/mmda/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@

constellation_discourseme = db.Table(
'constellation_discourseme',
db.Column('constellation_id', db.Integer, db.ForeignKey('constellation.id')),
db.Column('discourseme_id', db.Integer, db.ForeignKey('discourseme.id'))
db.Column('constellation_id', db.Integer, db.ForeignKey('constellation.id', ondelete='CASCADE')),
db.Column('discourseme_id', db.Integer, db.ForeignKey('discourseme.id', ondelete='CASCADE')),
)

constellation_discourseme_description = db.Table(
'constellation_discourseme_description',
db.Column('constellation_description_id', db.Integer, db.ForeignKey('constellation_description.id')),
db.Column('discourseme_description_id', db.Integer, db.ForeignKey('discourseme_description.id'))
db.Column('constellation_description_id', db.Integer, db.ForeignKey('constellation_description.id', ondelete='CASCADE')),
db.Column('discourseme_description_id', db.Integer, db.ForeignKey('discourseme_description.id', ondelete='CASCADE'))
)


Expand All @@ -44,7 +44,7 @@ class Discourseme(db.Model):

template = db.RelationshipProperty("DiscoursemeTemplateItems", backref="discourseme", cascade='all, delete')

descriptions = db.relationship("DiscoursemeDescription", backref="discourseme", lazy=True)
descriptions = db.relationship("DiscoursemeDescription", backref="discourseme", passive_deletes=True, cascade='all, delete')

def generate_template(self, p='word'):
# items = set(self.template_items)
Expand Down
2 changes: 1 addition & 1 deletion cads/version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-

__version__ = "0.3.0.dev7"
__version__ = "0.3.0.dev8"
15 changes: 1 addition & 14 deletions openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -507,12 +507,6 @@
},
"type": "array"
},
"discourseme_ids": {
"items": {
"type": "integer"
},
"type": "array"
},
"id": {
"type": "integer"
},
Expand All @@ -534,7 +528,6 @@
"required": [
"corpus_id",
"discourseme_descriptions",
"discourseme_ids",
"id",
"match_strategy",
"s",
Expand All @@ -554,12 +547,6 @@
},
"type": "array"
},
"discourseme_ids": {
"items": {
"type": "integer"
},
"type": "array"
},
"id": {
"type": "integer"
},
Expand Down Expand Up @@ -1956,7 +1943,7 @@
},
"description": "cwb-cads: CWB-based API for Corpus-Assisted Discourse Studies",
"title": "cwb-cads",
"version": "0.3.0.dev7"
"version": "0.3.0.dev8"
},
"openapi": "3.0.2",
"paths": {
Expand Down
37 changes: 35 additions & 2 deletions tests/test_constellation.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ def test_constellation_concordance_filter(client, auth):
# df = concat(dfs)


@pytest.mark.now
# @pytest.mark.now
def test_constellation_collocation(client, auth):

auth_header = auth.login()
Expand Down Expand Up @@ -937,7 +937,7 @@ def test_constellation_2nd_order_collocation(client, auth):
assert int(coll_conv["O11"]) == 18


@pytest.mark.now
# @pytest.mark.now
def test_constellation_keyword(client, auth):

auth_header = auth.login()
Expand Down Expand Up @@ -1244,3 +1244,36 @@ def test_constellation_collocation_empty_queries(client, auth):
collocation_id=collocation.json['id']),
headers=auth_header)
assert collocation_items.status_code == 200


@pytest.mark.now
def test_discourseme_deletion(client, auth):

auth_header = auth.login()
with client:
client.get("/")

# get discoursemes
discoursemes = client.get(url_for('mmda.discourseme.get_discoursemes'),
content_type='application/json',
headers=auth_header).json

# constellation
constellation = client.post(url_for('mmda.constellation.create'),
json={
'name': 'factions',
'comment': 'union and FDP',
'discourseme_ids': [discourseme['id'] for discourseme in discoursemes[0:2]]
},
headers=auth_header)
assert constellation.status_code == 200

description = client.post(url_for('mmda.constellation.create_description', id=constellation.json['id']),
json={
'corpus_id': 1
},
headers=auth_header)
assert description.status_code == 200

client.delete(url_for('mmda.discourseme.delete_discourseme', id=discoursemes[0]['id']),
headers=auth_header)
21 changes: 20 additions & 1 deletion tests/test_discourseme.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def test_discourseme_patch_add_remove(client, auth):
assert 'können' not in [item['surface'] for item in description.json['items']]


@pytest.mark.now
# @pytest.mark.now
def test_get_similar(client, auth):

auth_header = auth.login()
Expand Down Expand Up @@ -196,3 +196,22 @@ def test_get_similar(client, auth):
assert similar.status_code == 200
assert len(similar.json) == 200
assert similar.json[0]['surface'] == 'Bundesregierung'


# @pytest.mark.now
def test_deletion(client, auth):

auth_header = auth.login()
with client:
client.get("/")

discoursemes = client.get(url_for('mmda.discourseme.get_discoursemes'), headers=auth_header)
kanzler = discoursemes.json[2]
description = client.post(url_for('mmda.discourseme.create_description', id=kanzler['id']),
content_type='application/json',
json={'corpus_id': 1},
headers=auth_header)
assert description.status_code == 200

client.delete(url_for('mmda.discourseme.delete_discourseme', id=kanzler['id']),
headers=auth_header)

0 comments on commit ea5cde5

Please sign in to comment.