def testEmittedSampleDatetime(self):
    key = "bogus-test-key"

    # Establish initial sample datetime

    result = metric_utils.establishLastEmittedSampleDatetime(key, 300)

    # Cleanup
    self.addCleanup(
      collectorsdb.engineFactory().execute,
      schema.emittedSampleTracker.delete().where(  # pylint: disable=E1120
        (schema.emittedSampleTracker.c.key == key)
      )
    )

    self.assertIsInstance(result, datetime)

    # Update latest emitted sample datetime to now

    now = datetime.utcnow().replace(microsecond=0)
    metric_utils.updateLastEmittedSampleDatetime(key, now)

    # Verify that it was updated

    lastEmittedSample = metric_utils.queryLastEmittedSampleDatetime(key)

    self.assertEqual(now, lastEmittedSample)
    self.assertLess(result, lastEmittedSample)
Esempio n. 2
0
    def testEmittedSampleDatetime(self):
        key = "bogus-test-key"

        # Establish initial sample datetime

        result = metric_utils.establishLastEmittedSampleDatetime(key, 300)

        # Cleanup
        self.addCleanup(
            collectorsdb.engineFactory().execute,
            schema.emittedSampleTracker.delete().where(  # pylint: disable=E1120
                (schema.emittedSampleTracker.c.key == key)))

        self.assertIsInstance(result, datetime)

        # Update latest emitted sample datetime to now

        now = datetime.utcnow().replace(microsecond=0)
        metric_utils.updateLastEmittedSampleDatetime(key, now)

        # Verify that it was updated

        lastEmittedSample = metric_utils.queryLastEmittedSampleDatetime(key)

        self.assertEqual(now, lastEmittedSample)
        self.assertLess(result, lastEmittedSample)
  def testEstablishLastEmittedSampleDatetime(self, collectorsdbMock,
                                             datetimeMock):

    fakeNow = datetime(1997, 8, 29, 2, 14)
    datetimeMock.utcnow.return_value = fakeNow

    # Test with non-None queryLastEmittedSampleDatetime() result

    collectorsdbMock.engineFactory.return_value = Mock(
      spec_set=sqlalchemy.engine.Engine)
    result = metric_utils.establishLastEmittedSampleDatetime(
      "twitter-tweets-volume", 300)

    self.assertEqual(result, (collectorsdbMock
                              .engineFactory
                              .return_value
                              .execute
                              .return_value
                              .scalar
                              .return_value))

    # Test again with None queryLastEmittedSampleDatetime() result

    collectorsdbMock.engineFactory.return_value.execute.reset_mock()
    with patch("taurus_metric_collectors.metric_utils"
               ".queryLastEmittedSampleDatetime") \
         as queryLastEmittedSampleDatetime:
      queryLastEmittedSampleDatetime.return_value = None
      result = metric_utils.establishLastEmittedSampleDatetime(
        "twitter-tweets-volume", 300)

      args, _ = (
        collectorsdbMock.engineFactory.return_value.execute.call_args_list[0])

      params = args[0].compile().construct_params()

      self.assertEqual(params["sample_ts"].microsecond, 0)
      self.assertEqual(fakeNow - params["sample_ts"], timedelta(seconds=300))
      self.assertEqual(queryLastEmittedSampleDatetime.call_count, 2)
      args, _ = (collectorsdbMock
                 .engineFactory
                 .return_value
                 .execute
                 .call_args_list[0])
      self.assertTrue(args)
      self.assertIsInstance(args[0], sqlalchemy.sql.dml.Insert)
Esempio n. 4
0
    def testEstablishLastEmittedSampleDatetime(self, collectorsdbMock,
                                               datetimeMock):

        fakeNow = datetime(1997, 8, 29, 2, 14)
        datetimeMock.utcnow.return_value = fakeNow

        # Test with non-None queryLastEmittedSampleDatetime() result

        collectorsdbMock.engineFactory.return_value = Mock(
            spec_set=sqlalchemy.engine.Engine)
        result = metric_utils.establishLastEmittedSampleDatetime(
            "twitter-tweets-volume", 300)

        self.assertEqual(result, (collectorsdbMock.engineFactory.return_value.
                                  execute.return_value.scalar.return_value))

        # Test again with None queryLastEmittedSampleDatetime() result

        collectorsdbMock.engineFactory.return_value.execute.reset_mock()
        with patch("taurus_metric_collectors.metric_utils"
                   ".queryLastEmittedSampleDatetime") \
             as queryLastEmittedSampleDatetime:
            queryLastEmittedSampleDatetime.return_value = None
            result = metric_utils.establishLastEmittedSampleDatetime(
                "twitter-tweets-volume", 300)

            args, _ = (collectorsdbMock.engineFactory.return_value.execute.
                       call_args_list[0])

            params = args[0].compile().construct_params()

            self.assertEqual(params["sample_ts"].microsecond, 0)
            self.assertEqual(fakeNow - params["sample_ts"],
                             timedelta(seconds=300))
            self.assertEqual(queryLastEmittedSampleDatetime.call_count, 2)
            args, _ = (collectorsdbMock.engineFactory.return_value.execute.
                       call_args_list[0])
            self.assertTrue(args)
            self.assertIsInstance(args[0], sqlalchemy.sql.dml.Insert)