From: Chris Fulljames Date: Fri, 31 Jan 2025 23:52:10 +0000 (-0500) Subject: Add tests for deleting parent objects X-Git-Url: https://littlesong.place/gitweb/?a=commitdiff_plain;h=fee2123c20198c42a0b39b3d83fcde3f7dbb9a2c;p=littlesongplace.git Add tests for deleting parent objects --- diff --git a/schema_update.sql b/schema_update.sql index 6e81b3e..3e0a353 100644 --- a/schema_update.sql +++ b/schema_update.sql @@ -7,8 +7,8 @@ CREATE TABLE IF NOT EXISTS song_comments ( replytoid INTEGER, created TEXT NOT NULL, content TEXT NOT NULL, - FOREIGN KEY(songid) REFERENCES songs(songid), - FOREIGN KEY(userid) REFERENCES users(userid) + FOREIGN KEY(songid) REFERENCES songs(songid) ON DELETE CASCADE, + FOREIGN KEY(userid) REFERENCES users(userid) ON DELETE CASCADE ); CREATE INDEX IF NOT EXISTS idx_comments_by_song ON song_comments(songid); CREATE INDEX IF NOT EXISTS idx_comments_by_user ON song_comments(userid); @@ -18,8 +18,8 @@ CREATE TABLE IF NOT EXISTS song_comment_notifications ( notificationid INTEGER PRIMARY KEY, commentid INTEGER NOT NULL, targetuserid INTEGER NOT NULL, - FOREIGN KEY(commentid) REFERENCES song_comments(commentid), - FOREIGN KEY(targetuserid) REFERENCES users(userid) + FOREIGN KEY(commentid) REFERENCES song_comments(commentid) ON DELETE CASCADE, + FOREIGN KEY(targetuserid) REFERENCES users(userid) ON DELETE CASCADE ); CREATE INDEX IF NOT EXISTS idx_song_comment_notifications_by_target ON song_comment_notifications(targetuserid); diff --git a/test/test_offline.py b/test/test_offline.py index 4e0ae56..1833f57 100644 --- a/test/test_offline.py +++ b/test/test_offline.py @@ -575,6 +575,15 @@ def test_delete_comment(client): response = client.get("/song/1/1?action=view") assert b"comment text here" not in response.data +def test_delete_song_with_comments(client): + _create_user_song_and_comment(client, "comment text here") + response = client.get("/delete-song/1") + assert response.status_code == 302 + assert response.headers["Location"] == "None" # No previous page, use homepage + + response = client.get("/song/1/1?action=view") + assert response.status_code == 404 # Song deleted + def test_reply_to_comment(client): _create_user_song_and_comment(client, "parent comment") @@ -782,3 +791,29 @@ def test_activity_for_reply_to_reply(client): response = client.get("/activity") assert b"it really is cool" in response.data +def test_activity_deleted_when_song_deleted(client): + _create_user_and_song(client) + _create_user(client, "user2", login=True) + client.post("/comment?songid=1", data={"content": "hey cool song"}) + + client.post("/login", data={"username": "user", "password": "password"}) + response = client.get("/activity") + assert b"hey cool song" in response.data + + client.get("/delete-song/1") + response = client.get("/activity") + assert b"hey cool song" not in response.data + +def test_activity_deleted_when_comment_deleted(client): + _create_user_and_song(client) + _create_user(client, "user2", login=True) + client.post("/comment?songid=1", data={"content": "hey cool song"}) + + client.post("/login", data={"username": "user", "password": "password"}) + response = client.get("/activity") + assert b"hey cool song" in response.data + + client.get("/delete-comment/1") + response = client.get("/activity") + assert b"hey cool song" not in response.data + diff --git a/todo.txt b/todo.txt index 2c8b484..3d0b34c 100644 --- a/todo.txt +++ b/todo.txt @@ -1,5 +1,4 @@ UPDATE 1.1 -- Test deleting song with comments - Activity indicator - Tips and Tricks (or html helper for bio/descriptions?) - Multiline/html descriptions, comments