# Path: test/mock_device/mock_data.py
# def generate():
# 
# Path: test/mock_device/iec104device.py
# class IEC104Device:
#     def __init__(self, device, reader, writer):
#     async def receive(self):
#     def connection_lost(self):
#     def inc_ssn(self):
#     def inc_rsn(self):
#     def start_timer(self, timer_id):
#     def stop_timer(self, timer_id):
#     def on_timer0(self):
#     def on_timer1(self):
#     def on_timer2(self):
#     def on_timer3(self):
#     async def handle_u(self, frame):
#     async def handle_i(self, frame):
#     async def send_frame(self, frame, check=True):
#     async def check_to_send(self, frame):
#     def save_frame(self, frame, send=True):
#     async def generate_call_all_data(self):
#     def generate_call_power_data(self):
# def create_servers(io_loop):
# def run_server():
# 
# Path: pydatacoll/api_server.py
# HANDLER_TIME_OUT = config.getint('SERVER', 'web_timeout', fallback=10)
# class APIServer(ParamFunctionContainer):
#     def __init__(self, port: int = None, production: bool = None, io_loop: asyncio.AbstractEventLoop = None):
#     def _add_router(self):
#     def _install_plugins(self):
#     def _uninstall_plugins(self):
#     def stop_server(self):
#     def found_and_delete(self, match: str):
#     async def _read_data(request):
#     async def get_index(self, request):
#     async def get_redis_key(self, _):
#     async def get_device_protocol_list(self, _):
#     async def get_term_protocol_list(self, _):
#     async def get_formula_list(self, _):
#     async def get_formula(self, request):
#     async def get_device_list(self, _):
#     async def get_device(self, request):
#     async def get_term_list(self, _):
#     async def get_term(self, request):
#     async def get_item_list(self, _):
#     async def get_item(self, request):
#     async def get_device_term_list(self, request):
#     async def get_term_item_list(self, request):
#     async def get_term_item(self, request):
#     async def get_data_list(self, request):
#     async def get_data(self, request):
#     async def create_formula(self, request):
#     async def create_formula_batch(self, request):
#     async def update_formula(self, request):
#     async def del_formula(self, request):
#     async def del_formula_batch(self, request):
#     async def create_device(self, request):
#     async def create_device_batch(self, request):
#     async def update_device(self, request):
#     async def del_device(self, request):
#     async def del_device_batch(self, request):
#     async def create_term(self, request):
#     async def create_term_batch(self, request):
#     async def update_term(self, request):
#     async def del_term(self, request):
#     async def del_term_batch(self, request):
#     async def create_item(self, request):
#     async def create_item_batch(self, request):
#     async def update_item(self, request):
#     async def del_item(self, request):
#     async def del_item_batch(self, request):
#     async def create_term_item(self, request):
#     async def create_term_item_batch(self, request):
#     async def update_term_item(self, request):
#     async def del_term_item(self, request):
#     async def del_term_item_batch(self, request):
#     async def device_call(self, request):
#             async def reader(ch):
#     async def device_ctrl(self, request):
#             async def reader(ch):
#     async def formula_check(self, request):
#             async def reader(ch):
#     async def sql_check(self, request):
#             async def reader(ch):
# def main():


import asyncio
import aiohttp
import aioredis
import asynctest
import redis
    import ujson as json
    import json
import pydatacoll.utils.logger as my_logger
from pydatacoll.resources.protocol import *
from pydatacoll.resources.redis_key import *
from test.mock_device import mock_data, iec104device
from pydatacoll import api_server
from pydatacoll.utils.read_config import *
        formula_check['formula'] = 'p1[-1]+p2[-2]'
        async with aiohttp.post('http://127.0.0.1:8080/api/v1/formula_check', data=json.dumps(formula_check)) as r:
            self.assertEqual(r.status, 200)
            rst = await r.text()
            self.assertEqual(rst, """NameError
   p1[-1]+p2[-2]
           ^^^
name 'p2' is not defined
""")

        formula_check['p1'] = '123:45:6'
        async with aiohttp.post('http://127.0.0.1:8080/api/v1/formula_check', data=json.dumps(formula_check)) as r:
            self.assertEqual(r.status, 200)
            rst = await r.text()
            self.assertEqual(rst, "parameter not found: p1=123:45:6")

        formulas = [1]
        async with aiohttp.post('http://127.0.0.1:8080/api/v2/formulas/del', data=json.dumps(formulas)) as r:
            self.assertEqual(r.status, 200)
            rst = self.redis_client.smembers('SET:FORMULA')
            self.assertEqual(len(rst), 0)

    async def test_device_CRUD(self):
        async with aiohttp.get('http://127.0.0.1:8080/api/v1/devices') as r:
            self.assertEqual(r.status, 200)
            rst = await r.json()
            self.assertSetEqual(set(rst), {'1', '2', '3'})
        async with aiohttp.get('http://127.0.0.1:8080/api/v1/devices/1') as r:
            self.assertEqual(r.status, 200)
            rst = await r.json()

# Path: test/mock_device/mock_data.py
# def generate():
# 
# Path: test/mock_device/iec104device.py
# class IEC104Device:
#     def __init__(self, device, reader, writer):
#     async def receive(self):
#     def connection_lost(self):
#     def inc_ssn(self):
#     def inc_rsn(self):
#     def start_timer(self, timer_id):
#     def stop_timer(self, timer_id):
#     def on_timer0(self):
#     def on_timer1(self):
#     def on_timer2(self):
#     def on_timer3(self):
#     async def handle_u(self, frame):
#     async def handle_i(self, frame):
#     async def send_frame(self, frame, check=True):
#     async def check_to_send(self, frame):
#     def save_frame(self, frame, send=True):
#     async def generate_call_all_data(self):
#     def generate_call_power_data(self):
# def create_servers(io_loop):
# def run_server():
# 
# Path: pydatacoll/api_server.py
# HANDLER_TIME_OUT = config.getint('SERVER', 'web_timeout', fallback=10)
# class APIServer(ParamFunctionContainer):
#     def __init__(self, port: int = None, production: bool = None, io_loop: asyncio.AbstractEventLoop = None):
#     def _add_router(self):
#     def _install_plugins(self):
#     def _uninstall_plugins(self):
#     def stop_server(self):
#     def found_and_delete(self, match: str):
#     async def _read_data(request):
#     async def get_index(self, request):
#     async def get_redis_key(self, _):
#     async def get_device_protocol_list(self, _):
#     async def get_term_protocol_list(self, _):
#     async def get_formula_list(self, _):
#     async def get_formula(self, request):
#     async def get_device_list(self, _):
#     async def get_device(self, request):
#     async def get_term_list(self, _):
#     async def get_term(self, request):
#     async def get_item_list(self, _):
#     async def get_item(self, request):
#     async def get_device_term_list(self, request):
#     async def get_term_item_list(self, request):
#     async def get_term_item(self, request):
#     async def get_data_list(self, request):
#     async def get_data(self, request):
#     async def create_formula(self, request):
#     async def create_formula_batch(self, request):
#     async def update_formula(self, request):
#     async def del_formula(self, request):
#     async def del_formula_batch(self, request):
#     async def create_device(self, request):
#     async def create_device_batch(self, request):
#     async def update_device(self, request):
#     async def del_device(self, request):
#     async def del_device_batch(self, request):
#     async def create_term(self, request):
#     async def create_term_batch(self, request):
#     async def update_term(self, request):
#     async def del_term(self, request):
#     async def del_term_batch(self, request):
#     async def create_item(self, request):
#     async def create_item_batch(self, request):
#     async def update_item(self, request):
#     async def del_item(self, request):
#     async def del_item_batch(self, request):
#     async def create_term_item(self, request):
#     async def create_term_item_batch(self, request):
#     async def update_term_item(self, request):
#     async def del_term_item(self, request):
#     async def del_term_item_batch(self, request):
#     async def device_call(self, request):
#             async def reader(ch):
#     async def device_ctrl(self, request):
#             async def reader(ch):
#     async def formula_check(self, request):
#             async def reader(ch):
#     async def sql_check(self, request):
#             async def reader(ch):
# def main():


# Path: test/test_interface.py
import asyncio
import aiohttp
import aioredis
import asynctest
import redis
    import ujson as json
    import json
import pydatacoll.utils.logger as my_logger
from pydatacoll.resources.protocol import *
from pydatacoll.resources.redis_key import *
from test.mock_device import mock_data, iec104device
from pydatacoll import api_server
from pydatacoll.utils.read_config import *

        formula_check['formula'] = 'p1[-1]+p2[-2]'
        async with aiohttp.post('http://127.0.0.1:8080/api/v1/formula_check', data=json.dumps(formula_check)) as r:
            self.assertEqual(r.status, 200)
            rst = await r.text()
            self.assertEqual(rst, """NameError
   p1[-1]+p2[-2]
           ^^^
name 'p2' is not defined
""")

        formula_check['p1'] = '123:45:6'
        async with aiohttp.post('http://127.0.0.1:8080/api/v1/formula_check', data=json.dumps(formula_check)) as r:
            self.assertEqual(r.status, 200)
            rst = await r.text()
            self.assertEqual(rst, "parameter not found: p1=123:45:6")

        formulas = [1]
        async with aiohttp.post('http://127.0.0.1:8080/api/v2/formulas/del', data=json.dumps(formulas)) as r:
            self.assertEqual(r.status, 200)
            rst = self.redis_client.smembers('SET:FORMULA')
            self.assertEqual(len(rst), 0)

    async def test_device_CRUD(self):
        async with aiohttp.get('http://127.0.0.1:8080/api/v1/devices') as r:
            self.assertEqual(r.status, 200)
            rst = await r.json()
            self.assertSetEqual(set(rst), {'1', '2', '3'})
        async with aiohttp.get('http://127.0.0.1:8080/api/v1/devices/1') as r:
            self.assertEqual(r.status, 200)
            rst = await r.json()

            self.assertDictEqual(rst, mock_data.device1)