From b5da55bb1aa926bd1297871965a800f9dd4c0f9b Mon Sep 17 00:00:00 2001 From: Adrian Kumpf Date: Sun, 4 Oct 2020 11:13:16 +0200 Subject: [PATCH] Test cost calculation for negative cost --- test/teslamate/log/log_charging_test.exs | 28 ++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/test/teslamate/log/log_charging_test.exs b/test/teslamate/log/log_charging_test.exs index 40eb027aec4..13d30b98c48 100644 --- a/test/teslamate/log/log_charging_test.exs +++ b/test/teslamate/log/log_charging_test.exs @@ -543,6 +543,34 @@ defmodule TeslaMate.LogChargingTest do assert cproc.cost == Decimal.new("0.00") end + + test "cost per unit can be negative" do + car = car_fixture() + + assert %GeoFence{id: id} = + geofence_fixture(%{ + latitude: 50.1121, + longitude: 11.597, + radius: 50, + cost_per_unit: -0.15, + session_fee: 0.0 + }) + + assert {:ok, cproc} = + log_charging_process(charges_fixture(:phases_nil), + car: car, + attrs: %{ + date: DateTime.utc_now(), + latitude: 50.112198, + longitude: 11.597669 + } + ) + + assert cproc.charge_energy_added == Decimal.from_float(12.77) + assert cproc.charge_energy_used == Decimal.from_float(12.46) + assert cproc.cost == Decimal.new("-1.92") + end + test "sets charge cost to zero if free supercharging is enabled" do alias TeslaMate.Settings