Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
magnuselden authored and magnuselden committed Aug 29, 2024
1 parent aa9033e commit 1a0adc7
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 24 deletions.
17 changes: 16 additions & 1 deletion peaqevcore/tests/test_localetypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ async def test_avg_daily_same_peak_as_max_today():
assert p.data.query_model.get_currently_obeserved_peak(datetime.combine(date(2024, 1, 6), time(0, 0))) == 2

@pytest.mark.asyncio
async def test_avg_daily_same_peak_as_max_today_currentpeak_sensor():
async def test_average_of_three_days_should_increase_daily_peak():
p = await LocaleFactory.async_create(LOCALE_SE_GOTHENBURG)
c = CurrentPeak(data_type=float, initval=0, startpeaks={}, locale=p, options_use_history=False, mock_dt=datetime(2024, 1, 3, 10, 0))
await p.data.query_model.async_try_update(new_val=2, timestamp=datetime(2024, 1, 3, 10, 0))
Expand All @@ -442,6 +442,21 @@ async def test_avg_daily_same_peak_as_max_today_currentpeak_sensor():
p.data.query_model.set_mock_dt(datetime(2024, 1, 6, 0, 0))
assert c.observed_peak == 2

@pytest.mark.asyncio
async def test_average_of_three_hours_should_not_increase_daily_peak():
p = await LocaleFactory.async_create(LOCALE_SE_SOLLENTUNA)
c = CurrentPeak(data_type=float, initval=0, startpeaks={}, locale=p, options_use_history=False, mock_dt=datetime(2024, 1, 3, 10, 0))
await p.data.query_model.async_try_update(new_val=2, timestamp=datetime(2024, 1, 3, 10, 0))
c.observed_peak = 2
await p.data.query_model.async_try_update(new_val=2, timestamp=datetime(2024, 1, 4, 10, 0))
c.mock_dt = datetime(2024, 1, 4, 10, 0)
c.observed_peak = 2
await p.data.query_model.async_try_update(new_val=4.2, timestamp=datetime(2024, 1, 5, 10, 0))
c.mock_dt = datetime(2024, 1, 5, 10, 0)
p.data.query_model.set_mock_dt(datetime(2024, 1, 5, 10, 0))
c.observed_peak = 4.2
assert c.observed_peak == 2

@pytest.mark.asyncio
async def test_avg_daily_real_case_should_show_min():
p = await LocaleFactory.async_create(LOCALE_SE_GOTHENBURG)
Expand Down
152 changes: 129 additions & 23 deletions peaqevcore/tests/test_scheduler.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from datetime import datetime, timedelta
import pytest

from ..models.chargecontroller_states import ChargeControllerStates
from ..services.scheduler.scheduler_service import Scheduler
from ..models.hourselection.hourselection_options import HourSelectionOptions
from ..services.scheduler.update_scheduler_dto import UpdateSchedulerDTO

MOCKPRICES = [
0.422,
Expand Down Expand Up @@ -190,7 +192,7 @@ async def test_scheduler_overnight_3():
prices_tomorrow=MOCKPRICES_TOMORROW,
mockdt=datetime.strptime("22-07-20 13:01", "%y-%m-%d %H:%M"),
)
assert s.model.non_hours == [13,14, 15, 16, 17, 18, 19, 20, 21, 7, 8]
assert s.model.non_hours == [13, 14, 15, 16, 17, 18, 19, 20, 21, 7, 8]
await s.async_update(
avg24=610,
peak=1.5,
Expand All @@ -199,15 +201,15 @@ async def test_scheduler_overnight_3():
prices_tomorrow=MOCKPRICES_TOMORROW,
mockdt=datetime.strptime("22-07-20 22:11", "%y-%m-%d %H:%M"),
)
assert s.model.non_hours == [22,7, 8]
assert s.model.non_hours == [22, 7, 8]
await s.async_update(
avg24=310,
peak=1.5,
charged_amount=1,
prices=MOCKPRICES_TOMORROW,
mockdt=datetime.strptime("22-07-21 00:03", "%y-%m-%d %H:%M"),
)
assert s.model.non_hours == [0,6, 7, 8]
assert s.model.non_hours == [0, 6, 7, 8]


@pytest.mark.asyncio
Expand Down Expand Up @@ -239,7 +241,7 @@ async def test_scheduler_overnight_no_charge():
mockdt=datetime.strptime("22-07-20 13:01", "%y-%m-%d %H:%M"),
)
assert s.active == True
assert s.model.non_hours == [13,14, 15, 16, 17, 18, 19, 20, 21, 22, 7, 8]
assert s.model.non_hours == [13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 7, 8]
await s.async_update(
avg24=310,
peak=1.5,
Expand All @@ -248,7 +250,7 @@ async def test_scheduler_overnight_no_charge():
prices_tomorrow=MOCKPRICES_TOMORROW,
mockdt=datetime.strptime("22-07-20 22:11", "%y-%m-%d %H:%M"),
)
assert s.model.non_hours == [22,7, 8]
assert s.model.non_hours == [22, 7, 8]
await s.async_update(
avg24=310,
peak=1.5,
Expand All @@ -270,42 +272,146 @@ async def test_scheduler_start_after_departure():
)
assert s.active is False


@pytest.mark.asyncio
async def test_scheduler_correct_hours():
MOCK240511 = [[0.15,0.26,0.35,0.37,0.41,0.41,0.45,0.53,0.57,0.18,0.09,0.08,0.07,0.07,0.07,0.08,0.12,0.53,0.59,0.62,0.57,0.55,0.49,0.45],[0.43,0.36,0.29,0.21,0.15,0.14,0.14,0.14,0.1,0.06,0.04,0.02,0,-0.02,0,0.05,0.08,0.12,0.44,0.66,0.71,0.69,0.6,0.49]]
MOCK240511 = [
[0.15, 0.26, 0.35, 0.37, 0.41, 0.41, 0.45, 0.53, 0.57, 0.18, 0.09, 0.08, 0.07, 0.07, 0.07, 0.08, 0.12, 0.53,
0.59, 0.62, 0.57, 0.55, 0.49, 0.45],
[0.43, 0.36, 0.29, 0.21, 0.15, 0.14, 0.14, 0.14, 0.1, 0.06, 0.04, 0.02, 0, -0.02, 0, 0.05, 0.08, 0.12, 0.44,
0.66, 0.71, 0.69, 0.6, 0.49]]
s = Scheduler(test=True)
start_dt = datetime.strptime("24-05-11 22:10", "%y-%m-%d %H:%M")
dep_time_dt = datetime.strptime("24-05-12 11:00", "%y-%m-%d %H:%M")
await s.async_create(
desired_charge=10, departuretime=dep_time_dt, starttime=start_dt
)
await s.async_update(avg24=417, peak=1.9, charged_amount=0, prices=MOCK240511[0], prices_tomorrow=MOCK240511[1], mockdt=start_dt+timedelta(seconds=2))
assert s.model.hours_charge == {datetime(2024, 5, 12, 5, 0): 1,
datetime(2024, 5, 12, 6, 0): 1,
datetime(2024, 5, 12, 7, 0): 0.6,
datetime(2024, 5, 12, 8, 0): 1,
datetime(2024, 5, 12, 9, 0): 1,
datetime(2024, 5, 12, 10, 0): 1,
datetime(2024, 5, 12, 11, 0): 1}
assert s.model.non_hours == [22,23,0,1,2,3,4]
assert s.active == True

await s.async_update(avg24=417, peak=1.9, charged_amount=0, prices=MOCK240511[0], prices_tomorrow=MOCK240511[1],
mockdt=start_dt + timedelta(seconds=2))
assert s.model.hours_charge == {datetime(2024, 5, 12, 5, 0): 1,
datetime(2024, 5, 12, 6, 0): 1,
datetime(2024, 5, 12, 7, 0): 0.6,
datetime(2024, 5, 12, 8, 0): 1,
datetime(2024, 5, 12, 9, 0): 1,
datetime(2024, 5, 12, 10, 0): 1,
datetime(2024, 5, 12, 11, 0): 1}
assert s.model.non_hours == [22, 23, 0, 1, 2, 3, 4]
assert s.active is True


@pytest.mark.asyncio
async def test_scheduler_correct_hours_2():
MOCK240511 = [[0.15,0.26,0.35,0.37,0.41,0.41,0.45,0.53,0.57,0.18,0.09,0.08,0.07,0.07,0.07,0.08,0.12,0.53,0.59,0.62,0.57,0.55,0.49,0.45],[0.43,0.36,0.29,0.21,0.15,0.14,0.14,0.14,0.1,0.06,0.04,0.02,0,-0.02,0,0.05,0.08,0.12,0.44,0.66,0.71,0.69,0.6,0.49]]
MOCK240511 = [
[0.15, 0.26, 0.35, 0.37, 0.41, 0.41, 0.45, 0.53, 0.57, 0.18, 0.09, 0.08, 0.07, 0.07, 0.07, 0.08, 0.12, 0.53,
0.59, 0.62, 0.57, 0.55, 0.49, 0.45],
[0.43, 0.36, 0.29, 0.21, 0.15, 0.14, 0.14, 0.14, 0.1, 0.06, 0.04, 0.02, 0, -0.02, 0, 0.05, 0.08, 0.12, 0.44,
0.66, 0.71, 0.69, 0.6, 0.49]]
s = Scheduler(test=True)
start_dt = datetime.strptime("24-05-12 10:07", "%y-%m-%d %H:%M")
dep_time_dt = datetime.strptime("24-05-12 13:00", "%y-%m-%d %H:%M")
dep_time_dt = datetime.strptime("24-05-12 13:30", "%y-%m-%d %H:%M")
await s.async_create(
desired_charge=5, departuretime=dep_time_dt, starttime=start_dt
)
await s.async_update(avg24=417, peak=1.9, charged_amount=0, prices=MOCK240511[1], mockdt=start_dt+timedelta(seconds=2))
model = UpdateSchedulerDTO(
moving_avg24=417,
peak=1.9,
chargecontroller_state=ChargeControllerStates.Start,
charged_amount=0,
prices=MOCK240511[0],
prices_tomorrow=MOCK240511[1]
)
await s.async_update(
dto=model,
mockdt=start_dt + timedelta(seconds=2)
)

assert s.model.hours_charge == {
datetime(2024, 5, 12, 10, 0): 0.3,
datetime(2024, 5, 12, 11, 0): 1,
datetime(2024, 5, 12, 12, 0): 1,
datetime(2024, 5, 12, 13, 0): 1
}
assert s.model.non_hours == [datetime(2024, 5, 12, 10, 0)]
assert s.active == True
}
assert s.model.non_hours == []
assert s.active is True


@pytest.mark.asyncio
async def test_scheduler_correct_hours_low_prices():
MOCK240511 = [
[0.15, 0.26, 0.35, 0.37, 0.41, 0.41, 0.45, 0.53, 0.57, 0.18, 0.09, 0.08, 0.07, 0.07, 0.07, 0.08, 0.12, 0.53,
0.59, 0.62, 0.57, 0.55, 0.49, 0.45],
[0.43, 0.36, 0.29, 0.21, 0.15, 0.14, 0.14, 0.14, 0.1, 0.06, 0.04, 0.02, 0, -0.02, 0, 0.05, 0.08, 0.12, 0.44,
0.66, 0.71, 0.69, 0.6, 0.49]]
s = Scheduler(test=True)
start_dt = datetime.strptime("24-05-12 10:07", "%y-%m-%d %H:%M")
dep_time_dt = datetime.strptime("24-05-12 13:30", "%y-%m-%d %H:%M")
await s.async_create(
desired_charge=5, departuretime=dep_time_dt, starttime=start_dt
)
model = UpdateSchedulerDTO(
moving_avg24=417,
peak=1.9,
chargecontroller_state=ChargeControllerStates.Start,
charged_amount=0,
prices=MOCK240511[0],
prices_tomorrow=MOCK240511[1]
)
await s.async_update(
dto=model,
mockdt=start_dt + timedelta(seconds=2)
)

assert s.model.hours_charge == {
datetime(2024, 5, 12, 10, 0): 0.3,
datetime(2024, 5, 12, 11, 0): 1,
datetime(2024, 5, 12, 12, 0): 1,
datetime(2024, 5, 12, 13, 0): 1
}
assert s.model.non_hours == []
assert s.active is True

P240827CENT = [-1.392, -1.529, -1.164, -0.913, -0.057, 0.103, 2.226, 4.200, 5.627, 5.227, 5.033, 4.679,
4.451, 1.347, 2.602, 3.515, 4.109, 4.771, 5.227, 4.805, 3.778, 2.876, 1.267, 0.000]
P240828CENT = [-0.125, -0.638, -0.911, -0.672, -0.114, 0.000, 1.856, 4.315, 5.043, 5.180, 4.929, 4.770,
4.485, 4.668, 4.576, 4.554, 5.658, 8.641, 9.210, 8.732, 7.252, 5.692, 4.315, 2.857]

@pytest.mark.asyncio
async def test_scheduler_correct_hours_negative_prices_cent_and_kr():
start_dt = datetime.strptime("24-08-27 21:07", "%y-%m-%d %H:%M")
dep_time_dt = datetime.strptime("24-08-28 08:00", "%y-%m-%d %H:%M")
charge = 1.36
avg_24 = 800
peak = 1.9
model = UpdateSchedulerDTO(
moving_avg24=avg_24,
peak=peak,
chargecontroller_state=ChargeControllerStates.Stop,
charged_amount=0,
prices=P240827CENT,
prices_tomorrow=P240828CENT
)

"""Test with cents"""
s = Scheduler(test=True)

await s.async_create(desired_charge=charge, departuretime=dep_time_dt, starttime=start_dt)
await s.async_update(dto=model,mockdt=start_dt + timedelta(seconds=2))

"""Test with full kr"""
model.prices = [x / 100 for x in P240827CENT]
model.prices_tomorrow = [x / 100 for x in P240828CENT]

s2 = Scheduler(test=True)

await s2.async_create(desired_charge=charge, departuretime=dep_time_dt, starttime=start_dt)
await s2.async_update(dto=model,mockdt=start_dt + timedelta(seconds=2))

"""Assert"""
assert s2.model.hours_charge == s.model.hours_charge
assert s2.model.hours_charge == {
datetime(2024, 8, 28, 2, 0): 1,
datetime(2024, 8, 28, 3, 0): 0.2,
}
assert s.active is True
assert s2.active is True

0 comments on commit 1a0adc7

Please sign in to comment.