<commit_before>require "spec_helper"

describe "Mobility::Backends::ActiveRecord::Json", orm: :active_record, db: :postgres do
  require "mobility/backends/active_record/json"
  extend Helpers::ActiveRecord
  before do
    stub_const 'JsonPost', Class.new(ActiveRecord::Base)
    JsonPost.extend Mobility
  end

  context "with no plugins applied" do
    include_backend_examples described_class, (Class.new(ActiveRecord::Base) do
      extend Mobility
      self.table_name = 'json_posts'
    end)
  end

  context "with standard plugins applied" do
    let(:backend) { post.mobility.backend_for("title") }

    before { JsonPost.translates :title, :content, backend: :json, presence: false, cache: false }
    let(:post) { JsonPost.new }

    include_accessor_examples 'JsonPost'
    include_serialization_examples 'JsonPost'
    include_querying_examples 'JsonPost'
    include_validation_examples 'JsonPost'
    include_dup_examples 'JsonPost'
    include_cache_key_examples 'JsonPost'

    describe "non-text values" do
      it "stores non-string types as-is when saving", rails_version_geq: '5.0' do
        backend = post.mobility.backend_for("title")
        backend.write(:en, { foo: :bar } )
        post.save
        expect(post[:title]).to eq({ "en" => { "foo" => "bar" }})
      end
    end
  end

  context "with dirty plugin applied" do
    let(:backend) { post.mobility.backend_for("title") }

    before { JsonPost.translates :title, :content, backend: :json, cache: false, presence: false, dirty: true }
    let(:post) { JsonPost.new }

    include_accessor_examples 'JsonPost'
    include_serialization_examples 'JsonPost'
  end
end if Mobility::Loaded::ActiveRecord<commit_msg>Skip querying examples for AR json backend for Rails < 5.0<commit_after>require "spec_helper"

describe "Mobility::Backends::ActiveRecord::Json", orm: :active_record, db: :postgres do
  require "mobility/backends/active_record/json"
  extend Helpers::ActiveRecord
  before do
    stub_const 'JsonPost', Class.new(ActiveRecord::Base)
    JsonPost.extend Mobility
  end

  context "with no plugins applied" do
    include_backend_examples described_class, (Class.new(ActiveRecord::Base) do
      extend Mobility
      self.table_name = 'json_posts'
    end)
  end

  context "with standard plugins applied" do
    let(:backend) { post.mobility.backend_for("title") }

    before { JsonPost.translates :title, :content, backend: :json, presence: false, cache: false }
    let(:post) { JsonPost.new }

    include_accessor_examples 'JsonPost'
    include_serialization_examples 'JsonPost'
    include_querying_examples 'JsonPost' unless ENV['RAILS_VERSION'] < '5.0'
    include_validation_examples 'JsonPost'
    include_dup_examples 'JsonPost'
    include_cache_key_examples 'JsonPost'

    describe "non-text values" do
      it "stores non-string types as-is when saving", rails_version_geq: '5.0' do
        backend = post.mobility.backend_for("title")
        backend.write(:en, { foo: :bar } )
        post.save
        expect(post[:title]).to eq({ "en" => { "foo" => "bar" }})
      end
    end
  end

  context "with dirty plugin applied" do
    let(:backend) { post.mobility.backend_for("title") }

    before { JsonPost.translates :title, :content, backend: :json, cache: false, presence: false, dirty: true }
    let(:post) { JsonPost.new }

    include_accessor_examples 'JsonPost'
    include_serialization_examples 'JsonPost'
  end
end if Mobility::Loaded::ActiveRecord
{'fraction_non_alphanumeric': 0.10986482599942479, 'fraction_numerical': 0.0023008340523439745, 'mean_word_length': 4.04789550072569, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '5444605', 'n_tokens_mistral': 1106, 'n_tokens_neox': 1061, 'n_words': 265}