Remove OfficiallyNamed and UnofficiallyNamed.
[zzz-pokedex.git] / pokedex / tests / test_schema.py
1 # encoding: utf8
2 from nose.tools import *
3 import unittest
4 from sqlalchemy.orm import class_mapper
5
6 from pokedex.db import tables, markdown
7
8 def test_variable_names():
9 """We want pokedex.db.tables to export tables using the class name"""
10 for varname in dir(tables):
11 if not varname[0].isupper():
12 continue
13 table = getattr(tables, varname)
14 try:
15 if not issubclass(table, tables.TableBase) or table is tables.TableBase:
16 continue
17 except TypeError:
18 continue
19 classname = table.__name__
20 if classname and varname[0].isupper():
21 assert varname == classname, '%s refers to %s' % (varname, classname)
22 for table in tables.table_classes:
23 assert getattr(tables, table.__name__) is table
24
25 def test_texts():
26 """Check DB schema for integrity of text columns & translations.
27
28 Mostly protects against copy/paste oversights and rebase hiccups.
29 If there's a reason to relax the tests, do it
30 """
31 for table in sorted(tables.table_classes, key=lambda t: t.__name__):
32 if issubclass(table, tables.LanguageSpecific):
33 good_formats = 'markdown plaintext gametext'.split()
34 assert_text = '%s is language-specific'
35 else:
36 good_formats = 'identifier latex'.split()
37 assert_text = '%s is not language-specific'
38 mapper = class_mapper(table)
39 for column in sorted(mapper.c, key=lambda c: c.name):
40 format = column.info.get('format', None)
41 if format is not None:
42 if format not in good_formats:
43 raise AssertionError(assert_text % column)
44 is_markdown = isinstance(column.type, markdown.MarkdownColumn)
45 if is_markdown != (format == 'markdown'):
46 raise AssertionError('%s: markdown format/column type mismatch' % column)
47 if (format != 'identifier') and (column.name == 'identifier'):
48 raise AssertionError('%s: identifier column name/type mismatch' % column)
49 if column.info.get('official', None) and format not in 'gametext plaintext':
50 raise AssertionError('%s: official text with bad format' % column)
51 else:
52 if isinstance(column.type, (markdown.MarkdownColumn, tables.Unicode)):
53 raise AssertionError('%s: text column without format' % column)
54 if column.name == 'name' and format != 'plaintext':
55 raise AssertionError('%s: non-plaintext name' % column)
56 # No mention of English in the description
57 assert 'English' not in column.info['description'], column
58
59 def test_identifiers_with_names():
60 """Test that named tables have identifiers, and non-named tables don't
61
62 ...have either names or identifiers.
63 """
64 for table in sorted(tables.table_classes, key=lambda t: t.__name__):
65 if issubclass(table, tables.Named):
66 assert issubclass(table, tables.OfficiallyNamed) or issubclass(table, tables.UnofficiallyNamed), table
67 assert hasattr(table, 'identifier'), table
68 else:
69 assert not hasattr(table, 'identifier'), table
70 if not issubclass(table, tables.LanguageSpecific):
71 assert not hasattr(table, 'name'), table