Skip to content

Commit

Permalink
Use Postgres database for unit testing (#2920)
Browse files Browse the repository at this point in the history
* Use postgres for testing on Travis

* fixed tests

1. Removed get_api_400 test as it is no longer valid now that postgres db is used.
2. Fixed tests that were wrong the very start. Thanks to SQLite, they were able to get away.

* updated README with new test instructions

* fixed robot tests by re-initializing db after unittests
  • Loading branch information
aviaryan authored and niranjan94 committed Jan 11, 2017
1 parent b5c68ac commit d10dcfd
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 82 deletions.
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ before_script:
# command to run tests
script:
- nosetests tests/unittests -v --with-coverage
# re-init db
- psql -c 'drop database if exists test;' -U postgres
- psql -c 'create database test;' -U postgres
- python manage.py initialize_db -c open_event_test_user@fossasia.org:fossasia
- python manage.py runserver &
# robot
- robot tests/robot
# upload coverage
after_success:
Expand Down
26 changes: 17 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ OAuth is used to get information from Facebook and Google accounts, that enables

#### Twitter

Twitter feed integration is provided in the public event pages.
Twitter feed integration is provided in the public event pages.

Required keys can be obtained from [https://dev.twitter.com/overview/documentation](https://dev.twitter.com/overview/documentation)

#### Instagram

It is possible to extend the functionality and offer images from Instagram in the event service.
It is possible to extend the functionality and offer images from Instagram in the event service.

Required keys can be obtained from [https://www.instagram.com/developer/authentication/](https://www.instagram.com/developer/authentication/).

Expand All @@ -81,7 +81,7 @@ Required keys can be obtained from [https://maps.googleapis.com/maps/api](https:
#### Media Storage - Local/Amazon S3/Google Cloud

Media (like audio, avatars and logos) can be stored either Locally or on Amazon S3 or on Google Storage.

1. [Amazon S3 Setup Instructions](/docs/AMAZON_S3.md)
1. [Google Cloud Setup Instructions](https://cloud.google.com/storage/docs/migrating#defaultproj)

Expand Down Expand Up @@ -137,10 +137,10 @@ The event data and the sessions can be exported in various formats.

## Roles

The system has two kind of role type.
The system has two kind of role type.

1. System roles are related to the Open Event organization and operator of the application.
2. Event Roles are related to the users of the system with their different permissions.
1. System roles are related to the Open Event organization and operator of the application.
2. Event Roles are related to the users of the system with their different permissions.

Read more [here](/docs/ROLES.md).

Expand Down Expand Up @@ -181,13 +181,21 @@ pip install -r requirements/tests.txt

#### Running unit tests

* Go to the project directory and run the following command:
* Open Event uses Postgres database for testing. So set `DATABASE_URL` as a postgres database. Here is an example.

```sh
export DATABASE_URL=postgresql://test_user:test@127.0.0.1:5432/opev_test
# format is postgresql://USERNAME:PASSWORD@ADDRESS/DATABASE_NAME
export APP_CONFIG=config.TestingConfig
```

* Then go to the project directory and run the following command:
```
python -m unittest discover tests/unittests/
```
* It will run each test one by one.

* You can also use the following command to run tests using nosetests :
* You can also use the following command to run tests using nosetests:
```
nosetests tests/unittests/
```
Expand Down Expand Up @@ -219,7 +227,7 @@ Open Event is being translated using Weblate, a web tool designed to ease transl

If you would like to contribute to translation of Open Event, you need to [register on this server](https://hosted.weblate.org/accounts/register/).

Once you have activated your account just proceed to the [translation section](https://hosted.weblate.org/projects/open-event/).
Once you have activated your account just proceed to the [translation section](https://hosted.weblate.org/projects/open-event/).


## Contributions, Bug Reports, Feature Requests
Expand Down
65 changes: 0 additions & 65 deletions tests/unittests/api/test_get_api_400.py

This file was deleted.

6 changes: 5 additions & 1 deletion tests/unittests/functionality/test_microlocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ def setUp(self):
self.app = Setup.create_app()

def test_add_microlocation_to_db(self):
microlocation = ObjectMother.get_microlocation()
with app.test_request_context():
# create event
event = ObjectMother.get_event()
save_to_db(event, 'Event saved')
# test
microlocation = ObjectMother.get_microlocation()
save_to_db(microlocation, "Microlocation saved")
self.assertEqual(microlocation.id, 1)
self.assertEqual(microlocation.event_id, 1)
Expand Down
7 changes: 6 additions & 1 deletion tests/unittests/functionality/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@
class TestSessionApi(OpenEventViewTestCase):

def test_add_session_to_db(self):
session = ObjectMother.get_session()
with app.test_request_context():
# create event
event = ObjectMother.get_event()
save_to_db(event, 'Event saved')
# test
session = ObjectMother.get_session()
save_to_db(session, "Session saved")
self.assertEqual(session.id, 1)
self.assertEqual(session.event_id, 1)


if __name__ == '__main__':
unittest.main()
10 changes: 8 additions & 2 deletions tests/unittests/setup_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def create_app():
app.config['BROKER_BACKEND'] = 'memory'
# app.config['CELERY_BROKER_URL'] = ''
# app.config['CELERY_RESULT_BACKEND'] = ''
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(_basedir, 'test.db')
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL', 'sqlite:///' + os.path.join(_basedir, 'test.db'))
app.secret_key = 'super secret key'
app.logger.addHandler(logging.StreamHandler(sys.stdout))
app.logger.setLevel(logging.ERROR)
Expand All @@ -39,4 +39,10 @@ def create_app():
def drop_db():
with app.test_request_context():
db.session.remove()
db.drop_all()
if app.config['SQLALCHEMY_DATABASE_URI'].find('postgresql://') > -1:
# drop_all has problems with foreign keys in postgres database (cyclic dependency)
db.engine.execute("drop schema if exists public cascade")
db.engine.execute("create schema public")
else:
# drop all works for SQLite and should work for other DBMS like MySQL, Mongo etc
db.drop_all()
9 changes: 5 additions & 4 deletions tests/unittests/test_db_performance.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ def test_db_events(self):

def test_db_sessions(self):
with app.test_request_context():
# create event
event = ObjectMother.get_event()
db.session.add(event)
db.session.commit()
# test
for i in range(1, 10000):
session = ObjectMother.get_session()
session.name = 'Session' + str(i)
Expand Down Expand Up @@ -83,10 +88,6 @@ def test_db_users(self):
query.statement, query.parameters, query.duration, query.context))
text_file.write("\n")

def tearDown(self):
with app.test_request_context():
db.drop_all()


if __name__ == '__main__':
unittest.main()
2 changes: 2 additions & 0 deletions tests/unittests/views/admin/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ def test_session_reject(self):

def test_session_delete(self):
with app.test_request_context():
event = ObjectMother.get_event()
save_to_db(event, "Event Saved")
session = ObjectMother.get_session()
save_to_db(session, "Session Saved")
url = url_for('event_sessions.delete_session', event_id=1, session_id=session.id)
Expand Down

0 comments on commit d10dcfd

Please sign in to comment.