-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 Fix orm for tables in directories from kabulov…
…/fix_orm_for_tables_in_directories_2
- Loading branch information
Showing
2 changed files
with
72 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import pytest | ||
import sqlalchemy as sa | ||
from types import MethodType | ||
from sqlalchemy import Column, Integer, Unicode | ||
from sqlalchemy.orm import declarative_base, sessionmaker | ||
from sqlalchemy.testing.fixtures import TablesTest, config | ||
|
||
|
||
class TestDirectories(TablesTest): | ||
__backend__ = True | ||
|
||
def prepare_table(self, engine): | ||
base = declarative_base() | ||
|
||
class Table(base): | ||
__tablename__ = "dir/test" | ||
id = Column(Integer, primary_key=True) | ||
text = Column(Unicode) | ||
|
||
base.metadata.create_all(engine) | ||
session = sessionmaker(bind=engine)() | ||
session.add(Table(id=2, text="foo")) | ||
session.commit() | ||
return base, Table, session | ||
|
||
def try_update(self, session, Table): | ||
row = session.query(Table).first() | ||
row.text = "bar" | ||
session.commit() | ||
return row | ||
|
||
def drop_table(self, base, engine): | ||
base.metadata.drop_all(engine) | ||
|
||
def bind_old_method_to_dialect(self, dialect): | ||
def _handle_column_name(self, variable): | ||
return variable | ||
|
||
dialect._handle_column_name = MethodType(_handle_column_name, dialect) | ||
|
||
def test_directories(self): | ||
engine_good = sa.create_engine(config.db_url) | ||
base, Table, session = self.prepare_table(engine_good) | ||
row = self.try_update(session, Table) | ||
assert row.id == 2 | ||
assert row.text == "bar" | ||
self.drop_table(base, engine_good) | ||
|
||
engine_bad = sa.create_engine(config.db_url) | ||
self.bind_old_method_to_dialect(engine_bad.dialect) | ||
base, Table, session = self.prepare_table(engine_bad) | ||
with pytest.raises(Exception) as excinfo: | ||
self.try_update(session, Table) | ||
assert "Unknown name: $dir" in str(excinfo.value) | ||
self.drop_table(base, engine_bad) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters