yrashk / strokedb

StrokeDB is an embeddable distributed document database written in Ruby

This URL has Read+Write access

strokedb / spec / lib / strokedb / views / view_spec.rb
100644 138 lines (108 sloc) 4.418 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
require File.dirname(__FILE__) + '/spec_helper'
 
describe View, "without a name" do
 
  before(:each) do
    setup_default_store
  end
  
  it "could not be initialized" do
    lambda do
      @post_comments = View.define!
    end.should raise_error(ArgumentError)
  end
  
end
 
describe View, "without #map method defined" do
  before(:each) do
    setup_default_store
    @post_comments = View.define!(:name => "post_comments")
  end
  
  it "should raise exception when #map is used" do
     lambda { @post_comments.map("key","value") }.should raise_error(InvalidViewError)
  end
end
 
describe "'Has many comments' view" do
  
  before(:all) do
    setup_default_store
    @view = View.define!("post_comments") do |view|
      def view.map(uuid, doc)
        doc['type'] =~ /comment/ ? [[[doc.parent, doc.created_at], doc]] : nil
      end
    end
    
    @article1 = Document.create! :type => "post"
    @article2 = Document.create! :type => "post"
    @article3 = Document.create! :type => "post"
    
    @comment11 = Document.create! :type => "comment11", :parent => @article1, :created_at => Time.now
    @comment12 = Document.create! :type => "comment12", :parent => @article1, :created_at => Time.now
    @comment13 = Document.create! :type => "comment13", :parent => @article1, :created_at => Time.now
    
    @comment21 = Document.create! :type => "comment21", :parent => @article2, :created_at => Time.now
    @comment22 = Document.create! :type => "comment22", :parent => @article2, :created_at => Time.now
    
    # shuffled order to ensure, items are sorted correctly afterwards
    @view.update(@article3)
    @view.update(@comment22)
    @view.update(@comment11)
    @view.update(@article2)
    @view.update(@comment12)
    @view.update(@comment13)
    @view.update(@article1)
    @view.update(@comment21)
  end
  
  it "should find all the comments sorted by date" do
    results = @view.find
    # since article UUID can be anything
   (results == [@comment11, @comment12, @comment13, @comment21, @comment22] ||
    results == [@comment21, @comment22, @comment11, @comment12, @comment13]).should == true
  end
  
  it "should find all the article's comments" do
    @view.find(:key => @article1).should == [@comment11, @comment12, @comment13]
    @view.find(:key => @article2).should == [@comment21, @comment22]
    @view.find(:key => @article3).should == [ ]
  end
 
  it "should find all the article's comments in a reverse order" do
    @view.find(:key => @article1, :reverse => true).should == [@comment13, @comment12, @comment11]
    @view.find(:key => @article2, :reverse => true).should == [@comment22, @comment21]
    @view.find(:key => @article3, :reverse => true).should == [ ]
  end
  
  it "should find all the article's comments with limit" do
    @view.find(:key => @article1, :limit => 2).should == [@comment11, @comment12]
    @view.find(:key => @article2, :limit => 2).should == [@comment21, @comment22]
    @view.find(:key => @article3, :limit => 2).should == [ ]
  end
 
  it "should find all the article's comments with offset and limit" do
    @view.find(:key => @article1, :offset => 1, :limit => 2).should == [@comment12, @comment13]
    @view.find(:key => @article2, :offset => 1, :limit => 2).should == [@comment22]
    @view.find(:key => @article3, :offset => 1, :limit => 2).should == [ ]
  end
  
end
 
describe View, "with block defined and saved" do
  
  before(:each) do
    setup_default_store
    @view = View.define!("SomeView") do |view|
      def view.map(uuid, doc)
        [[doc,doc]]
      end
    end
  end
  
  it "should re-establish block when reloaded" do
    @view = @view.reload
    lambda { @view.map(1,2).should == [[1,2]]}.should_not raise_error(InvalidViewError)
  end
  
  it "should have the same storage when reloaded" do
    storage_id = @view.send(:storage).object_id
    @view = @view.reload
    @view.send(:storage).object_id.should == storage_id
  end
  
  it "should be findable with #[] syntax" do
    View["SomeView"].should == @view
  end
  
end
 
 
describe View, "with nsurl and block defined and saved" do
  
  before(:each) do
    setup_default_store
    @view = View.define!("SomeView", :nsurl => "http://strokedb.com/") do |view|
      def view.map(uuid, doc)
        [[doc,doc]]
      end
    end
  end
  it "should be findable with #[] syntax" do
    View["SomeView", "http://strokedb.com/"].should == @view
  end
  
end