Added unit tests for backend transtions.

This commit is contained in:
Martin Edenhofer 2013-01-04 14:14:20 +01:00
parent 14c63c0383
commit 237df49932
2 changed files with 47 additions and 0 deletions

View file

@ -1,6 +1,19 @@
class Translation < ApplicationModel
before_create :set_initial
def self.translate(locale, string)
# translate string
record = Translation.where( :locale => locale, :source => string ).first
return record.target if record
# fallback lookup in en
record = Translation.where( :locale => 'en', :source => string ).first
return record.target if record
return string
end
private
def set_initial
self.target_initial = self.target

View file

@ -0,0 +1,34 @@
# encoding: utf-8
require 'test_helper'
class TranslationTest < ActiveSupport::TestCase
test 'translation' do
tests = [
# test 1
{
:lang => 'en',
:string => 'New',
:result => 'New',
},
# test 2
{
:lang => 'de',
:string => 'New',
:result => 'Neu',
},
# test 3
{
:lang => 'de',
:string => 'not translated - lalala',
:result => 'not translated - lalala',
},
]
tests.each { |test|
result = Translation.translate( test[:lang], test[:string] )
assert_equal( result, test[:result], "verify result" )
}
end
end