Skip to content
Browse files

Added the test back and switched to running all tests

  • Loading branch information...
shankari committed Feb 25, 2018
1 parent a400bfc commit b5f441a5b9bde8501d8734accbe6f8debe356e22
Showing with 145 additions and 1 deletion.
  1. +144 −0 emission/tests/netTests/
  2. +1 −1
@@ -0,0 +1,144 @@
from __future__ import unicode_literals
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
from future import standard_library
from builtins import *
import unittest
import logging
import arrow
import os

import emission.core.get_database as edb
import emission.core.wrapper.localdate as ecwl

import emission.tests.common as etc

import as eaicf

import as estfm
import as esdldq

from import metrics

class TestMetricsInferredSections(unittest.TestCase):
def setUp(self):
self.seed_mode_path = etc.copy_dummy_seed_for_inference()
self.testUUID1 = self.testUUID
"After loading, timeseries db size = %s" % edb.get_timeseries_db().count())
self.aug_start_ts = 1438387200
self.aug_end_ts = 1441065600
self.day_start_dt = esdldq.get_local_date(self.aug_start_ts, "America/Los_Angeles")
self.day_end_dt = esdldq.get_local_date(self.aug_end_ts, "America/Los_Angeles")

def tearDown(self):

def clearRelatedDb(self):
edb.get_timeseries_db().delete_many({"user_id": self.testUUID})
edb.get_analysis_timeseries_db().delete_many({"user_id": self.testUUID})
edb.get_pipeline_state_db().delete_many({"user_id": self.testUUID})
edb.get_timeseries_db().delete_many({"user_id": self.testUUID1})
edb.get_analysis_timeseries_db().delete_many({"user_id": self.testUUID1})
edb.get_pipeline_state_db().delete_many({"user_id": self.testUUID1})

def testCountTimestampMetrics(self):
met_result = metrics.summarize_by_timestamp(self.testUUID,
self.aug_start_ts, self.aug_end_ts,
'd', ['count'], True)

import json
import bson.json_util as bju

logging.debug(json.dumps(met_result, default=bju.default))

self.assertEqual(list(met_result.keys()), ['aggregate_metrics', 'user_metrics'])
user_met_result = met_result['user_metrics'][0]
agg_met_result = met_result['aggregate_metrics'][0]

self.assertEqual(len(user_met_result), 2)
self.assertEqual([m.nUsers for m in user_met_result], [1,1])
self.assertEqual(user_met_result[0], 27)
self.assertEqual(user_met_result[1], 28)
self.assertEqual(user_met_result[0].WALKING, 7)
self.assertNotIn("BICYCLING", user_met_result[0])
# Changed from 3 to 4 - investigation at
self.assertEqual(user_met_result[0].BUS, 4)
# We are not going to make absolute value assertions about
# the aggregate values since they are affected by other
# entries in the database. However, because we have at least
# data for two days in the database, the aggregate data
# must be at least that much larger than the original data.
self.assertEqual(len(agg_met_result), 8)
# no overlap between users at the daily level
# bunch of intermediate entries with no users since this binning works
# by range
self.assertEqual([m.nUsers for m in agg_met_result], [1,1,0,0,0,0,1,1])
# If there are no users, there are no values for any of the fields
# since these are never negative, it implies that their sum is zero
self.assertTrue('WALKING' not in agg_met_result[2] and
'BICYCLING' not in agg_met_result[2] and
'IN_VEHICLE' not in agg_met_result[2])

def testCountLocalDateMetrics(self):
met_result = metrics.summarize_by_local_date(self.testUUID,
ecwl.LocalDate({'year': 2015, 'month': 8}),
ecwl.LocalDate({'year': 2015, 'month': 9}),
'MONTHLY', ['count'], True)
self.assertEqual(list(met_result.keys()), ['aggregate_metrics', 'user_metrics'])
user_met_result = met_result['user_metrics'][0]
agg_met_result = met_result['aggregate_metrics'][0]


# local timezone means that we only have one entry
self.assertEqual(len(user_met_result), 1)
self.assertEqual(user_met_result[0].nUsers, 1)
self.assertEqual(user_met_result[0].WALKING, 12)
self.assertNotIn('BICYCLING', user_met_result[0])
self.assertEqual(user_met_result[0].BUS, 4)
# We are not going to make assertions about the aggregate values since
# they are affected by other entries in the database but we expect them
# to be at least as much as the user values
self.assertEqual(len(agg_met_result), 1)
self.assertEqual(agg_met_result[0].nUsers, 2)
user_met_result[0].WALKING + 5) # 21s has three bike trips
user_met_result[0].BUS + 2) # 21s has three motorized trips

def testCountNoEntries(self):
# Ensure that we don't crash if we don't find any entries
# Should return empty array instead
# Unlike in
met_result_ld = metrics.summarize_by_local_date(self.testUUID,
ecwl.LocalDate({'year': 2000}),
ecwl.LocalDate({'year': 2001}),
'MONTHLY', ['count'], True)
self.assertEqual(list(met_result_ld.keys()), ['aggregate_metrics', 'user_metrics'])
self.assertEqual(met_result_ld['aggregate_metrics'][0], [])
self.assertEqual(met_result_ld['user_metrics'][0], [])

met_result_ts = metrics.summarize_by_timestamp(self.testUUID,
'm', ['count'], True)
self.assertEqual(list(met_result_ts.keys()), ['aggregate_metrics', 'user_metrics'])
self.assertEqual(met_result_ts['aggregate_metrics'][0], [])
self.assertEqual(met_result_ts['user_metrics'][0], [])

if __name__ == '__main__':
import emission.tests.common as etc
@@ -6,4 +6,4 @@ set -e
# + ./
# rm: missing operand
# Try `rm --help' for more information.
PYTHONPATH=. python -m unittest discover -s emission/individual_tests -p Test*;
PYTHONPATH=. python -m unittest discover -s emission/tests -p Test*;

0 comments on commit b5f441a

Please sign in to comment.
You can’t perform that action at this time.