public
Description: StrokeDB is an embeddable distributed document database written in Ruby
Homepage: http://strokedb.com/
Clone URL: git://github.com/yrashk/strokedb.git
commit  8427e9bcab2a06e6dd3117422d394c1912b09fd0
tree    086fe029fd9cf3ce163835cd9fb5d31b678e228c
parent  2d239726daba2e2e76bd16c445df4177539a3935
strokedb / spec / lib / strokedb / stores / store_spec.rb
100644 203 lines (161 sloc) 6.017 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
require File.dirname(__FILE__) + '/spec_helper'
 
describe "Store", :shared => true do
 
  it "should increment timestamp when storing a document" do
    @document = Document.new :stuff => '...'
    lambda do
      @store.save!(@document)
    end.should change(@store,:timestamp)
  end
 
  it "should store a document" do
    @document = Document.new :stuff => '...'
    @store.save!(@document)
    (doc = @store.find(@document.uuid)).should == @document
    doc.should_not be_a_kind_of(VersionedDocument)
  end
  
  it "should store VersionedDocument" do
    @document = Document.create! :stuff => '...'
    vd = @document.versions.all.first
    FileUtils.rm_rf TEMP_STORAGES + '/skiplist_store'
    another_cfg = StrokeDB::Config.build :base_path => TEMP_STORAGES + '/skiplist_store'
    another_store = another_cfg.stores[:default]
    another_store.save!(vd)
    another_store.find(vd.uuid,vd.version).should == vd
    another_store.find(vd.uuid).should be_nil
  end
 
  it "should be Enumerable" do
    @store.should be_a_kind_of(Enumerable)
  end
 
 
end
 
describe "New store" do
 
  before(:each) do
    @store = setup_default_store
  end
  
  it "should have its own UUID" do
    @store.uuid.should match(/^#{UUID_RE}$/)
  end
 
  it "should have 0 timestamp" do
    pending "Invalid spec: reconsider requirements (store can have several meta/view docs saved on init)"
    @store.timestamp.should == LTS.zero(@store.uuid)
  end
 
  it "should create corresponding StoreInfo document" do
    @store.document.should be_a_kind_of(StoreInfo)
    @store.document.uuid.should == @store.uuid
  end
 
 
  it "should return nil as head_version for unexistent document (well there is no documents at all)" do
    @store.head_version(Util.random_uuid).should be_nil
  end
 
  it_should_behave_like "Store"
 
end
 
 
describe "Non-empty store" do
 
  before(:each) do
    @store = setup_default_store
    setup_index
    @documents = []
    10.times do |i|
      @documents << Document.create!(:stuff => i)
    end
  end
 
 
  it "should report existing document as such" do
    @store.include?(@documents.first.uuid).should == true
  end
 
  it "should report existing versioned document as such" do
    @store.include?(@documents.first.uuid,@documents.first.version).should == true
  end
 
  it "should report versioned document that does not exist as such" do
    @store.include?(@documents.first.uuid,StrokeDB::Util.random_uuid).should == false
  end
 
  it "should report document that does not exist as such" do
    @store.include?('ouch, there is no way such UUID could be generated').should == false
  end
 
  it "should find a document" do
    (doc = @store.find(@documents.first.uuid)).should == @documents.first
    doc.should_not be_a_kind_of(VersionedDocument)
  end
 
  it "should find a versioned document" do
    (doc = @store.find(@documents.first.uuid,@documents.first.version)).should == @documents.first
    doc.should be_a_kind_of(VersionedDocument)
  end
 
  it "should not find a versioned document with version that does not exist" do
    @store.find(@documents.first.uuid,StrokeDB::Util.random_uuid).should be_nil
  end
 
 
  it "should iterate over all stored documents" do
    iterated_documents = []
    @store.each do |doc|
      iterated_documents << doc
    end
    iterated_documents.delete_if{|doc| !doc[:stuff] }.sort_by {|doc| doc.uuid}.should == @documents.sort_by {|doc| doc.uuid}
  end
 
  it "should iterate over all stored documents and their versions if told so" do
    iterated_documents = []
    @store.each(:include_versions => true) do |doc|
      iterated_documents << doc
    end
    documents_with_versions = @documents.clone
    @documents.each do |doc|
      doc.versions.all.each do |vd|
        documents_with_versions << vd
      end
    end
    iterated_documents.delete_if{|doc| !doc[:stuff] }.sort_by {|doc| doc.uuid}.should == documents_with_versions.sort_by {|doc| doc.uuid}
  end
 
  it "should iterate over all newly stored documents if told so" do
    timestamp = @store.timestamp.counter
    @new_documents = []
    10.times do |i|
      @new_documents << Document.create!(:stuff => i)
    end
 
    iterated_documents = []
    @store.each(:after_timestamp => timestamp) do |doc|
      iterated_documents << doc
    end
    iterated_documents.sort_by {|doc| doc.uuid}.should == @new_documents.sort_by {|doc| doc.uuid}
  end
 
  it "should iterate over all newly stored versions if told so" do
    timestamp = @store.timestamp.counter
    @new_documents = []
    @documents.each_with_index do |document,i|
      document.stuff = i+100
      @new_documents << document.save!
    end
 
    iterated_documents = []
    @store.each(:after_timestamp => timestamp, :include_versions => true) do |doc|
      iterated_documents << doc
    end
    iterated_documents.sort_by {|doc| doc.uuid}.should == (@documents + @new_documents).sort_by {|doc| doc.uuid}
  end
 
 
  it_should_behave_like "Store"
 
 
end
 
#
# describe "[Regression] First chunk cut" do
#
#
# before(:all) do
# @store = setup_default_store
# @doc1 = Document.new(@store,:stuff => 123)
# @doc2 = Document.new(@store,:stuff => 123)
# @doc3 = Document.new(@store,:stuff => 123)
# end
#
# it "should store a document with big uuid in a first chunk" do
# $DEBUG_CHEATERS_LEVEL = 2
# @store.save!(@doc3)
# @store.find(@doc3.uuid).uuid.should == @doc3.uuid
# # end
# # it "should store a document with lower uuid in a first chunk" do
# $DEBUG_CHEATERS_LEVEL = 2
# @store.save!(@doc1)
# @store.find(@doc1.uuid).uuid.should == @doc1.uuid
# @store.find(@doc3.uuid).uuid.should == @doc3.uuid
# # end
# # it "should cut a chunk with a document with medium uuid" do
# $DEBUG_CHEATERS_LEVEL = 5
# @store.save!(@doc2)
# @store.find(@doc1.uuid).uuid.should == @doc1.uuid
# @store.find(@doc3.uuid).uuid.should == @doc3.uuid
# @store.find(@doc2.uuid).uuid.should == @doc2.uuid
# end
#
# after(:each) do
# $DEBUG_CHEATERS_LEVEL = nil
# end
# end