def test_when_a_charged_battery_is_present_the_test_is_started(self): # ---- define voltage reading mock function and export it ---- def read_voltage(slot_id, i): vmax = 4.2 delta = 0.1 if i < 2: voltage_measured = 0 else: if slot_id == 1: voltage_measured = vmax - delta * (i - 2) if voltage_measured < 3: voltage_measured = 3 else: voltage_measured = 0 return voltage_measured with open('read_voltage.pkl', 'wb') as file: dill.dump(read_voltage, file) # launch the voltage reading for i = 0 df_slots_history = tester.main_function() # launch the voltage reading for i = 1 df_slots_history = tester.main_function() # there shall be 2 readings for each slot assert np.array_equal(df_slots_history.slot_id.values, [1, 2, 3, 4, 1, 2, 3, 4]) # the testing shall be ongoing only for the slot 1 at the time of the second measure assert np.array_equal(df_slots_history.testing.values, [0, 0, 0, 0, 1, 0, 0, 0]) # the first voltage of the battery during the test shall be 4.2 assert np.array_equal(df_slots_history.voltage.values, [0, 0, 0, 0, 4.2, 0, 0, 0]) # the calculated capacity in mAh shall be around 1s x 4.2V / 4Ohm / 3600s * 1000 = 0,2916 assert pytest.approx(0,2916, 0.01)
def test_when_we_remove_battery_before_end_of_test_no_csv_report_is_created(self): # ---- define voltage reading mock function and export it ---- def read_voltage(slot_id, i): vmax = 4.2 delta = 0.5 if i < 2: voltage_measured = 0 if i > 4: voltage_measured = 0 else: if slot_id == 1: voltage_measured = vmax - delta * (i - 2) if voltage_measured < 2.8: voltage_measured = 2.8 else: voltage_measured = 0 return voltage_measured with open('read_voltage.pkl', 'wb') as file: dill.dump(read_voltage, file) # list existing csv report files reports0 = [f for f in os.listdir('output') if re.match(r'[0-9]{4}.*mAh\.csv', f)] for i in range(5): tester.main_function() reports1 = [f for f in os.listdir('output') if re.match(r'[0-9]{4}.*mAh\.csv', f)] created_reports = np.setdiff1d(reports1, reports0) # the testing won't finish, so no report shall be created assert len(created_reports) == 0
def test_testing_of_a_battery_is_finished_voltage_rises_but_no_new_test_is_started(self): # ---- define voltage reading mock function and export it ---- def read_voltage(slot_id, i): vmax = 4.2 delta = 0.5 if i < 2: voltage_measured = 0 elif i >= 2 and i < 6: if slot_id == 1: voltage_measured = vmax - delta * (i - 2) if voltage_measured < 2.8: voltage_measured = 2.8 else: voltage_measured = 0 else: if slot_id == 1: voltage_measured = vmax - delta * (i - 6) if voltage_measured < 2.8: voltage_measured = 2.8 else: voltage_measured = 0 return voltage_measured with open('read_voltage.pkl', 'wb') as file: dill.dump(read_voltage, file) # list existing csv report files reports0 = [f for f in os.listdir('output') if re.match(r'[0-9]{4}.*mAh\.csv', f)] for i in range(10): tester.main_function() reports1 = [f for f in os.listdir('output') if re.match(r'[0-9]{4}.*mAh\.csv', f)] created_reports = np.setdiff1d(reports1, reports0) assert len(created_reports) == 1
def test_when_a_battery_under_test_passes_under_3V_the_test_is_stopped_and_a_report_is_created(self): # ---- define voltage reading mock function and export it ---- def read_voltage(slot_id, i): vmax = 4.2 delta = 0.5 if i < 2: voltage_measured = 0 else: if slot_id == 1: voltage_measured = vmax - delta * (i - 2) if voltage_measured < 2.8: voltage_measured = 2.8 else: voltage_measured = 0 return voltage_measured with open('read_voltage.pkl', 'wb') as file: dill.dump(read_voltage, file) # list existing csv report files reports0 = [f for f in os.listdir('output') if re.match(r'[0-9]{4}.*mAh\.csv', f)] for i in range(5): df_slots_history = tester.main_function() reports1 = [f for f in os.listdir('output') if re.match(r'[0-9]{4}.*mAh\.csv', f)] created_reports = np.setdiff1d(reports1, reports0) # the testing shall start when we insert the charged battery, and stop when it reaches 3V mask_3V = df_slots_history[df_slots_history.slot_id==1].voltage.values > 3 assert np.array_equal(mask_3V, df_slots_history[df_slots_history.slot_id==1].testing.values) assert len(created_reports) == 1 assert "_1_1_0mAh.csv" in created_reports[0]
def update_plot(self): df_measures = tester.main_function() self.df_measures = df_measures for slot_id in range(1, 5): df_values = df_measures[df_measures.slot_id == slot_id] self.axes[slot_id - 1].clear() df_session = df_values[df_values.testing_session == df_values.testing_session.max()] def draw_curve(df_session, slot_id): x = (pd.to_datetime(df_session.time) - pd.to_datetime( df_session.time.iloc[0])).astype('timedelta64[s]').values y = df_session.voltage.values text = str(round(df_session.spent_mah.max(), 1)) + "mAh" left = x[-1] / 2 bottom = 4 self.axes[slot_id - 1].text( left, bottom, text, horizontalalignment='left', verticalalignment='top', ) self.axes[slot_id - 1].plot(x, y) def write_text(text, slot_id): left = 0.2 bottom = 0.5 self.axes[slot_id - 1].text( left, bottom, text, horizontalalignment='left', verticalalignment='top', ) # calculate conditions to see in what case we are waiting_battery = df_values.voltage.values[-1] <= 2 inserted_discharged_battery = np.all( df_session.testing.values == False) finished_test = ( df_session.testing.values[-1] == False and df_session.testing.values[0] == True and df_session[df_session.testing == True].voltage.values.min() > 2.5) if waiting_battery: write_text('Waiting for battery', slot_id) elif inserted_discharged_battery: write_text( 'The inserted battery is \nnot fully charged \ntest not starting', slot_id) elif finished_test: text = 'The test is completed! \nCapacity: ' + str( round(df_session.spent_mah.max(), 1)) + "mAh" write_text(text, slot_id) else: draw_curve(df_session, slot_id) self.graph.draw() self.graph.flush_events() # flush the GUI events if not self.testing: # call this function again in the future self.after(1000, self.update_plot)