-
Notifications
You must be signed in to change notification settings - Fork 19
Compare Enum types #11
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
Changes from all commits
1f1212c
7c5505c
8c59af8
0f8e69c
b16a525
411c8f8
b67fa24
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
""" | ||
Adapt Enum across versions of SQLAlchemy. | ||
|
||
SQLAlchemy supports PEP 435 Enum classes as of 1.1. | ||
Prior versions supported only the values as strings. | ||
|
||
Export a suitable column type for either case. | ||
""" | ||
import enum | ||
import sqlalchemy | ||
|
||
|
||
def Enum(*enums, **kw): | ||
if sqlalchemy.__version__ >= '1.1': | ||
return sqlalchemy.Enum(*enums, **kw) | ||
|
||
if len(enums) == 1 and issubclass(enums[0], enum.Enum): | ||
return sqlalchemy.Enum(*(v.name for v in enums[0]), **kw) | ||
|
||
return sqlalchemy.Enum(*enums, **kw) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
import json | ||
|
||
import pytest | ||
from sqlalchemy import create_engine | ||
|
||
from sqlalchemydiff.comparer import compare | ||
from sqlalchemydiff.util import ( | ||
|
@@ -108,6 +109,39 @@ def test_errors_dict_catches_all_differences(uri_left, uri_right): | |
} | ||
}, | ||
'employees': { | ||
'columns': { | ||
'diff': [ | ||
{ | ||
'key': 'polarity', | ||
'left': { | ||
'default': None, | ||
'name': 'polarity', | ||
'nullable': True, | ||
'type': "ENUM('NEGATIVE','POSITIVE')"}, | ||
'right': { | ||
'default': None, | ||
'name': 'polarity', | ||
'nullable': True, | ||
'type': "ENUM('NEG','POS')" | ||
} | ||
}, | ||
{ | ||
'key': 'spin', | ||
'left': { | ||
'default': None, | ||
'name': 'spin', | ||
'nullable': True, | ||
'type': 'VARCHAR(9)' | ||
}, | ||
'right': { | ||
'default': None, | ||
'name': 'spin', | ||
'nullable': True, | ||
'type': 'VARCHAR(4)' | ||
} | ||
} | ||
] | ||
}, | ||
'foreign_keys': { | ||
'left_only': [ | ||
{ | ||
|
@@ -215,12 +249,27 @@ def test_errors_dict_catches_all_differences(uri_left, uri_right): | |
} | ||
} | ||
}, | ||
'enums': { | ||
}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm seeing an error when running this test:
It looks like we are now getting an extra There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (TIL) Comment support is new in SQLAlchemy 1.2.0: http://docs.sqlalchemy.org/en/latest/changelog/migration_12.html#change-1546 The change adds a I think the most straightforward way to go here is to key off that attribute (e.g., Perhaps, though, That selectivity should also help make the tests work for a dialect other than mysql (e.g., postgresql), though left_only and right_only would still fail without further work. Your thoughts? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree it would be better to be more selective in the assertions rather than comparing the whole result. But if that's too big a change, your first suggestion will do the job well enough for now. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For now, I've implemented the simpler change. I didn't want the more-directed tests to hold up this PR. |
||
'uris': { | ||
'left': uri_left, | ||
'right': uri_right, | ||
} | ||
} | ||
|
||
engine = create_engine(uri_left) | ||
dialect = engine.dialect | ||
if getattr(dialect, 'supports_comments', False): | ||
# sqlalchemy 1.2.0 adds support for SQL comments | ||
# expect them in the errors when supported | ||
for table in expected_errors['tables_data'].values(): | ||
for column in table['columns']['diff']: | ||
for side in ['left', 'right']: | ||
column[side].update(comment=None) | ||
for side in ['left_only', 'right_only']: | ||
for column in table['columns'].get(side, []): | ||
column.update(comment=None) | ||
|
||
assert not result.is_match | ||
|
||
compare_error_dicts(expected_errors, result.errors) | ||
|
@@ -297,8 +346,11 @@ def test_ignores(uri_left, uri_right): | |
ignores = [ | ||
'mobile_numbers', | ||
'phone_numbers', | ||
'*.enum.polarity', | ||
'companies.col.name', | ||
'companies.idx.name', | ||
'employees.col.polarity', | ||
'employees.col.spin', | ||
'employees.fk.fk_employees_companies', | ||
'employees.fk.fk_emp_comp', | ||
'employees.idx.ix_employees_name', | ||
|
@@ -328,8 +380,11 @@ def test_ignores_alternative_sep(uri_left, uri_right): | |
ignores = [ | ||
'mobile_numbers', | ||
'phone_numbers', | ||
'*#enum#polarity', | ||
'companies#col#name', | ||
'companies#idx#name', | ||
'employees#col#polarity', | ||
'employees#col#spin', | ||
'employees#fk#fk_employees_companies', | ||
'employees#fk#fk_emp_comp', | ||
'employees#idx#ix_employees_name', | ||
|
@@ -353,6 +408,7 @@ def test_ignores_alternative_sep(uri_left, uri_right): | |
@pytest.mark.parametrize('missing_ignore', [ | ||
'mobile_numbers', | ||
'phone_numbers', | ||
'*.enum.polarity', | ||
'companies.col.name', | ||
'companies.idx.name', | ||
'employees.fk.fk_employees_companies', | ||
|
@@ -375,6 +431,7 @@ def test_ignores_all_needed(uri_left, uri_right, missing_ignore): | |
ignores = [ | ||
'mobile_numbers', | ||
'phone_numbers', | ||
'*.enum.polarity', | ||
'companies.col.name', | ||
'companies.idx.name', | ||
'employees.fk.fk_employees_companies', | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is new in sqlalchemy 1.1.0 - could you add the
sqlalchemy>=1.1.0
requirement to setup.py?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe, instead, catch
AttributeError
alongsideNotImplementedError
and return the same empty list? (A comment mentioning 1.1.0, similar to the one you request below forget_enums
, would make sense.)I think better to leave the sqlalchemy version unconstrained if doing so requires so little.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah yes - good idea! - let's do the error catching instead.