projektkalender / tests /test_core.py
DecodeMarketing's picture
Update project calendar sorting and assignments UI
a1206d9
Raw History Blame Contribute Delete
7.33 kB
import unittest
from datetime import datetime
from pathlib import Path
from config import TASK_CONFIG, compute_date_range
from excel_export import generate_dates_excel
from gantt_renderer import active_items_for_day, phase_bar_style, render_gantt_html
from jira_client import build_issue_map, consolidate_items, match_issue
class JiraMappingTests(unittest.TestCase):
def test_match_issue_uses_summary_keywords(self):
self.assertEqual(match_issue("CMIX setup"), "CMIX")
self.assertEqual(match_issue("DiU programming/ implicit Survey"), "DiU")
self.assertEqual(match_issue("Data Processing EU"), "Data Processing & Delivery")
self.assertEqual(match_issue("Final reporting"), "Reporting")
def test_match_issue_falls_back_to_parent_summary(self):
self.assertEqual(match_issue("Story to Client", "Reporting"), "Reporting")
def test_consolidate_items_uses_earliest_start_and_latest_end(self):
items = [
{
"summary": "Field Time DE",
"start_date": "2026-02-10",
"end_date": "2026-02-20",
},
{
"summary": "Field Time US",
"start_date": "2026-02-05",
"end_date": "2026-02-25",
},
]
result = consolidate_items(items, match_issue)
self.assertEqual(result["Field Time"]["start"], "2026-02-05")
self.assertEqual(result["Field Time"]["end"], "2026-02-25")
def test_build_issue_map_tracks_issue_keys_for_min_max_dates(self):
items = [
{
"key": "ABC-1",
"summary": "Field Time DE",
"start_date": "2026-02-10",
"end_date": "2026-02-20",
},
{
"key": "ABC-2",
"summary": "Field Time US",
"start_date": "2026-02-05",
"end_date": "2026-02-25",
},
]
result = build_issue_map(items, match_issue)
self.assertEqual(result["Field Time"]["start_issue_key"], "ABC-2")
self.assertEqual(result["Field Time"]["end_issue_key"], "ABC-2")
self.assertEqual(len(result["Field Time"]["all_issues"]), 2)
class RenderingAndExportTests(unittest.TestCase):
def test_compute_date_range_starts_at_configured_gantt_start(self):
project_data = {
"ABC": {
"Set up": {"start": "2026-02-01", "end": "2026-02-05"},
}
}
min_date, max_date, days = compute_date_range(project_data)
self.assertEqual(min_date, datetime(2026, 1, 5))
self.assertGreaterEqual(max_date, datetime(2026, 2, 15))
self.assertGreater(len(days), 0)
def test_gantt_renderer_returns_html_without_external_calls(self):
projects = [
{
"key": "ABC",
"name": "123456 Test Project DE",
"lead": "Research Lead",
"research_backup": "",
"pm": "PM Lead",
"pm_backup": "",
"country": "DE",
}
]
project_data = {
"ABC": {
"Set up": {"start": "2026-02-01", "end": "2026-02-05"},
}
}
html, height = render_gantt_html(projects, [], project_data)
self.assertIn("123456 Test Project DE", html)
self.assertIn("Project", html)
self.assertEqual(height, 750)
def test_gantt_renderer_uses_plain_left_headers(self):
projects = [
{
"key": "ABC",
"name": "123456 Test Project DE",
"lead": "Research Lead",
"research_backup": "Research Backup",
"pm": "PM Lead",
"pm_backup": "PM Backup",
"country": "DE",
}
]
project_data = {
"ABC": {
"Set up": {"start": "2026-02-01", "end": "2026-02-05"},
}
}
html, _height = render_gantt_html(projects, [], project_data)
self.assertIn('rowspan="4">C</th>', html)
self.assertNotIn('class="sortbtn"', html)
def test_diu_color_matches_requested_rgb(self):
self.assertEqual(TASK_CONFIG["DiU"]["color"], "F2B1B5")
def test_gantt_renderer_splits_overlapping_phase_colors(self):
projects = [
{
"key": "ABC",
"name": "123456 Test Project DE",
"lead": "Research Lead",
"research_backup": "",
"pm": "PM Lead",
"pm_backup": "",
"country": "DE",
}
]
project_data = {
"ABC": {
"Set up": {"start": "2026-02-02", "end": "2026-02-05"},
"Field Time": {"start": "2026-02-03", "end": "2026-02-06"},
}
}
html, _height = render_gantt_html(projects, [], project_data)
self.assertIn("linear-gradient(135deg", html)
self.assertIn("Set up / Field Time", html)
def test_active_items_and_phase_bar_style_are_ordered(self):
items = {
"Field Time": {"start": "2026-02-03", "end": "2026-02-06"},
"Set up": {"start": "2026-02-02", "end": "2026-02-05"},
}
active = active_items_for_day(items, datetime(2026, 2, 3).date())
style = phase_bar_style(active)
self.assertEqual(active, ["Set up", "Field Time"])
self.assertIn("linear-gradient(135deg", style)
def test_dates_excel_contains_project_metadata(self):
project = {"key": "ABC", "name": "Test Project", "lead": "Research Lead"}
items_data = {"Set up": {"start": "2026-02-01", "end": "2026-02-05"}}
issue_map = {"Set up": {"issue_key": "ABC-1"}}
workbook_bytes = generate_dates_excel(project, items_data, issue_map, "PM Lead")
self.assertGreater(len(workbook_bytes.getvalue()), 1000)
class AssignmentPersistenceSourceTests(unittest.TestCase):
def test_assignment_save_result_is_not_erased_by_immediate_rerun(self):
app_source = Path("app.py").read_text(encoding="utf-8")
save_block = app_source.split("if st.button('Save Assignments'", 1)[1]
save_block = save_block.split("for p in active_projects:", 1)[0]
self.assertNotIn("st.rerun()", save_block)
self.assertIn("assignment_save_error", save_block)
self.assertIn("assignment_save_success", save_block)
def test_assignment_editor_warns_about_unsaved_changes(self):
app_source = Path("app.py").read_text(encoding="utf-8")
self.assertIn("has_unsaved_assignment_changes", app_source)
self.assertIn("You have unsaved assignment changes", app_source)
def test_project_calendar_has_streamlit_sort_controls(self):
app_source = Path("app.py").read_text(encoding="utf-8")
self.assertIn("sort_options = {", app_source)
self.assertIn("sort_by = st.selectbox('Sort by'", app_source)
self.assertIn("sort_order = st.selectbox('Order', ['Ascending', 'Descending'], index=1", app_source)
self.assertIn("sort_desc = sort_order == 'Descending'", app_source)
self.assertIn("filtered_active = sorted(", app_source)
if __name__ == "__main__":
unittest.main()