Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User Management backend - Base - DB migrations #46

Open
wants to merge 14 commits into
base: master
Choose a base branch
from

Conversation

ahinoamta
Copy link
Collaborator

fixes #27.
This PR depends on PR #38.
Added Flask-Migrate extension for database migrations support.
The content inside edison/migrations folder is auto-created by the extension.

@DoRTaL94 @RoniRush

DoRTaL94 and others added 8 commits April 25, 2020 19:18
Added Flask-Migrate extension for database migrations support.
The content inside edison/migrations folder is auto-created by the extension.
Added Flask-Migrate extension for database migrations support.
The content inside edison/migrations folder is auto-created by the extension.
…to db-migrations

# Conflicts:
#	requirements.txt
@ahinoamta ahinoamta marked this pull request as draft May 19, 2020 09:00
("ahinoam",))

@pytest.fixture
def reset_users_primary_key(cursor):
Copy link
Collaborator Author

@ahinoamta ahinoamta May 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @ifireball 😄
I wrote a few tests for checking the DB like you asked me in the last PR.
Can you please help me to find a way to reset the primary key sequence after the tests will be finished?
I tried to enter this fixture in the 'cursor' fixture (right after the 'yield'), but it didn't work, I assume it happened because after the 'yield' the cursor is not available anymore?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why would you want to reset the primary key sequence? It should have no special meaning, so it won't matter if the first user is 1 or 140

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In any case, probably the reason it didn't reset your sequence is because you didn't commit the transaction.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One last comment. The tests that you wrote here verify basic database functionality, which doesn't need to be tested.
What you should probably test is:

  • the database exists
  • you can connect to it
  • the tables you created exist.

Any other tests are really redundant.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. I want to reset the primary key in order to clean up my database after the tests and return it to the initial state. I think it will make more sense when the database show you data that begins with id 1.
  2. About the commit after the execute, I used the “cursor.execute” without commit after it but still the query was executed. So why in this case I should add the commit?
    Furthermore, I don’t know where to call this fixture, because if I inject it to “cursor” it will activate it in the beginning of the “cursor” fixture, no?
  3. Ack

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 - the alter sequence statement is correct and it should work. Try to remember, in the lifetime of an app the database sequence will very rarely stay sequential.
2 - Have you tried executing this code and then going into the database using psql and seeing if the data you added was still there? As far as i know pyscopg does not have automated commits. One possibly confusing thing about sequences and transactions is when you get a new sequence that ignores the transaction, so if you insert and then rollback the transaction, the sequence is still at the next number.
On line 20 you have db_connection.rollback() when is this code called? A rollback means erase everything that I've done during this current transaction.

Copy link
Collaborator Author

@ahinoamta ahinoamta May 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you tried executing this code and then going into the database using psql and seeing if the data you added was still there?

I thought that cursor.execute is enough because when I ran the test def test_username(cursor, build_users_table) it was passed, so the row 'ahinoam' did enter to the DB.
But when I checked it in the DB itself with psql, it didn't appear there (and it's not because db_connection.rollback() because I put it in a comment).
Do you have any idea what is the reason for that?
Maybe it's because I remain in the same transaction?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On line 20 you have db_connection.rollback() when is this code called? A rollback means erase everything that I've done during this current transaction.

I want it to be called after the cursor will finish his job every test, therefore I put db_connection.rollback() right after the yield cursor statement. But I have to be honest I don't understand the yield statements into their depth...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way the database works is that during the same transaction everything looks like it is saved, but it really isn't until you call commit.
If you don't manually call commit, it is the same as calling rollback.

@ahinoamta ahinoamta marked this pull request as ready for review May 19, 2020 17:21
("ahinoam",))

@pytest.fixture
def reset_users_primary_key(cursor):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why would you want to reset the primary key sequence? It should have no special meaning, so it won't matter if the first user is 1 or 140

("ahinoam",))

@pytest.fixture
def reset_users_primary_key(cursor):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In any case, probably the reason it didn't reset your sequence is because you didn't commit the transaction.

cursor.execute("""
INSERT INTO users (username) VALUES (%s);
""",
("ahinoam",))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume you don't want to save this data because its a test. If you did, you would need to call commit

@@ -0,0 +1,33 @@
import pytest
import psycopg2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might want to use sqlalchemy to test that the database was created properly, so that your tests are uniform with your code. Either way works the same, one difference is if you switch database engines (one of the benefits of using an ORM) you would have to rewrite this code.

.gitignore Show resolved Hide resolved
tests/db_test.py Outdated
import psycopg2


def test_users_table_rows_count(cursor, build_users_table):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is this cursor defined? The only place I see it is in conftest.py and this script doesn't import that one.
It also might make sense to have one database test file and not split it up.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, if I understood the code in the conftest.py correctly, you always call rollback, which erases all of your changes. So if you are expecting the data to be here because of that execution, it might not be.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is this cursor defined? The only place I see it is in conftest.py and this script doesn't import that one.
It also might make sense to have one database test file and not split it up.

The cursor is defined in conftest.py file that aggregate all the fixtures in one place. I tried to keep the db_test.py file short.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, if I understood the code in the conftest.py correctly, you always call rollback, which erases all of your changes. So if you are expecting the data to be here because of that execution, it might not be.

I call rollback after the cursor finish his job so the data still in the DB in this point. This test was passed.

("ahinoam",))

@pytest.fixture
def reset_users_primary_key(cursor):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One last comment. The tests that you wrote here verify basic database functionality, which doesn't need to be tested.
What you should probably test is:

  • the database exists
  • you can connect to it
  • the tables you created exist.

Any other tests are really redundant.

assert database_exists(db_uri) is True

def test_db_connection(engine):
assert engine.connect() is not None
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@simzacks I'm not sure with this db connection check, I tried to find something more correct but without success

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ahinoamta what isn't working with this code?
IIUC, you are:

  • connecting to the database, which verifies that it exists in the location/port that you are expecting it to be
  • verifying that the expected tables exist.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is working, but don't you think the check needs to be more specific and not just is not None?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 questions

  1. Is there ever a time that you call database_exists and it returns true, but engine.connect would fail?
  2. If you could not connect to the engine, what would engine.connect return?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@simzacks

  1. I think there is no such case, I googled it and I didn't find any other case that can cause failure to engine.connect. I did find that the engine provides the Connection from the connection pool but I'm not sure it can cause connection failures (assuming the DB is up).

  2. If the database doesn't exist, so I won't be able to connect to the engine, it will throw an exception- 'sqlalchemy.exc.OperationalError'.

@ahinoamta ahinoamta requested a review from simzacks May 22, 2020 09:14
tests/db_test.py Outdated

def test_db_tables_exist(engine):
tables = sqlalchemy.inspect(engine).get_table_names()
assert tables == ['users']
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assumedly, tables will return a list. you might want to try:
assert "users" in tables (if there are more tables you want to test) and "..." in tables

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack

@DoRTaL94 DoRTaL94 requested a review from simzacks May 30, 2020 11:10
@ahinoamta ahinoamta requested a review from ifireball May 31, 2020 20:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

User Management backend - Base
4 participants