Browse Source

Moved translation tests to spec.

Martin Edenhofer 8 years ago
parent
commit
6dc1e9ae80
3 changed files with 127 additions and 123 deletions
  1. 4 0
      app/models/translation.rb
  2. 123 0
      spec/models/translation_spec.rb
  3. 0 123
      test/unit/translation_test.rb

+ 4 - 0
app/models/translation.rb

@@ -311,6 +311,10 @@ all:
             translation.save
           end
         else
+          if !UserInfo.current_user_id
+            translation_raw['updated_by_id'] = 1
+            translation_raw['created_by_id'] = 1
+          end
           Translation.create(translation_raw.symbolize_keys!)
         end
       }

+ 123 - 0
spec/models/translation_spec.rb

@@ -0,0 +1,123 @@
+require 'rails_helper'
+
+RSpec.describe Translation do
+
+  context 'default translations' do
+    Translation.reset('de-de')
+    Translation.sync('de-de')
+
+    it 'en with existing word' do
+      expect(Translation.translate('en', 'New')).to eq('New')
+    end
+
+    it 'en-us with existing word' do
+      expect(Translation.translate('en-us', 'New')).to eq('New')
+    end
+
+    it 'en with not existing word' do
+      expect(Translation.translate('en', 'Some Not Existing Word')).to eq('Some Not Existing Word')
+    end
+
+    it 'de-de with existing word' do
+      expect(Translation.translate('de-de', 'New')).to eq('Neu')
+    end
+
+    it 'de-de with existing word' do
+      expect(Translation.translate('de-de', 'Some Not Existing Word')).to eq('Some Not Existing Word')
+    end
+
+  end
+
+  context 'custom translation tests' do
+    Translation.where(locale: 'de-de').destroy_all
+    Translation.sync('de-de')
+
+    locale = 'de-de'
+
+    it 'cycle of change and reload translation' do
+
+      # check for non existing custom changes
+      list = Translation.lang(locale)
+      list['list'].each { |item|
+        translation = Translation.find_by(source: item[1], locale: locale)
+        expect(translation.class).to be(Translation)
+        expect(locale).to eq(translation.locale)
+        expect(translation.target).to eq(translation.target_initial)
+      }
+
+      # add custom changes
+      translation = Translation.find_by(locale: locale, source: 'open')
+      expect(translation.target).to eq('offen')
+      expect(translation.target_initial).to eq('offen')
+      translation.target = 'offen2'
+      translation.save!
+
+      list = Translation.lang(locale)
+      list['list'].each { |item|
+        translation = Translation.find_by(source: item[1], locale: locale)
+        expect(translation.class).to be(Translation)
+        expect(locale).to eq(translation.locale)
+        if translation.source == 'open'
+          expect(translation.target).to eq('offen2')
+          expect(translation.target_initial).to eq('offen')
+        else
+          expect(translation.target).to eq(translation.target_initial)
+        end
+      }
+
+      # check for existing custom changes after new translations are loaded
+      Translation.load(locale)
+      list = Translation.lang(locale)
+      list['list'].each { |item|
+        translation = Translation.find_by(source: item[1], locale: locale)
+        expect(translation.class).to be(Translation)
+        expect(locale).to eq(translation.locale)
+        if translation.source == 'open'
+          expect(translation.target).to eq('offen2')
+          expect(translation.target_initial).to eq('offen')
+        else
+          expect(translation.target).to eq(translation.target_initial)
+        end
+      }
+
+      # reset custom translations and check for non existing custom changes
+      Translation.reset(locale)
+      list = Translation.lang(locale)
+      list['list'].each { |item|
+        translation = Translation.find_by(source: item[1], locale: locale)
+        expect(translation.class).to be(Translation)
+        expect(locale).to eq(translation.locale)
+        expect(translation.target).to eq(translation.target_initial)
+      }
+    end
+
+  end
+
+  context 'file based import' do
+
+    it 'check download of locales' do
+      directory = Rails.root.join('config')
+      file = Rails.root.join("#{directory}/locales.yml")
+      if File.exist?(file)
+        File.delete(file)
+      end
+      expect(File.exist?(file)).to be false
+      Locale.fetch
+      expect(File.exist?(file)).to be true
+    end
+
+    it 'check download of translations' do
+      locale = 'de-de'
+      directory = Rails.root.join('config/translations')
+      if File.directory?(directory)
+        FileUtils.rm_rf(directory)
+      end
+      file = Rails.root.join("#{directory}/#{locale}.yml")
+      expect(File.exist?(file)).to be false
+      Translation.fetch(locale)
+      expect(File.exist?(file)).to be true
+    end
+
+  end
+
+end

+ 0 - 123
test/unit/translation_test.rb

@@ -1,123 +0,0 @@
-# encoding: utf-8
-require 'test_helper'
-
-class TranslationTest < ActiveSupport::TestCase
-
-  test '1 - basics' do
-    Translation.reset('de-de')
-    Translation.sync('de-de')
-    tests = [
-      {
-        locale: 'en',
-        string: 'New',
-        result: 'New',
-      },
-      {
-        locale: 'en-us',
-        string: 'New',
-        result: 'New',
-      },
-      {
-        locale: 'de-de',
-        string: 'New',
-        result: 'Neu',
-      },
-      {
-        locale: 'de-de',
-        string: 'not translated - lalala',
-        result: 'not translated - lalala',
-      },
-    ]
-    tests.each { |test|
-      result = Translation.translate(test[:locale], test[:string])
-      assert_equal(result, test[:result], 'verify result')
-    }
-  end
-
-  test '2 - own translation tests' do
-    Translation.reset('de-de')
-    Translation.sync('de-de')
-    locale = 'de-de'
-
-    # check for custom changes
-    list = Translation.lang(locale)
-    list['list'].each { |item|
-      translation = Translation.find_by(source: item[1], locale: locale)
-      assert(translation)
-      assert_equal(locale, translation.locale)
-      assert_equal(translation.target, translation.target_initial)
-    }
-
-    # add custom changes
-    translation = Translation.find_by(locale: locale, source: 'open')
-    assert_equal('offen', translation.target)
-    assert_equal('offen', translation.target_initial)
-    translation.target = 'offen2'
-    translation.save!
-
-    list = Translation.lang(locale)
-    list['list'].each { |item|
-      translation = Translation.find_by(source: item[1], locale: locale)
-      assert(translation)
-      assert_equal(locale, translation.locale)
-      if translation.source == 'open'
-        assert_equal('offen2', translation.target)
-        assert_equal('offen', translation.target_initial)
-      else
-        assert_equal(translation.target, translation.target_initial)
-      end
-    }
-
-    Translation.load(locale)
-
-    list = Translation.lang(locale)
-    list['list'].each { |item|
-      translation = Translation.find_by(source: item[1], locale: locale)
-      assert(translation)
-      assert_equal(locale, translation.locale)
-      if translation.source == 'open'
-        assert_equal('offen2', translation.target)
-        assert_equal('offen', translation.target_initial)
-      else
-        assert_equal(translation.target, translation.target_initial)
-      end
-    }
-
-    Translation.reset(locale)
-
-    list = Translation.lang(locale)
-    list['list'].each { |item|
-      translation = Translation.find_by(source: item[1], locale: locale)
-      assert(translation)
-      assert_equal(locale, translation.locale)
-      assert_equal(translation.target, translation.target_initial)
-    }
-
-  end
-
-  test '3 - file based import' do
-
-    # locales
-    directory = Rails.root.join('config')
-    file = Rails.root.join("#{directory}/locales.yml")
-    if File.exist?(file)
-      File.delete(file)
-    end
-    assert_not(File.exist?(file))
-    Locale.fetch
-    assert(File.exist?(file))
-
-    # translations
-    locale = 'de-de'
-    directory = Rails.root.join('config/translations')
-    if File.directory?(directory)
-      FileUtils.rm_rf(directory)
-    end
-    file = Rails.root.join("#{directory}/#{locale}.yml")
-    assert_not(File.exist?(file))
-    Translation.fetch(locale)
-    assert(File.exist?(file))
-
-  end
-
-end