Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LosantGists/cbb8d052c65b201878ba3c85bf73cea6 to your computer and use it in GitHub Desktop.
Save LosantGists/cbb8d052c65b201878ba3c85bf73cea6 to your computer and use it in GitHub Desktop.
Automating Unit Tests Blog (12) 7.23.21
import pandas as pd
import importlib
nb = importlib.import_module("ipynb.fs.defs.battery-stats")
remove_consecutive_same_values = nb.remove_consecutive_same_values
def build_battery_history(values, device_id="60df87b4be3fc900069dac1f", start_time=1600000000000):
id_array = [device_id] * len(values)
timestamp_array = map(lambda t: start_time + t * 10000, range(len(values)))
return pd.DataFrame({
"ID": pd.Series(id_array),
"battery": pd.Series(values, dtype="int64"),
"Timestamp": pd.Series(timestamp_array, dtype="int64")
})
# it keeps a single value
df_single_value = build_battery_history([25])
remove_consecutive_same_values( df_single_value )
assert len(df_single_value) == 1
# it removes a duplicate value
df_duplicate_values = build_battery_history([25,25])
assert len( remove_consecutive_same_values( df_duplicate_values ) ) == 1
# it does not remove non-consecutive same values
df_non_consecutive_same = build_battery_history([25,50,25])
assert len( remove_consecutive_same_values( df_non_consecutive_same ) ) == 3
# it removes multiple duplicates
df_multiple_duplicates = build_battery_history([25,50,50,20,20])
assert len( remove_consecutive_same_values( df_multiple_duplicates ) ) == 3
# it ignores consecutive values when they are from different devices
df_device_a = build_battery_history([25,50])
df_device_b = build_battery_history([50,75],device_id="70df87b4be3fc900069ddd9")
df_combined = pd.concat([df_device_a,df_device_b])
assert len(remove_consecutive_same_values( df_combined )) == 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment