projects
/
zzz-pokedex.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Add a mapped_classes list, and a translation_classes list to each mapped class
[zzz-pokedex.git]
/
pokedex
/
tests
/
test_schema.py
diff --git
a/pokedex/tests/test_schema.py
b/pokedex/tests/test_schema.py
index
c050c87
..
88ee1e5
100644
(file)
--- a/
pokedex/tests/test_schema.py
+++ b/
pokedex/tests/test_schema.py
@@
-3,9
+3,11
@@
from nose.tools import *
import unittest
from sqlalchemy import Column, Integer, String, create_engine
from sqlalchemy.orm import class_mapper, joinedload, sessionmaker
import unittest
from sqlalchemy import Column, Integer, String, create_engine
from sqlalchemy.orm import class_mapper, joinedload, sessionmaker
+from sqlalchemy.orm.session import Session
from sqlalchemy.ext.declarative import declarative_base
from pokedex.db import tables, markdown
from sqlalchemy.ext.declarative import declarative_base
from pokedex.db import tables, markdown
+from pokedex.db.multilang import create_translation_table
def test_variable_names():
"""We want pokedex.db.tables to export tables using the class name"""
def test_variable_names():
"""We want pokedex.db.tables to export tables using the class name"""
@@
-30,7
+32,7
@@
def test_i18n_table_creation():
various proxies and columns works.
"""
Base = declarative_base()
various proxies and columns works.
"""
Base = declarative_base()
- engine = create_engine("sqlite:///:memory:")
+ engine = create_engine("sqlite:///:memory:"
, echo=True
)
Base.metadata.bind = engine
Base.metadata.bind = engine
@@
-45,26
+47,29
@@
def test_i18n_table_creation():
__singlename__ = 'foo'
id = Column(Integer, primary_key=True, nullable=False)
__singlename__ = 'foo'
id = Column(Integer, primary_key=True, nullable=False)
- FooText = tables.makeTextTable(
- foreign_table_class=Foo,
- table_suffix_plural='blorp',
- table_suffix_singular='klink',
- columns=[
- ('name', 'names', Column(String(100))),
- ],
- lazy='select',
- Language=Language,
+ FooText = create_translation_table('foo_text', Foo,
+ language_class=Language,
+ name = Column(String(100)),
)
)
+ class FauxSession(Session):
+ def execute(self, clause, params=None, *args, **kwargs):
+ if not params:
+ params = {}
+ params.setdefault('_default_language', 'en')
+ return super(FauxSession, self).execute(clause, params, *args, **kwargs)
+
# OK, create all the tables and gimme a session
Base.metadata.create_all()
# OK, create all the tables and gimme a session
Base.metadata.create_all()
- sess = sessionmaker(engine)()
+ sess = sessionmaker(engine
, class_=FauxSession
)()
# Create some languages and foos to bind together
lang_en = Language(identifier='en')
sess.add(lang_en)
lang_jp = Language(identifier='jp')
sess.add(lang_jp)
# Create some languages and foos to bind together
lang_en = Language(identifier='en')
sess.add(lang_en)
lang_jp = Language(identifier='jp')
sess.add(lang_jp)
+ lang_ru = Language(identifier='ru')
+ sess.add(lang_ru)
foo = Foo()
sess.add(foo)
foo = Foo()
sess.add(foo)
@@
-89,46
+94,57
@@
def test_i18n_table_creation():
sess.commit()
### Test 1: re-fetch foo and check its attributes
sess.commit()
### Test 1: re-fetch foo and check its attributes
- foo = sess.query(Foo).one()
+ foo = sess.query(Foo).
params(_default_language='en').
one()
# Dictionary of language identifiers => names
# Dictionary of language identifiers => names
- assert foo.name
s['en'
] == 'english'
- assert foo.name
s['jp'
] == 'nihongo'
+ assert foo.name
_map[lang_en
] == 'english'
+ assert foo.name
_map[lang_jp
] == 'nihongo'
# Default language, currently English
assert foo.name == 'english'
sess.expire_all()
# Default language, currently English
assert foo.name == 'english'
sess.expire_all()
- ### Test 2: joinedload on the default name should appear to work
+ ### Test 2: querying by default language name should work
+ foo = sess.query(Foo).filter_by(name='english').one()
+
+ assert foo.name == 'english'
+
+ sess.expire_all()
+
+ ### Test 3: joinedload on the default name should appear to work
+ # THIS SHOULD WORK SOMEDAY
+ # .options(joinedload(Foo.name)) \
foo = sess.query(Foo) \
foo = sess.query(Foo) \
- .options(joinedload(Foo.
name
)) \
- .
first
+ .options(joinedload(Foo.
foo_text_local
)) \
+ .
one()
assert foo.name == 'english'
sess.expire_all()
assert foo.name == 'english'
sess.expire_all()
- ### Test 3: joinedload on all the names should appear to work
+ ### Test 4: joinedload on all the names should appear to work
+ # THIS SHOULD ALSO WORK SOMEDAY
+ # .options(joinedload(Foo.name_map)) \
foo = sess.query(Foo) \
foo = sess.query(Foo) \
- .options(joinedload(Foo.
names
)) \
- .
first
+ .options(joinedload(Foo.
foo_text
)) \
+ .
one()
- assert foo.name
s['en'
] == 'english'
- assert foo.name
s['jp'
] == 'nihongo'
+ assert foo.name
_map[lang_en
] == 'english'
+ assert foo.name
_map[lang_jp
] == 'nihongo'
sess.expire_all()
sess.expire_all()
- ### Test
4
: Mutating the dict collection should work
- foo = sess.query(Foo).
first
+ ### Test
5
: Mutating the dict collection should work
+ foo = sess.query(Foo).
one()
- foo.name
s['en'
] = 'different english'
- del foo.names['jp']
+ foo.name
_map[lang_en
] = 'different english'
+ foo.name_map[lang_ru] = 'new russian'
sess.commit()
sess.commit()
- assert foo.name
s['en'
] == 'different english'
- assert
'jp' not in foo.names
+ assert foo.name
_map[lang_en
] == 'different english'
+ assert
foo.name_map[lang_ru] == 'new russian'
def test_texts():
"""Check DB schema for integrity of text columns & translations.
def test_texts():
"""Check DB schema for integrity of text columns & translations.