require 'assert' require 'mr/json_field/fake_record' require 'much-plugin' require 'mr/json_field' require 'mr/model' module MR::JsonField::FakeRecord class UnitTests < Assert::Context desc "MR::JsonField::FakeRecord" subject{ MR::JsonField::FakeRecord } should "use much-plugin" do assert_includes MuchPlugin, subject end end class MixinTests < UnitTests desc "when mixed in" setup do @fake_record_class = Class.new do include MR::JsonField::FakeRecord attribute :hash_data_json, :text attribute :array_data_json, :text end end subject{ @fake_record_class } end class InitTests < MixinTests desc "and init" setup do @model_class = Class.new do include MR::Model field_accessor :hash_data_json, :array_data_json include MR::JsonField json_field :hash_data json_field :array_data end @fake_record_class.model_class(@model_class) @fake_record = @fake_record_class.new @model = @fake_record.model end subject{ @fake_record } should "sync all of its json fields on save" do @model.hash_data = { Factory.string => Factory.string } @model.array_data = Factory.integer(3).times.map{ Factory.string } # since we used the writer the fields and their source should be equal assert_equal @model.hash_data, MR::JsonField.decode(subject.hash_data_json) assert_equal @model.array_data, MR::JsonField.decode(subject.array_data_json) @model.hash_data[Factory.string] = Factory.string @model.array_data << Factory.string # since we modified the fields and didn't use the writer, their source # should not be equal assert_not_equal @model.hash_data, MR::JsonField.decode(subject.hash_data_json) assert_not_equal @model.array_data, MR::JsonField.decode(subject.array_data_json) subject.save! # now that the callback has been run they should be equal once again assert_equal @model.hash_data, MR::JsonField.decode(subject.hash_data_json) assert_equal @model.array_data, MR::JsonField.decode(subject.array_data_json) end end end