public
Description: StrokeDB is an embeddable distributed document database written in Ruby
Homepage: http://strokedb.com/
Clone URL: git://github.com/yrashk/strokedb.git
strokedb / spec / lib / strokedb / sync / slot_diff_spec.rb
100644 165 lines (117 sloc) 4.272 kb
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
require File.dirname(__FILE__) + '/spec_helper'
 
describe "Diffing documents with slot changed and slot's diff strategy is specified on meta" do
 
  before(:each) do
    @store = setup_default_store
    Object.send!(:remove_const,'Slot1Diff') if defined?(Slot1Diff)
    Slot1Diff = Class.new(SlotDiffStrategy)
    Slot1Diff.should_receive(:diff).with(1,2).and_return(3)
 
    @meta = Document.create! :diff_strategy_slot1 => 'slot_1_diff', :name => "Slot1DiffMeta"
 
    @from = Document.create! :slot1 => 1, :meta => @meta
    @to = Document.create! :slot1 => 2, :meta => @meta
 
    @diff = @to.diff(@from)
  end
 
  it "should diff slot value according to its strategy" do
    @diff.updated_slots['slot1'].should == 3
  end
 
end
 
 
describe "Diffing documents with slot changed and slot's diff strategy is specified on meta, but there is no such diff strategy" do
 
  before(:each) do
    @store = setup_default_store
 
    Object.send!(:remove_const,'Slot1Diff') if defined?(Slot1Diff)
    @meta = Document.create! :diff_strategy_slot1 => 'slot_1_diff', :name => "Slot1DiffMeta"
 
    @from = Document.create! :slot1 => 1, :meta => @meta
    @to = Document.create! :slot1 => 2, :meta => @meta
    
    @diff = @to.diff(@from)
  end
 
  it "should not diff slot" do
    @diff.updated_slots['slot1'].should == 2
  end
 
end
 
describe "Diffing documents with slot changed and slot's diff strategy is specified on meta, but diff strategy is invalid" do
 
  before(:each) do
    @store = setup_default_store
    Object.send!(:remove_const,'Slot1Diff') if defined?(Slot1Diff)
 
    Slot1Diff = Class.new
 
    @meta = Document.create! :diff_strategy_slot1 => 'slot_1_diff', :name => "Slot1DiffMeta"
 
    @from = Document.create! :slot1 => 1, :meta => @meta
    @to = Document.create! :slot1 => 2, :meta => @meta
    
    @diff = @to.diff(@from)
  end
 
  it "should not diff slot" do
    @diff.updated_slots['slot1'].should == 2
  end
 
end
 
[DefaultSlotDiff].each do |strategy|
  
  describe "#{strategy.name} for String" do
    before(:each) do
      @string1 = "hello world"
      @string2 = "bye world"
    end
    
    it "should diff to an LCS structure" do
      strategy.diff(@string1, @string2).should be_a_kind_of(Array)
    end
    
    it "should patch String back" do
      strategy.patch(@string1,strategy.diff(@string1, @string2)).should == @string2
    end
 
  end
  
  describe "#{strategy.name} for Array" do
    before(:each) do
      @array1 = [1,2,3]
      @array2 = [1,5,3]
    end
    
    it "should diff to an LCS structure" do
      strategy.diff(@array1, @array2).should be_a_kind_of(Array)
    end
    
    it "should patch Array back" do
      strategy.patch(@array1,strategy.diff(@array1, @array2)).should == @array2
    end
  end
 
  describe "#{strategy.name} for Hash" do
    before(:each) do
      @hash1 = { :a => 1}
      @hash2 = { :a => 1, :b => 2}
    end
    
    it "should diff to an LCS structure" do
      strategy.diff(@hash1, @hash2).should be_a_kind_of(Array)
    end
    
    it "should patch Hash back" do
      strategy.patch(@hash1,strategy.diff(@hash1, @hash2)).should == @hash2
    end
  end
  
  
  describe "#{strategy.name} for document reference" do
    before(:each) do
      @ref1 = "@##{Util.random_uuid}"
      @ref2 = "@##{Util.random_uuid}"
    end
    
    it "should diff to an LCS structure" do
      strategy.diff(@ref1, @ref2).should be_a_kind_of(String)
    end
    
    it "should patch document reference back" do
      strategy.patch(@ref1,strategy.diff(@ref1, @ref2)).should == @ref2
    end
  end
  
  
  
  describe "#{strategy.name} for number" do
    before(:each) do
      @num1 = 1
      @num2 = 2
    end
    
    it "should diff to an LCS structure" do
      strategy.diff(@num1, @num2).should be_a_kind_of(Numeric)
    end
    
    it "should patch number back" do
      strategy.patch(@num1,strategy.diff(@num1, @num2)).should == @num2
    end
  end
  
  describe "#{strategy.name} for different types" do
    before(:each) do
      @val1 = 1
      @val2 = "a"
    end
    
    it "should diff to target value" do
      strategy.diff(@val1, @val2).should == @val2
    end
    
    it "should patch number back" do
      strategy.patch(@val1,strategy.diff(@val1, @val2)).should == @val2
    end
 
  end
end