Fix REST API tests (#4279)

* Conftest: check values instead of keys

* Rename SQL script for db restoring

* Use SQL dump file instead of custom format

* Use SQL dump file instead of custom format

* Update README

* Conftest: fix scope for restore_db
main
Kirill Sizov 4 years ago committed by GitHub
parent 61787ce749
commit 58a71a480d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -63,7 +63,7 @@ for i, color in enumerate(colormap):
To backup DB and data volume, please use commands below. To backup DB and data volume, please use commands below.
```console ```console
docker exec cvat_db pg_dump -c -C -Fc -U root -d cvat > assets/cvat_db.dump docker exec cvat_db pg_dump -c -Fp -U root -d cvat > assets/cvat_db/cvat_db.sql
docker run --rm --volumes-from cvat ubuntu tar -cjv /home/django/data > assets/cvat_data.tar.bz2 docker run --rm --volumes-from cvat ubuntu tar -cjv /home/django/data > assets/cvat_data.tar.bz2
``` ```
@ -81,7 +81,7 @@ python utils/dump_objects.py
To restore DB and data volume, please use commands below. To restore DB and data volume, please use commands below.
```console ```console
cat assets/cvat_db/cvat_db.dump | docker exec -i cvat_db pg_restore -1 -c -U root -d cvat cat assets/cvat_db/cvat_db.sql | docker exec -i cvat_db psql -U root -d cvat
cat assets/cvat_data.tar.bz2 | docker run --rm -i --volumes-from cvat ubuntu tar -xj --strip 3 -C /home/django/data cat assets/cvat_data.tar.bz2 | docker run --rm -i --volumes-from cvat ubuntu tar -xj --strip 3 -C /home/django/data
``` ```
@ -93,7 +93,7 @@ cat assets/cvat_data.tar.bz2 | docker run --rm -i --volumes-from cvat ubuntu tar
you have json description of all objects together with cvat_db.sql, it will you have json description of all objects together with cvat_db.sql, it will
be possible to recreate them manually. be possible to recreate them manually.
1. How to upgrade cvat_data.tar.bz2 and cvat_db.dump? 1. How to upgrade cvat_data.tar.bz2 and cvat_db.sql?
After every commit which changes the layout of DB and data directory it is After every commit which changes the layout of DB and data directory it is
possible to break these files. But failed tests should be a clear indicator possible to break these files. But failed tests should be a clear indicator

File diff suppressed because one or more lines are too long

@ -0,0 +1,7 @@
SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = 'cvat' AND pid <> pg_backend_pid();
DROP DATABASE cvat;
CREATE DATABASE cvat WITH TEMPLATE test_db;

@ -20,15 +20,18 @@ def restore_data_volume():
'--strip 3 -C /home/django/data -xjf /mnt/cvat_data.tar.bz2' '--strip 3 -C /home/django/data -xjf /mnt/cvat_data.tar.bz2'
run(command.split(), check=True) #nosec run(command.split(), check=True) #nosec
def restore_cvat_db():
cvat_db_container('psql -U root -d postgres -f /cvat_db/restore_db.sql')
def drop_test_db(): def drop_test_db():
cvat_db_container('pg_restore -c -U root -d cvat /cvat_db/cvat_db.dump') restore_cvat_db()
cvat_db_container('rm -rf /cvat_db') cvat_db_container('rm -rf /cvat_db')
cvat_db_container('dropdb test_db') cvat_db_container('dropdb test_db')
def create_test_db(): def create_test_db():
docker_cp(source=osp.join(ASSETS_DIR, 'cvat_db'), target='cvat_db:/') docker_cp(source=osp.join(ASSETS_DIR, 'cvat_db'), target='cvat_db:/')
cvat_db_container('createdb test_db') cvat_db_container('createdb test_db')
cvat_db_container('pg_restore -U root -d test_db /cvat_db/cvat_db.dump') cvat_db_container('psql -U root -d test_db -f /cvat_db/cvat_db.sql')
@pytest.fixture(scope='session', autouse=True) @pytest.fixture(scope='session', autouse=True)
def init_test_db(): def init_test_db():
@ -44,8 +47,8 @@ def init_test_db():
drop_test_db() drop_test_db()
@pytest.fixture(scope='function', autouse=True) @pytest.fixture(scope='function', autouse=True)
def restore_cvat_db(): def restore():
cvat_db_container('psql -U root -d postgres -f /cvat_db/cvat_db.sql') restore_cvat_db()
@pytest.fixture(scope='module') @pytest.fixture(scope='module')
def users(): def users():
@ -77,7 +80,7 @@ def users_by_name(users):
def find_users(test_db): def find_users(test_db):
def find(**kwargs): def find(**kwargs):
assert len(kwargs) > 0 assert len(kwargs) > 0
assert any(kwargs) assert any(kwargs.values())
data = test_db data = test_db
kwargs = dict(filter(lambda a: a[1] is not None, kwargs.items())) kwargs = dict(filter(lambda a: a[1] is not None, kwargs.items()))

Loading…
Cancel
Save