你好,我正在尝试使用SQLAlchemy和Alembic创建一个使用PostgreSQL数据库的单元测试。
同时,我将其运行在Docker PostgreSQL上。
我正在遵循testing.postgresql的文档(文档)来设置一个临时的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错误?
编辑:我也设置了我的数据路径,但仍返回相同的错误。
dockerfile:
FROM python:slim-jessie
ADD requirements.txt /app/requirements.txt
ADD . /app/
WORKDIR /app/
RUN pip install -r requirements.txt
docker-compose:
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')