diff --git a/.travis.yml b/.travis.yml index 67d8c0502a..4f772f6d5d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -30,11 +30,23 @@ before_install: script: - cd InvenTree && python3 manage.py makemigrations && cd .. - python3 ci/check_migration_files.py + # Run unit testing / code coverage tests - invoke coverage + # Run unit test for SQL database backend - cd InvenTree && python3 manage.py test --settings=InvenTree.ci_mysql && cd .. + # Run unit test for PostgreSQL database backend - cd InvenTree && python3 manage.py test --settings=InvenTree.ci_postgresql && cd .. - invoke translate - invoke style + # Create an empty database and fill it with test data + - rm inventree_db.sqlite3 + - invoke migrate + - invoke import-fixtures + # Export database records + - invoke export-records -f data.json + # Create a new empty database and import the saved data + - rm inventree_db.sqlite3 + - invoke import-records -f data.json after_success: - coveralls \ No newline at end of file diff --git a/InvenTree/common/apps.py b/InvenTree/common/apps.py index 0acc72c377..34b43fc68b 100644 --- a/InvenTree/common/apps.py +++ b/InvenTree/common/apps.py @@ -1,5 +1,6 @@ +# -*- coding: utf-8 -*- + from django.apps import AppConfig -from django.db.utils import OperationalError, ProgrammingError, IntegrityError class CommonConfig(AppConfig): diff --git a/InvenTree/common/fixtures/currency.yaml b/InvenTree/common/fixtures/currency.yaml deleted file mode 100644 index 639b0751df..0000000000 --- a/InvenTree/common/fixtures/currency.yaml +++ /dev/null @@ -1,16 +0,0 @@ -# Test fixtures for Currency objects - -- model: common.currency - fields: - symbol: '$' - suffix: 'AUD' - description: 'Australian Dollars' - base: True - -- model: common.currency - fields: - symbol: '$' - suffix: 'USD' - description: 'US Dollars' - base: False - value: 1.4 \ No newline at end of file diff --git a/tasks.py b/tasks.py index 49f3f9445b..0c33fcb4c7 100644 --- a/tasks.py +++ b/tasks.py @@ -238,6 +238,93 @@ def postgresql(c): c.run('sudo apt-get install postgresql postgresql-contrib libpq-dev') c.run('pip3 install psycopg2') +@task(help={'filename': "Output filename (default = 'data.json')"}) +def export_records(c, filename='data.json'): + """ + Export all database records to a file + """ + + # Get an absolute path to the file + if not os.path.isabs(filename): + filename = os.path.join(localDir(), filename) + filename = os.path.abspath(filename) + + print(f"Exporting database records to file '{filename}'") + + if os.path.exists(filename): + response = input("Warning: file already exists. Do you want to overwrite? [y/N]: ") + response = str(response).strip().lower() + + if response not in ['y', 'yes']: + print("Cancelled export operation") + return 0 + + cmd = f'dumpdata --exclude contenttypes --exclude auth.permission --indent 2 --output {filename}' + + manage(c, cmd, pty=True) + +@task(help={'filename': 'Input filename'}) +def import_records(c, filename='data.json'): + """ + Import database records from a file + """ + + # Get an absolute path to the supplied filename + if not os.path.isabs(filename): + filename = os.path.join(localDir(), filename) + + if not os.path.exists(filename): + print(f"Error: File '{filename}' does not exist") + return -1 + + print(f"Importing database records from '{filename}'") + + cmd = f'loaddata {filename}' + + manage(c, cmd, pty=True) + +@task +def import_fixtures(c): + """ + Import fixture data into the database. + + This command imports all existing test fixture data into the database. + + Warning: + - Intended for testing / development only! + - Running this command may overwrite existing database data!! + - Don't say you were not warned... + """ + + fixtures = [ + # Build model + 'build', + + # Company model + 'company', + 'price_breaks', + 'supplier_part', + + # Order model + 'order', + + # Part model + 'bom', + 'category', + 'params', + 'part', + 'test_templates', + + # Stock model + 'location', + 'stock_tests', + 'stock', + ] + + command = 'loaddata ' + ' '.join(fixtures) + + manage(c, command, pty=True) + @task def backup(c): """