Exemple #1
0
    def test_main_successful(self):
        """Run the main program successfully."""

        api_response = {
            'result': 1,
            'message': 'yes',
            'epidata': [{
                'foo': 'bar'
            }],
        }

        args = MagicMock(log_file="log")
        mock_epidata_impl = MagicMock()
        mock_epidata_impl.covidcast_meta.return_value = api_response
        mock_database = MagicMock()
        mock_database.compute_covidcast_meta.return_value = api_response[
            'epidata']
        fake_database_impl = lambda: mock_database

        main(args,
             epidata_impl=mock_epidata_impl,
             database_impl=fake_database_impl)

        self.assertTrue(mock_database.connect.called)

        self.assertTrue(mock_database.update_covidcast_meta_cache.called)
        actual_args = mock_database.update_covidcast_meta_cache.call_args[0]
        expected_args = (api_response['epidata'], )
        self.assertEqual(actual_args, expected_args)

        self.assertTrue(mock_database.disconnect.called)
        self.assertTrue(mock_database.disconnect.call_args[0][0])
Exemple #2
0
    def test_main_failure(self):
        """Run the main program with a query failure."""

        api_response = {
            'result': -123,
            'message': 'no',
        }

        args = MagicMock(log_file="log")
        mock_database = MagicMock()
        mock_database.compute_covidcast_meta.return_value = list()
        fake_database_impl = lambda: mock_database

        main(args, epidata_impl=None, database_impl=fake_database_impl)

        self.assertTrue(mock_database.compute_covidcast_meta.called)
  def test_caching(self):
    """Populate, query, cache, query, and verify the cache."""

    # insert dummy data
    self.cur.execute('''
      insert into covidcast values
        (0, 'src', 'sig', 'day', 'state', 20200422, 'pa',
          123, 1, 2, 3, 456, 1, 20200422, 0, 1, False),
        (0, 'src', 'sig', 'day', 'state', 20200422, 'wa',
          789, 1, 2, 3, 456, 1, 20200423, 1, 1, False)
    ''')
    self.cur.execute('''
      insert into covidcast values
        (100, 'src', 'wip_sig', 'day', 'state', 20200422, 'pa',
          456, 4, 5, 6, 789, -1, 20200422, 0, 1, True)
    ''')

    self.cnx.commit()

    # make sure the live utility is serving something sensible
    cvc_database = live.Database()
    cvc_database.connect()
    epidata1 = cvc_database.get_covidcast_meta()
    cvc_database.disconnect(False)
    self.assertEqual(len(epidata1),1)
    self.assertEqual(epidata1, [
      {
        'data_source': 'src',
        'signal': 'sig',
        'time_type': 'day',
        'geo_type': 'state',
        'min_time': 20200422,
        'max_time': 20200422,
        'num_locations': 2,
        'last_update': 789,
        'min_value': 1,
        'max_value': 1,
        'mean_value': 1,
        'stdev_value': 0,
        'max_issue': 20200423,
        'min_lag': 0,
        'max_lag': 1,
      }
    ])
    epidata1={'result':1, 'message':'success', 'epidata':epidata1}

    # make sure the API covidcast_meta is still blank, since it only serves
    # the cached version and we haven't cached anything yet
    epidata2 = Epidata.covidcast_meta()
    self.assertEqual(epidata2['result'], -2, json.dumps(epidata2))

    # update the cache
    args = None
    main(args)

    # fetch the cached version
    epidata3 = Epidata.covidcast_meta()

    # cached version should now equal live version
    self.assertEqual(epidata1, epidata3)

    # insert dummy data timestamped as of now
    self.cur.execute('''
      update covidcast_meta_cache set
        timestamp = UNIX_TIMESTAMP(NOW()),
        epidata = '[{"hello": "world"}]'
    ''')
    self.cnx.commit()

    # fetch the cached version (manually)
    params = {'endpoint': 'covidcast_meta', 'cached': 'true'}
    response = requests.get(BASE_URL, params=params)
    response.raise_for_status()
    epidata4 = response.json()

    # make sure the cache was actually served
    self.assertEqual(epidata4, {
      'result': 1,
      'epidata': [{
        'hello': 'world',
      }],
      'message': 'success',
    })

    # insert dummy data timestamped as 2 hours old
    self.cur.execute('''
      update covidcast_meta_cache set
        timestamp = UNIX_TIMESTAMP(NOW()) - 3600 * 2,
        epidata = '[{"hello": "world"}]'
    ''')
    self.cnx.commit()

    # fetch the cached version (manually)
    params = {'endpoint': 'covidcast_meta', 'cached': 'true'}
    response = requests.get(BASE_URL, params=params)
    response.raise_for_status()
    epidata5 = response.json()

    # make sure the cache was returned anyhow
    self.assertEqual(epidata4, epidata5)