|
500 | 500 |
|
501 | 501 | end |
502 | 502 |
|
| 503 | +describe "saving model when typed_store property is empty" do |
| 504 | + |
| 505 | + it "when db schema declares null: false, saves empty hash in db" do |
| 506 | + ActiveRecord::Schema.define do |
| 507 | + create_table :product1s, force: true do |t| |
| 508 | + t.json :properties, null: false, default: {} |
| 509 | + end |
| 510 | + end |
| 511 | + |
| 512 | + class Product1 < ActiveRecord::Base |
| 513 | + typed_store :properties, coder: ActiveRecord::TypedStore::IdentityCoder do |s| |
| 514 | + s.string :some_property |
| 515 | + end |
| 516 | + end |
| 517 | + |
| 518 | + Product1.new().save |
| 519 | + |
| 520 | + saved_properties = ActiveRecord::Base.connection.select_all("select * from product1s") |
| 521 | + .map { |row| row['properties'] } |
| 522 | + |
| 523 | + expect(saved_properties).to eq(['{}']) |
| 524 | + end |
| 525 | + |
| 526 | + it "when db schema declares null: true, with default {} saves {} in db" do |
| 527 | + ActiveRecord::Schema.define do |
| 528 | + create_table :product3s, force: true do |t| |
| 529 | + t.json :properties, default: {} |
| 530 | + end |
| 531 | + end |
| 532 | + |
| 533 | + class Product3 < ActiveRecord::Base |
| 534 | + typed_store :properties, coder: ActiveRecord::TypedStore::IdentityCoder do |s| |
| 535 | + s.string :some_property |
| 536 | + end |
| 537 | + end |
| 538 | + |
| 539 | + Product3.new().save |
| 540 | + |
| 541 | + saved_properties = ActiveRecord::Base.connection.select_all("select * from product3s") |
| 542 | + .map { |row| row['properties'] } |
| 543 | + |
| 544 | + expect(saved_properties).to eq(['{}']) |
| 545 | + end |
| 546 | + |
| 547 | + it "when db schema declares null: true, and no default, saves nil in db" do |
| 548 | + ActiveRecord::Schema.define do |
| 549 | + create_table :product2s, force: true do |t| |
| 550 | + t.json :properties |
| 551 | + end |
| 552 | + end |
| 553 | + |
| 554 | + class Product2 < ActiveRecord::Base |
| 555 | + typed_store :properties, coder: ActiveRecord::TypedStore::IdentityCoder do |s| |
| 556 | + s.string :some_property |
| 557 | + end |
| 558 | + end |
| 559 | + |
| 560 | + Product2.new().save |
| 561 | + |
| 562 | + saved_properties = ActiveRecord::Base.connection.select_all("select * from product2s") |
| 563 | + .map { |row| row['properties'] } |
| 564 | + |
| 565 | + expect(saved_properties).to eq([nil]) |
| 566 | + end |
| 567 | + |
| 568 | +end |
| 569 | + |
503 | 570 | shared_examples 'a store' do |retain_type = true, settings_type = :text| |
504 | 571 | let(:model) { described_class.new } |
505 | 572 |
|
|
799 | 866 | expect(model.explicit_settings[:signup][:counter]).to be_nil |
800 | 867 | end |
801 | 868 |
|
802 | | - it 'coerce hashes to HashWithIndifferentAccess' do # this is actually Rails behavior |
| 869 | + it 'coerce hashes to HashWithIndifferentAccess' do |
| 870 | + # this is actually Rails behavior |
803 | 871 | model.signup[:metadata] = { "signed_up_at" => Time.now } |
804 | 872 | expect(model.signup[:metadata]).to be_a ActiveSupport::HashWithIndifferentAccess |
805 | 873 | end |
|
832 | 900 |
|
833 | 901 | end |
834 | 902 |
|
835 | | -shared_examples 'a model supporting arrays' do |pg_native=false| |
| 903 | +shared_examples 'a model supporting arrays' do |pg_native = false| |
836 | 904 |
|
837 | 905 | let(:model) { described_class.new } |
838 | 906 |
|
|
871 | 939 | if pg_native |
872 | 940 |
|
873 | 941 | it 'raise on non rectangular multidimensianl arrays' do |
874 | | - expect{ |
| 942 | + expect { |
875 | 943 | model.update(grades: [[1, 2], [3, 4, 5]]) |
876 | 944 | }.to raise_error(ActiveRecord::StatementInvalid) |
877 | 945 | end |
878 | 946 |
|
879 | 947 | it 'raise on non nil assignation if column is non nullable' do |
880 | | - expect{ |
| 948 | + expect { |
881 | 949 | model.update(tags: nil) |
882 | 950 | }.to raise_error(ActiveRecord::StatementInvalid) |
883 | 951 | end |
|
0 commit comments