def test_charge_session_cost_calculation(self): """Test if charge session cost is calculated correctly. current (reference) time = 0 sec Post a tariff to the server with validity starting in +5 sec. Post a scpp message to the server for user `pete` with startTime=+10 sec. Check the charge cost for user 'pete' for session with startTime=+10 sec.""" now = datetime.now() # reference time # tariff tariff_req = self.tariff_req(5.2, 1, .25, iso8601ZFormat(now + timedelta(0, 5))) # tariff activeStarting=ref. time + 2 sec response = urlopen(tariff_req) self.assertEqual(200, response.getcode()) # scpp five_sec_later = now + timedelta(0, 5) half_hr_five_sec_later = now + timedelta(0, 1803) scpp_req = self.scpp_req('pete', five_sec_later, half_hr_five_sec_later, 100) response = urlopen(scpp_req) self.assertEqual(200, response.getcode()) # verify the charge cost for `pete` for the above session charge_session_info_pete = 'from {startTime} to {endTime}: 100.00 kWh @ 30.7\n'.format(startTime=five_sec_later.strftime('%Y-%m-%d %H:%M'), endTime=half_hr_five_sec_later.strftime('%Y-%m-%d %H:%M')) print(charge_session_info_pete) month = five_sec_later.month if month < 10: month = '0' + str(month) pete_invoice_url = self.server_url + 'invoices/{year}/{month}/pete.txt'.format(year=five_sec_later.year, month=month) with urlopen(pete_invoice_url) as response: while 1: line = response.readline().decode('utf-8') self.assertTrue(line != '') # EOF if line == charge_session_info_pete: return # success!i
def test_valid_tariff_post(self): """Post a valid tariff msg to the server""" tariff_req = self.tariff_req(.2, 1, .25, iso8601ZFormat(datetime.now() + timedelta(0, 20))) # activeStarting = current time + 20 sec response = urlopen(tariff_req) self.assertEqual(200, response.getcode())