<commit_before>require 'test_helper'
require 'fileutils'

class SyncTest < ActiveSupport::TestCase
  def setup
    Tolk::Locale.delete_all
    Tolk::Translation.delete_all
    Tolk::Phrase.delete_all

    Tolk::Locale.locales_config_path = RAILS_ROOT + "/test/locales/sync/"

    I18n.backend.reload!
    I18n.load_path = [Tolk::Locale.locales_config_path + 'en.yml']
    I18n.backend.send :init_translations

    Tolk::Locale.primary_locale(true)
  end

  def test_flat_hash
    data = {'home' => {'hello' => 'hola', 'sidebar' => {'title' => 'something'}}}
    result = Tolk::Locale.send(:flat_hash, data)

    assert_equal 2, result.keys.size
    assert_equal ['home.hello', 'home.sidebar.title'], result.keys.sort
    assert_equal ['hola', 'something'], result.values.sort
  end

  def test_sync_sets_previous_text_for_primary_locale
    Tolk::Locale.sync!

    # Change 'Hello World' to 'Hello Super World'
    Tolk::Locale.expects(:load_translations).returns({"hello_world" => "Hello Super World"})
    Tolk::Locale.sync!

    translation = Tolk::Locale.primary_locale(true).translations.first
    assert_equal 'Hello Super World', translation.text
    assert_equal 'Hello World', translation.previous_text
  end

  def test_sync_sets_primary_updated_for_secondary_translations_on_update
    spanish = Tolk::Locale.create!(:name => 'es')

    Tolk::Locale.sync!

    phrase1 = Tolk::Phrase.all.detect {|p| p.key == 'hello_world'}
    t1 = spanish.translations.create!(:text => 'hola', :phrase => phrase1)
    phrase2 = Tolk::Phrase.all.detect {|p| p.key == 'nested.hello_country'}
    t2 = spanish.translations.create!(:text => 'nested hola', :phrase => phrase2)

    # Change 'Hello World' to 'Hello Super World'. But don't change nested.hello_country
    Tolk::Locale.expects(:load_translations).returns({'hello_world' => 'Hello Super World', 'nested.hello_country' => 'Nested Hello Country'})
    Tolk::Locale.sync!

    t1.reload
    t2.reload

    assert t1.primary_updated?
    assert ! t2.primary_updated?
  end

  def test_sync_creates_locale_phrases_translations
    Tolk::Locale.sync!

    # Created by sync!
    primary_locale = Tolk::Locale.find_by_name!(Tolk::Locale.primary_locale_name)

    assert_equal ["Hello World", "Nested Hello Country"], primary_locale.translations.map(&:text).sort
    assert_equal ["hello_world", "nested.hello_country"], Tolk::Phrase.all.map(&:key).sort
  end

  def test_sync_deletes_stale_translations_for_secondary_locales_on_delete_all
    spanish = Tolk::Locale.create!(:name => 'es')

    Tolk::Locale.sync!

    phrase = Tolk::Phrase.all.detect {|p| p.key == 'hello_world'}
    hola = spanish.translations.create!(:text => 'hola', :phrase => phrase)

    # Mimic deleting all the translations
    Tolk::Locale.expects(:load_translations).returns({})
    Tolk::Locale.sync!

    assert_equal 0, Tolk::Phrase.count
    assert_equal 0, Tolk::Translation.count

    assert_raises(ActiveRecord::RecordNotFound) { hola.reload }
  end

  def test_sync_deletes_stale_translations_for_secondary_locales_on_delete_some
    spanish = Tolk::Locale.create!(:name => 'es')

    Tolk::Locale.sync!

    phrase = Tolk::Phrase.all.detect {|p| p.key == 'hello_world'}
    hola = spanish.translations.create!(:text => 'hola', :phrase => phrase)

    # Mimic deleting 'hello_world'
    Tolk::Locale.expects(:load_translations).returns({'nested.hello_country' => 'Nested Hello World'})
    Tolk::Locale.sync!

    assert_equal 1, Tolk::Phrase.count
    assert_equal 1, Tolk::Translation.count
    assert_equal 0, spanish.translations.count

    assert_raises(ActiveRecord::RecordNotFound) { hola.reload }
  end

  def test_sync_handles_deleted_keys_and_updated_translations
    Tolk::Locale.sync!

    # Mimic deleting 'nested.hello_country' and updating 'hello_world'
    Tolk::Locale.expects(:load_translations).returns({"hello_world" => "Hello Super World"})
    Tolk::Locale.sync!

    primary_locale = Tolk::Locale.find_by_name!(Tolk::Locale.primary_locale_name)

    assert_equal ['Hello Super World'], primary_locale.translations.map(&:text)
    assert_equal ['hello_world'], Tolk::Phrase.all.map(&:key).sort
  end

  def test_sync_doesnt_mess_with_existing_translations
    spanish = Tolk::Locale.create!(:name => 'es')

    Tolk::Locale.sync!

    phrase = Tolk::Phrase.all.detect {|p| p.key == 'hello_world'}
    hola = spanish.translations.create!(:text => 'hola', :phrase => phrase)

    # Mimic deleting 'nested.hello_country' and updating 'hello_world'
    Tolk::Locale.expects(:load_translations).returns({"hello_world" => "Hello Super World"})
    Tolk::Locale.sync!

    hola.reload
    assert_equal 'hola', hola.text
  end

  def test_dump_all_after_sync
    spanish = Tolk::Locale.create!(:name => 'es')

    Tolk::Locale.sync!

    phrase = Tolk::Phrase.all.detect {|p| p.key == 'hello_world'}
    hola = spanish.translations.create!(:text => 'hola', :phrase => phrase)

    tmpdir = RAILS_ROOT + "/tmp/sync/locales"
    FileUtils.mkdir_p(tmpdir)
    Tolk::Locale.dump_all(tmpdir)

    spanish_file = "#{tmpdir}/es.yml"
    data = YAML::load(IO.read(spanish_file))['es']
    assert_equal ['hello_world'], data.keys
    assert_equal 'hola', data['hello_world']
  ensure
    FileUtils.rm_f(tmpdir)
  end
end<commit_msg>Sanity test for serialized values<commit_after>require 'test_helper'
require 'fileutils'

class SyncTest < ActiveSupport::TestCase
  def setup
    Tolk::Locale.delete_all
    Tolk::Translation.delete_all
    Tolk::Phrase.delete_all

    Tolk::Locale.locales_config_path = RAILS_ROOT + "/test/locales/sync/"

    I18n.backend.reload!
    I18n.load_path = [Tolk::Locale.locales_config_path + 'en.yml']
    I18n.backend.send :init_translations

    Tolk::Locale.primary_locale(true)
  end

  def test_flat_hash
    data = {'home' => {'hello' => 'hola', 'sidebar' => {'title' => 'something'}}}
    result = Tolk::Locale.send(:flat_hash, data)

    assert_equal 2, result.keys.size
    assert_equal ['home.hello', 'home.sidebar.title'], result.keys.sort
    assert_equal ['hola', 'something'], result.values.sort
  end

  def test_sync_sets_previous_text_for_primary_locale
    Tolk::Locale.sync!

    # Change 'Hello World' to 'Hello Super World'
    Tolk::Locale.expects(:load_translations).returns({"hello_world" => "Hello Super World"})
    Tolk::Locale.sync!

    translation = Tolk::Locale.primary_locale(true).translations.first
    assert_equal 'Hello Super World', translation.text
    assert_equal 'Hello World', translation.previous_text
  end

  def test_sync_sets_primary_updated_for_secondary_translations_on_update
    spanish = Tolk::Locale.create!(:name => 'es')

    Tolk::Locale.sync!

    phrase1 = Tolk::Phrase.all.detect {|p| p.key == 'hello_world'}
    t1 = spanish.translations.create!(:text => 'hola', :phrase => phrase1)
    phrase2 = Tolk::Phrase.all.detect {|p| p.key == 'nested.hello_country'}
    t2 = spanish.translations.create!(:text => 'nested hola', :phrase => phrase2)

    # Change 'Hello World' to 'Hello Super World'. But don't change nested.hello_country
    Tolk::Locale.expects(:load_translations).returns({'hello_world' => 'Hello Super World', 'nested.hello_country' => 'Nested Hello Country'})
    Tolk::Locale.sync!

    t1.reload
    t2.reload

    assert t1.primary_updated?
    assert ! t2.primary_updated?
  end

  def test_sync_creates_locale_phrases_translations
    Tolk::Locale.sync!

    # Created by sync!
    primary_locale = Tolk::Locale.find_by_name!(Tolk::Locale.primary_locale_name)

    assert_equal ["Hello World", "Nested Hello Country"], primary_locale.translations.map(&:text).sort
    assert_equal ["hello_world", "nested.hello_country"], Tolk::Phrase.all.map(&:key).sort
  end

  def test_sync_deletes_stale_translations_for_secondary_locales_on_delete_all
    spanish = Tolk::Locale.create!(:name => 'es')

    Tolk::Locale.sync!

    phrase = Tolk::Phrase.all.detect {|p| p.key == 'hello_world'}
    hola = spanish.translations.create!(:text => 'hola', :phrase => phrase)

    # Mimic deleting all the translations
    Tolk::Locale.expects(:load_translations).returns({})
    Tolk::Locale.sync!

    assert_equal 0, Tolk::Phrase.count
    assert_equal 0, Tolk::Translation.count

    assert_raises(ActiveRecord::RecordNotFound) { hola.reload }
  end

  def test_sync_deletes_stale_translations_for_secondary_locales_on_delete_some
    spanish = Tolk::Locale.create!(:name => 'es')

    Tolk::Locale.sync!

    phrase = Tolk::Phrase.all.detect {|p| p.key == 'hello_world'}
    hola = spanish.translations.create!(:text => 'hola', :phrase => phrase)

    # Mimic deleting 'hello_world'
    Tolk::Locale.expects(:load_translations).returns({'nested.hello_country' => 'Nested Hello World'})
    Tolk::Locale.sync!

    assert_equal 1, Tolk::Phrase.count
    assert_equal 1, Tolk::Translation.count
    assert_equal 0, spanish.translations.count

    assert_raises(ActiveRecord::RecordNotFound) { hola.reload }
  end

  def test_sync_handles_deleted_keys_and_updated_translations
    Tolk::Locale.sync!

    # Mimic deleting 'nested.hello_country' and updating 'hello_world'
    Tolk::Locale.expects(:load_translations).returns({"hello_world" => "Hello Super World"})
    Tolk::Locale.sync!

    primary_locale = Tolk::Locale.find_by_name!(Tolk::Locale.primary_locale_name)

    assert_equal ['Hello Super World'], primary_locale.translations.map(&:text)
    assert_equal ['hello_world'], Tolk::Phrase.all.map(&:key).sort
  end

  def test_sync_doesnt_mess_with_existing_translations
    spanish = Tolk::Locale.create!(:name => 'es')

    Tolk::Locale.sync!

    phrase = Tolk::Phrase.all.detect {|p| p.key == 'hello_world'}
    hola = spanish.translations.create!(:text => 'hola', :phrase => phrase)

    # Mimic deleting 'nested.hello_country' and updating 'hello_world'
    Tolk::Locale.expects(:load_translations).returns({"hello_world" => "Hello Super World"})
    Tolk::Locale.sync!

    hola.reload
    assert_equal 'hola', hola.text
  end

  def test_sync_array_values
    spanish = Tolk::Locale.create!(:name => 'es')

    # Mimic deleting 'nested.hello_country' and updating 'hello_world'
    data = {"weekend" => ['Friday', 'Saturday', 'Sunday']}
    Tolk::Locale.expects(:load_translations).returns(data)
    Tolk::Locale.sync!

    assert_equal 1, Tolk::Locale.primary_locale.translations.count

    translation = Tolk::Locale.primary_locale.translations.first
    assert_equal data['weekend'], translation.text

    yaml = ['Saturday', 'Sunday'].to_yaml
    spanish_weekends = spanish.translations.create!(:text => yaml, :phrase => Tolk::Phrase.first)
    assert_equal YAML.load(yaml), spanish_weekends.text
  end

  def test_dump_all_after_sync
    spanish = Tolk::Locale.create!(:name => 'es')

    Tolk::Locale.sync!

    phrase = Tolk::Phrase.all.detect {|p| p.key == 'hello_world'}
    hola = spanish.translations.create!(:text => 'hola', :phrase => phrase)

    tmpdir = RAILS_ROOT + "/tmp/sync/locales"
    FileUtils.mkdir_p(tmpdir)
    Tolk::Locale.dump_all(tmpdir)

    spanish_file = "#{tmpdir}/es.yml"
    data = YAML::load(IO.read(spanish_file))['es']
    assert_equal ['hello_world'], data.keys
    assert_equal 'hola', data['hello_world']
  ensure
    FileUtils.rm_f(tmpdir)
  end
end
{'fraction_non_alphanumeric': 0.13859976870385196, 'fraction_numerical': 0.0040032025620496394, 'mean_word_length': 4.5216110019646365, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2935901', 'n_tokens_mistral': 3923, 'n_tokens_neox': 3748, 'n_words': 657}