Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 1 addition & 12 deletions library/src/iqb/calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,9 @@ def calculate_binary_requirement_score(self, network_requirement, value, thresho
f"The binary requirement score method is not implemented for the network_requirement: {network_requirement}"
)

def calculate_iqb_score(self, data=None, print_details=False):
def calculate_iqb_score(self, data, print_details=False):
"""Calculates IQB score based on given data."""

# TODO(bassosimone): remove the default data sample in a subsequent interation.
sample_data = {
"m-lab": {
"download_throughput_mbps": 15,
"upload_throughput_mbps": 20,
"latency_ms": 75,
"packet_loss": 0.007,
}
}
data = sample_data if data is None else data

# TODO(bassosimone): remove printing from the current function and instead
# add more tests to gain better confidence about it being WAI
doprint = print if print_details else lambda *args, **kwargs: None
Expand Down
33 changes: 17 additions & 16 deletions library/tests/iqb/calculator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,41 +90,42 @@ def test_invalid_network_requirement_raises_error(self):
class TestIQBCalculatorScoreCalculation:
"""Tests for IQBCalculator score calculation."""

_SAMPLE_DATA = {
"m-lab": {
"download_throughput_mbps": 15,
"upload_throughput_mbps": 20,
"latency_ms": 75,
"packet_loss": 0.007,
}
}

def test_calculate_iqb_score_default_data(self):
"""Test that IQBCalculator score can be calculated with default data."""
"""Test that IQBCalculator score can be calculated with explicit input data."""
iqb = IQBCalculator(name="test1")
score = iqb.calculate_iqb_score()
score = iqb.calculate_iqb_score(data=self._SAMPLE_DATA)
# The score should be a float between 0 and 1
assert isinstance(score, (int, float))
assert 0 <= score <= 1

def test_calculate_iqb_score_with_custom_data(self):
"""Test that passing custom data raises NotImplementedError."""
iqb = IQBCalculator()
sample_data = {
"m-lab": {
"download_throughput_mbps": 15,
"upload_throughput_mbps": 20,
"latency_ms": 75,
"packet_loss": 0.007,
}
}
score = iqb.calculate_iqb_score(data=sample_data)
"""Test that IQBCalculator score can be calculated with custom data."""
iqb = IQBCalculator()
score = iqb.calculate_iqb_score(data=self._SAMPLE_DATA)
assert isinstance(score, (int, float))
assert 0 <= score <= 1

def test_calculate_iqb_score_print_details(self):
"""Test that IQBCalculator score calculation works with print_details=True."""
iqb = IQBCalculator()
score = iqb.calculate_iqb_score(print_details=True)
score = iqb.calculate_iqb_score(data=self._SAMPLE_DATA, print_details=True)
assert isinstance(score, (int, float))
assert 0 <= score <= 1

def test_calculate_iqb_score_consistency(self):
"""Test that IQBCalculator score calculation is consistent across calls."""
iqb = IQBCalculator()
score1 = iqb.calculate_iqb_score()
score2 = iqb.calculate_iqb_score()
score1 = iqb.calculate_iqb_score(data=self._SAMPLE_DATA)
score2 = iqb.calculate_iqb_score(data=self._SAMPLE_DATA)
assert score1 == score2


Expand Down