|
| 1 | +require 'test_helper' |
| 2 | +require 'pry' |
| 3 | + |
| 4 | +# Applicable only for LIT_STORAGE=hybrid |
| 5 | +class HybridStorageTest < ActiveSupport::TestCase |
| 6 | + if ENV['LIT_STORAGE'] == 'hybrid' |
| 7 | + class Backend < Lit::I18nBackend |
| 8 | + end |
| 9 | + |
| 10 | + fixtures :all |
| 11 | + |
| 12 | + def setup |
| 13 | + Lit.init |
| 14 | + Lit::Localization.delete_all |
| 15 | + Lit::LocalizationKey.delete_all |
| 16 | + Lit::LocalizationVersion.delete_all |
| 17 | + @old_humanize_key = Lit.humanize_key |
| 18 | + Lit.humanize_key = false |
| 19 | + @old_load_path = I18n.load_path |
| 20 | + Lit.reset_hash |
| 21 | + I18n.backend.cache.clear |
| 22 | + @locale = Lit::Locale.find_by_locale(I18n.locale) |
| 23 | + super |
| 24 | + end |
| 25 | + |
| 26 | + def teardown |
| 27 | + Lit.loader = @old_loader |
| 28 | + Lit.humanize_key = @old_humanize_key |
| 29 | + I18n.backend = @old_backend |
| 30 | + I18n.load_path = @old_load_path |
| 31 | + super |
| 32 | + end |
| 33 | + |
| 34 | + test 'it should update translation both in hash and in redis' do |
| 35 | + # assertions to ensure that storage has been properly cleared |
| 36 | + assert_nil Lit._hash['en.fizz'] |
| 37 | + assert_nil Lit.redis.get(Lit.prefix + 'en.fizz') |
| 38 | + I18n.t('fizz', default: 'buzz') |
| 39 | + assert_equal 'buzz', Lit._hash['en.fizz'] |
| 40 | + assert_equal 'buzz', Lit.redis.get(Lit.prefix + 'en.fizz') |
| 41 | + end |
| 42 | + |
| 43 | + test 'it should clear hash when loading from redis something not yet in hash' do |
| 44 | + # let's do something that creates a hash snapshot timestamp |
| 45 | + assert_nil Lit._hash['en.fizz'] |
| 46 | + old_hash_snapshot = Lit.hash_snapshot |
| 47 | + I18n.t('fizz', default: 'buzz') |
| 48 | + assert_operator Lit.hash_snapshot, :>, old_hash_snapshot |
| 49 | + |
| 50 | + # in the meantime let's create some new translation |
| 51 | + # simulate as if it were created and redis snapshot has been updated |
| 52 | + lk = Lit::LocalizationKey.create(localization_key: 'abcd') |
| 53 | + l = lk.localizations.create!(locale: @locale, default_value: 'efgh') |
| 54 | + |
| 55 | + Lit.redis.set(Lit.prefix + 'en.abcd', 'efgh') |
| 56 | + Lit.saved_redis_snapshot = Lit.now_timestamp |
| 57 | + Lit.redis_snapshot = Lit.saved_redis_snapshot |
| 58 | + # TODO: consider if this is not too implementation-specific |
| 59 | + |
| 60 | + # assert that the newly created localization has been fetched into hash |
| 61 | + assert_equal 'efgh', I18n.t('abcd') |
| 62 | + assert_equal 'efgh', Lit._hash['en.abcd'] |
| 63 | + assert_equal 'efgh', Lit.redis.get(Lit.prefix + 'en.abcd') |
| 64 | + |
| 65 | + # assert that hash cache has been cleared |
| 66 | + assert_nil Lit._hash['en.fizz'] |
| 67 | + I18n.t('fizz') |
| 68 | + |
| 69 | + # assert that the value then gets loaded into hash again |
| 70 | + assert_equal 'buzz', Lit._hash['en.fizz'] |
| 71 | + end |
| 72 | + |
| 73 | + test 'local cache is used even when redis is cleared' do |
| 74 | + # define a translation by specifying default value |
| 75 | + assert_nil Lit._hash['en.fizz'] |
| 76 | + I18n.t('fizz', default: 'buzz') |
| 77 | + assert_equal 'buzz', Lit._hash['en.fizz'] |
| 78 | + |
| 79 | + # clear redis |
| 80 | + I18n.backend.cache.clear |
| 81 | + |
| 82 | + # modify local cache and then see if it's used for loading translation |
| 83 | + Lit._hash['en.fizz'] = 'fizzbuzz' |
| 84 | + assert_equal 'fizzbuzz', I18n.t('fizz') |
| 85 | + end |
| 86 | + else |
| 87 | + puts 'Skipping hybrid storage test' |
| 88 | + end |
| 89 | +end |
0 commit comments