def testScalarTally(self): with weewx.manager.open_manager_with_config(self.config_dict, 'wx_binding') as manager: # Pick a random day, say 15 March: start_ts = int(time.mktime((2010, 3, 15, 0, 0, 0, 0, 0, -1))) stop_ts = int(time.mktime((2010, 3, 16, 0, 0, 0, 0, 0, -1))) # Sanity check that this is truly the start of day: self.assertEqual(start_ts, weeutil.weeutil.startOfDay(start_ts)) # Get a day's stats from the daily summaries: allStats = manager._get_day_summary(start_ts) # Now calculate the same summaries from the raw data in the archive. # Here are some random observation types: for stats_type in ['barometer', 'outTemp', 'rain']: # Now test all the aggregates: for aggregate in ['min', 'max', 'sum', 'count', 'avg']: # Compare to the main archive: res = manager.getSql( "SELECT %s(%s) FROM archive WHERE dateTime>? AND dateTime <=?;" % (aggregate, stats_type), (start_ts, stop_ts)) # The results from the daily summaries for this aggregation allStats_res = getattr(allStats[stats_type], aggregate) self.assertAlmostEqual(allStats_res, res[0], msg="Value check. Failing type %s, aggregate: %s" % (stats_type, aggregate)) # Check the times of min and max as well: if aggregate in ['min', 'max']: res2 = manager.getSql( "SELECT dateTime FROM archive WHERE %s = ? AND dateTime>? AND dateTime <=?" % (stats_type,), (res[0], start_ts, stop_ts)) stats_time = getattr(allStats[stats_type], aggregate + 'time') self.assertEqual(stats_time, res2[0], "Time check. Failing type %s, aggregate: %s" % (stats_type, aggregate))
def testRebuild(self): with weewx.manager.open_manager_with_config(self.config_dict, 'wx_binding') as manager: # Pick a random day, say 15 March: start_d = datetime.date(2010, 3, 15) stop_d = datetime.date(2010, 3, 15) start_ts = int(time.mktime(start_d.timetuple())) # Get the day's statistics: origStats = manager._get_day_summary(start_ts) # Rebuild that day: manager.backfill_day_summary(start_d=start_d, stop_d=stop_d) # Get the new statistics newStats = manager._get_day_summary(start_ts) # Check for equality for obstype in ('outTemp', 'barometer', 'windSpeed'): self.assertTrue(all([getattr(origStats[obstype], prop) == \ getattr(newStats[obstype], prop) \ for prop in ('min', 'mintime', 'max', 'maxtime', 'sum', 'count', 'wsum', 'sumtime', 'last', 'lasttime')]))
def testWindTally(self): with weewx.manager.open_manager_with_config(self.config_dict, 'wx_binding') as manager: # Pick a random day, say 15 March: start_ts = int(time.mktime((2010, 3, 15, 0, 0, 0, 0, 0, -1))) stop_ts = int(time.mktime((2010, 3, 16, 0, 0, 0, 0, 0, -1))) # Sanity check that this is truly the start of day: self.assertEqual(start_ts, weeutil.weeutil.startOfDay(start_ts)) allStats = manager._get_day_summary(start_ts) # Test all the aggregates: for aggregate in ['min', 'max', 'sum', 'count', 'avg']: if aggregate == 'max': res = manager.getSql( "SELECT MAX(windGust) FROM archive WHERE dateTime>? AND dateTime <=?;", (start_ts, stop_ts)) else: res = manager.getSql( "SELECT %s(windSpeed) FROM archive WHERE dateTime>? AND dateTime <=?;" % (aggregate, ), (start_ts, stop_ts)) # From StatsDb: allStats_res = getattr(allStats['wind'], aggregate) self.assertAlmostEqual(allStats_res, res[0]) # Check the times of min and max as well: if aggregate == 'min': resmin = manager.getSql( "SELECT dateTime FROM archive WHERE windSpeed = ? AND dateTime>? AND dateTime <=?", (res[0], start_ts, stop_ts)) self.assertEqual(allStats['wind'].mintime, resmin[0]) elif aggregate == 'max': resmax = manager.getSql( "SELECT dateTime FROM archive WHERE windGust = ? AND dateTime>? AND dateTime <=?", (res[0], start_ts, stop_ts)) self.assertEqual(allStats['wind'].maxtime, resmax[0]) # Check RMS: (squaresum, count) = manager.getSql( "SELECT SUM(windSpeed*windSpeed), COUNT(windSpeed) from archive where dateTime>? AND dateTime<=?;", (start_ts, stop_ts)) rms = math.sqrt(squaresum / count) if count else None self.assertAlmostEqual(allStats['wind'].rms, rms)