6

Hi i'm trying to make a unittest with postgresql database that use sqlalchemy and alembic

Also im running it on docker postgresql

I'm following the docs of testing.postgresql(docs) to set up a temporary postgresql instance handle database testing and wrote the the following test:

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')

The crawl_bds_obj() basically save information from an URL and save it to the database using session.commit() and return the type data

When i tried to run the test it return the following error:

ERROR: test_crawl_bds_obj (tests.test_utils.TestUtils)
raise RuntimeError("command not found: %s" % name)
RuntimeError: command not found: initdb

In the docs it said that "testing.postgresql.Postgresql executes initdb and postgres on instantiation. On deleting Postgresql object, it terminates PostgreSQL instance and removes temporary directory."

So why am i getting initdb error when i already installed testing.postgresql and had postgresql running on my docker?

EDIT:

I aslo had set my data path but it still return the same error

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')

1 Answer 1

1

you need to run this command as postgresql user not root, so you may try to run your commands using:

runuser -l  postgres -c 'command'    

or

su -c "command" postgres

or add USER postgres to your Dockerfile

and check the requirments:

Python 2.6, 2.7, 3.2, 3.3, 3.4, 3.5
pg8000 1.10

UPDATE

To make copy_data_from works you should generate the folder first:

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

and then add this:

pg = testing.postgresql.Postgresql(copy_data_from='myData')
Sign up to request clarification or add additional context in comments.

11 Comments

but I already had POSTGRES_USER=postgres and set the password of it in the enviroment of postgres in docker-compose, do i need to add it again in my dockerfile ?
so right now i'm running "python -m unittest discover" in my current "worker"(main project container) not "postgres" container. do i need to change it to the postgres container ?
i added "USER postgres" to my dockerfile which run with the worker container, and restart my containers but it still has the error
Restart won't help you need to rebuild it
"Cannot start service worker: unable to find user postgres: no matching entries in passwd file" when i rebuild it, do i need anything else in the docker file ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.