You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
be-optimumoh/tests/unit/test_budget_constrains.py

45 lines
1.6 KiB
Python

import pytest
from src.calculation_budget_constrains.service import greedy_selection, knapsack_selection
def test_greedy_selection():
equipments = [
{"id": 1, "total_cost": 100, "priority_score": 10, "cost": 100},
{"id": 2, "total_cost": 50, "priority_score": 20, "cost": 50},
{"id": 3, "total_cost": 60, "priority_score": 15, "cost": 60},
]
budget = 120
# Items sorted by priority_score: id 2 (20), id 3 (15), id 1 (10)
# 2 (50) + 3 (60) = 110. Item 1 (100) won't fit.
selected, excluded = greedy_selection(equipments, budget)
selected_ids = [e["id"] for e in selected]
assert 2 in selected_ids
assert 3 in selected_ids
assert len(selected) == 2
assert excluded[0]["id"] == 1
def test_knapsack_selection_basic():
# Similar items but where greedy might fail if cost/value ratio is tricky
# item 1: value 10, cost 60
# item 2: value 7, cost 35
# item 3: value 7, cost 35
# budget: 70
# Greedy would take item 1 (value 10, remaining budget 10, can't take more)
# Optimal would take item 2 and 3 (value 14, remaining budget 0)
scale = 1 # No scaling for simplicity in this test
equipments = [
{"id": 1, "total_cost": 60, "priority_score": 10},
{"id": 2, "total_cost": 35, "priority_score": 7},
{"id": 3, "total_cost": 35, "priority_score": 7},
]
budget = 70
selected, excluded = knapsack_selection(equipments, budget, scale=1)
selected_ids = [e["id"] for e in selected]
assert 2 in selected_ids
assert 3 in selected_ids
assert len(selected) == 2
assert 1 not in selected_ids