Skip to content

Commit

Permalink
[#1808] Add paster create-test-data translations
Browse files Browse the repository at this point in the history
  • Loading branch information
Sean Hammond committed Feb 24, 2012
1 parent 520a81d commit 0209f98
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 101 deletions.
94 changes: 89 additions & 5 deletions ckan/lib/create_test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ class CreateTestData(cli.CkanCommand):
'''Create test data in the database.
Tests can also delete the created objects easily with the delete() method.
create-test-data - annakarenina and warandpeace
create-test-data search - realistic data to test search
create-test-data gov - government style data
create-test-data family - package relationships data
create-test-data user - create a user 'tester' with api key 'tester'
create-test-data - annakarenina and warandpeace
create-test-data search - realistic data to test search
create-test-data gov - government style data
create-test-data family - package relationships data
create-test-data user - create a user 'tester' with api key 'tester'
create-test-data translations - annakarenina, warandpeace, and some test
translations of terms
'''
summary = __doc__.split('\n')[0]
usage = __doc__
Expand Down Expand Up @@ -51,6 +53,8 @@ def command(self):
self.create_gov_test_data()
elif cmd == 'family':
self.create_family_test_data()
elif cmd == 'translations':
self.create_translations_test_data()
else:
print 'Command %s not recognized' % cmd
raise NotImplementedError
Expand Down Expand Up @@ -87,6 +91,44 @@ def create_test_user(cls):
model.Session.remove()
cls.user_refs.append(u'tester')

@classmethod
def create_translations_test_data(cls):
import ckan.model
CreateTestData.create()
rev = ckan.model.repo.new_revision()
rev.author = CreateTestData.author
rev.message = u'Creating test translations.'

sysadmin_user = ckan.model.User.get('testsysadmin')
package = ckan.model.Package.get('annakarenina')

# Add some new tags to the package.
# These tags are codes that are meant to be always translated before
# display, if not into the user's current language then into the
# fallback language.
package.add_tags([ckan.model.Tag('123'), ckan.model.Tag('456'),
ckan.model.Tag('789')])

# Add the above translations to CKAN.
for (lang_code, translations) in (('de', german_translations),
('fr', french_translations), ('en', english_translations)):
for term in terms:
if term in translations:
data_dict = {
'term': term,
'term_translation': translations[term],
'lang_code': lang_code,
}
context = {
'model': ckan.model,
'session': ckan.model.Session,
'user': sysadmin_user.name,
}
ckan.logic.action.update.term_translation_update(context,
data_dict)

ckan.model.Session.commit()

@classmethod
def create_arbitrary(cls, package_dicts, relationships=[],
extra_user_names=[], extra_group_names=[],
Expand Down Expand Up @@ -729,3 +771,45 @@ def get_all_data(cls):
}
}
]

# Some test terms and translations.
terms = ('A Novel By Tolstoy',
'Index of the novel',
'russian',
'tolstoy',
"Dave's books",
"Roger's books",
'Other (Open)',
'romantic novel',
'book',
'123',
'456',
'789',
)
english_translations = {
'123': 'jealousy',
'456': 'realism',
'789': 'hypocrisy',
}
german_translations = {
'A Novel By Tolstoy': 'Roman von Tolstoi',
'Index of the novel': 'Index des Romans',
'russian': 'Russisch',
'tolstoy': 'Tolstoi',
"Dave's books": 'Daves Bucher',
"Roger's books": 'Rogers Bucher',
'Other (Open)': 'Andere (Open)',
'romantic novel': 'Liebesroman',
'book': 'Buch',
'456': 'Realismus',
'789': 'Heuchelei',
}
french_translations = {
'A Novel By Tolstoy': 'A Novel par Tolstoi',
'Index of the novel': 'Indice du roman',
'russian': 'russe',
'romantic novel': 'roman romantique',
'book': 'livre',
'123': 'jalousie',
'789': 'hypocrisie',
}
Empty file.
105 changes: 9 additions & 96 deletions ckanext/multilingual/tests/test_multilingual_plugin.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import ckan.plugins
import ckan.lib.helpers
import ckan.lib.create_test_data
import ckan.logic.action.update
import ckan.tests.html_check
import routes
import paste.fixture
import pylons.test


class TestDatasetTermTranslation(ckan.tests.html_check.HtmlCheckMethods):
'''Test the translation of datasets by the multilingual_dataset plugin.
'''
@classmethod
def setup(cls):
ckan.tests.CreateTestData.create()
cls.normal_user = ckan.model.User.get('annafan')
cls.sysadmin_user = ckan.model.User.get('testsysadmin')
cls.app = paste.fixture.TestApp(pylons.test.pylonsapp)
ckan.plugins.load('multilingual_dataset')
ckan.lib.create_test_data.CreateTestData.create_translations_test_data()

@classmethod
def teardown(cls):
Expand All @@ -28,106 +26,21 @@ def test_dataset_view_translation(self):
multilingual_dataset plugin.
'''
# Get a package.
context = {
'model': ckan.model,
'session': ckan.model.Session,
'user': self.normal_user.name,
'allow_partial_update': True
}
package = ckan.logic.action.get.package_show(context,
{'id': "annakarenina"})

# Add some new tags to the package.
# These tags are codes that are meant to be always translated before
# display, if not into the user's current language then into the
# fallback language.
new_tag_list = package['tags'] + [
{'name': '123'},
{'name': '456'},
{'name': '789'},
]
data_dict = {
'id': package['id'],
'tags': new_tag_list
}
package = ckan.logic.action.update.package_update(context, data_dict)

# Test translations for some of the package's fields.
terms = ('A Novel By Tolstoy',
'Index of the novel',
'russian',
'tolstoy',
"Dave's books",
"Roger's books",
'Other (Open)',
'romantic novel',
'book',
'123',
'456',
'789',
)
english_translations = {
'123': 'jealousy',
'456': 'realism',
'789': 'hypocrisy',
}
german_translations = {
'A Novel By Tolstoy': 'Roman von Tolstoi',
'Index of the novel': 'Index des Romans',
'russian': 'Russisch',
'tolstoy': 'Tolstoi',
"Dave's books": 'Daves Bucher',
"Roger's books": 'Rogers Bucher',
'Other (Open)': 'Andere (Open)',
'romantic novel': 'Liebesroman',
'book': 'Buch',
'456': 'Realismus',
'789': 'Heuchelei',
}
french_translations = {
'A Novel By Tolstoy': 'A Novel par Tolstoi',
'Index of the novel': 'Indice du roman',
'russian': 'russe',
'romantic novel': 'roman romantique',
'book': 'livre',
'123': 'jalousie',
'789': 'hypocrisie',
}

# Use the term_translation_update API to add the above translations to
# CKAN.
for (lang_code, translations) in (('de', german_translations),
('fr', french_translations), ('en', english_translations)):
for term in terms:
if term in translations:
paramd = {
'term': term,
'term_translation': translations[term],
'lang_code': lang_code,
}
response = self.app.post(
'/api/action/term_translation_update',
params=ckan.lib.helpers.json.dumps(paramd),
extra_environ={
'Authorization': str(self.sysadmin_user.apikey)
},
status=200)
assert response.json['success'] is True

# Fetch the dataset view page for a number of different languages and
# test for the presence of translated and not translated terms.
offset = routes.url_for(controller='package', action='read',
id='annakarenina')
for (lang_code, translations) in (('de', german_translations),
('fr', french_translations), ('pl', {})):
for (lang_code, translations) in (
('de', ckan.lib.create_test_data.german_translations),
('fr', ckan.lib.create_test_data.french_translations), ('pl', {})):
response = self.app.get(offset, status=200,
extra_environ={'CKAN_LANG': lang_code,
'CKAN_CURRENT_URL': offset})
for term in terms:
for term in ckan.lib.create_test_data.terms:
if term in translations:
response.mustcontain(translations[term])
elif term in english_translations:
response.mustcontain(english_translations[term])
elif term in ckan.lib.create_test_data.english_translations:
response.mustcontain(
ckan.lib.create_test_data.english_translations[term])
else:
response.mustcontain(term)

0 comments on commit 0209f98

Please sign in to comment.