helpdesk_rating_five_stars/tests/test_rating_views.py
2025-11-26 10:39:26 +07:00

280 lines
12 KiB
Python

# -*- coding: utf-8 -*-
from odoo.tests import TransactionCase, tagged
@tagged('post_install', '-at_install')
class TestRatingViews(TransactionCase):
"""Test rating backend views display stars correctly"""
def setUp(self):
super().setUp()
self.Rating = self.env['rating.rating']
# Create a test partner
self.partner = self.env['ref']('base.partner_demo')
# Create a test helpdesk ticket (if helpdesk is available)
if 'helpdesk.ticket' in self.env:
self.ticket = self.env['helpdesk.ticket'].create({
'name': 'Test Ticket for Rating Views',
'partner_id': self.partner.id,
})
def test_view_tree_loads(self):
"""Test that the tree view with stars can be loaded"""
view = self.env.ref('helpdesk_rating_five_stars.rating_rating_view_tree_stars')
self.assertTrue(view.exists(), "Tree view should exist")
self.assertEqual(view.model, 'rating.rating', "View should be for rating.rating model")
def test_view_form_loads(self):
"""Test that the form view with stars can be loaded"""
view = self.env.ref('helpdesk_rating_five_stars.rating_rating_view_form_stars')
self.assertTrue(view.exists(), "Form view should exist")
self.assertEqual(view.model, 'rating.rating', "View should be for rating.rating model")
def test_view_kanban_loads(self):
"""Test that the kanban view with stars can be loaded"""
view = self.env.ref('helpdesk_rating_five_stars.rating_rating_view_kanban_five_stars')
self.assertTrue(view.exists(), "Kanban view should exist")
self.assertEqual(view.model, 'rating.rating', "View should be for rating.rating model")
def test_rating_display_in_views(self):
"""Test that ratings display correctly with computed star fields"""
# Create a rating with 4 stars
rating = self.Rating.create({
'rating': 4.0,
'partner_id': self.partner.id,
'rated_partner_id': self.partner.id,
'res_model': 'res.partner',
'res_id': self.partner.id,
})
# Verify computed fields
self.assertEqual(rating.rating_stars_filled, 4, "Should have 4 filled stars")
self.assertEqual(rating.rating_stars_empty, 1, "Should have 1 empty star")
# Verify HTML generation
html = rating._get_rating_stars_html()
self.assertIn('', html, "HTML should contain filled star character")
self.assertIn('', html, "HTML should contain empty star character")
self.assertEqual(html.count(''), 4, "Should have 4 filled stars in HTML")
self.assertEqual(html.count(''), 1, "Should have 1 empty star in HTML")
def test_rating_zero_display(self):
"""Test that zero rating displays correctly"""
rating = self.Rating.create({
'rating': 0.0,
'partner_id': self.partner.id,
'rated_partner_id': self.partner.id,
'res_model': 'res.partner',
'res_id': self.partner.id,
})
# Verify computed fields for zero rating
self.assertEqual(rating.rating_stars_filled, 0, "Should have 0 filled stars")
self.assertEqual(rating.rating_stars_empty, 5, "Should have 5 empty stars")
def test_rating_five_stars_display(self):
"""Test that 5-star rating displays correctly"""
rating = self.Rating.create({
'rating': 5.0,
'partner_id': self.partner.id,
'rated_partner_id': self.partner.id,
'res_model': 'res.partner',
'res_id': self.partner.id,
})
# Verify computed fields for 5-star rating
self.assertEqual(rating.rating_stars_filled, 5, "Should have 5 filled stars")
self.assertEqual(rating.rating_stars_empty, 0, "Should have 0 empty stars")
@tagged('post_install', '-at_install')
class TestHelpdeskTicketViews(TransactionCase):
"""Test helpdesk ticket views display stars correctly
Requirements: 5.1, 5.2, 5.4
"""
def setUp(self):
super().setUp()
# Skip tests if helpdesk module is not installed
if 'helpdesk.ticket' not in self.env:
self.skipTest("Helpdesk module not installed")
self.HelpdeskTicket = self.env['helpdesk.ticket']
self.Rating = self.env['rating.rating']
# Create a test partner
self.partner = self.env['res.partner'].create({
'name': 'Test Customer',
'email': 'test@example.com',
})
# Create a test helpdesk team
self.team = self.env['helpdesk.team'].create({
'name': 'Test Support Team',
'use_rating': True,
})
# Create a test helpdesk ticket
self.ticket = self.HelpdeskTicket.create({
'name': 'Test Ticket for Star Display',
'partner_id': self.partner.id,
'team_id': self.team.id,
})
def test_helpdesk_ticket_form_view_loads(self):
"""Test that the helpdesk ticket form view with stars can be loaded"""
view = self.env.ref('helpdesk_rating_five_stars.helpdesk_ticket_view_form_stars')
self.assertTrue(view.exists(), "Helpdesk ticket form view should exist")
self.assertEqual(view.model, 'helpdesk.ticket', "View should be for helpdesk.ticket model")
def test_helpdesk_ticket_tree_view_loads(self):
"""Test that the helpdesk ticket tree view with stars can be loaded"""
view = self.env.ref('helpdesk_rating_five_stars.helpdesk_ticket_view_tree_stars')
self.assertTrue(view.exists(), "Helpdesk ticket tree view should exist")
self.assertEqual(view.model, 'helpdesk.ticket', "View should be for helpdesk.ticket model")
def test_helpdesk_ticket_kanban_view_loads(self):
"""Test that the helpdesk ticket kanban view with stars can be loaded"""
view = self.env.ref('helpdesk_rating_five_stars.helpdesk_ticket_view_kanban_stars')
self.assertTrue(view.exists(), "Helpdesk ticket kanban view should exist")
self.assertEqual(view.model, 'helpdesk.ticket', "View should be for helpdesk.ticket model")
def test_ticket_rating_stars_html_with_rating(self):
"""Test that ticket displays star HTML when it has a rating
Requirement 5.1: Display ratings as filled star icons in ticket views
"""
# Create a rating for the ticket with 3 stars
rating = self.Rating.create({
'rating': 3.0,
'partner_id': self.partner.id,
'rated_partner_id': self.partner.id,
'res_model': 'helpdesk.ticket',
'res_id': self.ticket.id,
})
# Refresh ticket to get computed field
self.ticket.invalidate_recordset()
# Verify the computed HTML field
html = self.ticket.rating_stars_html
self.assertIsNotNone(html, "Rating stars HTML should not be None")
self.assertIn('', html, "HTML should contain filled star character")
self.assertIn('', html, "HTML should contain empty star character")
def test_ticket_rating_stars_html_three_stars(self):
"""Test that ticket with 3-star rating displays 3 filled and 2 empty stars
Requirement 5.2: Display 3 filled stars and 2 empty stars for rating value of 3
"""
# Create a rating for the ticket with exactly 3 stars
rating = self.Rating.create({
'rating': 3.0,
'partner_id': self.partner.id,
'rated_partner_id': self.partner.id,
'res_model': 'helpdesk.ticket',
'res_id': self.ticket.id,
})
# Refresh ticket to get computed field
self.ticket.invalidate_recordset()
# Verify the star counts
html = self.ticket.rating_stars_html
filled_count = html.count('')
empty_count = html.count('')
self.assertEqual(filled_count, 3, "Should have exactly 3 filled stars")
self.assertEqual(empty_count, 2, "Should have exactly 2 empty stars")
def test_ticket_rating_stars_html_no_rating(self):
"""Test that ticket without rating displays empty stars or not rated indicator
Requirement 5.3: Display five empty stars or "Not Rated" indicator when no rating
"""
# Ticket has no rating yet
self.ticket.invalidate_recordset()
# Verify the computed HTML field shows empty stars or not rated
html = self.ticket.rating_stars_html
self.assertIsNotNone(html, "Rating stars HTML should not be None even without rating")
# Should show 5 empty stars
empty_count = html.count('')
self.assertEqual(empty_count, 5, "Should have 5 empty stars when not rated")
def test_ticket_rating_stars_html_five_stars(self):
"""Test that ticket with 5-star rating displays correctly"""
# Create a rating for the ticket with 5 stars
rating = self.Rating.create({
'rating': 5.0,
'partner_id': self.partner.id,
'rated_partner_id': self.partner.id,
'res_model': 'helpdesk.ticket',
'res_id': self.ticket.id,
})
# Refresh ticket to get computed field
self.ticket.invalidate_recordset()
# Verify the star counts
html = self.ticket.rating_stars_html
filled_count = html.count('')
empty_count = html.count('')
self.assertEqual(filled_count, 5, "Should have 5 filled stars")
self.assertEqual(empty_count, 0, "Should have 0 empty stars")
def test_ticket_rating_stars_html_one_star(self):
"""Test that ticket with 1-star rating displays correctly"""
# Create a rating for the ticket with 1 star
rating = self.Rating.create({
'rating': 1.0,
'partner_id': self.partner.id,
'rated_partner_id': self.partner.id,
'res_model': 'helpdesk.ticket',
'res_id': self.ticket.id,
})
# Refresh ticket to get computed field
self.ticket.invalidate_recordset()
# Verify the star counts
html = self.ticket.rating_stars_html
filled_count = html.count('')
empty_count = html.count('')
self.assertEqual(filled_count, 1, "Should have 1 filled star")
self.assertEqual(empty_count, 4, "Should have 4 empty stars")
def test_ticket_rating_stars_compact_format(self):
"""Test that star display is compact and suitable for list views
Requirement 5.4: Display star ratings in compact format for list views
"""
# Create a rating for the ticket
rating = self.Rating.create({
'rating': 4.0,
'partner_id': self.partner.id,
'rated_partner_id': self.partner.id,
'res_model': 'helpdesk.ticket',
'res_id': self.ticket.id,
})
# Refresh ticket to get computed field
self.ticket.invalidate_recordset()
# Verify the HTML is compact (no excessive whitespace or formatting)
html = self.ticket.rating_stars_html
# Should contain the compact class
self.assertIn('o_rating_stars', html, "Should use rating stars class")
# Should not be excessively long (compact format)
self.assertLess(len(html), 500, "HTML should be compact for list views")