okfn-brasil / jarbas

🎩 API for information and suspicions about reimbursements by Brazilian congresspeople
https://jarbas.serenata.ai/
296 stars 61 forks source link

Use parallel argument in tests #300

Closed cuducos closed 6 years ago

cuducos commented 6 years ago

What is the purpose of this Pull Request? Make tests run quicker.

What was done to achieve this purpose? Added --parallel argument to docs and .travis.yml as well as a lib to fix traceback when using this option.

How to test if it really works? Only only in UNIX based systems (sorry, according to Django --parallel argument is not compatible with Windows): Copy and paste the test command from the README.md and compare to the time taken without the added argument.

For example:

$ docker-compose run --rm django test && docker-compose run --rm django test  --parallel

The first result is what we have today, the second is with multiple tests running in parallel.

Who can help reviewing it? @anaschwendler

anaschwendler commented 6 years ago

Hi @cuducos can you solve the conflicts? Then I'll test it!

cuducos commented 6 years ago

Done.

anaschwendler commented 6 years ago

What I did to test this PR:

  1. Clone Jarbas:

    $ git clone git@github.com:datasciencebr/jarbas.git
  2. Open Jarbas' folder:

    $ cd jarbas
  3. Change to @cuducos branch:

    $ git fetch origin
    $ git checkout -b cuducos-parallel-tests origin/cuducos-parallel-tests
    $ git merge master
  4. Run its tests:

    $ docker-compose up -d
    $ docker-compose run --rm django python manage.py migrate
    $ docker-compose run --rm django python manage.py test --parallel (not applied)

Result:

It seems that it is not resolving postgres:

```console ➜ jarbas git:(cuducos-parallel-tests) docker-compose run --rm django python manage.py migrate Starting jarbas_postgres_1 ... Starting jarbas_postgres_1 Starting jarbas_postgres_1 ... done Starting jarbas_tasks_1 ... done Traceback (most recent call last): File "/usr/local/lib/python3.5/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection self.connect() File "/usr/local/lib/python3.5/site-packages/django/db/backends/base/base.py", line 189, in connect self.connection = self.get_new_connection(conn_params) File "/usr/local/lib/python3.5/site-packages/django/db/backends/postgresql/base.py", line 176, in get_new_connection connection = Database.connect(**conn_params) File "/usr/local/lib/python3.5/site-packages/psycopg2/__init__.py", line 130, in connect conn = _connect(dsn, connection_factory=connection_factory, **kwasync) psycopg2.OperationalError: could not translate host name "postgres" to address: Name does not resolve The above exception was the direct cause of the following exception: Traceback (most recent call last): File "manage.py", line 22, in execute_from_command_line(sys.argv) File "/usr/local/lib/python3.5/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line utility.execute() File "/usr/local/lib/python3.5/site-packages/django/core/management/__init__.py", line 356, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/usr/local/lib/python3.5/site-packages/django/core/management/base.py", line 283, in run_from_argv self.execute(*args, **cmd_options) File "/usr/local/lib/python3.5/site-packages/django/core/management/base.py", line 330, in execute output = self.handle(*args, **options) File "/usr/local/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 83, in handle executor = MigrationExecutor(connection, self.migration_progress_callback) File "/usr/local/lib/python3.5/site-packages/django/db/migrations/executor.py", line 20, in __init__ self.loader = MigrationLoader(self.connection) File "/usr/local/lib/python3.5/site-packages/django/db/migrations/loader.py", line 52, in __init__ self.build_graph() File "/usr/local/lib/python3.5/site-packages/django/db/migrations/loader.py", line 209, in build_graph self.applied_migrations = recorder.applied_migrations() File "/usr/local/lib/python3.5/site-packages/django/db/migrations/recorder.py", line 65, in applied_migrations self.ensure_schema() File "/usr/local/lib/python3.5/site-packages/django/db/migrations/recorder.py", line 52, in ensure_schema if self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor()): File "/usr/local/lib/python3.5/site-packages/django/db/backends/base/base.py", line 254, in cursor return self._cursor() File "/usr/local/lib/python3.5/site-packages/django/db/backends/base/base.py", line 229, in _cursor self.ensure_connection() File "/usr/local/lib/python3.5/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection self.connect() File "/usr/local/lib/python3.5/site-packages/django/db/utils.py", line 94, in __exit__ six.reraise(dj_exc_type, dj_exc_value, traceback) File "/usr/local/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise raise value.with_traceback(tb) File "/usr/local/lib/python3.5/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection self.connect() File "/usr/local/lib/python3.5/site-packages/django/db/backends/base/base.py", line 189, in connect self.connection = self.get_new_connection(conn_params) File "/usr/local/lib/python3.5/site-packages/django/db/backends/postgresql/base.py", line 176, in get_new_connection connection = Database.connect(**conn_params) File "/usr/local/lib/python3.5/site-packages/psycopg2/__init__.py", line 130, in connect conn = _connect(dsn, connection_factory=connection_factory, **kwasync) django.db.utils.OperationalError: could not translate host name "postgres" to address: Name does not resolve ```
cuducos commented 6 years ago

django.db.utils.OperationalError: could not translate host name "postgres" to address: Name does not resolve

I'm afraid this error has nothing to do with this PR. You might want to re-do the same steps with the master branch and open an issue related to PostgreSQL and Docker setup.

anaschwendler commented 6 years ago

Thanks @cuducos, I'll try to check what is happening in the master branch.

anaschwendler commented 6 years ago

Everything seems to be working on master, I'll test this PR again.

```console ➜ jarbas git:(master) docker-compose up -d Pulling queue (rabbitmq:3.6.11-alpine)... 3.6.11-alpine: Pulling from library/rabbitmq 88286f41530e: Pull complete f37b1a2424b3: Pull complete 2799929042f7: Pull complete 1a0ac93c08db: Pull complete 7dc18c248a7b: Pull complete 9085838e09d8: Pull complete 0f793b591a3e: Pull complete 42da39c9c3ca: Pull complete 0a442df56342: Pull complete Digest: sha256:109570cf2ede004fcc5374f8cf35ad26308b393591f5e05736073ed0c264cac2 Status: Downloaded newer image for rabbitmq:3.6.11-alpine Pulling postgres (postgres:9.6.5-alpine)... 9.6.5-alpine: Pulling from library/postgres b1f00a6a160c: Pull complete 25a88a2feda9: Pull complete bcd866a92fb7: Pull complete 8c43cb896f3e: Pull complete 341573b3bddd: Pull complete d7ccfa970786: Pull complete 32d28928466b: Pull complete 8b424957445a: Pull complete 88c1161620fb: Pull complete Digest: sha256:bafac9478d8cca8965d091ee228c6897bfa0e2ad4704c26f58bedc8b37c4cde1 Status: Downloaded newer image for postgres:9.6.5-alpine Pulling tasks (datasciencebr/jarbas-backend:latest)... latest: Pulling from datasciencebr/jarbas-backend ab7e51e37a18: Pull complete 4a57a4e05b89: Pull complete 81dad7471918: Pull complete 4ca462e5d889: Pull complete fd1617db8f72: Pull complete 5ddc99c7db2c: Pull complete 6355dbbeab0b: Pull complete e14145e7058b: Pull complete 10cc0679d7d8: Pull complete 77f7c41f98e3: Pull complete 6b5bf642f25b: Pull complete Digest: sha256:ccccedcbee6b0b7b022e9f119d3952ebd8eebc06afbc9b1d6e9f9cb18f72da41 Status: Downloaded newer image for datasciencebr/jarbas-backend:latest Pulling elm (datasciencebr/jarbas-frontend:latest)... latest: Pulling from datasciencebr/jarbas-frontend aa18ad1a0d33: Pull complete 90f6d19ae388: Pull complete 94273a890192: Pull complete c9110c904324: Pull complete 788d73c0fb6b: Pull complete f221bb562f24: Pull complete 14414a6a768d: Pull complete af6d2b2ad991: Pull complete 574bc18e6744: Pull complete f253697e5e0c: Pull complete d21fb13953fc: Pull complete 24cd1af1ea45: Pull complete 108248703c25: Pull complete 5526289b4ba1: Pull complete 84d577100d45: Pull complete 35a3767a03e8: Pull complete Digest: sha256:7f5a1fbdecaf27af3fd8efb6b5e60a21fbd8419064e8e7bf06e98a74f523e86c Status: Downloaded newer image for datasciencebr/jarbas-frontend:latest Creating jarbas_postgres_1 ... Creating jarbas_elm_1 ... Creating jarbas_queue_1 ... Creating jarbas_queue_1 Creating jarbas_elm_1 Creating jarbas_postgres_1 ... done Creating jarbas_tasks_1 ... Creating jarbas_tasks_1 ... done Creating jarbas_django_1 ... Creating jarbas_django_1 ... done ➜ jarbas git:(master) docker-compose run --rm django python manage.py migrate Starting jarbas_postgres_1 ... done Starting jarbas_elm_1 ... Starting jarbas_queue_1 ... done Starting jarbas_tasks_1 ... done Operations to perform: Apply all migrations: admin, auth, chamber_of_deputies, contenttypes, core, sessions Running migrations: Applying contenttypes.0001_initial... OK Applying auth.0001_initial... OK Applying admin.0001_initial... OK Applying admin.0002_logentry_remove_auto_add... OK Applying contenttypes.0002_remove_content_type_name... OK Applying auth.0002_alter_permission_name_max_length... OK Applying auth.0003_alter_user_email_max_length... OK Applying auth.0004_alter_user_username_opts... OK Applying auth.0005_alter_user_last_login_null... OK Applying auth.0006_require_contenttypes_0002... OK Applying auth.0007_alter_validators_add_error_messages... OK Applying auth.0008_alter_user_username_max_length... OK Applying core.0001_initial... OK Applying core.0002_add_indexes... OK Applying core.0003_remove_some_indexes... OK Applying core.0004_add_receipt_url... OK Applying core.0005_add_receipt_url_last_update... OK Applying core.0006_lazy_backend_receipt_url... OK Applying core.0007_add_suppliers... OK Applying core.0008_optimize_char_field_lengths... OK Applying core.0009_add_latitude_and_longitude... OK Applying core.0010_extract_receipt... OK Applying core.0011_subquota_description_length... OK Applying core.0012_use_date_not_datetime... OK Applying core.0013_create_model_reimbursement... OK Applying core.0014_add_suspicions_and_probability_to_reimbursements... OK Applying core.0015_add_receipt_to_reimbursement... OK Applying core.0016_add_custom_ordering_to_reimbursement... OK Applying core.0017_make_some_fields_optional... OK Applying core.0018_make_issue_date_required... OK Applying core.0019_cleanup_remove_old_api... OK Applying core.0020_rename_supplier_to_company... OK Applying core.0021_make_reciept_fetched_a_db_index... OK Applying core.0022_remove_unique_together_from_reimbursement... OK Applying core.0023_add_last_update_field_to_reimbursements... OK Applying core.0024_add_available_in_latest_dataset_field_to_reimbursement... OK Applying core.0025_historicalreimbursement... OK Applying core.0026_order_reimbursements_by_year_and_issue_date... OK Applying core.0027_translate_verbose_names... OK Applying core.0028_auto_20170601_1701... OK Applying core.0029_make_issue_date_an_index... OK Applying core.0030_remove_unused_indexes... OK Applying core.0031_add_index_together_for_reimbursement... OK Applying core.0032_auto_20170613_0641... OK Applying core.0033_add_index_for_subquota_description... OK Applying core.0034_auto_20170629_2150... OK Applying core.0035_create_model_tweet... OK Applying core.0036_alter_tweet_status_to_decimal... OK Applying core.0037_auto_20170727_1624... OK Applying core.0038_auto_20170728_1748... OK Applying core.0039_add_search_vector_to_reimbursement... OK Applying core.0040_create_gin_index_with_search_vector... OK Applying core.0041_migrate_data_to_chamber_of_deputies_app... OK Applying chamber_of_deputies.0001_initial... OK Applying sessions.0001_initial... OK ➜ jarbas git:(master) docker-compose run --rm django python manage.py check Starting jarbas_queue_1 ... Starting jarbas_postgres_1 ... Starting jarbas_elm_1 ... done Starting jarbas_tasks_1 ... done System check identified no issues (0 silenced). ➜ jarbas git:(master) docker-compose run --rm django python manage.py test Starting jarbas_postgres_1 ... Starting jarbas_postgres_1 ... done Starting jarbas_tasks_1 ... done Creating test database for alias 'default'... System check identified no issues (0 silenced). .......................................................................................................................................................................... ---------------------------------------------------------------------- Ran 170 tests in 5.329s OK Destroying test database for alias 'default'... ```
anaschwendler commented 6 years ago

Nice! By applying docker-compose run --rm django python manage.py test --parallel the result is:

➜  jarbas git:(cuducos-parallel-tests) docker-compose run --rm django python manage.py test --parallel
Starting jarbas_queue_1 ... 
Starting jarbas_queue_1 ... done
Starting jarbas_postgres_1 ... done
Starting jarbas_tasks_1 ... done
Creating test database for alias 'default'...
Cloning test database for alias 'default'...
Cloning test database for alias 'default'...
System check identified no issues (0 silenced).
..........................................................................................................................................................................
----------------------------------------------------------------------
Ran 170 tests in 3.703s

OK
Destroying test database for alias 'default'...
Destroying test database for alias 'default'...
Destroying test database for alias 'default'...

Works fine, merging it