def test_no_dates_in_the_future(db: me_db.Database): """Test that there are no dates in the future.""" now = datetime.datetime.now() with db.Session() as s: q = s.query(me_db.Measurement).filter(me_db.Measurement.date > now) assert q.count() == 0
def test_measurements_by_source_count(db: me_db.Database): """Test the number of measurements.""" # Query: # SELECT source,count(*) # FROM measurements # WHERE source like 'HealthKit:%' # GROUP BY source; with db.Session() as s: assert s.query(me_db.Measurement) \ .filter(me_db.Measurement.source == 'HealthKit:AppleWatch') \ .count() == 35 assert s.query(me_db.Measurement) \ .filter(me_db.Measurement.source == 'HealthKit:Health') \ .count() == 5 assert s.query(me_db.Measurement) \ .filter(me_db.Measurement.source == 'HealthKit:Iphone') \ .count() == 6 assert s.query(me_db.Measurement) \ .filter(me_db.Measurement.source == 'HealthKit:MiFit') \ .count() == 4 assert s.query(me_db.Measurement) \ .filter(me_db.Measurement.source == 'HealthKit:Workflow') \ .count() == 5 assert s.query(me_db.Measurement) \ .filter(me_db.Measurement.source == 'HealthKit:Health') \ .count() == 5
def test_measurements_count(db: me_db.Database): """Test the number of measurements.""" with db.Session() as s: q = s.query(me_db.Measurement) \ .filter(me_db.Measurement.source.like('HealthKit:%')) assert q.count() == 58
def test_group_measurements_counts(db: me_db.Database): """Test the number of measurements in Series.""" with db.Session() as s: q = (s.query(me_db.Measurement).filter( me_db.Measurement.source == "LifeCycle").filter( me_db.Measurement.series == "AlphaTime").filter( me_db.Measurement.group == "default")) assert q.count() == 19 with db.Session() as s: q = (s.query(me_db.Measurement).filter( me_db.Measurement.source == "LifeCycle").filter( me_db.Measurement.series == "AlphaTime").filter( me_db.Measurement.group == "Alpha")) assert q.count() == 9
def test_measurements_count(db: me_db.Database): """Test the number of measurements.""" with db.Session() as s: q = s.query( me_db.Measurement).filter(me_db.Measurement.source == "LifeCycle") # Test dataset has 25 entries - the first of which spans three days. assert q.count() == 28
def test_groups_not_empty(db: me_db.Database): """Test the number of measurements.""" with db.Session() as s: q = s.query(me_db.Measurement).filter(me_db.Measurement.group == "") # There should be no missing "group" field. Instead of an empty group, use # the value "default". assert q.count() == 0
def test_life_cycle_daily_total(db: me_db.Database): """Test that the sum of all measurements for a day is <= 24 hours.""" with db.Session() as s: q = s.query(func.DATE(me_db.Measurement.date).label('date'), (func.sum(me_db.Measurement.value)).label('time')) \ .filter(me_db.Measurement.source == 'LifeCycle') \ .group_by(me_db.Measurement.date) df = pd.read_sql(q.statement, q.session.bind) assert df[df.time > MILLISECONDS_IN_A_DAY].empty
def test_date_seconds(db: me_db.Database): """Test the date field values.""" # The start date (UTC) column of the test_inbox dataset begins at the given # datetime: 2017-01-20 01:55:01. start_date = datetime.datetime.strptime("2017-01-20 01:55:01", "%Y-%m-%d %H:%M:%S") with db.Session() as s: q = (s.query(me_db.Measurement.date).filter( me_db.Measurement.source == "LifeCycle").order_by( me_db.Measurement.date).limit(1)) (measurement_date, ) = q.one() assert measurement_date == start_date
def test_life_cycle_dates_are_unique(db: me_db.Database): """There can be no duplicate dates in Life Cycle measurements.""" with db.Session() as s: q = s.query(me_db.Measurement.date).filter( me_db.Measurement.source == "LifeCycle") num_life_cycle_dates = q.count() q = (s.query(me_db.Measurement.date).filter( me_db.Measurement.source == "LifeCycle").distinct()) num_distinct_life_cycle_dates = q.count() # There are no duplicate start dates. assert num_distinct_life_cycle_dates == num_life_cycle_dates
def test_life_cycle_dates_do_not_overflow(db: me_db.Database): """Test that no LifeCycle measurements overflow to the next day.""" with db.Session() as s: q = s.query(me_db.Measurement.date, me_db.Measurement.value) \ .filter(me_db.Measurement.source == 'LifeCycle') for start_date, value in q.distinct(): end_date = start_date + datetime.timedelta(milliseconds=value) # The number of days between the end and start of the measurement. day_diff = (end_date - start_date).days if not day_diff: continue # The only case where the end date is allowed to differ from the start # date is when we have overflowed to midnight (00:00:00) the next day. if not (day_diff == 1 and end_date.hour == 0 and end_date.minute == 0 and end_date.second == 0): pytest.fail( f'Date {start_date} overflows when adding measurement {value} ms ' f'(calculated end date: {end_date})')
def test_num_measurements(db: me_db.Database): """Test that at least measurement has been imported.""" with db.Session() as s: q = s.query(me_db.Measurement) assert q.count() > 1