Example #1
0
    def setUp(self):
        self.pool = yield from aiomysql.create_pool(host='127.0.0.1',
                                                    port=3306,
                                                    user='******',
                                                    password='******',
                                                    db='test',
                                                    loop=self.loop)

        self.message_handler = MysqlCurrentCostMessageHandler(self.pool)
        self.current_cost_service = CurrentCostDatabaseReader(
            self.pool, time(8, 0), time(22, 0))
        current_cost_mysql_service.now = lambda: datetime(
            2015, 6, 1, 12, 0, 0, tzinfo=get_localzone())
        with (yield from self.pool) as conn:
            cur = yield from conn.cursor()
            yield from cur.execute("truncate current_cost")
Example #2
0
    def setUp(self):
        self.pool = yield from aiomysql.create_pool(
            host='127.0.0.1',
            port=3306,
            user='******',
            password='******',
            db='test',
            loop=asyncio.get_event_loop())

        with (yield from self.pool) as conn:
            cur = yield from conn.cursor()
            yield from cur.execute("drop table if EXISTS current_cost")
            yield from cur.close()

        redis_toolbox.now = lambda: datetime(
            2012, 12, 13, 14, 2, 0, tzinfo=timezone.utc)
        self.message_handler = MysqlCurrentCostMessageHandler(self.pool)
Example #3
0
    def setUp(self):
        self.pool = yield from aiomysql.create_pool(
            host='127.0.0.1',
            port=3306,
            user='******',
            password='******',
            db='test',
            loop=asyncio.get_event_loop())

        self.server = yield from domopyc_server.init(asyncio.get_event_loop(),
                                                     self.pool,
                                                     port=12345)
        self.message_handler = MysqlCurrentCostMessageHandler(self.pool)
        with (yield from self.pool) as conn:
            cur = yield from conn.cursor()
            yield from cur.execute("truncate current_cost")
            yield from cur.close()
Example #4
0
class MysqlAverageMessageHandlerTest(asynctest.TestCase):
    @asyncio.coroutine
    def setUp(self):
        self.pool = yield from aiomysql.create_pool(host='127.0.0.1', port=3306,
                                                    user='******', password='******', db='test',
                                                    loop=asyncio.get_event_loop())

        with (yield from self.pool) as conn:
            cur = yield from conn.cursor()
            yield from cur.execute("drop table if EXISTS current_cost")
            yield from cur.close()

        redis_toolbox.now = lambda: datetime(2012, 12, 13, 14, 2, 0, tzinfo=timezone.utc)
        self.message_handler = MysqlCurrentCostMessageHandler(self.pool)

    @asyncio.coroutine
    def tearDown(self):
        self.pool.close()
        yield from self.pool.wait_closed()

    @asyncio.coroutine
    def test_create_table_if_it_doesnot_exist(self):
        with (yield from self.pool) as conn:
            cur = yield from conn.cursor()

            yield from cur.execute("show tables like 'current_cost'")
            current_cost_table = yield from cur.fetchall()
            self.assertEqual((('current_cost',),), current_cost_table)

            MysqlCurrentCostMessageHandler(self.pool, average_period_minutes=10)

            yield from cur.execute("show tables like 'current_cost'")
            current_cost_table = yield from cur.fetchall()
            self.assertEquals((('current_cost',),), current_cost_table)
            yield from cur.close()

    @asyncio.coroutine
    def test_save_event_mysql(self):
        with (yield from self.pool) as conn:
            now = datetime(2012, 12, 13, 14, 0, 7, tzinfo=timezone.utc)

            yield from self.message_handler.handle({'date': now, 'watt': 305.0, 'temperature': 21.4})

            table_rows = yield from self.nb_table_rows('current_cost')
            self.assertEquals(1, table_rows)

            cursor = yield from conn.cursor()
            yield from cursor.execute("select timestamp, watt, minutes, nb_data, temperature from current_cost")
            rows = yield from cursor.fetchall()
            self.assertEqual((datetime(2012, 12, 13, 14, 0, 7), 305, 0, 1, 21.4), rows[0])
            yield from cursor.close()

    def nb_table_rows(self, table):
        with (yield from self.pool) as conn:
            cursor = yield from conn.cursor()
            yield from cursor.execute("select * from %s" % table)
            allrows = yield from cursor.fetchall()
            return len(allrows)
class RedisGetDataOfDay(TestCase):
    @asyncio.coroutine
    def setUp(self):
        self.pool = yield from aiomysql.create_pool(host='127.0.0.1', port=3306,
                                                    user='******', password='******', db='test',
                                                    loop=asyncio.get_event_loop())

        self.server = yield from domopyc_server.init(asyncio.get_event_loop(), self.pool, port=12345)
        self.message_handler = MysqlCurrentCostMessageHandler(self.pool)
        with (yield from self.pool) as conn:
            cur = yield from conn.cursor()
            yield from cur.execute("truncate current_cost")
            yield from cur.close()

    @asyncio.coroutine
    def tearDown(self):
        self.server.close()
        self.pool.close()
        yield from self.pool.wait_closed()

    @asyncio.coroutine
    def test_get_current_cost_history_one_line(self):
        yield from self.message_handler.save({'date': datetime(2015, 5, 28, 12, 0, 0, tzinfo=timezone.utc), 'watt': 123, 'minutes': 60, 'nb_data': 120, 'temperature': 20.2})

        response = yield from aiohttp.request('GET', 'http://127.0.0.1:12345/power/history')
        json_response = yield from response.json()

        self.assertEqual(1, len(json_response['data']))
        self.assertEqual(['2015-05-28T00:00:00', 0.123], json_response['data'][0])

    @asyncio.coroutine
    def test_get_current_cost_by_day_with_previous_day(self):
        yield from self.message_handler.save({'date': datetime(2015, 5, 29, 12, 10, 0), 'watt': 1000, 'minutes': 60, 'nb_data': 120, 'temperature': 20.6})
        yield from self.message_handler.save({'date': datetime(2015, 5, 30, 23, 20, 0), 'watt': 500, 'minutes': 180, 'nb_data': 120, 'temperature': 20.2})

        response = yield from aiohttp.request('GET', 'http://127.0.0.1:12345/power/day/%s' % datetime(2015, 5, 30, 0, 0).isoformat())
        json_response = yield from response.json()

        self.assertEqual(1, len(json_response['day_data']))
        self.assertEqual(1, len(json_response['previous_day_data']))
        self.assertEqual(['2015-05-30T23:20:00', 500, 20.2], json_response['day_data'][0])
        self.assertEqual(['2015-05-29T12:10:00', 1000, 20.6], json_response['previous_day_data'][0])
    def setUp(self):
        self.pool = yield from aiomysql.create_pool(host='127.0.0.1', port=3306,
                                                    user='******', password='******', db='test',
                                                    loop=self.loop)

        self.message_handler = MysqlCurrentCostMessageHandler(self.pool)
        self.current_cost_service = CurrentCostDatabaseReader(self.pool, time(8, 0), time(22, 0))
        current_cost_mysql_service.now = lambda: datetime(2015, 6, 1, 12, 0, 0, tzinfo=get_localzone())
        with (yield from self.pool) as conn:
            cur = yield from conn.cursor()
            yield from cur.execute("truncate current_cost")
    def setUp(self):
        self.pool = yield from aiomysql.create_pool(host='127.0.0.1', port=3306,
                                                    user='******', password='******', db='test',
                                                    loop=asyncio.get_event_loop())

        self.server = yield from domopyc_server.init(asyncio.get_event_loop(), self.pool, port=12345)
        self.message_handler = MysqlCurrentCostMessageHandler(self.pool)
        with (yield from self.pool) as conn:
            cur = yield from conn.cursor()
            yield from cur.execute("truncate current_cost")
            yield from cur.close()
Example #8
0
    def setUp(self):
        self.pool = yield from aiomysql.create_pool(host='127.0.0.1', port=3306,
                                                    user='******', password='******', db='test',
                                                    loop=asyncio.get_event_loop())

        with (yield from self.pool) as conn:
            cur = yield from conn.cursor()
            yield from cur.execute("drop table if EXISTS current_cost")
            yield from cur.close()

        redis_toolbox.now = lambda: datetime(2012, 12, 13, 14, 2, 0, tzinfo=timezone.utc)
        self.message_handler = MysqlCurrentCostMessageHandler(self.pool)
Example #9
0
    def test_create_table_if_it_doesnot_exist(self):
        with (yield from self.pool) as conn:
            cur = yield from conn.cursor()

            yield from cur.execute("show tables like 'current_cost'")
            current_cost_table = yield from cur.fetchall()
            self.assertEqual((('current_cost', ), ), current_cost_table)

            MysqlCurrentCostMessageHandler(self.pool,
                                           average_period_minutes=10)

            yield from cur.execute("show tables like 'current_cost'")
            current_cost_table = yield from cur.fetchall()
            self.assertEquals((('current_cost', ), ), current_cost_table)
            yield from cur.close()
Example #10
0
def run_application(mysq_pool, config):
    # backend
    redis_pool_ = yield from create_redis_pool()
    daq_rfxcom = yield from create_rfxtrx433e(config['rfxcom'])
    current_cost = create_current_cost(redis_pool_, config['current_cost'])
    current_cost_recorder = AsyncRedisSubscriber(
        redis_pool_,
        MysqlCurrentCostMessageHandler(mysq_pool, average_period_minutes=10),
        CURRENT_COST_KEY).start()
    pool_temp_recorder = AsyncRedisSubscriber(
        redis_pool_,
        MysqlTemperatureMessageHandler(mysq_pool,
                                       'pool_temperature',
                                       average_period_minutes=10),
        RFXCOM_KEY).start()
class GetCurrentCostData(TestCase):
    @asyncio.coroutine
    def setUp(self):
        self.pool = yield from aiomysql.create_pool(host='127.0.0.1', port=3306,
                                                    user='******', password='******', db='test',
                                                    loop=self.loop)

        self.message_handler = MysqlCurrentCostMessageHandler(self.pool)
        self.current_cost_service = CurrentCostDatabaseReader(self.pool, time(8, 0), time(22, 0))
        current_cost_mysql_service.now = lambda: datetime(2015, 6, 1, 12, 0, 0, tzinfo=get_localzone())
        with (yield from self.pool) as conn:
            cur = yield from conn.cursor()
            yield from cur.execute("truncate current_cost")

    @asyncio.coroutine
    def tearDown(self):
        self.pool.close()
        yield from self.pool.wait_closed()

    @asyncio.coroutine
    def test_get_history_three_days(self):
        yield from self.message_handler.save({'date': datetime(2015, 5, 28, 12, 0, 0), 'watt': 1000, 'minutes': 60, 'nb_data': 120, 'temperature': 20.2})
        yield from self.message_handler.save({'date': datetime(2015, 5, 29, 10, 10, 0), 'watt': 1000, 'minutes': 120, 'nb_data': 120, 'temperature': 20.2})
        yield from self.message_handler.save({'date': datetime(2015, 5, 29, 12, 10, 0), 'watt': 1000, 'minutes': 60, 'nb_data': 120, 'temperature': 20.2})
        yield from self.message_handler.save({'date': datetime(2015, 5, 30, 12, 20, 0), 'watt': 1000, 'minutes': 180, 'nb_data': 120, 'temperature': 20.2})

        data = yield from self.current_cost_service.get_history()

        self.assertEqual(3, len(data))
        self.assertEqual((datetime(2015, 5, 28, 0, 0), Decimal(1)), data[0])
        self.assertEqual((datetime(2015, 5, 29, 0, 0), Decimal(3)), data[1])
        self.assertEqual((datetime(2015, 5, 30, 0, 0), Decimal(3)), data[2])

    @asyncio.coroutine
    def test_get_costs_since_and_only_empty_hours(self):
        yield from self.message_handler.save({'date': datetime(2015, 5, 28, 12, 0, 0), 'watt': 1000, 'minutes': 60, 'nb_data': 120, 'temperature': 20.2})
        yield from self.message_handler.save({'date': datetime(2015, 5, 30, 7, 0, 0), 'watt': 1000, 'minutes': 60, 'nb_data': 120, 'temperature': 20.2})

        data = yield from self.current_cost_service.get_costs(since=datetime(2015, 5, 30, tzinfo=get_localzone()))

        self.assertTupleEqual((datetime(2015, 5, 30), (Decimal(0.0), Decimal(1.0))), data[0])

    @asyncio.coroutine
    def test_get_costs_without_empty_and_full_hours(self):
        current_cost_service_without_discount_hours = CurrentCostDatabaseReader(self.pool)
        yield from self.message_handler.save({'date': datetime(2015, 5, 28, 12, 0, 0), 'watt': 1000, 'minutes': 60, 'nb_data': 120, 'temperature': 20.2})
        yield from self.message_handler.save({'date': datetime(2015, 5, 28, 7, 0, 0), 'watt': 1000, 'minutes': 60, 'nb_data': 120, 'temperature': 20.2})

        data = yield from current_cost_service_without_discount_hours.get_costs(since=datetime(2015, 5, 28, tzinfo=get_localzone()))

        self.assertEqual(1, len(data))
        self.assertEqual((datetime(2015, 5, 28), (Decimal(2.0), Decimal(0.0))), data[0])

    @asyncio.coroutine
    def test_get_costs_full_and_empty_hours(self):
        yield from self.message_handler.save({'date': datetime(2015, 5, 28, 12, 0, 0), 'watt': 1000, 'minutes': 60, 'nb_data': 120, 'temperature': 20.2})
        yield from self.message_handler.save({'date': datetime(2015, 5, 28, 7, 0, 0), 'watt': 1000, 'minutes': 60, 'nb_data': 120, 'temperature': 20.2})
        yield from self.message_handler.save({'date': datetime(2015, 5, 28, 23, 0, 0), 'watt': 1000, 'minutes': 60, 'nb_data': 120, 'temperature': 20.2})

        data = yield from self.current_cost_service.get_costs(since=datetime(2015, 5, 28, 0, 0, tzinfo=get_localzone()))

        self.assertEqual(1, len(data))
        self.assertEqual((datetime(2015, 5, 28), (Decimal(1.0), Decimal(2.0))), data[0])

    @asyncio.coroutine
    def test_get_costs_since_16_days_is_grouped_by_week(self):
        current_cost_mysql_service.now = lambda: datetime(2015, 6, 17, 12, 0, 0)
        for i in range(1, 17):
            yield from self.message_handler.save({'date': datetime(2015, 6, i, 12, 0, 0), 'watt': 1000, 'minutes': 60, 'nb_data': 120, 'temperature': 20.2})

        data = yield from self.current_cost_service.get_costs(since=datetime(2015, 6, 1, 0, 0))

        self.assertEqual(3, len(data))
        self.assertEqual((datetime(2015, 6, 1), (Decimal(6.0), Decimal(0.0))), data[0])
        self.assertEqual((datetime(2015, 6, 7), (Decimal(7.0), Decimal(0.0))), data[1])
        self.assertEqual((datetime(2015, 6, 14), (Decimal(3.0), Decimal(0.0))), data[2])

    @asyncio.coroutine
    def test_get_costs_since_11_weeks_is_grouped_by_month(self):
        current_cost_mysql_service.now = lambda: datetime(2015, 8, 17, 12, 0, 0)
        for i in range(0, 65):
            yield from self.message_handler.save({'date': datetime(2015, 6, 1, 12, 0, 0) + timedelta(days=i), 'watt': 1000, 'minutes': 60, 'nb_data': 120, 'temperature': 20.2})

        data = yield from self.current_cost_service.get_costs(since=datetime(2015, 6, 1, 0, 0))

        self.assertEqual(3, len(data))
        self.assertEqual((datetime(2015, 6, 1), (Decimal(30.0), Decimal(0.0))), data[0])
        self.assertEqual((datetime(2015, 7, 1), (Decimal(31.0), Decimal(0.0))), data[1])
        self.assertEqual((datetime(2015, 8, 1), (Decimal(4.0), Decimal(0.0))), data[2])

    @asyncio.coroutine
    def test_get_costs_since_period__do_not_gather_months_from_different_years(self):
        current_cost_mysql_service.now = lambda: datetime(2015, 8, 17, 12, 0, 0)
        for i in range(0, 427):
            yield from self.message_handler.save({'date': datetime(2014, 7, 1, 12, 0, 0) + timedelta(days=i), 'watt': 1000, 'minutes': 60, 'nb_data': 120, 'temperature': 20.2})

        data = yield from self.current_cost_service.get_costs(since=datetime(2014, 7, 1, 0, 0))

        self.assertEqual(14, len(data))
        self.assertEqual((datetime(2014, 7, 1), (Decimal(31.0), Decimal(0.0))), data[0])
        self.assertEqual((datetime(2014, 8, 1), (Decimal(31.0), Decimal(0.0))), data[1])
        self.assertEqual((datetime(2015, 7, 1), (Decimal(31.0), Decimal(0.0))), data[12])
        self.assertEqual((datetime(2015, 8, 1), (Decimal(31.0), Decimal(0.0))), data[13])
Example #12
0
class MysqlAverageMessageHandlerTest(asynctest.TestCase):
    @asyncio.coroutine
    def setUp(self):
        self.pool = yield from aiomysql.create_pool(
            host='127.0.0.1',
            port=3306,
            user='******',
            password='******',
            db='test',
            loop=asyncio.get_event_loop())

        with (yield from self.pool) as conn:
            cur = yield from conn.cursor()
            yield from cur.execute("drop table if EXISTS current_cost")
            yield from cur.close()

        redis_toolbox.now = lambda: datetime(
            2012, 12, 13, 14, 2, 0, tzinfo=timezone.utc)
        self.message_handler = MysqlCurrentCostMessageHandler(self.pool)

    @asyncio.coroutine
    def tearDown(self):
        self.pool.close()
        yield from self.pool.wait_closed()

    @asyncio.coroutine
    def test_create_table_if_it_doesnot_exist(self):
        with (yield from self.pool) as conn:
            cur = yield from conn.cursor()

            yield from cur.execute("show tables like 'current_cost'")
            current_cost_table = yield from cur.fetchall()
            self.assertEqual((('current_cost', ), ), current_cost_table)

            MysqlCurrentCostMessageHandler(self.pool,
                                           average_period_minutes=10)

            yield from cur.execute("show tables like 'current_cost'")
            current_cost_table = yield from cur.fetchall()
            self.assertEquals((('current_cost', ), ), current_cost_table)
            yield from cur.close()

    @asyncio.coroutine
    def test_save_event_mysql(self):
        with (yield from self.pool) as conn:
            now = datetime(2012, 12, 13, 14, 0, 7, tzinfo=timezone.utc)

            yield from self.message_handler.handle({
                'date': now,
                'watt': 305.0,
                'temperature': 21.4
            })

            table_rows = yield from self.nb_table_rows('current_cost')
            self.assertEquals(1, table_rows)

            cursor = yield from conn.cursor()
            yield from cursor.execute(
                "select timestamp, watt, minutes, nb_data, temperature from current_cost"
            )
            rows = yield from cursor.fetchall()
            self.assertEqual(
                (datetime(2012, 12, 13, 14, 0, 7), 305, 0, 1, 21.4), rows[0])
            yield from cursor.close()

    def nb_table_rows(self, table):
        with (yield from self.pool) as conn:
            cursor = yield from conn.cursor()
            yield from cursor.execute("select * from %s" % table)
            allrows = yield from cursor.fetchall()
            return len(allrows)
Example #13
0
class RedisGetDataOfDay(TestCase):
    @asyncio.coroutine
    def setUp(self):
        self.pool = yield from aiomysql.create_pool(
            host='127.0.0.1',
            port=3306,
            user='******',
            password='******',
            db='test',
            loop=asyncio.get_event_loop())

        self.server = yield from domopyc_server.init(asyncio.get_event_loop(),
                                                     self.pool,
                                                     port=12345)
        self.message_handler = MysqlCurrentCostMessageHandler(self.pool)
        with (yield from self.pool) as conn:
            cur = yield from conn.cursor()
            yield from cur.execute("truncate current_cost")
            yield from cur.close()

    @asyncio.coroutine
    def tearDown(self):
        self.server.close()
        self.pool.close()
        yield from self.pool.wait_closed()

    @asyncio.coroutine
    def test_get_current_cost_history_one_line(self):
        yield from self.message_handler.save({
            'date':
            datetime(2015, 5, 28, 12, 0, 0, tzinfo=timezone.utc),
            'watt':
            123,
            'minutes':
            60,
            'nb_data':
            120,
            'temperature':
            20.2
        })

        response = yield from aiohttp.request(
            'GET', 'http://127.0.0.1:12345/power/history')
        json_response = yield from response.json()

        self.assertEqual(1, len(json_response['data']))
        self.assertEqual(['2015-05-28T00:00:00', 0.123],
                         json_response['data'][0])

    @asyncio.coroutine
    def test_get_current_cost_by_day_with_previous_day(self):
        yield from self.message_handler.save({
            'date':
            datetime(2015, 5, 29, 12, 10, 0),
            'watt':
            1000,
            'minutes':
            60,
            'nb_data':
            120,
            'temperature':
            20.6
        })
        yield from self.message_handler.save({
            'date':
            datetime(2015, 5, 30, 23, 20, 0),
            'watt':
            500,
            'minutes':
            180,
            'nb_data':
            120,
            'temperature':
            20.2
        })

        response = yield from aiohttp.request(
            'GET', 'http://127.0.0.1:12345/power/day/%s' %
            datetime(2015, 5, 30, 0, 0).isoformat())
        json_response = yield from response.json()

        self.assertEqual(1, len(json_response['day_data']))
        self.assertEqual(1, len(json_response['previous_day_data']))
        self.assertEqual(['2015-05-30T23:20:00', 500, 20.2],
                         json_response['day_data'][0])
        self.assertEqual(['2015-05-29T12:10:00', 1000, 20.6],
                         json_response['previous_day_data'][0])
Example #14
0
class GetCurrentCostData(TestCase):
    @asyncio.coroutine
    def setUp(self):
        self.pool = yield from aiomysql.create_pool(host='127.0.0.1',
                                                    port=3306,
                                                    user='******',
                                                    password='******',
                                                    db='test',
                                                    loop=self.loop)

        self.message_handler = MysqlCurrentCostMessageHandler(self.pool)
        self.current_cost_service = CurrentCostDatabaseReader(
            self.pool, time(8, 0), time(22, 0))
        current_cost_mysql_service.now = lambda: datetime(
            2015, 6, 1, 12, 0, 0, tzinfo=get_localzone())
        with (yield from self.pool) as conn:
            cur = yield from conn.cursor()
            yield from cur.execute("truncate current_cost")

    @asyncio.coroutine
    def tearDown(self):
        self.pool.close()
        yield from self.pool.wait_closed()

    @asyncio.coroutine
    def test_get_history_three_days(self):
        yield from self.message_handler.save({
            'date':
            datetime(2015, 5, 28, 12, 0, 0),
            'watt':
            1000,
            'minutes':
            60,
            'nb_data':
            120,
            'temperature':
            20.2
        })
        yield from self.message_handler.save({
            'date':
            datetime(2015, 5, 29, 10, 10, 0),
            'watt':
            1000,
            'minutes':
            120,
            'nb_data':
            120,
            'temperature':
            20.2
        })
        yield from self.message_handler.save({
            'date':
            datetime(2015, 5, 29, 12, 10, 0),
            'watt':
            1000,
            'minutes':
            60,
            'nb_data':
            120,
            'temperature':
            20.2
        })
        yield from self.message_handler.save({
            'date':
            datetime(2015, 5, 30, 12, 20, 0),
            'watt':
            1000,
            'minutes':
            180,
            'nb_data':
            120,
            'temperature':
            20.2
        })

        data = yield from self.current_cost_service.get_history()

        self.assertEqual(3, len(data))
        self.assertEqual((datetime(2015, 5, 28, 0, 0), Decimal(1)), data[0])
        self.assertEqual((datetime(2015, 5, 29, 0, 0), Decimal(3)), data[1])
        self.assertEqual((datetime(2015, 5, 30, 0, 0), Decimal(3)), data[2])

    @asyncio.coroutine
    def test_get_costs_since_and_only_empty_hours(self):
        yield from self.message_handler.save({
            'date':
            datetime(2015, 5, 28, 12, 0, 0),
            'watt':
            1000,
            'minutes':
            60,
            'nb_data':
            120,
            'temperature':
            20.2
        })
        yield from self.message_handler.save({
            'date':
            datetime(2015, 5, 30, 7, 0, 0),
            'watt':
            1000,
            'minutes':
            60,
            'nb_data':
            120,
            'temperature':
            20.2
        })

        data = yield from self.current_cost_service.get_costs(
            since=datetime(2015, 5, 30, tzinfo=get_localzone()))

        self.assertTupleEqual(
            (datetime(2015, 5, 30), (Decimal(0.0), Decimal(1.0))), data[0])

    @asyncio.coroutine
    def test_get_costs_without_empty_and_full_hours(self):
        current_cost_service_without_discount_hours = CurrentCostDatabaseReader(
            self.pool)
        yield from self.message_handler.save({
            'date':
            datetime(2015, 5, 28, 12, 0, 0),
            'watt':
            1000,
            'minutes':
            60,
            'nb_data':
            120,
            'temperature':
            20.2
        })
        yield from self.message_handler.save({
            'date':
            datetime(2015, 5, 28, 7, 0, 0),
            'watt':
            1000,
            'minutes':
            60,
            'nb_data':
            120,
            'temperature':
            20.2
        })

        data = yield from current_cost_service_without_discount_hours.get_costs(
            since=datetime(2015, 5, 28, tzinfo=get_localzone()))

        self.assertEqual(1, len(data))
        self.assertEqual((datetime(2015, 5, 28), (Decimal(2.0), Decimal(0.0))),
                         data[0])

    @asyncio.coroutine
    def test_get_costs_full_and_empty_hours(self):
        yield from self.message_handler.save({
            'date':
            datetime(2015, 5, 28, 12, 0, 0),
            'watt':
            1000,
            'minutes':
            60,
            'nb_data':
            120,
            'temperature':
            20.2
        })
        yield from self.message_handler.save({
            'date':
            datetime(2015, 5, 28, 7, 0, 0),
            'watt':
            1000,
            'minutes':
            60,
            'nb_data':
            120,
            'temperature':
            20.2
        })
        yield from self.message_handler.save({
            'date':
            datetime(2015, 5, 28, 23, 0, 0),
            'watt':
            1000,
            'minutes':
            60,
            'nb_data':
            120,
            'temperature':
            20.2
        })

        data = yield from self.current_cost_service.get_costs(
            since=datetime(2015, 5, 28, 0, 0, tzinfo=get_localzone()))

        self.assertEqual(1, len(data))
        self.assertEqual((datetime(2015, 5, 28), (Decimal(1.0), Decimal(2.0))),
                         data[0])

    @asyncio.coroutine
    def test_get_costs_since_16_days_is_grouped_by_week(self):
        current_cost_mysql_service.now = lambda: datetime(
            2015, 6, 17, 12, 0, 0)
        for i in range(1, 17):
            yield from self.message_handler.save({
                'date':
                datetime(2015, 6, i, 12, 0, 0),
                'watt':
                1000,
                'minutes':
                60,
                'nb_data':
                120,
                'temperature':
                20.2
            })

        data = yield from self.current_cost_service.get_costs(
            since=datetime(2015, 6, 1, 0, 0))

        self.assertEqual(3, len(data))
        self.assertEqual((datetime(2015, 6, 1), (Decimal(6.0), Decimal(0.0))),
                         data[0])
        self.assertEqual((datetime(2015, 6, 7), (Decimal(7.0), Decimal(0.0))),
                         data[1])
        self.assertEqual((datetime(2015, 6, 14), (Decimal(3.0), Decimal(0.0))),
                         data[2])

    @asyncio.coroutine
    def test_get_costs_since_11_weeks_is_grouped_by_month(self):
        current_cost_mysql_service.now = lambda: datetime(
            2015, 8, 17, 12, 0, 0)
        for i in range(0, 65):
            yield from self.message_handler.save({
                'date':
                datetime(2015, 6, 1, 12, 0, 0) + timedelta(days=i),
                'watt':
                1000,
                'minutes':
                60,
                'nb_data':
                120,
                'temperature':
                20.2
            })

        data = yield from self.current_cost_service.get_costs(
            since=datetime(2015, 6, 1, 0, 0))

        self.assertEqual(3, len(data))
        self.assertEqual((datetime(2015, 6, 1), (Decimal(30.0), Decimal(0.0))),
                         data[0])
        self.assertEqual((datetime(2015, 7, 1), (Decimal(31.0), Decimal(0.0))),
                         data[1])
        self.assertEqual((datetime(2015, 8, 1), (Decimal(4.0), Decimal(0.0))),
                         data[2])

    @asyncio.coroutine
    def test_get_costs_since_period__do_not_gather_months_from_different_years(
            self):
        current_cost_mysql_service.now = lambda: datetime(
            2015, 8, 17, 12, 0, 0)
        for i in range(0, 427):
            yield from self.message_handler.save({
                'date':
                datetime(2014, 7, 1, 12, 0, 0) + timedelta(days=i),
                'watt':
                1000,
                'minutes':
                60,
                'nb_data':
                120,
                'temperature':
                20.2
            })

        data = yield from self.current_cost_service.get_costs(
            since=datetime(2014, 7, 1, 0, 0))

        self.assertEqual(14, len(data))
        self.assertEqual((datetime(2014, 7, 1), (Decimal(31.0), Decimal(0.0))),
                         data[0])
        self.assertEqual((datetime(2014, 8, 1), (Decimal(31.0), Decimal(0.0))),
                         data[1])
        self.assertEqual((datetime(2015, 7, 1), (Decimal(31.0), Decimal(0.0))),
                         data[12])
        self.assertEqual((datetime(2015, 8, 1), (Decimal(31.0), Decimal(0.0))),
                         data[13])