基于Python访问Hive的pytest测试代码实现

根据《用Java、Python来开发Hive应用》一文,建立了使用Python、来开发Hive应用的方法,产生的代码如下(做了修改):

import pandas as pd
from pyhive import hive
from sqlalchemy import create_engine
from pyhive import hive
               
class Hive:
    def __init__(self):
        self.database= 'demo'
        self.host = '192.168.31.184'
        self.port = '10000'
        
    def getConnect(self):
        conn = hive.Connection(host=self.host, port=self.port,database=self.database)
        return conn;
                
    def getEngine(self):    
        # 创建 Hive 数据库连接
        hive_uri = f"hive://"+self.host+":"+self.port+"/"+self.database
        return create_engine(hive_uri)
           
    def disconnect(self,engine,conn):
        engine.dispose()
        conn.close()
           
    #执行查询
    def query(self,sql,engine,condition=None):
        try:
            if condition is None:
            # 执行 SQL 查询
                rs = pd.read_sql(sql, engine)
                return rs
            else:
                values = []
                where = " where "
                for key in condition:
                    where = where+key+" = %s and "
                    values.append(condition[key])
                where = where+"1=1"    
                sql = sql + where
                params = tuple(values)
                rs = pd.read_sql(sql, engine, params=params)
                return rs
        except Exception as e:
            print("Error occurred:", e)
           
    #添加数据
    def addDataToHiveTable(self,conn,tableName,data):
        like_array = f"array({', '.join(map(lambda x: f'\'{x}\'', data['like']))})"  # 使用单引号包裹字符串
        address_map = f"map('{list(data['address'].keys())[0]}', '{list(data['address'].values())[0]}')"  # 创建 MAP 格式
        # 创建游标
        cursor = conn.cursor()
        insertSql = "INSERT INTO person SELECT %s,%s,%s,"+like_array+","+address_map
        # 执行插入操作
        try:
            cursor.execute(insertSql, (data['id'], data['name'], data['age']))
        except Exception as e:
            print(f"Error inserting data: {e}")    
        conn.commit()
        cursor.close()
           
    #将文件中的数据加载到表中
    def loadDataForLocal(self,conn,tableName,path):
        cursor = conn.cursor()
        query = "LOAD DATA LOCAL INPATH '"+path+"' INTO TABLE "+tableName
        cursor.execute(query)
        conn.commit()
        cursor.close()
    
    #清空数据表
    def truncateTable(self,conn,tableName):
        cursor = conn.cursor()
        query = "truncate table "+tableName;
        #con.setAutoCommit(true) #确保自动提交
        cursor.execute(query)
        conn.commit()
        cursor.close()

现在,使用pytest来进行测试。

1)建立全局变量  

 hive = Hive()
    tableName = "person"
    sql = "SELECT * FROM "+tableName
    conn = None
    engine = None

2)建立setup_class(self)和teardown_class(self)函数      

 hive = Hive()
    tableName = "person"
    sql = "SELECT * FROM "+tableName
    conn = None
    engine = None

3)测试查询

    @allure.feature('Python访问Hive数据库')
    @allure.story('根据query进行查询')
    @allure.severity('Critical')
    #测试根据Query查询
    def test_query(self):
        #建立查询
        rs = self.hive.query(self.sql,self.engine)
        #获得记录个数
        row_count = len(rs)
        #验证记录个数
        assert row_count == 6
        #遍历记录
        for index, row in rs.iterrows():
            #验证编号行是不是数字
            assert isinstance(row.iloc[0], int)
            #验证姓名行是不是包含"elite"
            assert "elite" in row.iloc[1]
            #验证年龄行是不是数字
            assert isinstance(row.iloc[2], int)
            #验证爱好行是不是包含"basketball"
            assert "basketball" in row.iloc[3]
            #验证地址行是不是包含"address"    
            assert "address" in row.iloc[4]
           
    @allure.feature('Python访问Hive数据库')
    @allure.story('带一个的条件查询')
    @allure.severity('Normal')
    def test_query_with_one_condition(self):
        #查询条件
        condition={"name":"elite1"}
        #建立查询
        rs = self.hive.query(self.sql,self.engine,condition)
        #获得记录个数
        row_count = len(rs)
        #验证记录个数
        assert row_count == 1
        #遍历记录
        for index, row in rs.iterrows():
            #验证是不是符合查询条件
            #验证姓名行是不是包含"elite"
            assert "elite1" == row.iloc[1]
            
           
    @allure.feature('Python访问Hive数据库')    
    @allure.story('带两个的条件查询')
    @allure.severity('Normal')
    def test_query_with_Two_condition(self):
        #查询条件
        condition={"name":"elite1","age":"20"}
        #建立查询
        rs = self.hive.query(self.sql,self.engine,condition)
        #获得记录个数
        row_count = len(rs)
        #验证记录个数
        assert row_count == 1
        #遍历记录
        for index, row in rs.iterrows():
            #验证是不是符合查询条件
            #验证姓名行是不是"elite"
            assert "elite1" == row.iloc[1]
            #验证年龄行是不是50
            assert "50" == row.iloc[2]
            #验证编号行是不是数字
            assert isinstance(row.iloc[0], int)
            #验证姓名行是不是包含"elite"
            assert "elite" in row.iloc[1]    
            #验证年龄行是不是数字
            assert isinstance(row.iloc[2], int)
            #验证爱好行是不是包含"basketball"
            assert "basketball" in row.iloc[3]
            #验证地址行是不是包含"address"
            assert "address" in row.iloc[4]
           
    @allure.feature('Python访问Hive数据库')
    @allure.story('带两个的条件查询')
    @allure.severity('Normal')
    def test_query_with_Two_condition(self):
        #查询条件
        condition={"name":"elite1","age":"20"}
        #建立查询
        rs = self.hive.query(self.sql,self.engine,condition)
        #获得记录个数
        row_count = len(rs)
        #验证记录个数
        assert row_count == 1
        #遍历记录
        for index, row in rs.iterrows():
            #验证是不是符合查询条件    
            #验证姓名行是不是"elite"
            assert "elite1" == row.iloc[1]
            #验证年龄行是不是20
            assert 20 == row.iloc[2]
                     
    @allure.feature('Python访问Hive数据库')
    @allure.story('带三个的条件查询')
    @allure.severity('Normal')
    def test_query_with_three_condition(self):
        #查询条件
        condition={"id":"1","name":"elite0","age":"10"}
        #建立查询
        rs = self.hive.query(self.sql,self.engine,condition)
        #获得记录个数
        row_count = len(rs)
        #验证记录个数
        assert row_count == 1
        #遍历记录
        for index, row in rs.iterrows():
            #验证是不是符合查询条件
            #验证编号行是不是数字    
            assert 1 == row.iloc[0]
            #验证姓名行是不是包含"elite"
            assert "elite0" in row.iloc[1]
            #验证年龄行是不是数字
            assert 10 == row.iloc[2]

4)测试添加数据

    @allure.feature('Python访问Hive数据库')
    @allure.story('插入数据')
    @allure.severity('Normal')
    def test_addDataToHiveTable(self):
        #构造插入数据
        data = {
        'id': "50",
        'name': "Jerry",
        'age': "50",
        'like': ["basketball", "music", "dance"],
        'address': {"address": "xx"}
        }
        #插入数据
        self.hive.addDataToHiveTable(self.conn,self.tableName,data)
        #查询插入数据是否可以查询出来
        condition = {"name":"Jerry","age":"50"}    
        rs = self.hive.query(self.sql,self.engine,condition)
        row_count = len(rs)
        assert row_count == 1
        #验证插入数据
        for index, row in rs.iterrows():
            assert "Jerry" in row.iloc[1]
            assert "50" in str(row.iloc[2])

主函数改为

if __name__ == '__main__':
pytest.main(['-sv', '-q', '--alluredir', './report/xml'])

建立项目文件

environment.properties
Project Name=Hive
Author = Jerry Gu
System Version= Win11
java version "17.0.10"
Allure Version= 2.20.1
pytest --alluredir=.\report\xml
copy environment.properties .\report\xml
allure serve .\report\xml\

最后: 下方这份完整的软件测试视频教程已经整理上传完成,需要的朋友们可以自行领取【保证100%免费】

软件测试面试文档

我们学习必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有字节大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。

在这里插入图片描述

在这里插入图片描述

### Kali Linux 中安装或更新 Python 的方法 #### 下载并安装 Python 3.7 可以通过源码编译的方式在 Kali Linux 上安装特定版本的 Python,例如 Python 3.7。以下是具体操作: 1. **安装依赖项** 首先需要确保系统中有必要的构建工具和库文件: ```bash sudo apt update && sudo apt upgrade -y sudo apt install build-essential libssl-dev zlib1g-dev \ libncurses5-dev libgdbm-dev libnss3-dev libsqlite3-dev \ libreadline-dev libffi-dev curl wget git -y ``` 2. **下载 Python 源码包** 访问官方 Python 网站获取最新稳定版的 Python 3.7 源码压缩包,并将其解压至本地环境[^1]: ```bash cd ~ wget https://www.python.org/ftp/python/3.7.9/Python-3.7.9.tgz tar xzf Python-3.7.9.tgz cd Python-3.7.9/ ``` 3. **配置与编译** 使用 `./configure` 和 `make altinstall` 命令来完成编译过程,避免覆盖默认的 Python 版本: ```bash ./configure --enable-optimizations make -j $(nproc) sudo make altinstall ``` 4. **验证安装** 完成上述步骤后,可通过以下命令确认新版本是否成功安装: ```bash python3.7 --version ``` #### 设置默认 Python 解析器 如果希望更改系统的默认 Python 版本(如从 Python 2 到 Python 3),可利用 `update-alternatives` 工具实现这一目标[^2]: ```bash sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 100 sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 150 ``` 运行以上命令后,执行 `update-alternatives --config python` 来手动选择所需的解析器。 #### 安装第三方库 对于后续开发需求中的额外模块支持,推荐使用 pip 进行管理。首先需保证 pip 被正确安装于环境中[^3]: ```bash sudo apt-get install python3-pip pip3 install requests beautifulsoup4 numpy pandas scipy matplotlib seaborn sklearn tensorflow keras pytorch torchvision torchaudio pillow opencv-python scikit-image sympy cython jupyterlab flask django fastapi uvicorn gunicorn sqlalchemy psycopg2-binary mysqlclient pymongo redis celery kafka boto3 awscli azure-cli google-cloud-storage google-auth oauthlib httpie docker-compose virtualenv tox pytest pylint black isort mypy flake8 autopep8 yapf pre-commit bandit safety coverage codecov sphinx restructuredtext-lint markdownlint pandoc asciidoctor doxygen graphviz plantuml mermaid jsdoc typedoc swagger-codegen postman-collection-generator openapi-generator conda miniconda anaconda poetry hatch masonite laravel symfony yii zend-framework cakephp codeigniter phalcon drupal wordpress magento shopware prestashop woocommerce odoo sugarcrm salesforce servicenow tableau powerbi grafana prometheus kibana elasticsearch logstash filebeat metricbeat packetbeat winlogbeat auditbeat heartbeat fluentd splunk datadog newrelic dynatrace appdynamics instana signalfx sumologic thundra honeycomb lightstep zipkin jaeger tempo loki cortex thanos victoria-metrics clickhouse influxdb timescaledb mongodb postgresql mysql mariadb sqlite oracle-db snowflake bigquery redshift cockroachdb yugabyte neo4j arangodb cosmos-db dynamodb aurora spanner firestore firebase cloud-firestore cloud-spanner cloud-datastore cloud-bigtable cloud-sql cloud-database cloud-functions cloud-run cloud-build cloud-deploy cloud-workflows cloud-composer airflow luigi prefect marquez great-expectations dbt dagster mlflow tensorboard wandb neptune comet clearml weights-and-biases huggingface-transformers spacy nltk gensim torchserve onnxruntime tf-serving triton-inference-server ray dask modin polars cuio cuml cudf rmm rapids spark pyspark hive impala drill pig knime talend dataprep dataiku alteryx sas-spss-tableau-powerbi-qlikview-domoinfosecinfosecinfosec ``` 随后可根据项目实际需求调用对应指令完成所需库的引入工作, 如: ```bash pip3 install <library_name> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值