File: serialization_modification_detection_spec.rb

package info (click to toggle)
ruby-sequel 5.63.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,408 kB
  • sloc: ruby: 113,747; makefile: 3
file content (105 lines) | stat: -rw-r--r-- 2,581 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
require_relative "spec_helper"
require 'yaml'

describe "serialization_modification_detection plugin" do
  before do
    @c = Class.new(Sequel::Model(:items))
    @c.class_eval do
      columns :id, :h
      plugin :serialization, :yaml, :h
      plugin :serialization_modification_detection
    end
    @o1 = @c.new(:h=>{})
    @o2 = @c.load(:id=>1, :h=>"--- {}\n\n")
    @o3 = @c.new
    @o4 = @c.load(:id=>1, :h=>nil)
    DB.reset
  end
  
  it "should not detect columns that haven't been changed" do
    @o1.changed_columns.must_equal []
    @o1.h.must_equal({})
    @o1.h[1] = 2
    @o1.h.clear
    @o1.changed_columns.must_equal []

    @o2.changed_columns.must_equal []
    @o2.h.must_equal({})
    @o2.h[1] = 2
    @o2.h.clear
    @o2.changed_columns.must_equal []
  end
  
  it "should detect columns that have been changed" do
    @o1.changed_columns.must_equal []
    @o1.h.must_equal({})
    @o1.h[1] = 2
    @o1.changed_columns.must_equal [:h]

    @o2.changed_columns.must_equal []
    @o2.h.must_equal({})
    @o2.h[1] = 2
    @o2.changed_columns.must_equal [:h]

    @o3.changed_columns.must_equal []
    @o3.h.must_be_nil
    @o3.h = {}
    @o3.changed_columns.must_equal [:h]

    @o4.changed_columns.must_equal []
    @o4.h.must_be_nil
    @o4.h = {}
    @o4.changed_columns.must_equal [:h]
  end
  
  it "should report correct changed_columns after saving" do
    @o1.h[1] = 2
    @o1.save
    @o1.changed_columns.must_equal []

    @o2.h[1] = 2
    @o2.save_changes
    @o2.changed_columns.must_equal []

    @o3.h = {1=>2}
    @o3.save
    @o3.changed_columns.must_equal []

    @o4.h = {1=>2}
    @o4.save
    @o4.changed_columns.must_equal []
  end

  it "should work with frozen objects" do
    @o1.changed_columns.must_equal []
    @o1.h.must_equal({})
    @o1.freeze.must_be_same_as @o1
    @o1.h[1] = 2
    @o1.changed_columns.must_equal [:h]
  end

  it "should work with frozen objects when checking changed_colums during validation" do
    @c.send(:define_method, :validate){changed_columns}
    @o1.h[1] = 2
    @o1.freeze
    @o1.changed_columns.must_equal [:h]
  end

  it "should work with duplicating objects" do
    @o2.changed_columns.must_equal []
    o = @o2.dup
    @o2.h.must_equal({})
    @o2.h[1] = 2
    @o2.changed_columns.must_equal [:h]
    o.changed_columns.must_equal []
  end

  it "should work with duplicating objects after modifying them" do
    @o2.changed_columns.must_equal []
    @o2.h.must_equal({})
    @o2.h[1] = 2
    @o2.changed_columns.must_equal [:h]
    o = @o2.dup
    o.changed_columns.must_equal [:h]
  end
end