Esempio n. 1
0
    def test_capped_collection(self):
        pool_temp = RedisTimeCappedSubscriber(self.connection,
                                              'pool_temperature', 10).start(3)
        redis_toolbox.now = lambda: datetime(
            2015, 2, 14, 15, 0, 10, tzinfo=timezone.utc)

        yield from self.connection.publish(
            rfxcom_emiter_receiver.RFXCOM_KEY,
            dumps({
                'date': datetime(2015, 2, 14, 15, 0, 0).isoformat(),
                'temperature': 2.0
            }))
        yield from self.connection.publish(
            rfxcom_emiter_receiver.RFXCOM_KEY,
            dumps({
                'date': datetime(2015, 2, 14, 15, 0, 1).isoformat(),
                'temperature': 4.0
            }))
        yield from self.connection.publish(
            rfxcom_emiter_receiver.RFXCOM_KEY,
            dumps({
                'date': datetime(2015, 2, 14, 15, 0, 2).isoformat(),
                'temperature': 6.0
            }))
        yield from asyncio.wait_for(pool_temp.message_loop_task, timeout=1)

        value = yield from pool_temp.get_average()
        self.assertEqual(5.0, value)
Esempio n. 2
0
    def test_average_one_data(self):
        pool_temp = RedisTimeCappedSubscriber(self.connection, 'pool_temperature').start(1)

        yield from self.connection.publish(rfxcom_emiter_receiver.RFXCOM_KEY, dumps({'date': datetime.now().isoformat(), 'temperature': 3.0}))
        yield from asyncio.wait_for(pool_temp.message_loop_task, timeout=1)

        value = yield from pool_temp.get_average()
        self.assertEqual(3.0, value)
Esempio n. 3
0
    def test_get_live_data(self):
        live_data_handler = RedisTimeCappedSubscriber(self.connection, 'live_data', 3600, pubsub_key=CURRENT_COST_KEY, indicator_key='watt').start()
        yield from live_data_handler.handle({'date': redis_toolbox.now(), 'watt': 100})

        data = yield from live_data_handler.get_data()

        self.assertEqual(1, len(data))
        self.assertEqual({'date': redis_toolbox.now(), 'watt': 100}, data[0])
Esempio n. 4
0
    def test_average_two_equal_data(self):
        pool_temp = RedisTimeCappedSubscriber(self.connection, 'pool_temperature').start(2)

        yield from self.connection.publish(rfxcom_emiter_receiver.RFXCOM_KEY, dumps({'date': datetime(2015, 2, 14, 15, 0, 0).isoformat(), 'temperature': 3.0}))
        yield from self.connection.publish(rfxcom_emiter_receiver.RFXCOM_KEY, dumps({'date': datetime(2015, 2, 14, 15, 0, 1).isoformat(), 'temperature': 3.0}))
        yield from asyncio.wait_for(pool_temp.message_loop_task, timeout=1)

        value = yield from pool_temp.get_average()
        self.assertEqual(2, (yield from self.connection.zcard('pool_temperature')))
        self.assertEqual(3, value)
Esempio n. 5
0
    def test_capped_collection(self):
        pool_temp = RedisTimeCappedSubscriber(self.connection, 'pool_temperature', 10).start(3)
        redis_toolbox.now = lambda: datetime(2015, 2, 14, 15, 0, 10, tzinfo=timezone.utc)

        yield from self.connection.publish(rfxcom_emiter_receiver.RFXCOM_KEY, dumps({'date': datetime(2015, 2, 14, 15, 0, 0).isoformat(), 'temperature': 2.0}))
        yield from self.connection.publish(rfxcom_emiter_receiver.RFXCOM_KEY, dumps({'date': datetime(2015, 2, 14, 15, 0, 1).isoformat(), 'temperature': 4.0}))
        yield from self.connection.publish(rfxcom_emiter_receiver.RFXCOM_KEY, dumps({'date': datetime(2015, 2, 14, 15, 0, 2).isoformat(), 'temperature': 6.0}))
        yield from asyncio.wait_for(pool_temp.message_loop_task, timeout=1)

        value = yield from pool_temp.get_average()
        self.assertEqual(5.0, value)
Esempio n. 6
0
    def test_get_live_data_keeps_one_hour_data(self):
        test_now = datetime.now()
        live_data_handler = RedisTimeCappedSubscriber(self.connection, 'live_data', 3600, pubsub_key=CURRENT_COST_KEY, indicator_key='watt')

        redis_toolbox.now = lambda: test_now
        yield from live_data_handler.handle({'date': redis_toolbox.now(), 'watt': 100})
        redis_toolbox.now = lambda: test_now + timedelta(seconds=1800)
        yield from live_data_handler.handle({'date': redis_toolbox.now(), 'watt': 200})
        redis_toolbox.now = lambda: test_now + timedelta(seconds=3660)
        yield from live_data_handler.handle({'date': redis_toolbox.now(), 'watt': 300})

        self.assertEqual(2, len((yield from live_data_handler.get_data(since_seconds=3600))))
        self.assertEqual(1, len((yield from live_data_handler.get_data(since_seconds=1800))))
Esempio n. 7
0
    def test_average_one_data(self):
        pool_temp = RedisTimeCappedSubscriber(self.connection,
                                              'pool_temperature').start(1)

        yield from self.connection.publish(
            rfxcom_emiter_receiver.RFXCOM_KEY,
            dumps({
                'date': datetime.now().isoformat(),
                'temperature': 3.0
            }))
        yield from asyncio.wait_for(pool_temp.message_loop_task, timeout=1)

        value = yield from pool_temp.get_average()
        self.assertEqual(3.0, value)
Esempio n. 8
0
    def test_get_live_data(self):
        live_data_handler = RedisTimeCappedSubscriber(
            self.connection,
            'live_data',
            3600,
            pubsub_key=CURRENT_COST_KEY,
            indicator_key='watt').start()
        yield from live_data_handler.handle({
            'date': redis_toolbox.now(),
            'watt': 100
        })

        data = yield from live_data_handler.get_data()

        self.assertEqual(1, len(data))
        self.assertEqual({'date': redis_toolbox.now(), 'watt': 100}, data[0])
Esempio n. 9
0
    def test_get_live_data_keeps_one_hour_data(self):
        test_now = datetime.now()
        live_data_handler = RedisTimeCappedSubscriber(
            self.connection,
            'live_data',
            3600,
            pubsub_key=CURRENT_COST_KEY,
            indicator_key='watt')

        redis_toolbox.now = lambda: test_now
        yield from live_data_handler.handle({
            'date': redis_toolbox.now(),
            'watt': 100
        })
        redis_toolbox.now = lambda: test_now + timedelta(seconds=1800)
        yield from live_data_handler.handle({
            'date': redis_toolbox.now(),
            'watt': 200
        })
        redis_toolbox.now = lambda: test_now + timedelta(seconds=3660)
        yield from live_data_handler.handle({
            'date': redis_toolbox.now(),
            'watt': 300
        })

        self.assertEqual(
            2, len((yield from
                    live_data_handler.get_data(since_seconds=3600))))
        self.assertEqual(
            1, len((yield from
                    live_data_handler.get_data(since_seconds=1800))))
Esempio n. 10
0
    def test_average_two_equal_data(self):
        pool_temp = RedisTimeCappedSubscriber(self.connection,
                                              'pool_temperature').start(2)

        yield from self.connection.publish(
            rfxcom_emiter_receiver.RFXCOM_KEY,
            dumps({
                'date': datetime(2015, 2, 14, 15, 0, 0).isoformat(),
                'temperature': 3.0
            }))
        yield from self.connection.publish(
            rfxcom_emiter_receiver.RFXCOM_KEY,
            dumps({
                'date': datetime(2015, 2, 14, 15, 0, 1).isoformat(),
                'temperature': 3.0
            }))
        yield from asyncio.wait_for(pool_temp.message_loop_task, timeout=1)

        value = yield from pool_temp.get_average()
        self.assertEqual(
            2, (yield from self.connection.zcard('pool_temperature')))
        self.assertEqual(3, value)
Esempio n. 11
0
    def test_average_no_data(self):
        pool_temp = RedisTimeCappedSubscriber(self.connection,
                                              'pool_temperature').start()

        value = yield from pool_temp.get_average()
        self.assertEqual(0.0, value)
Esempio n. 12
0
    def test_average_no_data(self):
        pool_temp = RedisTimeCappedSubscriber(self.connection, 'pool_temperature').start()

        value = yield from pool_temp.get_average()
        self.assertEqual(0.0, value)