2 from nose
.tools
import *
4 from sqlalchemy
import Column
, Integer
, String
, create_engine
5 from sqlalchemy
.orm
import class_mapper
, joinedload
, sessionmaker
6 from sqlalchemy
.orm
.session
import Session
7 from sqlalchemy
.ext
.declarative
import declarative_base
9 from pokedex
.db
import tables
, markdown
10 from pokedex
.db
.multilang
import MultilangScopedSession
, MultilangSession
, \
11 create_translation_table
13 def test_variable_names():
14 """We want pokedex.db.tables to export tables using the class name"""
15 for varname
in dir(tables
):
16 if not varname
[0].isupper():
18 table
= getattr(tables
, varname
)
20 if not issubclass(table
, tables
.TableBase
) or table
is tables
.TableBase
:
24 classname
= table
.__name__
25 if classname
and varname
[0].isupper():
26 assert varname
== classname
, '%s refers to %s' %
(varname
, classname
)
27 for table
in tables
.mapped_classes
:
28 assert getattr(tables
, table
.__name__
) is table
30 def test_class_order():
31 """The declarative classes should be defined in alphabetical order.
32 Except for Language which should be first.
34 class_names
= [table
.__name__
for table
in tables
.mapped_classes
]
36 return name
!= 'Language', name
37 print [(a
,b
) for (a
,b
) in zip(class_names
, sorted(class_names
, key
=key
)) if a
!=b
]
38 assert class_names
== sorted(class_names
, key
=key
)
40 def test_i18n_table_creation():
41 """Creates and manipulates a magical i18n table, completely independent of
42 the existing schema and data. Makes sure that the expected behavior of the
43 various proxies and columns works.
45 Base
= declarative_base()
46 engine
= create_engine("sqlite:///:memory:", echo
=True)
48 Base
.metadata
.bind
= engine
50 # Need this for the foreign keys to work!
52 __tablename__
= 'languages'
53 id = Column(Integer
, primary_key
=True, nullable
=False)
54 identifier
= Column(String(2), nullable
=False, unique
=True)
57 __tablename__
= 'foos'
58 __singlename__
= 'foo'
59 id = Column(Integer
, primary_key
=True, nullable
=False)
60 translation_classes
= []
62 FooText
= create_translation_table('foo_text', Foo
, 'texts',
63 language_class
=Language
,
64 name
= Column(String(100)),
67 # OK, create all the tables and gimme a session
68 Base
.metadata
.create_all()
69 sm
= sessionmaker(class_
=MultilangSession
)
70 sess
= MultilangScopedSession(sm
)
72 # Create some languages and foos to bind together
73 lang_en
= Language(identifier
='en')
75 lang_jp
= Language(identifier
='jp')
77 lang_ru
= Language(identifier
='ru')
83 # Commit so the above get primary keys filled in
85 sess
.default_language
= lang_en
.id
87 # Give our foo some names, as directly as possible
89 foo_text
.foreign_id
= foo
.id
90 foo_text
.local_language_id
= lang_en
.id
91 foo_text
.name
= 'english'
95 foo_text
.foo_id
= foo
.id
96 foo_text
.local_language_id
= lang_jp
.id
97 foo_text
.name
= 'nihongo'
100 # Commit! This will expire all of the above.
103 ### Test 1: re-fetch foo and check its attributes
104 foo
= sess
.query(Foo
).params(_default_language
='en').one()
106 # Dictionary of language identifiers => names
107 assert foo
.name_map
[lang_en
] == 'english'
108 assert foo
.name_map
[lang_jp
] == 'nihongo'
110 # Default language, currently English
111 assert foo
.name
== 'english'
115 ### Test 2: querying by default language name should work
116 foo
= sess
.query(Foo
).filter_by(name
='english').one()
118 assert foo
.name
== 'english'
122 ### Test 3: joinedload on the default name should appear to work
123 # THIS SHOULD WORK SOMEDAY
124 # .options(joinedload(Foo.name)) \
125 foo
= sess
.query(Foo
) \
126 .options(joinedload(Foo
.texts_local
)) \
129 assert foo
.name
== 'english'
133 ### Test 4: joinedload on all the names should appear to work
134 # THIS SHOULD ALSO WORK SOMEDAY
135 # .options(joinedload(Foo.name_map)) \
136 foo
= sess
.query(Foo
) \
137 .options(joinedload(Foo
.texts
)) \
140 assert foo
.name_map
[lang_en
] == 'english'
141 assert foo
.name_map
[lang_jp
] == 'nihongo'
145 ### Test 5: Mutating the dict collection should work
146 foo
= sess
.query(Foo
).one()
148 foo
.name_map
[lang_en
] = 'different english'
149 foo
.name_map
[lang_ru
] = 'new russian'
153 assert foo
.name_map
[lang_en
] == 'different english'
154 assert foo
.name_map
[lang_ru
] == 'new russian'
157 """Check DB schema for integrity of text columns & translations.
159 Mostly protects against copy/paste oversights and rebase hiccups.
160 If there's a reason to relax the tests, do it
163 for cls
in tables
.mapped_classes
:
165 classes
+= cls
.translation_classes
167 if hasattr(cls
, 'local_language') or hasattr(cls
, 'language'):
168 good_formats
= 'markdown plaintext gametext'.split()
169 assert_text
= '%s is language-specific'
171 good_formats
= 'identifier latex'.split()
172 assert_text
= '%s is not language-specific'
173 columns
= sorted(cls
.__table__
.c
, key
=lambda c
: c
.name
)
174 for column
in columns
:
175 format
= column
.info
.get('format', None)
176 if format
is not None:
177 if format
not in good_formats
:
178 raise AssertionError(assert_text % column
)
179 is_markdown
= isinstance(column
.type, markdown
.MarkdownColumn
)
180 if is_markdown
!= (format
== 'markdown'):
181 raise AssertionError('%s: markdown format/column type mismatch' % column
)
182 if (format
!= 'identifier') and (column
.name
== 'identifier'):
183 raise AssertionError('%s: identifier column name/type mismatch' % column
)
184 if column
.info
.get('official', None) and format
not in 'gametext plaintext':
185 raise AssertionError('%s: official text with bad format' % column
)
187 if isinstance(column
.type, (markdown
.MarkdownColumn
, tables
.Unicode
)):
188 raise AssertionError('%s: text column without format' % column
)
189 if column
.name
== 'name' and format
!= 'plaintext':
190 raise AssertionError('%s: non-plaintext name' % column
)
191 # No mention of English in the description
192 assert 'English' not in column
.info
['description'], column
194 def test_identifiers_with_names():
195 """Test that named tables have identifiers, and non-named tables don't
197 ...have either names or identifiers.
199 for table
in sorted(tables
.mapped_classes
, key
=lambda t
: t
.__name__
):
200 if hasattr(table
, 'name'):
201 assert hasattr(table
, 'identifier'), table
203 assert not hasattr(table
, 'identifier'), table