Пример #1
0
    def test_modify_variables(self):
        prepare_variables(self.variable_dao)

        # use test variable in 'new_app' to check
        new_app_variable = SAMPLE_VARIABLE.copy()
        new_app_variable.app = 'new_app'
        new_app_variable.name = 'var1'

        # first check
        url = "/default/variable_models?app=new_app"
        response = self.fetch(url)
        res = json.loads(response.body)
        self.assertEqual(len(res['values']), 1)
        get_variable = VariableModel.from_dict(res['values'][0])
        self.assertEqual(new_app_variable, get_variable)

        # 1. modify variable and test
        # 1.1 modify test variable, so it is doesn't equal now
        new_app_variable.remark = 'new_remark'
        self.assertNotEquals(new_app_variable, get_variable)

        # 1.2 post the modified variable
        url = "/default/variable_models"
        response = self.fetch(url, method='POST', body='[%s]' % new_app_variable.get_json())
        res = json.loads(response.body)
        self.assertEqual(res['status'], 0)
        self.assertEqual(res['msg'], 'ok')

        # 1.3 verify again
        url = "/default/variable_models?app=new_app"
        response = self.fetch(url)
        res = json.loads(response.body)
        self.assertEqual(len(res['values']), 1)
        get_variable = VariableModel.from_dict(res['values'][0])
        # now equal again
        self.assertEqual(new_app_variable, get_variable)

        # 2. add an variable with post
        # 2.1 now there are 4 variables
        url = "/default/variable_models"
        response = self.fetch(url)
        res = json.loads(response.body)
        self.assertEqual(len(res['values']), 4)

        # 2.2 add one
        added_variable = SAMPLE_VARIABLE.copy()
        added_variable.name = 'added_variable'
        response = self.fetch(url, method='POST', body='[%s]' % added_variable.get_json())
        res = json.loads(response.body)
        self.assertEqual(res['status'], 0)
        self.assertEqual(res['msg'], 'ok')

        # 2.3 now there are 5
        response = self.fetch(url)
        res = json.loads(response.body)
        self.assertEqual(len(res['values']), 5)
Пример #2
0
    def tests_modify_variable(self):
        prepare_variables(self.cust_dao)
        url = "/platform/variable_models/variable/{}/{}".format(SAMPLE_VARIABLE.app, SAMPLE_VARIABLE.name)
        response = self.fetch(url)
        res = json.loads(response.body)
        self.assertEqual(res['status'], 0)
        self.assertEqual(res['msg'], 'ok')
        self.assertEqual(len(res['values']), 1)
        get_variable = VariableModel.from_dict(res['values'][0])
        self.assertEqual(get_variable, SAMPLE_VARIABLE)

        # 1. modify SAMPLE_VARIABLE
        # 1.1 first not equal
        new_sample = SAMPLE_VARIABLE.copy()
        new_sample.remark = 'modified'
        self.assertNotEquals(get_variable, new_sample)

        # 1.2 second modify
        response = self.fetch(url, method='POST', body=new_sample.get_json())
        res = json.loads(response.body)
        self.assertEqual(res['status'], 0)
        self.assertEqual(res['msg'], 'ok')

        # 1.3 third  check
        response = self.fetch(url)
        res = json.loads(response.body)
        self.assertEqual(res['status'], 0)
        self.assertEqual(res['msg'], 'ok')
        self.assertEqual(len(res['values']), 1)
        get_variable = VariableModel.from_dict(res['values'][0])
        self.assertEqual(get_variable, new_sample)

        # 2. add a new one
        # 2.1 get 404
        url = "/platform/variable_models/variable/{}/{}".format(SAMPLE_VARIABLE.app, 'variable_to_be_add')
        response = self.fetch(url)
        res = json.loads(response.body)
        self.assertEqual(res['status'], 404)

        # 2.2 add one
        variable_to_be_add = SAMPLE_VARIABLE.copy()
        variable_to_be_add.name = 'variable_to_be_add'
        response = self.fetch(url, method='POST', body=variable_to_be_add.get_json())
        res = json.loads(response.body)
        self.assertEqual(res['status'], 0)
        self.assertEqual(res['msg'], 'ok')

        # 2.3 check
        response = self.fetch(url)
        res = json.loads(response.body)
        self.assertEqual(res['status'], 0)
        self.assertEqual(res['msg'], 'ok')
        self.assertEqual(len(res['values']), 1)
        get_variable = VariableModel.from_dict(res['values'][0])
        self.assertEqual(get_variable, variable_to_be_add)
Пример #3
0
def load_config():
    print("loading config")
    for _ in get_file_json_content('data/event_model_default.json'):
        add_event_to_registry(EventModel.from_dict(_))

    for _ in get_file_json_content('data/common_variable_default.json'):
        add_variable_to_registry(VariableModel.from_dict(_))
    for _ in get_file_json_content('data/realtime_variable_default.json'):
        add_variable_to_registry(VariableModel.from_dict(_))
    for _ in get_file_json_content('data/profile_variable_default.json'):
        add_variable_to_registry(VariableModel.from_dict(_))
Пример #4
0
    def post(self):
        """
        美化一组variable

        @API
        summary: 美化一组variable
        notes: 美化一组variable
        tags:
          - platform
        parameters:
          -
            name: variable
            in: body
            required: true
            type: json
            description: 一组variable的json表示
        produces:
          - application/json
        """

        self.set_header('content-type', 'application/json')
        body = self.request.body
        try:
            variables = list()
            for _ in json.loads(body):
                variable = VariableModel.from_dict(_)
                variables.append(variable)
            variables = [_.get_simplified_ordered_dict() for _ in variables]
            self.finish(json_dumps({"status": 0, "msg": "ok", "values": variables}))
        except Exception as err:
            print_exc()
            return self.process_error(400, str(err))
Пример #5
0
    def test_delete_variable(self):
        prepare_variables(self.cust_dao)
        url = "/platform/variable_models/variable/{}/{}".format(SAMPLE_VARIABLE.app, SAMPLE_VARIABLE.name)
        response = self.fetch(url)
        res = json.loads(response.body)
        self.assertEqual(res['status'], 0)
        self.assertEqual(res['msg'], 'ok')
        self.assertEqual(len(res['values']), 1)
        self.assertEqual(VariableModel.from_dict(res['values'][0]), SAMPLE_VARIABLE)

        # delete
        url = "/platform/variable_models/variable/{}/{}".format(SAMPLE_VARIABLE.app, SAMPLE_VARIABLE.name)
        response = self.fetch(url, method='DELETE')
        res = json.loads(response.body)
        self.assertEqual(res['status'], 0)
        self.assertEqual(res['msg'], 'ok')

        # check
        response = self.fetch(url)
        res = json.loads(response.body)
        self.assertEqual(res['status'], 404)

        # can not delete default
        clear_variables(self.cust_dao)
        prepare_variables(self.default_dao)
        url = "/platform/variable_models/variable/{}/{}".format(SAMPLE_VARIABLE.app, SAMPLE_VARIABLE.name)
        response = self.fetch(url, method='DELETE')
        res = json.loads(response.body)
        self.assertEqual(res['status'], 0)
        self.assertEqual(res['msg'], 'ok')
        response = self.fetch(url)
        res = json.loads(response.body)
        self.assertEqual(res['status'], 0)
Пример #6
0
    def test_get_variables(self):
        prepare_variables(self.cust_dao)

        # default 0, cust 3
        url = "/default/variable_models"
        response = self.fetch(url)
        res = json.loads(response.body)
        self.assertEqual(self.default_dao.count(), 0)
        self.assertEqual(res['status'], 0)
        self.assertEqual(res['msg'], 'ok')
        self.assertEqual(len(res['values']), 0)

        url = '/platform/variable_models'
        response = self.fetch(url)
        res = json.loads(response.body)
        self.assertEqual(self.cust_dao.count(), 4)
        self.assertEqual(res['status'], 0)
        self.assertEqual(res['msg'], 'ok')
        self.assertEqual(len(res['values']), 4)
        return_variables = res['values']
        return_variable = [_ for _ in return_variables if _['name'] == SAMPLE_VARIABLE.name][0]
        self.assertEqual(VariableModel.from_dict(return_variable), SAMPLE_VARIABLE)

        # check get by app
        response = self.fetch(url + '?app=new_app')
        res = json.loads(response.body)
        self.assertEqual(len(res['values']), 1)

        # check get by type
        response = self.fetch(url + '?type=new_type')
        res = json.loads(response.body)
        self.assertEqual(len(res['values']), 1)
Пример #7
0
    def test_get_variable(self):
        prepare_variables(self.variable_dao)
        url = "/default/variable_models/variable/{}/{}".format(SAMPLE_VARIABLE.app, SAMPLE_VARIABLE.name)
        response = self.fetch(url)
        res = json.loads(response.body)
        self.assertEqual(res['status'], 0)
        self.assertEqual(res['msg'], 'ok')
        self.assertEqual(len(res['values']), 1)
        self.assertEqual(VariableModel.from_dict(res['values'][0]), SAMPLE_VARIABLE)

        # get not exist
        url = "/default/variable_models/variable/{}/{}".format(SAMPLE_VARIABLE.app, 'not_exist')
        response = self.fetch(url)
        res = json.loads(response.body)
        self.assertEqual(res['status'], 404)
Пример #8
0
    def post(self):
        """
        增加一组变量

        @API
        summary: 增加一组变量
        notes: 增加一组变量,如果变量已存在,则修改
        tags:
          - platform
        parameters:
          -
            name: variables
            in: body
            required: true
            type: json
            description: 变量的json表示,list结构
        produces:
          - application/json   
        """

        self.set_header('content-type', 'application/json')
        body = self.request.body
        try:
            variables = list()
            for _ in json.loads(body):
                v = VariableModel.from_dict(_)
                add_variable_to_registry(v)
                variables.append(v)
        except Exception as err:
            print_exc()
            return self.process_error(400, str(err))

        try:
            dao = VariableModelCustDao()
            for v in variables:
                dao.add_model(v)
            self.finish(json_dumps({"status": 0, "msg": "ok", "values": []}))
        except Exception as err:
            print_exc()
            return self.process_error(500, str(err))
Пример #9
0
    def sync_to_base_events(self, body):
        """
        第二步:新增基础事件到基础事件记录表
        参数:依赖/platform/event_models中post的参数
        :param body:
        :return:
        """
        try:
            events = list()
            base_event = dict()
            for _ in json.loads(body):
                # 自动设置一些默认值,无需前端重复传递
                base_event["status"] = "enable"
                base_event["type"] = "event"
                base_event["source"] = [{
                    "app": _.get("app"),
                    "name": _.get("name")
                }]

                # 获取body中我们需要的值
                base_event["app"] = _.get("app")
                base_event["name"] = _.get("name")
                base_event["visible_name"] = _.get("visible_name")
                base_event["remark"] = _.get("remark")
                base_event["module"] = _.get("type")
                event = VariableModel.from_dict(base_event)
                events.append(event)
        except:
            logger.error(traceback.format_exc())
            return False
        try:
            dao = VariableModelCustDao()
            for event in events:
                dao.add_model(event)
            return True
        except:
            logger.error(traceback.format_exc())
            return False
Пример #10
0
    SAMPLE_EVENTS = json.load(json_file)
    new_events = list()
    for _ in SAMPLE_EVENTS:
        new_event = EventModel.from_dict(_)
        add_event_to_registry(new_event)
        new_events.append(new_event)

    SAMPLE_EVENTS = new_events
    SAMPLE_EVENT = SAMPLE_EVENTS[0]


with open('nebula/tests/data/variable_model.json') as json_file:
    SAMPLE_VARIABLES = json.load(json_file)
    new_variables = list()
    for _ in SAMPLE_VARIABLES:
        new_variable = VariableModel.from_dict(_)
        add_variable_to_registry(new_variable)
        new_variables.append(new_variable)
    SAMPLE_VARIABLES = new_variables
    SAMPLE_VARIABLE = SAMPLE_VARIABLES[-1]  # did变量


def prepare_events(event_dao):
    for _ in SAMPLE_EVENTS:
        event_dao.add_model(_)


def prepare_variables(variable_dao):
    variable_dao.add_model(SAMPLE_VARIABLES[0])
    variable_dao.add_model(SAMPLE_VARIABLES[1])