# Path: test/mock_device/mock_data.py
# def generate():


import asyncio
import datetime
    import ujson as json
    import json
import asynctest
import redis
import pandas as pd
import numpy as np
import pydatacoll.utils.logger as my_logger
import pydatacoll.plugins.formula_calc as formula_calc
from test.mock_device import mock_data
from pydatacoll.utils.read_config import *

    def tearDown(self):
        self.loop.run_until_complete(self.formula_calc.uninstall())

    async def test_formula(self):
        time_str1 = datetime.datetime.now().isoformat()
        pub_data = {'device_id': 1, 'term_id': 10, 'item_id': 1000, 'time': time_str1, 'value': 123.4}
        self.redis_client.rpush('LST:DATA_TIME:1:10:1000', time_str1)
        self.redis_client.hset('HS:DATA:1:10:1000', time_str1, 123.4)
        self.redis_client.publish('CHANNEL:DEVICE_DATA:1:10:1000', json.dumps(pub_data))
        await asyncio.sleep(1)
        time_str2 = datetime.datetime.now().isoformat()
        self.redis_client.rpush('LST:DATA_TIME:1:10:1000', time_str2)
        self.redis_client.hset('HS:DATA:1:10:1000', time_str2, 123.4)
        self.redis_client.publish('CHANNEL:DEVICE_DATA:1:10:1000', json.dumps(pub_data))
        time_str3 = datetime.datetime.now().isoformat()
        pub_data = {'device_id': 2, 'term_id': 30, 'item_id': 1000, 'time': time_str3, 'value': 999}
        self.redis_client.rpush('LST:DATA_TIME:2:30:1000', time_str3)
        self.redis_client.hset('HS:DATA:2:30:1000', time_str3, 999)
        self.redis_client.publish('CHANNEL:DEVICE_DATA:2:30:1000', json.dumps(pub_data))
        await asyncio.sleep(1)
        rst = self.redis_client.hgetall('HS:DATA:3:40:1000')
        self.assertEqual(len(rst), 3)
        lst = sorted(rst.keys())
        self.assertEqual(rst[lst[0]], '102.0')
        self.assertEqual(rst[lst[1]], '123.4')
        self.assertEqual(rst[lst[2]], '999.0')

        # 统计06年1月份的累计值
        formula_dict = {

# Path: test/mock_device/mock_data.py
# def generate():


# Path: test/test_formula_calc.py
import asyncio
import datetime
    import ujson as json
    import json
import asynctest
import redis
import pandas as pd
import numpy as np
import pydatacoll.utils.logger as my_logger
import pydatacoll.plugins.formula_calc as formula_calc
from test.mock_device import mock_data
from pydatacoll.utils.read_config import *


    def tearDown(self):
        self.loop.run_until_complete(self.formula_calc.uninstall())

    async def test_formula(self):
        time_str1 = datetime.datetime.now().isoformat()
        pub_data = {'device_id': 1, 'term_id': 10, 'item_id': 1000, 'time': time_str1, 'value': 123.4}
        self.redis_client.rpush('LST:DATA_TIME:1:10:1000', time_str1)
        self.redis_client.hset('HS:DATA:1:10:1000', time_str1, 123.4)
        self.redis_client.publish('CHANNEL:DEVICE_DATA:1:10:1000', json.dumps(pub_data))
        await asyncio.sleep(1)
        time_str2 = datetime.datetime.now().isoformat()
        self.redis_client.rpush('LST:DATA_TIME:1:10:1000', time_str2)
        self.redis_client.hset('HS:DATA:1:10:1000', time_str2, 123.4)
        self.redis_client.publish('CHANNEL:DEVICE_DATA:1:10:1000', json.dumps(pub_data))
        time_str3 = datetime.datetime.now().isoformat()
        pub_data = {'device_id': 2, 'term_id': 30, 'item_id': 1000, 'time': time_str3, 'value': 999}
        self.redis_client.rpush('LST:DATA_TIME:2:30:1000', time_str3)
        self.redis_client.hset('HS:DATA:2:30:1000', time_str3, 999)
        self.redis_client.publish('CHANNEL:DEVICE_DATA:2:30:1000', json.dumps(pub_data))
        await asyncio.sleep(1)
        rst = self.redis_client.hgetall('HS:DATA:3:40:1000')
        self.assertEqual(len(rst), 3)
        lst = sorted(rst.keys())
        self.assertEqual(rst[lst[0]], '102.0')
        self.assertEqual(rst[lst[1]], '123.4')
        self.assertEqual(rst[lst[2]], '999.0')

        # 统计06年1月份的累计值
        formula_dict = {

            'id': '2', 'device_id': '3', 'term_id': '40', 'item_id': '1000',