|
646 | 646 |
|
647 | 647 | let(:klass_with_set_of_custom_type) do
|
648 | 648 | new_class do
|
649 |
| - field :users, :set, of: DirtySpec::User |
| 649 | + field :users, :set, of: DirtySpec::UserWithEquality |
650 | 650 | end
|
651 | 651 | end
|
652 | 652 |
|
|
686 | 686 | end
|
687 | 687 | end
|
688 | 688 |
|
| 689 | + let(:klass_with_comparable_custom_type) do |
| 690 | + new_class do |
| 691 | + field :user, DirtySpec::UserWithEquality, comparable: true |
| 692 | + end |
| 693 | + end |
| 694 | + |
689 | 695 | context 'string type' do
|
690 | 696 | it 'detects in-place modifying a String value' do
|
691 | 697 | obj = klass_with_string.create!(name: +'Alex')
|
|
711 | 717 | end
|
712 | 718 |
|
713 | 719 | it 'detects in-place modifying of a Set element' do
|
714 |
| - obj = klass_with_set_of_custom_type.create!(users: [DirtySpec::User.new(+'Alex')]) |
| 720 | + obj = klass_with_set_of_custom_type.create!(users: [DirtySpec::UserWithEquality.new(+'Alex')]) |
715 | 721 | obj.users.map { |u| u.name.upcase! }
|
716 | 722 |
|
717 |
| - expect(obj.changes).to eq('users' => [Set[DirtySpec::User.new('Alex')], Set[DirtySpec::User.new('ALEX')]]) |
| 723 | + expect(obj.changes).to eq( |
| 724 | + 'users' => [ |
| 725 | + Set[DirtySpec::UserWithEquality.new('Alex')], |
| 726 | + Set[DirtySpec::UserWithEquality.new('ALEX')] |
| 727 | + ] |
| 728 | + ) |
718 | 729 | end
|
719 | 730 | end
|
720 | 731 |
|
|
792 | 803 | end
|
793 | 804 |
|
794 | 805 | context 'custom type' do
|
795 |
| - it 'detects in-place modifying a String value' do |
| 806 | + it 'detects in-place modifying' do |
796 | 807 | obj = klass_with_custom_type.create!(user: DirtySpec::User.new(+'Alex'))
|
797 | 808 | obj.user.name.upcase!
|
| 809 | + ScratchPad.record [] |
| 810 | + |
| 811 | + old_value, new_value = obj.changes['user'] |
| 812 | + |
| 813 | + expect(old_value.name).to eq 'Alex' |
| 814 | + expect(new_value.name).to eq 'ALEX' |
| 815 | + expect(ScratchPad.recorded).to eq([]) |
| 816 | + end |
| 817 | + |
| 818 | + it 'detects in-place modifying when custom type is safely comparable' do |
| 819 | + obj = klass_with_comparable_custom_type.create!(user: DirtySpec::UserWithEquality.new(+'Alex')) |
| 820 | + obj.user.name.upcase! |
| 821 | + ScratchPad.record [] |
| 822 | + |
| 823 | + old_value, new_value = obj.changes['user'] |
| 824 | + |
| 825 | + expect(old_value.name).to eq 'Alex' |
| 826 | + expect(new_value.name).to eq 'ALEX' |
| 827 | + |
| 828 | + expect(ScratchPad.recorded.size).to eq(1) |
| 829 | + record = ScratchPad.recorded[0] |
| 830 | + |
| 831 | + expect(record[0]).to eq('==') |
| 832 | + expect(record[1]).to equal(new_value) |
| 833 | + expect(record[2]).to equal(old_value) |
| 834 | + end |
| 835 | + |
| 836 | + it 'reports no in-place changes when field is not modified' do |
| 837 | + obj = klass_with_custom_type.create!(user: DirtySpec::User.new('Alex')) |
| 838 | + |
| 839 | + ScratchPad.record [] |
| 840 | + expect(obj.changes['user']).to eq(nil) |
| 841 | + expect(ScratchPad.recorded).to eq([]) |
| 842 | + end |
| 843 | + |
| 844 | + it 'reports no in-place changes when field is not modified and custom type is safely comparable' do |
| 845 | + obj = klass_with_comparable_custom_type.create!(user: DirtySpec::UserWithEquality.new('Alex')) |
| 846 | + ScratchPad.record [] |
| 847 | + |
| 848 | + expect(obj.changes['user']).to eq(nil) |
| 849 | + |
| 850 | + expect(ScratchPad.recorded.size).to eq(1) |
| 851 | + record = ScratchPad.recorded[0] |
798 | 852 |
|
799 |
| - expect(obj.changes).to eq('user' => [DirtySpec::User.new('Alex'), DirtySpec::User.new('ALEX')]) |
| 853 | + expect(record[0]).to eq('==') |
| 854 | + expect(record[1]).to equal(obj.user) |
| 855 | + expect(record[2]).to eq(obj.user) # an implicit 'from-database' copy |
800 | 856 | end
|
801 | 857 | end
|
802 | 858 | end
|
|
0 commit comments