Skip to content

Commit

Permalink
Added unit test for rescale energy value
Browse files Browse the repository at this point in the history
  • Loading branch information
ArneTR committed Nov 14, 2024
1 parent a120bad commit cf1a0aa
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion api/api_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def rescale_energy_value(value, unit):
# We only expect values to be uJ for energy in the future. Changing values now temporarily.
# TODO: Refactor this once all data in the DB is uJ
if unit != 'uJ' and not unit.startswith('ugCO2e/'):
raise RuntimeError('Unexpected unit occured for energy rescaling: ', unit)
raise ValueError('Unexpected unit occured for energy rescaling: ', unit)

unit_type = unit[1:]

Expand Down
25 changes: 25 additions & 0 deletions tests/api/test_api_helpers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from pydantic import BaseModel

from api import api_helpers
import pytest

class Run(BaseModel):
name: str
Expand All @@ -23,6 +24,30 @@ class CI_Measurement(BaseModel):
duration: int


def test_rescale_energy_value():
assert api_helpers.rescale_energy_value(100, 'uJ') == [100, 'uJ']

assert api_helpers.rescale_energy_value(10000, 'uJ') == [10, 'mJ']

assert api_helpers.rescale_energy_value(10000, 'mJ') == [10, 'J']

assert api_helpers.rescale_energy_value(324_000_000_000, 'uJ') == [324, 'kJ']

assert api_helpers.rescale_energy_value(324_000_000_000, 'ugCO2e/Page Request') == [324, 'kgCO2e/Page Request']

assert api_helpers.rescale_energy_value(222_000_000_000_000, 'ugCO2e/Kill') == [222, 'MgCO2e/Kill']

assert api_helpers.rescale_energy_value(0.0003, 'ugCO2e/Kill') == [0.3, 'ngCO2e/Kill']


with pytest.raises(ValueError):
api_helpers.rescale_energy_value(100, 'xJ') # expecting only mJ and uJ

with pytest.raises(ValueError):
api_helpers.rescale_energy_value(100, 'uj') # expecting only mJ and uJ



def test_escape_dict():
messy_dict = {"link": '<a href="http://www.github.com">Click me</a>'}
escaped_link = '&lt;a href=&quot;http://www.github.com&quot;&gt;Click me&lt;/a&gt;'
Expand Down

0 comments on commit cf1a0aa

Please sign in to comment.