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

206 lines
7.9 KiB
Python

# -*- coding: utf-8 -*-
from odoo.tests import tagged
from odoo.tests.common import TransactionCase
@tagged('post_install', '-at_install', 'helpdesk_rating_five_stars')
class TestRatingReports(TransactionCase):
"""
Test rating statistics and reports with 0-5 scale
Requirements: 4.1, 4.2, 4.4, 4.5
- Requirement 4.1: Display ratings using the 0-5 scale in reports
- Requirement 4.2: Calculate average ratings based on the 0-5 scale
- Requirement 4.4: Use 0-5 scale for filtering and grouping
- Requirement 4.5: Include 0-5 scale values in exports
"""
@classmethod
def setUpClass(cls):
super().setUpClass()
# Create a helpdesk team with rating enabled
cls.team = cls.env['helpdesk.team'].create({
'name': 'Test Support Team',
'use_rating': True,
})
# Create test tickets
cls.ticket1 = cls.env['helpdesk.ticket'].create({
'name': 'Test Ticket 1',
'team_id': cls.team.id,
})
cls.ticket2 = cls.env['helpdesk.ticket'].create({
'name': 'Test Ticket 2',
'team_id': cls.team.id,
})
cls.ticket3 = cls.env['helpdesk.ticket'].create({
'name': 'Test Ticket 3',
'team_id': cls.team.id,
})
# Create ratings with 0-5 scale values
cls.rating1 = cls.env['rating.rating'].create({
'res_model': 'helpdesk.ticket',
'res_id': cls.ticket1.id,
'rating': 5.0,
'consumed': True,
})
cls.rating2 = cls.env['rating.rating'].create({
'res_model': 'helpdesk.ticket',
'res_id': cls.ticket2.id,
'rating': 3.0,
'consumed': True,
})
cls.rating3 = cls.env['rating.rating'].create({
'res_model': 'helpdesk.ticket',
'res_id': cls.ticket3.id,
'rating': 1.0,
'consumed': True,
})
def test_report_model_exists(self):
"""Test that the helpdesk ticket report analysis model exists"""
report_model = self.env['helpdesk.ticket.report.analysis']
self.assertTrue(report_model, "Report model should exist")
def test_rating_fields_exist(self):
"""Test that rating fields exist in the report model"""
report_model = self.env['helpdesk.ticket.report.analysis']
# Check that rating fields are defined
self.assertIn('rating_avg', report_model._fields,
"rating_avg field should exist")
self.assertIn('rating_last_value', report_model._fields,
"rating_last_value field should exist")
def test_rating_avg_calculation(self):
"""
Test that average rating is calculated correctly using 0-5 scale
Requirement 4.2: Calculate average ratings based on the 0-5 scale
"""
# Refresh the report view
self.env['helpdesk.ticket.report.analysis'].init()
# Search for report records for our tickets
report_records = self.env['helpdesk.ticket.report.analysis'].search([
('ticket_id', 'in', [self.ticket1.id, self.ticket2.id, self.ticket3.id])
])
# Verify we have report records
self.assertTrue(len(report_records) > 0,
"Should have report records for test tickets")
# Check that rating values are in 0-5 range
for record in report_records:
if record.rating_last_value:
self.assertGreaterEqual(record.rating_last_value, 0,
"Rating should be >= 0")
self.assertLessEqual(record.rating_last_value, 5,
"Rating should be <= 5")
def test_rating_filtering(self):
"""
Test that rating filtering works with 0-5 scale
Requirement 4.4: Use 0-5 scale for filtering and grouping
"""
# Refresh the report view
self.env['helpdesk.ticket.report.analysis'].init()
# Test high rating filter (4-5 stars)
high_rated = self.env['helpdesk.ticket.report.analysis'].search([
('rating_last_value', '>=', 4),
('ticket_id', 'in', [self.ticket1.id, self.ticket2.id, self.ticket3.id])
])
# Should find ticket1 with rating 5
self.assertTrue(len(high_rated) >= 1,
"Should find high-rated tickets (4-5 stars)")
# Test medium rating filter (3 stars)
medium_rated = self.env['helpdesk.ticket.report.analysis'].search([
('rating_last_value', '>=', 3),
('rating_last_value', '<', 4),
('ticket_id', 'in', [self.ticket1.id, self.ticket2.id, self.ticket3.id])
])
# Should find ticket2 with rating 3
self.assertTrue(len(medium_rated) >= 1,
"Should find medium-rated tickets (3 stars)")
# Test low rating filter (1-2 stars)
low_rated = self.env['helpdesk.ticket.report.analysis'].search([
('rating_last_value', '>=', 1),
('rating_last_value', '<', 3),
('ticket_id', 'in', [self.ticket1.id, self.ticket2.id, self.ticket3.id])
])
# Should find ticket3 with rating 1
self.assertTrue(len(low_rated) >= 1,
"Should find low-rated tickets (1-2 stars)")
def test_rating_export_values(self):
"""
Test that exported rating data contains 0-5 scale values
Requirement 4.5: Include 0-5 scale values in exports
"""
# Refresh the report view
self.env['helpdesk.ticket.report.analysis'].init()
# Get report records
report_records = self.env['helpdesk.ticket.report.analysis'].search([
('ticket_id', 'in', [self.ticket1.id, self.ticket2.id, self.ticket3.id])
])
# Simulate export by reading field values
for record in report_records:
if record.rating_last_value:
# Verify rating value is in valid range
self.assertGreaterEqual(record.rating_last_value, 0,
"Exported rating should be >= 0")
self.assertLessEqual(record.rating_last_value, 5,
"Exported rating should be <= 5")
# Verify it's one of our test values
self.assertIn(record.rating_last_value, [1.0, 3.0, 5.0],
"Exported rating should match test data")
def test_rating_grouping(self):
"""
Test that rating grouping works with 0-5 scale
Requirement 4.4: Use 0-5 scale for filtering and grouping
"""
# Refresh the report view
self.env['helpdesk.ticket.report.analysis'].init()
# Test grouping by rating level
report_records = self.env['helpdesk.ticket.report.analysis'].search([
('ticket_id', 'in', [self.ticket1.id, self.ticket2.id, self.ticket3.id])
])
# Group by rating_last_value
grouped_data = {}
for record in report_records:
if record.rating_last_value:
rating_key = int(record.rating_last_value)
if rating_key not in grouped_data:
grouped_data[rating_key] = []
grouped_data[rating_key].append(record)
# Verify grouping worked
self.assertTrue(len(grouped_data) > 0,
"Should have grouped data by rating")
# Verify all groups are in valid range
for rating_key in grouped_data.keys():
self.assertGreaterEqual(rating_key, 0,
"Grouped rating should be >= 0")
self.assertLessEqual(rating_key, 5,
"Grouped rating should be <= 5")