Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions gameplan/gameplan/doctype/gp_discussion/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,11 @@ def include_last_post_content(discussions):

def clause_discussions_commented_by_user(user):
Discussion = frappe.qb.DocType("GP Discussion")
commented_in = list(
set(
frappe.db.get_all(
"GP Comment",
fields=["reference_name"],
filters={"reference_doctype": "GP Discussion", "owner": user},
pluck="reference_name",
)
)
Comment = frappe.qb.DocType("GP Comment")
commented_in = (
frappe.qb.from_(Comment)
.select(Comment.reference_name)
.where((Comment.reference_doctype == "GP Discussion") & (Comment.owner == user))
)
return Discussion.name.isin(commented_in)

Expand Down
36 changes: 34 additions & 2 deletions gameplan/gameplan/doctype/gp_discussion/test_gp_discussion.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,41 @@
# Copyright (c) 2023, Frappe Technologies Pvt Ltd and Contributors
# See license.txt

# import frappe
import frappe
from frappe.tests.utils import FrappeTestCase

from gameplan.gameplan.doctype.gp_discussion.api import clause_discussions_commented_by_user


class TestGPDiscussion(FrappeTestCase):
pass
def test_clause_discussions_commented_by_user_with_no_comments(self):
"""Test that clause_discussions_commented_by_user handles users with no comments gracefully"""
# Create a test user who has no comments
test_user = "test_no_comments@example.com"
if not frappe.db.exists("User", test_user):
frappe.get_doc(
{
"doctype": "User",
"email": test_user,
"first_name": "Test",
"last_name": "NoComments",
"send_welcome_email": 0,
}
).insert(ignore_permissions=True)

# Ensure the user has no comments
frappe.db.delete("GP Comment", {"owner": test_user})

# This should not raise an SQL error (MariaDB error 1064)
clause = clause_discussions_commented_by_user(test_user)

# The clause should be valid and can be used in a query
Discussion = frappe.qb.DocType("GP Discussion")
query = frappe.qb.from_(Discussion).select(Discussion.name).where(clause).limit(1)

# This should execute without error
result = query.run()
self.assertIsInstance(result, list)

# Cleanup
frappe.db.rollback()
Loading