public
Description: ffi based ruby library to access Tokyo Cabinet and Tokyo Tyrant
Homepage: http://rufus.rubyforge.org
Clone URL: git://github.com/jmettraux/rufus-tokyo.git
rufus-tokyo / spec / table_spec.rb
100644 217 lines (164 sloc) 4.037 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#
# Specifying rufus-tokyo
#
# Sun Feb 8 16:07:16 JST 2009
#
 
require File.join(File.dirname(__FILE__), 'spec_base')
require File.join(File.dirname(__FILE__), 'shared_table_spec')
 
require 'rufus/tokyo'
 
FileUtils.mkdir('tmp') rescue nil
 
 
describe Rufus::Tokyo::Table do
 
  it 'should open in write/create mode by default' do
 
    t = Rufus::Tokyo::Table.new('tmp/default.tct')
    t.close
    File.exist?('tmp/default.tct').should.equal(true)
    FileUtils.rm('tmp/default.tct')
  end
 
  it 'should raise an error when file is missing' do
 
    lambda {
      Rufus::Tokyo::Table.new('tmp/missing.tct', :mode => 'r')
    }.should.raise(
      Rufus::Tokyo::TokyoError).message.should.equal('(err 3) file not found')
  end
end
 
describe Rufus::Tokyo::Table do
 
  before do
    @t = Rufus::Tokyo::Table.new('tmp/table.tct')
    @t.clear
  end
  after do
    @t.close
  end
 
  it 'should return its path' do
 
    @t.path.should.equal('tmp/table.tct')
  end
 
  behaves_like 'table'
end
 
describe Rufus::Tokyo::Table do
 
  before do
    @t = Rufus::Tokyo::Table.new('tmp/table.tct')
    @t.clear
  end
  after do
    @t.close
  end
 
  behaves_like 'table with transactions'
end
 
describe 'Rufus::Tokyo::Table #keys' do
 
  before do
    @n = 50
    @t = Rufus::Tokyo::Table.new('tmp/test_new.tct')
    @t.clear
    @n.times { |i| @t["person#{i}"] = { 'name' => 'whoever' } }
    @n.times { |i| @t["animal#{i}"] = { 'name' => 'whichever' } }
    @t["toto#{0.chr}5"] = { 'name' => 'toto' }
  end
  after do
    @t.close
  end
 
  behaves_like 'table #keys'
end
 
def prepare_table_with_data
 
  t = Rufus::Tokyo::Table.new('tmp/test_new.tct')
  t.clear
  t['pk0'] = { 'name' => 'jim', 'age' => '25', 'lang' => 'ja,en' }
  t['pk1'] = { 'name' => 'jeff', 'age' => '32', 'lang' => 'en,es' }
  t['pk2'] = { 'name' => 'jack', 'age' => '44', 'lang' => 'en' }
  t['pk3'] = { 'name' => 'jake', 'age' => '45', 'lang' => 'en,li' }
  t
end
 
describe Rufus::Tokyo::Table do
 
  before do
    @t = prepare_table_with_data
  end
  after do
    @t.close
  end
 
  behaves_like 'table indexes'
end
 
describe 'Rufus::Tokyo::Table#lget' do
 
  before do
    @t = prepare_table_with_data
  end
  after do
    @t.close
  end
 
  behaves_like 'table lget'
end
 
# DONE
 
describe 'Rufus::Tokyo::Table, like a Ruby Hash' do
 
  before do
    @t = prepare_table_with_data
  end
  after do
    @t.close
  end
 
  behaves_like 'table like a hash'
end
 
describe Rufus::Tokyo::TableQuery do
 
  before do
    @t = prepare_table_with_data
  end
  after do
    @t.close
  end
 
  behaves_like 'table query'
end
 
describe 'Rufus::Tokyo::TableQuery (fts)' do
 
  before do
    @t = Rufus::Tokyo::Table.new('tmp/test_new.tct')
    @t.clear
    [
      "consul readableness choleric hopperdozer juckies",
      "fume overharshness besprinkler whirling erythrene",
      "trumper defiable detractively cattiness superioress",
      "vivificative consul agglomerated Peterloo way",
      "unkilned bituminate antimatrimonial uran polyphony",
      "kurumaya unannexed renownedly apetaloid consul",
      "overdare nescience seronegative nagster overfatten",
    ].each_with_index { |w, i|
      @t["pk#{i}"] = { 'name' => "lambda#{i}", 'words' => w }
    }
  end
  after do
    @t.close
  end
 
  behaves_like 'table query (fts)'
end
 
describe 'Rufus::Tokyo::TableQuery#process' do
 
  before do
    @t = prepare_table_with_data
  end
  after do
    @t.close
  end
 
  behaves_like 'table query #process'
end
 
describe 'results from queries on Rufus::Tokyo::Table' do
 
  before do
    @t = prepare_table_with_data
  end
  after do
    @t.close
  end
 
  behaves_like 'table query results'
end
 
describe Rufus::Tokyo::Table do
 
  before do
    @t = Rufus::Tokyo::Table.new('tmp/table.tct')
    @t.clear
  end
  after do
    @t.close
  end
 
  behaves_like 'a table structure flattening keys and values'
end
 
describe 'Rufus::Tokyo::Table\'s queries' do
 
  before do
    @t = prepare_table_with_data
  end
  after do
    @t.close
  end
 
  behaves_like 'a table structure to_s-ing query stuff'
end