Lin*_*yen 6 python postgresql sqlalchemy docker python-unittest
你好,我正在尝试使用 sqlalchemy 和 alembic 使用 postgresql 数据库进行单元测试
我也在 docker postgresql 上运行它
我按照testing.postgresql( docs )的文档设置一个临时的postgresql实例处理数据库测试并编写了以下测试:
def test_crawl_bds_obj(self):
with testing.postgresql.Postgresql() as postgresql:
engine.create_engine(postgresql.url())
result = crawl_bds_obj(1 ,'/ban-can-ho-chung-cu-duong-mai-chi-tho-phuong-an-phu-prj-the-sun-avenue/nh-chu-gap-mat-tien-t-q2-thuoc-du-tphcm-pr22171626')
self.assertEqual(result, 'sell')
Run Code Online (Sandbox Code Playgroud)
crawl_bds_obj() 基本上保存来自 URL 的信息,并使用 session.commit() 将其保存到数据库并返回类型数据
当我尝试运行测试时,它返回以下错误:
ERROR: test_crawl_bds_obj (tests.test_utils.TestUtils)
raise RuntimeError("command not found: %s" % name)
RuntimeError: command not found: initdb
Run Code Online (Sandbox Code Playgroud)
在文档中,它说“testing.postgresql.Postgresql 在实例化时执行 initdb 和 postgres。在删除 Postgresql 对象时,它会终止 PostgreSQL 实例并删除临时目录。”
那么,当我已经安装了testing.postgresql并且postgresql在我的docker上运行时,为什么我会收到initdb错误呢?
编辑:
我也设置了我的数据路径,但它仍然返回相同的错误
docker文件:
FROM python:slim-jessie
ADD requirements.txt /app/requirements.txt
ADD . /app/
WORKDIR /app/
RUN pip install -r requirements.txt
Run Code Online (Sandbox Code Playgroud)
docker-撰写:
postgres:
image: postgres
restart: always
environment:
- POSTGRES_USER=${POSTGRES_DEFAULT_USER}
- POSTGRES_PASSWORD=${POSTGRES_DEFAULT_PASSWORD}
- POSTGRES_DB=${POSTGRES_DEFAULT_DB}
- POSTGRES_PORT=${POSTGRES_DEFAULT_PORT}
volumes:
- ./data/postgres:/var/lib/postgresql/data
pgadmin:
image: dpage/pgadmin4
environment:
PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL}
PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD}
volumes:
- ./data/pgadmin:/root/.pgadmin
ports:
- "${PGADMIN_PORT}:80"
logging:
driver: none
restart: unless-stopped
worker:
build:
context: .
dockerfile: Dockerfile
command: "watchmedo auto-restart --recursive -p '*.py'"
environment:
- C_FORCE_ROOT=1
volumes:
- .:/app
links:
- rabbit
depends_on:
- rabbit
- postgres
Run Code Online (Sandbox Code Playgroud)
test.postgresql.Postgresql(copy_data_from='data/postgres:/var/lib/postgresql/data')
您需要以postgresqlnot 用户身份运行此命令root,因此您可以尝试使用以下命令运行命令:
runuser -l postgres -c 'command'
Run Code Online (Sandbox Code Playgroud)
或者
su -c "command" postgres
Run Code Online (Sandbox Code Playgroud)
或添加USER postgres到您的Dockerfile
并检查要求:
Python 2.6, 2.7, 3.2, 3.3, 3.4, 3.5
pg8000 1.10
Run Code Online (Sandbox Code Playgroud)
更新
要制作copy_data_from作品,您应该首先生成文件夹:
FROM python:slim-jessie
ADD requirements.txt /app/requirements.txt
ADD . /app/
WORKDIR /app/
RUN pip install -r requirements.txt
RUN /PATH/TO/initdb -D myData -U postgres
Run Code Online (Sandbox Code Playgroud)
然后添加:
pg = testing.postgresql.Postgresql(copy_data_from='myData')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3755 次 |
| 最近记录: |