【发布时间】:2020-01-03 08:13:12
【问题描述】:
您好,我正在尝试使用使用 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')
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
在文档中它说“testing.postgresql.Postgresql 在实例化时执行 initdb 和 postgres。在删除 Postgresql 对象时,它会终止 PostgreSQL 实例并删除临时目录。”
那么,当我已经安装了 testing.postgresql 并在我的 docker 上运行了 postgresql 时,为什么会出现 initdb 错误?
编辑:
我也设置了数据路径,但它仍然返回相同的错误
码头文件:
FROM python:slim-jessie
ADD requirements.txt /app/requirements.txt
ADD . /app/
WORKDIR /app/
RUN pip install -r requirements.txt
码头工人撰写:
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
testing.postgresql.Postgresql(copy_data_from='data/postgres:/var/lib/postgresql/data')
【问题讨论】:
标签: python postgresql docker sqlalchemy python-unittest