rroonga 0.9.2-mswin32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (202) hide show
  1. data/AUTHORS +5 -0
  2. data/NEWS.ja.rdoc +114 -0
  3. data/NEWS.rdoc +116 -0
  4. data/README.ja.rdoc +65 -0
  5. data/README.rdoc +66 -0
  6. data/Rakefile +217 -0
  7. data/benchmark/common.rb +49 -0
  8. data/benchmark/read-write-many-small-items.rb +144 -0
  9. data/benchmark/write-many-small-items.rb +135 -0
  10. data/example/bookmark.rb +161 -0
  11. data/example/index-html.rb +89 -0
  12. data/example/search/config.ru +211 -0
  13. data/example/search/public/css/groonga.css +122 -0
  14. data/ext/.gitignore +2 -0
  15. data/ext/libruby-groonga.a +0 -0
  16. data/ext/rb-grn-accessor.c +52 -0
  17. data/ext/rb-grn-array-cursor.c +36 -0
  18. data/ext/rb-grn-array.c +210 -0
  19. data/ext/rb-grn-column.c +573 -0
  20. data/ext/rb-grn-context.c +655 -0
  21. data/ext/rb-grn-database.c +472 -0
  22. data/ext/rb-grn-encoding-support.c +64 -0
  23. data/ext/rb-grn-encoding.c +257 -0
  24. data/ext/rb-grn-exception.c +1110 -0
  25. data/ext/rb-grn-expression-builder.c +75 -0
  26. data/ext/rb-grn-expression.c +735 -0
  27. data/ext/rb-grn-fix-size-column.c +166 -0
  28. data/ext/rb-grn-hash-cursor.c +38 -0
  29. data/ext/rb-grn-hash.c +294 -0
  30. data/ext/rb-grn-index-column.c +488 -0
  31. data/ext/rb-grn-logger.c +504 -0
  32. data/ext/rb-grn-object.c +1369 -0
  33. data/ext/rb-grn-operation.c +198 -0
  34. data/ext/rb-grn-patricia-trie-cursor.c +39 -0
  35. data/ext/rb-grn-patricia-trie.c +488 -0
  36. data/ext/rb-grn-procedure.c +52 -0
  37. data/ext/rb-grn-query.c +260 -0
  38. data/ext/rb-grn-record.c +40 -0
  39. data/ext/rb-grn-snippet.c +334 -0
  40. data/ext/rb-grn-table-cursor-key-support.c +69 -0
  41. data/ext/rb-grn-table-cursor.c +247 -0
  42. data/ext/rb-grn-table-key-support.c +731 -0
  43. data/ext/rb-grn-table.c +2141 -0
  44. data/ext/rb-grn-type.c +181 -0
  45. data/ext/rb-grn-utils.c +769 -0
  46. data/ext/rb-grn-variable-size-column.c +36 -0
  47. data/ext/rb-grn-variable.c +108 -0
  48. data/ext/rb-grn-view-accessor.c +53 -0
  49. data/ext/rb-grn-view-cursor.c +35 -0
  50. data/ext/rb-grn-view-record.c +41 -0
  51. data/ext/rb-grn-view.c +421 -0
  52. data/ext/rb-grn.h +700 -0
  53. data/ext/rb-groonga.c +117 -0
  54. data/extconf.rb +266 -0
  55. data/html/bar.svg +153 -0
  56. data/html/developer.html +117 -0
  57. data/html/developer.svg +469 -0
  58. data/html/download.svg +253 -0
  59. data/html/favicon.ico +0 -0
  60. data/html/favicon.xcf +0 -0
  61. data/html/footer.html.erb +28 -0
  62. data/html/head.html.erb +4 -0
  63. data/html/header.html.erb +17 -0
  64. data/html/index.html +147 -0
  65. data/html/install.svg +636 -0
  66. data/html/logo.xcf +0 -0
  67. data/html/ranguba.css +250 -0
  68. data/html/tutorial.svg +559 -0
  69. data/lib/groonga.rb +90 -0
  70. data/lib/groonga/context.rb +184 -0
  71. data/lib/groonga/expression-builder.rb +285 -0
  72. data/lib/groonga/patricia-trie.rb +53 -0
  73. data/lib/groonga/record.rb +311 -0
  74. data/lib/groonga/schema.rb +1191 -0
  75. data/lib/groonga/view-record.rb +56 -0
  76. data/license/GPL +340 -0
  77. data/license/LGPL +504 -0
  78. data/license/RUBY +59 -0
  79. data/misc/grnop2ruby.rb +49 -0
  80. data/pkg-config.rb +333 -0
  81. data/test-unit/Rakefile +40 -0
  82. data/test-unit/TODO +5 -0
  83. data/test-unit/bin/testrb +5 -0
  84. data/test-unit/html/classic.html +15 -0
  85. data/test-unit/html/index.html +25 -0
  86. data/test-unit/html/index.html.ja +27 -0
  87. data/test-unit/lib/test/unit.rb +323 -0
  88. data/test-unit/lib/test/unit/assertionfailederror.rb +25 -0
  89. data/test-unit/lib/test/unit/assertions.rb +1230 -0
  90. data/test-unit/lib/test/unit/attribute.rb +125 -0
  91. data/test-unit/lib/test/unit/autorunner.rb +360 -0
  92. data/test-unit/lib/test/unit/collector.rb +36 -0
  93. data/test-unit/lib/test/unit/collector/descendant.rb +23 -0
  94. data/test-unit/lib/test/unit/collector/dir.rb +108 -0
  95. data/test-unit/lib/test/unit/collector/load.rb +136 -0
  96. data/test-unit/lib/test/unit/collector/objectspace.rb +34 -0
  97. data/test-unit/lib/test/unit/color-scheme.rb +102 -0
  98. data/test-unit/lib/test/unit/color.rb +96 -0
  99. data/test-unit/lib/test/unit/diff.rb +724 -0
  100. data/test-unit/lib/test/unit/error.rb +130 -0
  101. data/test-unit/lib/test/unit/exceptionhandler.rb +39 -0
  102. data/test-unit/lib/test/unit/failure.rb +136 -0
  103. data/test-unit/lib/test/unit/fixture.rb +176 -0
  104. data/test-unit/lib/test/unit/notification.rb +129 -0
  105. data/test-unit/lib/test/unit/omission.rb +191 -0
  106. data/test-unit/lib/test/unit/pending.rb +150 -0
  107. data/test-unit/lib/test/unit/priority.rb +180 -0
  108. data/test-unit/lib/test/unit/runner/console.rb +52 -0
  109. data/test-unit/lib/test/unit/runner/emacs.rb +8 -0
  110. data/test-unit/lib/test/unit/runner/tap.rb +8 -0
  111. data/test-unit/lib/test/unit/testcase.rb +476 -0
  112. data/test-unit/lib/test/unit/testresult.rb +89 -0
  113. data/test-unit/lib/test/unit/testsuite.rb +110 -0
  114. data/test-unit/lib/test/unit/ui/console/outputlevel.rb +14 -0
  115. data/test-unit/lib/test/unit/ui/console/testrunner.rb +466 -0
  116. data/test-unit/lib/test/unit/ui/emacs/testrunner.rb +63 -0
  117. data/test-unit/lib/test/unit/ui/tap/testrunner.rb +92 -0
  118. data/test-unit/lib/test/unit/ui/testrunner.rb +28 -0
  119. data/test-unit/lib/test/unit/ui/testrunnermediator.rb +77 -0
  120. data/test-unit/lib/test/unit/ui/testrunnerutilities.rb +41 -0
  121. data/test-unit/lib/test/unit/util/backtracefilter.rb +41 -0
  122. data/test-unit/lib/test/unit/util/method-owner-finder.rb +28 -0
  123. data/test-unit/lib/test/unit/util/observable.rb +90 -0
  124. data/test-unit/lib/test/unit/util/procwrapper.rb +48 -0
  125. data/test-unit/lib/test/unit/version.rb +7 -0
  126. data/test-unit/sample/adder.rb +13 -0
  127. data/test-unit/sample/subtracter.rb +12 -0
  128. data/test-unit/sample/test_adder.rb +20 -0
  129. data/test-unit/sample/test_subtracter.rb +20 -0
  130. data/test-unit/sample/test_user.rb +23 -0
  131. data/test-unit/test/collector/test-descendant.rb +133 -0
  132. data/test-unit/test/collector/test-load.rb +348 -0
  133. data/test-unit/test/collector/test_dir.rb +406 -0
  134. data/test-unit/test/collector/test_objectspace.rb +100 -0
  135. data/test-unit/test/run-test.rb +15 -0
  136. data/test-unit/test/test-attribute.rb +86 -0
  137. data/test-unit/test/test-color-scheme.rb +67 -0
  138. data/test-unit/test/test-color.rb +47 -0
  139. data/test-unit/test/test-diff.rb +518 -0
  140. data/test-unit/test/test-emacs-runner.rb +60 -0
  141. data/test-unit/test/test-fixture.rb +287 -0
  142. data/test-unit/test/test-notification.rb +33 -0
  143. data/test-unit/test/test-omission.rb +81 -0
  144. data/test-unit/test/test-pending.rb +70 -0
  145. data/test-unit/test/test-priority.rb +119 -0
  146. data/test-unit/test/test-testcase.rb +544 -0
  147. data/test-unit/test/test_assertions.rb +1151 -0
  148. data/test-unit/test/test_error.rb +26 -0
  149. data/test-unit/test/test_failure.rb +33 -0
  150. data/test-unit/test/test_testresult.rb +113 -0
  151. data/test-unit/test/test_testsuite.rb +129 -0
  152. data/test-unit/test/testunit-test-util.rb +14 -0
  153. data/test-unit/test/ui/test_testrunmediator.rb +20 -0
  154. data/test-unit/test/util/test-method-owner-finder.rb +38 -0
  155. data/test-unit/test/util/test_backtracefilter.rb +41 -0
  156. data/test-unit/test/util/test_observable.rb +102 -0
  157. data/test-unit/test/util/test_procwrapper.rb +36 -0
  158. data/test/.gitignore +1 -0
  159. data/test/groonga-test-utils.rb +133 -0
  160. data/test/run-test.rb +58 -0
  161. data/test/test-array.rb +97 -0
  162. data/test/test-column.rb +316 -0
  163. data/test/test-context-select.rb +93 -0
  164. data/test/test-context.rb +73 -0
  165. data/test/test-database.rb +113 -0
  166. data/test/test-encoding.rb +33 -0
  167. data/test/test-exception.rb +93 -0
  168. data/test/test-expression-builder.rb +156 -0
  169. data/test/test-expression.rb +134 -0
  170. data/test/test-fix-size-column.rb +65 -0
  171. data/test/test-gqtp.rb +72 -0
  172. data/test/test-hash.rb +312 -0
  173. data/test/test-index-column.rb +81 -0
  174. data/test/test-logger.rb +37 -0
  175. data/test/test-patricia-trie.rb +189 -0
  176. data/test/test-procedure.rb +37 -0
  177. data/test/test-query.rb +22 -0
  178. data/test/test-record.rb +279 -0
  179. data/test/test-remote.rb +54 -0
  180. data/test/test-schema-view.rb +90 -0
  181. data/test/test-schema.rb +459 -0
  182. data/test/test-snippet.rb +130 -0
  183. data/test/test-table-cursor.rb +153 -0
  184. data/test/test-table-offset-and-limit.rb +102 -0
  185. data/test/test-table-select-normalize.rb +53 -0
  186. data/test/test-table-select.rb +150 -0
  187. data/test/test-table.rb +690 -0
  188. data/test/test-type.rb +71 -0
  189. data/test/test-variable-size-column.rb +98 -0
  190. data/test/test-variable.rb +28 -0
  191. data/test/test-vector-column.rb +76 -0
  192. data/test/test-version.rb +31 -0
  193. data/test/test-view.rb +72 -0
  194. data/text/TUTORIAL.ja.rdoc +392 -0
  195. data/text/expression.rdoc +284 -0
  196. data/vendor/local/bin/grntest.exe +0 -0
  197. data/vendor/local/bin/groonga.exe +0 -0
  198. data/vendor/local/bin/libgroonga.dll +0 -0
  199. data/vendor/local/bin/libmecab.dll +0 -0
  200. data/vendor/local/include/groonga.h +2285 -0
  201. data/vendor/local/lib/libgroonga.lib +0 -0
  202. metadata +280 -0
@@ -0,0 +1,690 @@
1
+ # Copyright (C) 2009-2010 Kouhei Sutou <kou@clear-code.com>
2
+ #
3
+ # This library is free software; you can redistribute it and/or
4
+ # modify it under the terms of the GNU Lesser General Public
5
+ # License version 2.1 as published by the Free Software Foundation.
6
+ #
7
+ # This library is distributed in the hope that it will be useful,
8
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
9
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10
+ # Lesser General Public License for more details.
11
+ #
12
+ # You should have received a copy of the GNU Lesser General Public
13
+ # License along with this library; if not, write to the Free Software
14
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
+
16
+ class TableTest < Test::Unit::TestCase
17
+ include GroongaTestUtils
18
+
19
+ setup :setup_database
20
+
21
+ def test_create
22
+ table_path = @tables_dir + "bookmarks"
23
+ assert_not_predicate(table_path, :exist?)
24
+ table = Groonga::PatriciaTrie.create(:name => "Bookmarks",
25
+ :path => table_path.to_s)
26
+ assert_equal("Bookmarks", table.name)
27
+ assert_predicate(table_path, :exist?)
28
+ end
29
+
30
+ def test_temporary
31
+ table = Groonga::PatriciaTrie.create
32
+ assert_nil(table.name)
33
+ assert_predicate(table, :temporary?)
34
+ assert_not_predicate(table, :persistent?)
35
+ assert_equal([], @tables_dir.children)
36
+ end
37
+
38
+ def test_open
39
+ table_path = @tables_dir + "bookmarks"
40
+ table = Groonga::Hash.create(:name => "Bookmarks",
41
+ :path => table_path.to_s)
42
+ assert_equal("Bookmarks", table.name)
43
+
44
+ called = false
45
+ Groonga::Table.open(:name => "Bookmarks") do |_table|
46
+ table = _table
47
+ assert_not_predicate(table, :closed?)
48
+ assert_equal("Bookmarks", _table.name)
49
+ called = true
50
+ end
51
+ assert_true(called)
52
+ assert_predicate(table, :closed?)
53
+ end
54
+
55
+ def test_open_by_path
56
+ table_path = @tables_dir + "bookmarks"
57
+ table = Groonga::PatriciaTrie.create(:name => "Bookmarks",
58
+ :path => table_path.to_s)
59
+ assert_equal("Bookmarks", table.name)
60
+ table.close
61
+
62
+ called = false
63
+ Groonga::Table.open(:path => table_path.to_s) do |_table|
64
+ table = _table
65
+ assert_not_predicate(table, :closed?)
66
+ assert_nil(_table.name)
67
+ called = true
68
+ end
69
+ assert_true(called)
70
+ assert_predicate(table, :closed?)
71
+ end
72
+
73
+ def test_open_override_name
74
+ table_path = @tables_dir + "bookmarks"
75
+ table = Groonga::PatriciaTrie.create(:name => "Bookmarks",
76
+ :path => table_path.to_s)
77
+ assert_equal("Bookmarks", table.name)
78
+ table.close
79
+
80
+ called = false
81
+ Groonga::Table.open(:name => "Marks", :path => table_path.to_s) do |_table|
82
+ table = _table
83
+ assert_not_predicate(table, :closed?)
84
+ assert_equal("Marks", _table.name)
85
+ called = true
86
+ end
87
+ assert_true(called)
88
+ assert_predicate(table, :closed?)
89
+ end
90
+
91
+ def test_open_wrong_table
92
+ table_path = @tables_dir + "bookmarks"
93
+ Groonga::Hash.create(:name => "Bookmarks",
94
+ :path => table_path.to_s) do
95
+ end
96
+
97
+ assert_raise(TypeError) do
98
+ Groonga::PatriciaTrie.open(:name => "Bookmarks",
99
+ :path => table_path.to_s)
100
+ end
101
+ end
102
+
103
+ def test_new
104
+ table_path = @tables_dir + "no-name"
105
+ assert_raise(Groonga::NoSuchFileOrDirectory) do
106
+ Groonga::Hash.new(:path => table_path.to_s)
107
+ end
108
+
109
+ Groonga::Hash.create(:path => table_path.to_s)
110
+ assert_not_predicate(Groonga::Hash.new(:path => table_path.to_s), :closed?)
111
+ end
112
+
113
+ def test_define_column
114
+ table_path = @tables_dir + "bookmarks"
115
+ table = Groonga::Hash.create(:name => "Bookmarks",
116
+ :path => table_path.to_s)
117
+ column = table.define_column("name", "Text")
118
+ assert_equal("Bookmarks.name", column.name)
119
+ assert_equal(column, table.column("name"))
120
+ end
121
+
122
+ def test_temporary_table_define_column_default_persistent
123
+ table = Groonga::Hash.create
124
+ name = table.define_column("name", "ShortText")
125
+ assert_predicate(name, :temporary?)
126
+ end
127
+
128
+ def test_temporary_table_define_index_column_default_persistent
129
+ bookmarks = Groonga::Hash.create(:name => "Bookmarks")
130
+ terms = Groonga::Hash.create
131
+ url = terms.define_index_column("url", bookmarks)
132
+ assert_predicate(url, :temporary?)
133
+ end
134
+
135
+ def test_define_column_default_persistent
136
+ bookmarks = Groonga::Hash.create(:name => "Bookmarks")
137
+ real_name = bookmarks.define_column("real_name", "ShortText")
138
+ assert_predicate(real_name, :persistent?)
139
+ end
140
+
141
+ def test_define_column_not_persistent
142
+ bookmarks = Groonga::Hash.create(:name => "Bookmarks")
143
+ real_name = bookmarks.define_column("real_name", "ShortText",
144
+ :persistent => false)
145
+ assert_predicate(real_name, :temporary?)
146
+ end
147
+
148
+ def test_define_column_not_persistent_and_path
149
+ column_path = @tables_dir + "bookmakrs.real_name.column"
150
+
151
+ bookmarks = Groonga::Hash.create(:name => "Bookmarks")
152
+ message = "should not pass :path if :persistent is false: <#{column_path}>"
153
+ assert_raise(ArgumentError.new(message)) do
154
+ real_name = bookmarks.define_column("real_name", "ShortText",
155
+ :path => column_path.to_s,
156
+ :persistent => false)
157
+ end
158
+ end
159
+
160
+ def test_define_index_column_default_persistent
161
+ bookmarks = Groonga::Hash.create(:name => "Bookmarks")
162
+ terms = Groonga::Hash.create(:name => "Terms")
163
+ real_name = terms.define_index_column("real_name", bookmarks)
164
+ assert_predicate(real_name, :persistent?)
165
+ end
166
+
167
+ def test_define_index_column_not_persistent
168
+ bookmarks = Groonga::Hash.create(:name => "Bookmarks")
169
+ terms = Groonga::Hash.create(:name => "Terms")
170
+ real_name = terms.define_index_column("real_name", bookmarks,
171
+ :persistent => false)
172
+ assert_predicate(real_name, :temporary?)
173
+ end
174
+
175
+ def test_define_index_column_not_persistent_and_path
176
+ column_path = @tables_dir + "bookmakrs.real_name.column"
177
+
178
+ bookmarks = Groonga::Hash.create(:name => "Bookmarks")
179
+ terms = Groonga::Hash.create(:name => "Terms")
180
+ message = "should not pass :path if :persistent is false: <#{column_path}>"
181
+ assert_raise(ArgumentError.new(message)) do
182
+ real_name = terms.define_index_column("real_name", bookmarks,
183
+ :path => column_path.to_s,
184
+ :persistent => false)
185
+ end
186
+ end
187
+
188
+ def test_define_index_column
189
+ bookmarks = Groonga::Hash.create(:name => "Bookmarks")
190
+ bookmarks.define_column("content", "Text")
191
+ terms = Groonga::Hash.create(:name => "Terms")
192
+ terms.default_tokenizer = "TokenBigram"
193
+ index = terms.define_index_column("content_index", bookmarks,
194
+ :with_section => true,
195
+ :source => "Bookmarks.content")
196
+ bookmarks.add("google", :content => "Search engine")
197
+ assert_equal(["google"],
198
+ index.search("engine").collect {|record| record.key.key})
199
+ end
200
+
201
+ def test_add_column
202
+ bookmarks = Groonga::Hash.create(:name => "Bookmarks",
203
+ :path => (@tables_dir + "bookmarks").to_s)
204
+
205
+ description_column_path = @columns_dir + "description"
206
+ bookmarks_description =
207
+ bookmarks.define_index_column("description", "Text",
208
+ :path => description_column_path.to_s)
209
+
210
+ books = Groonga::Hash.create(:name => "Books",
211
+ :path => (@tables_dir + "books").to_s)
212
+ books_description = books.add_column("description",
213
+ "LongText",
214
+ description_column_path.to_s)
215
+ assert_equal("Books.description", books_description.name)
216
+ assert_equal(books_description, books.column("description"))
217
+
218
+ assert_equal(bookmarks_description, bookmarks.column("description"))
219
+ end
220
+
221
+ def test_column_nonexistent
222
+ table_path = @tables_dir + "bookmarks"
223
+ table = Groonga::Hash.create(:name => "Bookmarks",
224
+ :path => table_path.to_s)
225
+ assert_nil(table.column("nonexistent"))
226
+ end
227
+
228
+ def test_set_value
229
+ table_path = @tables_dir + "bookmarks"
230
+ bookmarks = Groonga::Hash.create(:name => "Bookmarks",
231
+ :value_type => "Int32",
232
+ :path => table_path.to_s)
233
+ comment_column_path = @columns_dir + "comment"
234
+ bookmarks_comment =
235
+ bookmarks.define_column("comment", "ShortText",
236
+ :type => "scalar",
237
+ :path => comment_column_path.to_s)
238
+ groonga = bookmarks.add("groonga")
239
+ groonga.value = 29
240
+ bookmarks_comment[groonga.id] = "fulltext search engine"
241
+
242
+ assert_equal([29, "fulltext search engine"],
243
+ [groonga.value, bookmarks_comment[groonga.id]])
244
+ end
245
+
246
+ def test_array_set
247
+ bookmarks = Groonga::Hash.create(:name => "Bookmarks",
248
+ :value_type => "Int32")
249
+ bookmarks.set_value("groonga", 29)
250
+
251
+ values = bookmarks.records.collect do |record|
252
+ record.value
253
+ end
254
+ assert_equal([29], values)
255
+ end
256
+
257
+ def test_add_without_name
258
+ users_path = @tables_dir + "users"
259
+ users = Groonga::Array.create(:name => "Users",
260
+ :path => users_path.to_s)
261
+ name_column_path = @columns_dir + "name"
262
+ users_name = users.define_column("name", "ShortText",
263
+ :path => name_column_path.to_s)
264
+ morita = users.add
265
+ users_name[morita.id] = "morita"
266
+ assert_equal("morita", users_name[morita.id])
267
+ end
268
+
269
+ def test_add_by_id
270
+ users_path = @tables_dir + "users"
271
+ users = Groonga::Hash.create(:name => "Users",
272
+ :path => users_path.to_s)
273
+
274
+ bookmarks_path = @tables_dir + "bookmarks"
275
+ bookmarks = Groonga::Hash.create(:name => "Bookmarks",
276
+ :key_type => users,
277
+ :value_type => "Int32",
278
+ :path => bookmarks_path.to_s)
279
+ morita = users.add("morita")
280
+ groonga = bookmarks.add(morita.id)
281
+ groonga.value = 29
282
+ assert_equal(29, groonga.value)
283
+ end
284
+
285
+ def test_columns
286
+ bookmarks_path = @tables_dir + "bookmarks"
287
+ bookmarks = Groonga::Array.create(:name => "Bookmarks",
288
+ :path => bookmarks_path.to_s)
289
+
290
+ uri_column = bookmarks.define_column("uri", "ShortText")
291
+ comment_column = bookmarks.define_column("comment", "Text")
292
+ assert_equal([uri_column.name, comment_column.name].sort,
293
+ bookmarks.columns.collect {|column| column.name}.sort)
294
+ end
295
+
296
+ def test_column_by_symbol
297
+ bookmarks_path = @tables_dir + "bookmarks"
298
+ bookmarks = Groonga::Array.create(:name => "Bookmarks",
299
+ :path => bookmarks_path.to_s)
300
+
301
+ uri_column = bookmarks.define_column("uri", "Text")
302
+ assert_equal(uri_column, bookmarks.column(:uri))
303
+ end
304
+
305
+ def test_size
306
+ bookmarks_path = @tables_dir + "bookmarks"
307
+ bookmarks = Groonga::Array.create(:name => "Bookmarks",
308
+ :path => bookmarks_path.to_s)
309
+
310
+ assert_equal(0, bookmarks.size)
311
+
312
+ bookmarks.add
313
+ bookmarks.add
314
+ bookmarks.add
315
+
316
+ assert_equal(3, bookmarks.size)
317
+ end
318
+
319
+ def test_empty?
320
+ bookmarks_path = @tables_dir + "bookmarks"
321
+ bookmarks = Groonga::Array.create(:name => "Bookmarks",
322
+ :path => bookmarks_path.to_s)
323
+
324
+ assert_predicate(bookmarks, :empty?)
325
+ bookmarks.add
326
+ assert_not_predicate(bookmarks, :empty?)
327
+ end
328
+
329
+ def test_path
330
+ bookmarks_path = @tables_dir + "bookmarks"
331
+ bookmarks = Groonga::Array.create(:name => "Bookmarks",
332
+ :path => bookmarks_path.to_s)
333
+ assert_equal(bookmarks_path.to_s, bookmarks.path)
334
+ end
335
+
336
+ def test_time_column
337
+ bookmarks_path = @tables_dir + "bookmarks"
338
+ bookmarks = Groonga::Array.create(:name => "Bookmarks",
339
+ :path => bookmarks_path.to_s)
340
+ column = bookmarks.define_column("created_at", "Time")
341
+
342
+ bookmark = bookmarks.add
343
+ now = Time.now
344
+ bookmark["created_at"] = now
345
+ assert_equal(now.to_a,
346
+ bookmark["created_at"].to_a)
347
+ end
348
+
349
+ def test_delete
350
+ bookmarks_path = @tables_dir + "bookmarks"
351
+ bookmarks = Groonga::Array.create(:name => "Bookmarks",
352
+ :path => bookmarks_path.to_s)
353
+
354
+ bookmark1 = bookmarks.add
355
+ bookmark2 = bookmarks.add
356
+ bookmark3 = bookmarks.add
357
+
358
+ assert_equal(3, bookmarks.size)
359
+ bookmarks.delete(bookmark2.id)
360
+ assert_equal(2, bookmarks.size)
361
+ end
362
+
363
+ def test_remove
364
+ bookmarks_path = @tables_dir + "bookmarks"
365
+ bookmarks = Groonga::Array.create(:name => "Bookmarks",
366
+ :path => bookmarks_path.to_s)
367
+ assert_predicate(bookmarks_path, :exist?)
368
+ bookmarks.remove
369
+ assert_not_predicate(bookmarks_path, :exist?)
370
+ end
371
+
372
+ def test_temporary_add
373
+ table = Groonga::Hash.create(:key_type => "ShortText")
374
+ assert_equal(0, table.size)
375
+ table.add("key")
376
+ assert_equal(1, table.size)
377
+ end
378
+
379
+ def test_each
380
+ users = Groonga::Array.create(:name => "Users")
381
+ user_name = users.define_column("name", "ShortText")
382
+
383
+ names = ["daijiro", "gunyarakun", "yu"]
384
+ names.each do |name|
385
+ user = users.add
386
+ user["name"] = name
387
+ end
388
+
389
+ assert_equal(names.sort, users.collect {|user| user["name"]}.sort)
390
+ end
391
+
392
+ def test_truncate
393
+ users = Groonga::Array.create(:name => "Users")
394
+ users.add
395
+ users.add
396
+ users.add
397
+ assert_equal(3, users.size)
398
+ assert_nothing_raised do
399
+ users.truncate
400
+ end
401
+ omit("truncate isn't implemented in groonga.")
402
+ assert_equal(0, users.size)
403
+ end
404
+
405
+ def test_sort
406
+ bookmarks = create_bookmarks
407
+ add_shuffled_ids(bookmarks)
408
+
409
+ results = bookmarks.sort([
410
+ {
411
+ :key => "id",
412
+ :order => :descending,
413
+ },
414
+ ],
415
+ :limit => 20)
416
+ assert_equal((180..199).to_a.reverse,
417
+ results.collect {|record| record["id"]})
418
+ end
419
+
420
+ def test_sort_simple
421
+ bookmarks = create_bookmarks
422
+ add_shuffled_ids(bookmarks)
423
+
424
+ results = bookmarks.sort(["id"], :limit => 20)
425
+ assert_equal((100..119).to_a,
426
+ results.collect {|record| record["id"]})
427
+ end
428
+
429
+ def test_sort_by_array
430
+ bookmarks = create_bookmarks
431
+ add_shuffled_ids(bookmarks)
432
+
433
+ results = bookmarks.sort([["id", "descending"]], :limit => 20)
434
+ assert_equal((180..199).to_a.reverse,
435
+ results.collect {|record| record["id"]})
436
+ end
437
+
438
+ def test_sort_without_limit_and_offset
439
+ bookmarks = create_bookmarks
440
+ add_shuffled_ids(bookmarks)
441
+
442
+ results = bookmarks.sort([{:key => "id", :order => :descending}])
443
+ assert_equal((100..199).to_a.reverse,
444
+ results.collect {|record| record["id"]})
445
+ end
446
+
447
+ def test_sort_with_limit
448
+ bookmarks = create_bookmarks
449
+ add_shuffled_ids(bookmarks)
450
+
451
+ results = bookmarks.sort([{:key => "id", :order => :descending}],
452
+ :limit => 20)
453
+ assert_equal((180..199).to_a.reverse,
454
+ results.collect {|record| record["id"]})
455
+ end
456
+
457
+ def test_sort_with_offset
458
+ bookmarks = create_bookmarks
459
+ add_shuffled_ids(bookmarks)
460
+
461
+ results = bookmarks.sort([{:key => "id", :order => :descending}],
462
+ :offset => 20)
463
+ assert_equal((100..179).to_a.reverse,
464
+ results.collect {|record| record["id"]})
465
+ end
466
+
467
+ def test_sort_with_limit_and_offset
468
+ bookmarks = create_bookmarks
469
+ add_shuffled_ids(bookmarks)
470
+
471
+ results = bookmarks.sort([{:key => "id", :order => :descending}],
472
+ :limit => 20, :offset => 20)
473
+ assert_equal((160..179).to_a.reverse,
474
+ results.collect {|record| record["id"]})
475
+ end
476
+
477
+ def test_sort_with_nonexistent_key
478
+ bookmarks = create_bookmarks
479
+ add_shuffled_ids(bookmarks)
480
+ message = "no such column: <\"nonexistent\">: <#{bookmarks.inspect}>"
481
+ assert_raise(Groonga::NoSuchColumn.new(message)) do
482
+ bookmarks.sort([{:key => "nonexistent", :order => :descending}])
483
+ end
484
+ end
485
+
486
+ def test_sort_with_nonexistent_value
487
+ bookmarks = create_bookmarks
488
+ bookmarks.define_column("uri", "ShortText")
489
+ empty1 = bookmarks.add
490
+ groonga = bookmarks.add(:uri => "http://groonga.org/")
491
+ empty2 = bookmarks.add
492
+ assert_equal([groonga, empty1, empty2],
493
+ bookmarks.sort([{:key => "uri", :order => :descending}]))
494
+ end
495
+
496
+ def test_group
497
+ bookmarks = Groonga::Hash.create(:name => "Bookmarks")
498
+ bookmarks.define_column("title", "Text")
499
+ comments = Groonga::Array.create(:name => "Comments")
500
+ comments.define_column("bookmark", bookmarks)
501
+ comments.define_column("content", "Text")
502
+ comments.define_column("issued", "Int32")
503
+
504
+ groonga = bookmarks.add("http://groonga.org/", :title => "groonga")
505
+ ruby = bookmarks.add("http://ruby-lang.org/", :title => "Ruby")
506
+
507
+ now = Time.now.to_i
508
+ comments.add(:bookmark => groonga,
509
+ :content => "full-text search",
510
+ :issued => now)
511
+ comments.add(:bookmark => groonga,
512
+ :content => "column store",
513
+ :issued => now)
514
+ comments.add(:bookmark => ruby,
515
+ :content => "object oriented script language",
516
+ :issued => now)
517
+
518
+ records = comments.select do |record|
519
+ record["issued"] > 0
520
+ end
521
+ assert_equal([[2, "groonga", "http://groonga.org/"],
522
+ [1, "Ruby", "http://ruby-lang.org/"]],
523
+ records.group(".bookmark").collect do |record|
524
+ bookmark = record.key
525
+ [record.n_sub_records,
526
+ bookmark["title"],
527
+ bookmark.key]
528
+ end)
529
+ end
530
+
531
+ def test_union!
532
+ bookmarks = Groonga::Hash.create(:name => "Bookmarks")
533
+ bookmarks.define_column("title", "ShortText")
534
+
535
+ groonga = bookmarks.add("http://groonga.org/", :title => "groonga")
536
+ ruby = bookmarks.add("http://ruby-lang.org/", :title => "Ruby")
537
+
538
+ ruby_bookmarks = bookmarks.select {|record| record["title"] == "Ruby"}
539
+ groonga_bookmarks = bookmarks.select {|record| record["title"] == "groonga"}
540
+ assert_equal(["Ruby", "groonga"],
541
+ ruby_bookmarks.union!(groonga_bookmarks).collect do |record|
542
+ record[".title"]
543
+ end)
544
+ end
545
+
546
+ def test_intersection!
547
+ bookmarks = Groonga::Hash.create(:name => "bookmarks")
548
+ bookmarks.define_column("title", "ShortText")
549
+
550
+ bookmarks.add("http://groonga.org/", :title => "groonga")
551
+ bookmarks.add("http://ruby-lang.org/", :title => "Ruby")
552
+
553
+ ruby_bookmarks = bookmarks.select {|record| record["title"] == "Ruby"}
554
+ all_bookmarks = bookmarks.select
555
+ assert_equal(["Ruby"],
556
+ ruby_bookmarks.intersection!(all_bookmarks).collect do |record|
557
+ record[".title"]
558
+ end)
559
+ end
560
+
561
+ def test_difference!
562
+ bookmarks = Groonga::Hash.create(:name => "Bookmarks")
563
+ bookmarks.define_column("title", "ShortText")
564
+
565
+ bookmarks.add("http://groonga.org/", :title => "groonga")
566
+ bookmarks.add("http://ruby-lang.org/", :title => "Ruby")
567
+
568
+ ruby_bookmarks = bookmarks.select {|record| record["title"] == "Ruby"}
569
+ all_bookmarks = bookmarks.select
570
+ assert_equal(["groonga"],
571
+ all_bookmarks.difference!(ruby_bookmarks).collect do |record|
572
+ record[".title"]
573
+ end)
574
+ end
575
+
576
+ def test_merge!
577
+ bookmarks = Groonga::Hash.create(:name => "Bookmarks")
578
+ bookmarks.define_column("title", "ShortText")
579
+
580
+ bookmarks.add("http://groonga.org/", :title => "groonga")
581
+ bookmarks.add("http://ruby-lang.org/", :title => "Ruby")
582
+
583
+ ruby_bookmarks = bookmarks.select {|record| (record["title"] == "Ruby") &
584
+ (record["title"] == "Ruby") }
585
+ all_bookmarks = bookmarks.select
586
+ assert_equal([["groonga", 1], ["Ruby", 2]],
587
+ all_bookmarks.merge!(ruby_bookmarks).collect do |record|
588
+ [record[".title"], record.score]
589
+ end)
590
+ end
591
+
592
+ def test_lock
593
+ bookmarks = Groonga::Array.create(:name => "Bookmarks")
594
+ bookmark = bookmarks.add
595
+
596
+ assert_not_predicate(bookmarks, :locked?)
597
+ bookmarks.lock
598
+ assert_predicate(bookmarks, :locked?)
599
+ bookmarks.unlock
600
+ assert_not_predicate(bookmarks, :locked?)
601
+ end
602
+
603
+ def test_lock_failed
604
+ bookmarks = Groonga::Array.create(:name => "Bookmarks")
605
+ bookmark = bookmarks.add
606
+
607
+ bookmarks.lock
608
+ assert_raise(Groonga::ResourceDeadlockAvoided) do
609
+ bookmarks.lock
610
+ end
611
+ end
612
+
613
+ def test_lock_block
614
+ bookmarks = Groonga::Array.create(:name => "Bookmarks")
615
+ bookmark = bookmarks.add
616
+
617
+ assert_not_predicate(bookmarks, :locked?)
618
+ bookmarks.lock do
619
+ assert_predicate(bookmarks, :locked?)
620
+ end
621
+ assert_not_predicate(bookmarks, :locked?)
622
+ end
623
+
624
+ def test_clear_lock
625
+ bookmarks = Groonga::Array.create(:name => "Bookmarks")
626
+ bookmark = bookmarks.add
627
+
628
+ assert_not_predicate(bookmarks, :locked?)
629
+ bookmarks.lock
630
+ assert_predicate(bookmarks, :locked?)
631
+ bookmarks.clear_lock
632
+ assert_not_predicate(bookmarks, :locked?)
633
+ end
634
+
635
+ def test_auto_record_register
636
+ users = Groonga::Hash.create(:name => "Users",
637
+ :key_type => "ShortText")
638
+ books = Groonga::Hash.create(:name => "Books",
639
+ :key_type => "ShortText")
640
+ users.define_column("book", "Books")
641
+
642
+ assert_equal([], books.select.collect {|book| book.key})
643
+ users.add("ryoqun", :book => "XP")
644
+ assert_equal([books["XP"]],
645
+ books.select.collect {|book| book.key})
646
+ end
647
+
648
+ def test_get_common_prefix_column
649
+ users = Groonga::Array.create(:name => "Users")
650
+ name_kana = users.define_column("name_kana", "ShortText")
651
+ name = users.define_column("name", "ShortText")
652
+
653
+ assert_equal(name, users.column("name"))
654
+ end
655
+
656
+ def test_empty_reference_column_value
657
+ users = Groonga::Hash.create(:name => "Users",
658
+ :key_type => "ShortText")
659
+ books = Groonga::Hash.create(:name => "Books",
660
+ :key_type => "ShortText")
661
+ book = users.define_column("book", books)
662
+ users.add("morita", :book => "")
663
+ assert_equal({"id" => 1, "key" => "morita", "book" => nil},
664
+ users["morita"].attributes)
665
+ end
666
+
667
+ def test_have_column
668
+ users = Groonga::Hash.create(:name => "Users",
669
+ :key_type => "ShortText")
670
+ users.define_column("name", "ShortText")
671
+ assert_true(users.have_column?("name"), "name")
672
+ assert_false(users.have_column?("description"), "description")
673
+ end
674
+
675
+ private
676
+ def create_bookmarks
677
+ bookmarks = Groonga::Array.create(:name => "Bookmarks")
678
+ bookmarks.define_column("id", "Int32")
679
+ bookmarks
680
+ end
681
+
682
+ def add_shuffled_ids(bookmarks)
683
+ srand(Time.now.to_i)
684
+ (0...100).to_a.shuffle.each do |i|
685
+ bookmark = bookmarks.add
686
+ bookmark["id"] = i + 100
687
+ end
688
+ bookmarks
689
+ end
690
+ end