Spaces:
Paused
Paused
File size: 28,663 Bytes
27dbadf f3473c1 27dbadf f3473c1 27dbadf e466dd5 27dbadf e466dd5 27dbadf f3473c1 27dbadf f3473c1 27dbadf f3473c1 27dbadf f3473c1 27dbadf f3473c1 27dbadf f3473c1 27dbadf f3473c1 27dbadf f3473c1 27dbadf f3473c1 27dbadf f3473c1 27dbadf f3473c1 27dbadf f3473c1 27dbadf f3473c1 27dbadf f3473c1 27dbadf f3473c1 27dbadf f3473c1 27dbadf f3473c1 27dbadf f3473c1 27dbadf e3a1efe 27dbadf e3a1efe 27dbadf e3a1efe 27dbadf e3a1efe 27dbadf e3a1efe 27dbadf e466dd5 e3a1efe e466dd5 e3a1efe e466dd5 e3a1efe e466dd5 27dbadf e3a1efe 27dbadf e3a1efe 27dbadf e3a1efe 27dbadf e3a1efe 27dbadf e3a1efe 27dbadf e3a1efe 27dbadf e3a1efe 27dbadf e3a1efe 27dbadf e3a1efe 27dbadf e3a1efe e466dd5 e3a1efe e466dd5 e3a1efe 27dbadf f3473c1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 |
import pytest
import sys
from datetime import date, timedelta
from decimal import Decimal
from timefold.solver.test import ConstraintVerifier
from timefold.solver.score import HardSoftDecimalScore
# Import standardized test utilities
from tests.test_utils import get_test_logger, create_test_results
# Initialize standardized test logger
logger = get_test_logger(__name__)
from src.constraint_solvers.timetable.constraints import (
define_constraints,
required_skill,
no_overlapping_tasks,
task_within_schedule,
task_fits_in_schedule,
unavailable_employee,
maintain_project_task_order,
undesired_day_for_employee,
desired_day_for_employee,
balance_employee_task_assignments,
no_lunch_break_spanning,
no_weekend_scheduling,
)
from src.constraint_solvers.timetable.working_hours import task_spans_lunch_break
from src.constraint_solvers.timetable.domain import (
Employee,
Task,
EmployeeSchedule,
ScheduleInfo,
)
class TestConstraints:
"""
Comprehensive test suite for all timetable constraints using ConstraintVerifier.
Each constraint is tested in isolation to verify correct behavior.
"""
def setup_method(self):
"""Set up common test data and ConstraintVerifier instance."""
logger.debug("Setting up test constraints and data...")
self.constraint_verifier = ConstraintVerifier.build(
define_constraints, EmployeeSchedule, Task
)
# Create common test data using generator functions
self.dates = create_test_dates()
self.employees = create_standard_employees(self.dates)
self.schedule_info = create_schedule_info()
# Create shortcuts for commonly used employees
self.employee_alice = self.employees["alice"]
self.employee_bob = self.employees["bob"]
self.employee_charlie = self.employees["charlie"]
logger.debug(f"Created {len(self.employees)} test employees and schedule info")
# ==================== HARD CONSTRAINT TESTS ====================
def test_required_skill_constraint_violation(self):
"""Test that tasks requiring skills not possessed by assigned employee are penalized."""
logger.debug("Testing required skill constraint violation...")
task = create_task(
task_id="task1",
description="Python Development",
required_skill="Python",
employee=self.employee_bob, # Bob doesn't have Python skill
)
(
self.constraint_verifier.verify_that(required_skill)
.given(task, self.employee_bob, self.schedule_info)
.penalizes_by(1)
)
logger.debug("β
Required skill constraint violation test passed")
def test_required_skill_constraint_satisfied(self):
"""Test that tasks assigned to employees with required skills are not penalized."""
logger.debug("Testing required skill constraint satisfaction...")
task = create_task(
task_id="task1",
description="Python Development",
required_skill="Python",
employee=self.employee_alice, # Alice has Python skill
)
(
self.constraint_verifier.verify_that(required_skill)
.given(task, self.employee_alice, self.schedule_info)
.penalizes_by(0)
)
logger.debug("β
Required skill constraint satisfaction test passed")
def test_required_skill_constraint_unassigned_task(self):
"""Test that unassigned tasks don't trigger required skill constraint."""
logger.debug("Testing required skill constraint with unassigned task...")
task = create_task(
task_id="task1",
description="Python Development",
required_skill="Python",
employee=None, # Unassigned
)
(
self.constraint_verifier.verify_that(required_skill)
.given(task, self.schedule_info)
.penalizes_by(0)
)
logger.debug("β
Required skill constraint unassigned task test passed")
def test_no_overlapping_tasks_constraint_violation(self):
"""Test that overlapping tasks for the same employee are penalized."""
logger.debug("Testing no overlapping tasks constraint violation...")
task1 = create_task(
task_id="task1",
description="Task 1",
duration_slots=4, # slots 0-3
start_slot=0,
required_skill="Python",
employee=self.employee_alice,
)
task2 = create_task(
task_id="task2",
description="Task 2",
duration_slots=3, # slots 2-4 (overlaps with task1 by 2 slots)
start_slot=2,
required_skill="Java",
employee=self.employee_alice, # Same employee
)
# Verify constraint violation (overlap of 2 slots: 2 and 3)
(
self.constraint_verifier.verify_that(no_overlapping_tasks)
.given(task1, task2, self.employee_alice, self.schedule_info)
.penalizes_by(2)
)
logger.debug("β
No overlapping tasks constraint violation test passed")
def test_no_overlapping_tasks_constraint_different_employees(self):
"""Test that overlapping tasks for different employees are not penalized."""
logger.debug("Testing no overlapping tasks with different employees...")
task1 = create_task(
task_id="task1",
description="Task 1",
duration_slots=4,
start_slot=0,
required_skill="Python",
employee=self.employee_alice,
)
task2 = create_task(
task_id="task2",
description="Task 2",
duration_slots=3,
start_slot=2, # Overlaps in time but different employee
required_skill="Java",
employee=self.employee_bob, # Different employee
)
(
self.constraint_verifier.verify_that(no_overlapping_tasks)
.given(
task1, task2, self.employee_alice, self.employee_bob, self.schedule_info
)
.penalizes_by(0)
)
logger.debug("β
No overlapping tasks different employees test passed")
def test_no_overlapping_tasks_constraint_adjacent_tasks(self):
"""Test that adjacent (non-overlapping) tasks for the same employee are not penalized."""
logger.debug("Testing no overlapping tasks with adjacent tasks...")
task1 = create_task(
task_id="task1",
description="Task 1",
duration_slots=4, # slots 0-3
start_slot=0,
required_skill="Python",
employee=self.employee_alice,
)
task2 = create_task(
task_id="task2",
description="Task 2",
duration_slots=3, # slots 4-6 (no overlap)
start_slot=4,
required_skill="Java",
employee=self.employee_alice, # Same employee
)
(
self.constraint_verifier.verify_that(no_overlapping_tasks)
.given(task1, task2, self.employee_alice, self.schedule_info)
.penalizes_by(0)
)
logger.debug("β
No overlapping tasks adjacent tasks test passed")
def test_task_within_schedule_constraint_violation(self):
"""Test that tasks starting before slot 0 are penalized."""
logger.debug("Testing task within schedule constraint violation...")
task = create_task(
task_id="task1",
description="Invalid Task",
start_slot=-1, # Invalid start slot
required_skill="Python",
employee=self.employee_alice,
)
(
self.constraint_verifier.verify_that(task_within_schedule)
.given(task, self.employee_alice, self.schedule_info)
.penalizes_by(1)
)
logger.debug("β
Task within schedule constraint violation test passed")
def test_task_within_schedule_constraint_satisfied(self):
"""Test that tasks starting at valid slots are not penalized."""
logger.debug("Testing task within schedule constraint satisfaction...")
task = create_task(
task_id="task1",
description="Valid Task",
start_slot=0, # Valid start slot
required_skill="Python",
employee=self.employee_alice,
)
(
self.constraint_verifier.verify_that(task_within_schedule)
.given(task, self.employee_alice, self.schedule_info)
.penalizes_by(0)
)
logger.debug("β
Task within schedule constraint satisfaction test passed")
def test_task_fits_in_schedule_constraint_violation(self):
"""Test that tasks extending beyond schedule end are penalized."""
task = create_task(
task_id="task1",
description="Overlong Task",
duration_slots=10, # Task extends to slot 65 (beyond 59)
start_slot=56, # Start at slot 56, end at slot 65 (beyond schedule)
required_skill="Python",
employee=self.employee_alice,
)
(
self.constraint_verifier.verify_that(task_fits_in_schedule)
.given(task, self.employee_alice, self.schedule_info)
.penalizes_by(1)
)
def test_task_fits_in_schedule_constraint_satisfied(self):
"""Test that tasks fitting within schedule are not penalized."""
task = create_task(
task_id="task1",
description="Valid Task",
start_slot=0,
required_skill="Python",
employee=self.employee_alice,
)
(
self.constraint_verifier.verify_that(task_fits_in_schedule)
.given(task, self.employee_alice, self.schedule_info)
.penalizes_by(0)
)
def test_unavailable_employee_constraint_violation(self):
"""Test that tasks assigned to unavailable employees are penalized."""
# With 20 slots per working day, tomorrow starts at slot 20
task = create_task(
task_id="task1",
description="Task on unavailable day",
start_slot=20, # Tomorrow (when Alice is unavailable)
required_skill="Python",
employee=self.employee_alice,
)
(
self.constraint_verifier.verify_that(unavailable_employee)
.given(task, self.employee_alice, self.schedule_info)
.penalizes_by(1)
)
def test_unavailable_employee_constraint_satisfied(self):
"""Test that tasks not on unavailable days are not penalized."""
task = create_task(
task_id="task1",
description="Task on available day",
start_slot=0, # Today (Alice is available)
required_skill="Python",
employee=self.employee_alice,
)
(
self.constraint_verifier.verify_that(unavailable_employee)
.given(task, self.employee_alice, self.schedule_info)
.penalizes_by(0)
)
def test_maintain_project_task_order_constraint_violation(self):
"""Test that tasks violating project sequence order are penalized."""
task1 = create_task(
task_id="task1",
description="Second Task",
start_slot=0, # Starts first but should come second
required_skill="Python",
project_id="project1",
sequence_number=2,
employee=self.employee_alice,
)
task2 = create_task(
task_id="task2",
description="First Task",
start_slot=2, # Starts during task1 but should come first
required_skill="Java",
project_id="project1",
sequence_number=1,
employee=self.employee_bob,
)
(
self.constraint_verifier.verify_that(maintain_project_task_order)
.given(
task1, task2, self.employee_alice, self.employee_bob, self.schedule_info
)
.penalizes_by(6)
)
def test_maintain_project_task_order_constraint_satisfied(self):
"""Test that tasks maintaining correct project sequence are not penalized."""
task1 = create_task(
task_id="task1",
description="First Task",
start_slot=0, # Comes first and finishes before task2
required_skill="Python",
project_id="project1",
sequence_number=1,
employee=self.employee_alice,
)
task2 = create_task(
task_id="task2",
description="Second Task",
start_slot=5, # Starts after task1 finishes
required_skill="Java",
project_id="project1",
sequence_number=2,
employee=self.employee_bob,
)
(
self.constraint_verifier.verify_that(maintain_project_task_order)
.given(
task1, task2, self.employee_alice, self.employee_bob, self.schedule_info
)
.penalizes_by(0)
)
def test_maintain_project_task_order_different_projects(self):
"""Test that tasks in different projects don't affect each other's sequence."""
task1 = create_task(
task_id="task1",
description="Task in Project A",
start_slot=0,
required_skill="Python",
project_id="projectA",
sequence_number=2,
employee=self.employee_alice,
)
task2 = create_task(
task_id="task2",
description="Task in Project B",
start_slot=2, # Overlaps but different project
required_skill="Java",
project_id="projectB",
sequence_number=1,
employee=self.employee_bob,
)
(
self.constraint_verifier.verify_that(maintain_project_task_order)
.given(
task1, task2, self.employee_alice, self.employee_bob, self.schedule_info
)
.penalizes_by(0)
)
def test_no_lunch_break_spanning_constraint_violation(self):
"""Test that tasks spanning lunch break are penalized."""
task = create_task(
task_id="task1",
description="Task spanning lunch",
start_slot=7, # Starts at 12:30 (slot 7), spans lunch break
duration_slots=4, # 2 hours, ends at 14:30 (slot 11)
required_skill="Python",
employee=self.employee_alice,
)
(
self.constraint_verifier.verify_that(no_lunch_break_spanning)
.given(task, self.employee_alice, self.schedule_info)
.penalizes_by(1)
)
def test_no_lunch_break_spanning_constraint_satisfied(self):
"""Test that tasks not spanning lunch break are not penalized."""
task = create_task(
task_id="task1",
description="Task before lunch",
start_slot=0, # Starts at 9:00
duration_slots=4, # 2 hours, ends at 11:00
required_skill="Python",
employee=self.employee_alice,
)
(
self.constraint_verifier.verify_that(no_lunch_break_spanning)
.given(task, self.employee_alice, self.schedule_info)
.penalizes_by(0)
)
def test_no_weekend_scheduling_constraint_satisfied(self):
"""Test that weekday tasks are not penalized.
Note: Since our slot system only includes working days,
is_weekend_slot should always return False for valid slots.
"""
task = create_task(
task_id="task1",
description="Weekday task",
start_slot=0, # First slot of first working day
required_skill="Python",
employee=self.employee_alice,
)
(
self.constraint_verifier.verify_that(no_weekend_scheduling)
.given(task, self.employee_alice, self.schedule_info)
.penalizes_by(0)
)
# ==================== SOFT CONSTRAINT TESTS ====================
def test_undesired_day_for_employee_constraint_violation(self):
"""Test that tasks on undesired days incur soft penalty."""
# With 20 slots per working day, day after tomorrow starts at slot 40
task = create_task(
task_id="task1",
description="Task on undesired day",
start_slot=40, # Day after tomorrow (Alice's undesired date)
required_skill="Python",
employee=self.employee_alice,
)
(
self.constraint_verifier.verify_that(undesired_day_for_employee)
.given(task, self.employee_alice, self.schedule_info)
.penalizes_by(1)
)
def test_undesired_day_for_employee_constraint_satisfied(self):
"""Test that tasks not on undesired days are not penalized."""
task = create_task(
task_id="task1",
description="Task on neutral day",
start_slot=0, # Today (neutral for Alice)
required_skill="Python",
employee=self.employee_alice,
)
(
self.constraint_verifier.verify_that(undesired_day_for_employee)
.given(task, self.employee_alice, self.schedule_info)
.penalizes_by(0)
)
def test_desired_day_for_employee_constraint_reward(self):
"""Test that tasks on desired days provide reward."""
# Alice's desired day is today (slot 0-19)
task = create_task(
task_id="task1",
description="Task on desired day",
start_slot=0, # Today (Alice's desired date)
required_skill="Python",
employee=self.employee_alice,
)
(
self.constraint_verifier.verify_that(desired_day_for_employee)
.given(task, self.employee_alice, self.schedule_info)
.rewards()
)
def test_desired_day_for_employee_constraint_neutral(self):
"""Test that tasks on neutral days don't provide desired day reward."""
task = create_task(
task_id="task1",
description="Task on neutral day",
start_slot=20, # Tomorrow (neutral for Alice)
required_skill="Python",
employee=self.employee_alice,
)
(
self.constraint_verifier.verify_that(desired_day_for_employee)
.given(task, self.employee_alice, self.schedule_info)
.justifies_with()
)
def test_balance_employee_task_assignments_constraint_balanced(self):
"""Test that balanced task assignments don't incur penalty."""
# Create balanced task assignments (2 tasks each)
tasks = [
create_task(
"task1",
"Alice Task 1",
start_slot=0,
required_skill="Python",
employee=self.employee_alice,
),
create_task(
"task2",
"Alice Task 2",
start_slot=5,
required_skill="Testing",
employee=self.employee_alice,
),
create_task(
"task3",
"Bob Task 1",
start_slot=10,
required_skill="Java",
employee=self.employee_bob,
),
create_task(
"task4",
"Bob Task 2",
start_slot=15,
required_skill="Documentation",
employee=self.employee_bob,
),
]
# Verify balanced assignment (both employees have 2 tasks)
(
self.constraint_verifier.verify_that(balance_employee_task_assignments)
.given(
*tasks,
self.employee_alice,
self.employee_bob,
self.schedule_info,
)
.penalizes_by(0)
)
def test_balance_employee_task_assignments_constraint_imbalanced(self):
"""Test that imbalanced task assignments incur penalty."""
# Create imbalanced task assignments (Alice: 3 tasks, Bob: 0 tasks)
tasks = [
create_task(
"task1",
"Alice Task 1",
start_slot=0,
required_skill="Python",
employee=self.employee_alice,
),
create_task(
"task2",
"Alice Task 2",
start_slot=5,
required_skill="Testing",
employee=self.employee_alice,
),
create_task(
"task3",
"Alice Task 3",
start_slot=10,
required_skill="Java",
employee=self.employee_alice,
),
]
(
self.constraint_verifier.verify_that(balance_employee_task_assignments)
.given(
*tasks,
self.employee_alice,
self.employee_bob,
self.schedule_info,
)
.penalizes()
)
# ==================== INTEGRATION TESTS ====================
def test_all_constraints_together_feasible_solution(self):
"""Test all constraints working together on a feasible solution."""
# Create a feasible mini schedule
tasks = [
create_task(
"task1",
"Valid Python Task",
start_slot=0, # Today (Alice's desired day)
required_skill="Python",
project_id="project1",
sequence_number=1,
employee=self.employee_alice,
),
create_task(
"task2",
"Valid Java Task",
start_slot=10, # After lunch break (14:00), non-overlapping
required_skill="Java",
project_id="project1",
sequence_number=2,
employee=self.employee_alice,
),
create_task(
"task3",
"Bob's Valid Task",
start_slot=14, # After lunch break (14:00)
required_skill="Java",
project_id="project2",
sequence_number=1,
employee=self.employee_bob,
),
]
(
self.constraint_verifier.verify_that()
.given(
*tasks,
self.employee_alice,
self.employee_bob,
self.schedule_info,
)
.scores(HardSoftDecimalScore.of(Decimal("0"), Decimal("1.292893")))
)
def test_all_constraints_together_infeasible_solution(self):
"""Test all constraints working together on an infeasible solution."""
# Create a mini schedule with multiple constraint violations
tasks = [
create_task(
"task1",
"Valid Python Task",
start_slot=0, # Today (Alice's desired day)
required_skill="Python",
project_id="project1",
sequence_number=1,
employee=self.employee_alice,
),
create_task(
"task2",
"Invalid Skill Task",
start_slot=20, # Tomorrow (Alice unavailable)
required_skill="NonExistentSkill",
project_id="project1",
sequence_number=2,
employee=self.employee_alice,
),
create_task(
"task3",
"Overlapping Task",
start_slot=2, # Overlaps with task1
required_skill="Testing",
project_id="project2",
sequence_number=1,
employee=self.employee_alice,
),
]
(
self.constraint_verifier.verify_that()
.given(
*tasks,
self.employee_alice,
self.employee_bob,
self.schedule_info,
)
.scores(HardSoftDecimalScore.of(Decimal("-4"), Decimal("-0.12132")))
)
# ==================== DATA GENERATOR FUNCTIONS ====================
def create_test_dates():
"""Generate common test dates for consistent usage across tests."""
today = date.today()
return {
"today": today,
"tomorrow": today + timedelta(days=1),
"day_after": today + timedelta(days=2),
}
def create_employee(
name, skills=None, unavailable_dates=None, undesired_dates=None, desired_dates=None
):
"""Create an employee with specified attributes."""
return Employee(
name=name,
skills=skills or set(),
unavailable_dates=unavailable_dates or set(),
undesired_dates=undesired_dates or set(),
desired_dates=desired_dates or set(),
)
def create_task(
task_id,
description="Test Task",
duration_slots=4,
start_slot=0,
required_skill="Python",
project_id=None,
sequence_number=None,
employee=None,
):
"""Create a task with specified attributes."""
return Task(
id=task_id,
description=description,
duration_slots=duration_slots,
start_slot=start_slot,
required_skill=required_skill,
project_id=project_id,
sequence_number=sequence_number,
employee=employee,
)
def create_schedule_info(total_slots=60):
"""Create a schedule info object with specified total slots.
Default is 60 slots = 3 working days * 20 slots per working day.
"""
return ScheduleInfo(total_slots=total_slots, base_date=date.today())
def create_standard_employees(dates):
"""Create the standard set of test employees used across multiple tests."""
return {
"alice": create_employee(
name="Alice",
skills={"Python", "Java", "Testing"},
unavailable_dates={dates["tomorrow"]},
undesired_dates={dates["day_after"]},
desired_dates={dates["today"]},
),
"bob": create_employee(
name="Bob",
skills={"Java", "Documentation", "Management"},
),
"charlie": create_employee(
name="Charlie",
skills={"Python", "Testing", "DevOps"},
),
}
if __name__ == "__main__":
"""Direct execution for non-pytest testing"""
logger.section("Constraint Solver Tests")
logger.info(
"Note: This test suite is designed for pytest. For best results, run with:"
)
logger.info(" pytest tests/test_constraints.py -v")
logger.info(" YUGA_DEBUG=true pytest tests/test_constraints.py -v -s")
# Create test results tracker
results = create_test_results(logger)
try:
# Create test instance
test_instance = TestConstraints()
test_instance.setup_method()
# Run a few sample tests
logger.info("Running sample constraint tests...")
sample_tests = [
(
"required_skill_violation",
test_instance.test_required_skill_constraint_violation,
),
(
"required_skill_satisfied",
test_instance.test_required_skill_constraint_satisfied,
),
(
"no_overlapping_violation",
test_instance.test_no_overlapping_tasks_constraint_violation,
),
(
"task_within_schedule",
test_instance.test_task_within_schedule_constraint_satisfied,
),
]
for test_name, test_func in sample_tests:
results.run_test(test_name, test_func)
logger.info(f"β
Completed {len(sample_tests)} sample constraint tests")
except Exception as e:
logger.error(f"Failed to run constraint tests: {e}")
results.add_result("constraint_tests_setup", False, str(e))
# Generate summary and exit with appropriate code
all_passed = results.summary()
if not all_passed:
logger.info(
"π‘ Hint: Use 'pytest tests/test_constraints.py' for full test coverage"
)
sys.exit(0 if all_passed else 1)
|