]> littlesong.place Git - littlesongplace.git/commitdiff
Add tests for deleting parent objects
authorChris Fulljames <christianfulljames@gmail.com>
Fri, 31 Jan 2025 23:52:10 +0000 (18:52 -0500)
committerChris Fulljames <christianfulljames@gmail.com>
Fri, 31 Jan 2025 23:52:40 +0000 (18:52 -0500)
schema_update.sql
test/test_offline.py
todo.txt

index 6e81b3e8c1de5ed2ff7369df8288d9c225680506..3e0a353d8e408822c6c2dcbf4658e9eb54bf5b77 100644 (file)
@@ -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);
 
index 4e0ae564fcfb35b9b8849fb88c970ea41269faf7..1833f57128be02635b479aefeffbd90328623ca4 100644 (file)
@@ -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
+
index 2c8b48413328fbe3b4d4bb832b1d087d7d799554..3d0b34cc4b1be1bff0a67164f25d758c6b114f2a 100644 (file)
--- 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