Exemplo n.º 1
0
def sql(step):

    result = {}

    element = step['element']
    _sql = e.get(element)[1]

    logger.info('SQL: %s' % repr(_sql))
    # 获取连接参数
    value = e.get(step['page'] + '-' + 'config')[1]
    arg = data_format(value)

    if step['page'] not in g.db.keys():
        g.db[step['page']] = DB(arg)
    if _sql.lower().startswith('select'):
        row = g.db[step['page']].fetchone(_sql)
        logger.info('SQL result: %s' % repr(row))
        if not row:
            raise Exception('*** Fetch None ***')

    elif _sql.lower().startswith('db.'):
        _sql_ = _sql.split('.', 2)
        collection = _sql_[1]
        sql = _sql_[2]
        row = g.db[step['page']].mongo(collection, sql)
        if sql.startswith('find'):
            result = row
            logger.info('keys result: %s' % repr(result))
    else:
        g.db[step['page']].execute(_sql)

    if _sql.lower().startswith('select'):
        text = _sql[6:].split('FROM')[0].split('from')[0].strip()
        keys = dedup(text).split(',')
        for i, k in enumerate(keys):
            keys[i] = k.split(' ')[-1]
        result = dict(zip(keys, row))
        logger.info('keys result: %s' % repr(result))

    data = step['data']
    if not data:
        data = step['expected']
    if data:
        for key in data:
            sv, pv = data[key], result[key]
            logger.info('key: %s, expect: %s, real: %s' %
                        (repr(key), repr(sv), repr(pv)))

            compare(sv, pv)

    output = step['output']
    if output:
        _output = {}
        for key in output:
            _output[key] = result[output[key]]
            g.var[key] = result[output[key]]
        logger.info('output: %s' % repr(_output))
Exemplo n.º 2
0
def sql(step):
    element = step['element']
    el, _sql = e.get(element)

    logger.info('SQL: %s' % repr(_sql))
    # 获取连接参数
    el, value = e.get(step['page'] + '-' + 'config')
    arg = data_format(value)

    if step['page'] not in g.db.keys():
        g.db[step['page']] = DB(arg)
    if _sql.lower().startswith('select'):
        row = g.db[step['page']].fetchone(_sql)
        logger.info('SQL result: %s' % repr(row))
        if not row:
            raise Exception('*** Fetch None ***')
    else:
        g.db[step['page']].execute(_sql)

    result = {}
    if _sql.lower().startswith('select'):
        keys = _sql[6:].split('FROM')[0].split('from')[0].strip().split(',')
        for i, k in enumerate(keys):
            keys[i] = k.split(' ')[-1]
        result = dict(zip(keys, row))
        logger.info('keys result: %s' % repr(result))

    data = step['data']
    if not data:
        data = step['expected']
    if data:
        for key in data:
            logger.info('key: %s, expect: %s, real: %s' %
                        (repr(key), repr(data[key]), repr(result[key])))
            if data[key].startswith('*'):
                assert data[key][1:] in result[key]
            else:
                assert data[key] == result[key]

    output = step['output']
    if output:
        logger.info('output: %s' % repr(output))
        for key in output:
            g.var[key] = result[output[key]]
Exemplo n.º 3
0
def sql(step):

    response = {}

    element = step['element']
    _sql = e.get(element)[1]

    logger.info('SQL: %s' % repr(_sql))
    # 获取连接参数
    value = e.get(step['page'] + '-' + 'config')[1]
    arg = data_format(value)

    if step['page'] not in g.db.keys():
        g.db[step['page']] = DB(arg)
    if _sql.lower().startswith('select'):
        row = g.db[step['page']].fetchone(_sql)
        logger.info('SQL response: %s' % repr(row))
        if not row:
            raise Exception('*** Fetch None ***')
 
    elif _sql.lower().startswith('db.'):
        _sql_ = _sql.split('.', 2)
        collection = _sql_[1]
        sql = _sql_[2]
        response = g.db[step['page']].mongo(collection, sql)
        if response:
            logger.info('find result: %s' % repr(response))
    else:
        g.db[step['page']].execute(_sql)

    if _sql.lower().startswith('select'):
        text = _sql[6:].split('FROM')[0].split('from')[0].strip()
        keys = dedup(text).split(',')
        for i, k in enumerate(keys):
            keys[i] = k.split(' ')[-1]
        response = dict(zip(keys, row))
        logger.info('select result: %s' % repr(response))

    expected = step['data']
    if not expected:
        expected = step['expected']
    if 'json' in expected:
        expected['json'] = json2dict(expected.get('json', '{}'))
        result = check(expected.pop('json'), response['json'])
        logger.info('json check result: %s' % result)
        if result['code'] != 0:
            raise Exception(f'json | EXPECTED:{repr(expected["json"])}, REAL:{repr(response["json"])}, RESULT: {result}')
        elif result['var']:
            var = dict(var, **result['var'])
            g.var = dict(g.var, **result['var'])
            logger.info('json var: %s' % (repr(result['var']))) 

    if expected:
        for key in expected:
            sv, pv = expected[key], response[key]
            logger.info('key: %s, expect: %s, real: %s' %
                        (repr(key), repr(sv), repr(pv)))

            compare(sv, pv)

    output = step['output']
    if output:
        _output = {}
        for k, v in output.items():
            if k == 'json':
                sub = json2dict(output.get('json', '{}'))
                result = check(sub, response['json'])
                # logger.info('Compare json result: %s' % result)
                var = dict(var, **result['var'])
                g.var = dict(g.var, **result['var'])
                logger.info('json var: %s' % (repr(result['var'])))
            else:
                _output[k] = response[v]
                g.var[k] = response[v]
        logger.info('output: %s' % repr(_output))
Exemplo n.º 4
0
def sql(step):
    element = step['element']
    el, _sql = e.get(element)

    logger.info('SQL: %s' % repr(_sql))
    # 获取连接参数
    el, value = e.get(step['page'] + '-' + 'config')
    arg = data_format(value)

    if step['page'] not in g.db.keys():
        g.db[step['page']] = DB(arg)
    if _sql.lower().startswith('select'):
        row = g.db[step['page']].fetchone(_sql)
        logger.info('SQL result: %s' % repr(row))
        if not row:
            raise Exception('*** Fetch None ***')
    else:
        g.db[step['page']].execute(_sql)

    result = {}
    if _sql.lower().startswith('select'):
        keys = _sql[6:].split('FROM')[0].split('from')[0].strip().split(',')
        for i, k in enumerate(keys):
            keys[i] = k.split(' ')[-1]
        result = dict(zip(keys, row))
        logger.info('keys result: %s' % repr(result))

    data = step['data']
    if not data:
        data = step['expected']
    if data:
        for key in data:
            sv, pv = data[key], result[key]
            logger.info('key: %s, expect: %s, real: %s' %
                        (repr(key), repr(sv), repr(pv)))

            compare(sv, pv)
            # if isinstance(sv, str):
            #
            #     if sv.startswith('#'):
            #         assert sv[1:] != str(pv)
            #
            #     assert isinstance(pv, str)
            #
            #     if sv.startswith('*'):
            #         assert sv[1:] in pv
            #
            #     elif sv.startswith('^'):
            #         assert pv.startswith(sv[1:])
            #
            #     elif sv.startswith('$'):
            #         assert pv.endswith(sv[1:])
            #
            #     elif sv.startswith('\\'):
            #         sv = sv[1:]
            #
            #     assert sv == pv
            #
            # elif isinstance(sv, int):
            #     assert isinstance(pv, int)
            #     assert sv == pv
            #
            # elif isinstance(sv, float):
            #     assert absisinstance(pv, float)
            #     assert sv == pv
            #
            # else:
            #     assert sv == pv

    output = step['output']
    if output:
        logger.info('output: %s' % repr(output))
        for key in output:
            g.var[key] = result[output[key]]