rroonga 0.9.2-x86-mingw32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (199) 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 +206 -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 +230 -0
  13. data/example/search/public/css/groonga.css +122 -0
  14. data/ext/.gitignore +2 -0
  15. data/ext/groonga/extconf.rb +93 -0
  16. data/ext/groonga/rb-grn-accessor.c +52 -0
  17. data/ext/groonga/rb-grn-array-cursor.c +36 -0
  18. data/ext/groonga/rb-grn-array.c +210 -0
  19. data/ext/groonga/rb-grn-column.c +573 -0
  20. data/ext/groonga/rb-grn-context.c +662 -0
  21. data/ext/groonga/rb-grn-database.c +472 -0
  22. data/ext/groonga/rb-grn-encoding-support.c +64 -0
  23. data/ext/groonga/rb-grn-encoding.c +257 -0
  24. data/ext/groonga/rb-grn-exception.c +1110 -0
  25. data/ext/groonga/rb-grn-expression-builder.c +75 -0
  26. data/ext/groonga/rb-grn-expression.c +731 -0
  27. data/ext/groonga/rb-grn-fix-size-column.c +166 -0
  28. data/ext/groonga/rb-grn-hash-cursor.c +38 -0
  29. data/ext/groonga/rb-grn-hash.c +294 -0
  30. data/ext/groonga/rb-grn-index-column.c +488 -0
  31. data/ext/groonga/rb-grn-logger.c +504 -0
  32. data/ext/groonga/rb-grn-object.c +1369 -0
  33. data/ext/groonga/rb-grn-operation.c +198 -0
  34. data/ext/groonga/rb-grn-patricia-trie-cursor.c +39 -0
  35. data/ext/groonga/rb-grn-patricia-trie.c +488 -0
  36. data/ext/groonga/rb-grn-procedure.c +52 -0
  37. data/ext/groonga/rb-grn-query.c +260 -0
  38. data/ext/groonga/rb-grn-record.c +40 -0
  39. data/ext/groonga/rb-grn-snippet.c +334 -0
  40. data/ext/groonga/rb-grn-table-cursor-key-support.c +69 -0
  41. data/ext/groonga/rb-grn-table-cursor.c +247 -0
  42. data/ext/groonga/rb-grn-table-key-support.c +714 -0
  43. data/ext/groonga/rb-grn-table.c +1977 -0
  44. data/ext/groonga/rb-grn-type.c +181 -0
  45. data/ext/groonga/rb-grn-utils.c +769 -0
  46. data/ext/groonga/rb-grn-variable-size-column.c +36 -0
  47. data/ext/groonga/rb-grn-variable.c +108 -0
  48. data/ext/groonga/rb-grn-view-accessor.c +53 -0
  49. data/ext/groonga/rb-grn-view-cursor.c +35 -0
  50. data/ext/groonga/rb-grn-view-record.c +41 -0
  51. data/ext/groonga/rb-grn-view.c +421 -0
  52. data/ext/groonga/rb-grn.h +698 -0
  53. data/ext/groonga/rb-groonga.c +107 -0
  54. data/extconf.rb +130 -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/1.8/groonga.so +0 -0
  70. data/lib/1.9/groonga.so +0 -0
  71. data/lib/groonga.rb +90 -0
  72. data/lib/groonga/context.rb +184 -0
  73. data/lib/groonga/expression-builder.rb +324 -0
  74. data/lib/groonga/patricia-trie.rb +85 -0
  75. data/lib/groonga/record.rb +311 -0
  76. data/lib/groonga/schema.rb +1191 -0
  77. data/lib/groonga/view-record.rb +56 -0
  78. data/license/GPL +340 -0
  79. data/license/LGPL +504 -0
  80. data/license/RUBY +59 -0
  81. data/misc/grnop2ruby.rb +49 -0
  82. data/pkg-config.rb +333 -0
  83. data/rroonga-build.rb +57 -0
  84. data/test-unit/Rakefile +40 -0
  85. data/test-unit/TODO +5 -0
  86. data/test-unit/bin/testrb +5 -0
  87. data/test-unit/html/classic.html +15 -0
  88. data/test-unit/html/index.html +25 -0
  89. data/test-unit/html/index.html.ja +27 -0
  90. data/test-unit/lib/test/unit.rb +323 -0
  91. data/test-unit/lib/test/unit/assertionfailederror.rb +25 -0
  92. data/test-unit/lib/test/unit/assertions.rb +1230 -0
  93. data/test-unit/lib/test/unit/attribute.rb +125 -0
  94. data/test-unit/lib/test/unit/autorunner.rb +360 -0
  95. data/test-unit/lib/test/unit/collector.rb +36 -0
  96. data/test-unit/lib/test/unit/collector/descendant.rb +23 -0
  97. data/test-unit/lib/test/unit/collector/dir.rb +108 -0
  98. data/test-unit/lib/test/unit/collector/load.rb +144 -0
  99. data/test-unit/lib/test/unit/collector/objectspace.rb +34 -0
  100. data/test-unit/lib/test/unit/color-scheme.rb +102 -0
  101. data/test-unit/lib/test/unit/color.rb +96 -0
  102. data/test-unit/lib/test/unit/diff.rb +724 -0
  103. data/test-unit/lib/test/unit/error.rb +130 -0
  104. data/test-unit/lib/test/unit/exceptionhandler.rb +39 -0
  105. data/test-unit/lib/test/unit/failure.rb +136 -0
  106. data/test-unit/lib/test/unit/fixture.rb +176 -0
  107. data/test-unit/lib/test/unit/notification.rb +129 -0
  108. data/test-unit/lib/test/unit/omission.rb +191 -0
  109. data/test-unit/lib/test/unit/pending.rb +150 -0
  110. data/test-unit/lib/test/unit/priority.rb +180 -0
  111. data/test-unit/lib/test/unit/runner/console.rb +52 -0
  112. data/test-unit/lib/test/unit/runner/emacs.rb +8 -0
  113. data/test-unit/lib/test/unit/runner/tap.rb +8 -0
  114. data/test-unit/lib/test/unit/testcase.rb +476 -0
  115. data/test-unit/lib/test/unit/testresult.rb +89 -0
  116. data/test-unit/lib/test/unit/testsuite.rb +110 -0
  117. data/test-unit/lib/test/unit/ui/console/outputlevel.rb +14 -0
  118. data/test-unit/lib/test/unit/ui/console/testrunner.rb +466 -0
  119. data/test-unit/lib/test/unit/ui/emacs/testrunner.rb +63 -0
  120. data/test-unit/lib/test/unit/ui/tap/testrunner.rb +92 -0
  121. data/test-unit/lib/test/unit/ui/testrunner.rb +28 -0
  122. data/test-unit/lib/test/unit/ui/testrunnermediator.rb +77 -0
  123. data/test-unit/lib/test/unit/ui/testrunnerutilities.rb +41 -0
  124. data/test-unit/lib/test/unit/util/backtracefilter.rb +41 -0
  125. data/test-unit/lib/test/unit/util/method-owner-finder.rb +28 -0
  126. data/test-unit/lib/test/unit/util/observable.rb +90 -0
  127. data/test-unit/lib/test/unit/util/procwrapper.rb +48 -0
  128. data/test-unit/lib/test/unit/version.rb +7 -0
  129. data/test-unit/sample/adder.rb +13 -0
  130. data/test-unit/sample/subtracter.rb +12 -0
  131. data/test-unit/sample/test_adder.rb +20 -0
  132. data/test-unit/sample/test_subtracter.rb +20 -0
  133. data/test-unit/sample/test_user.rb +23 -0
  134. data/test-unit/test/collector/test-descendant.rb +133 -0
  135. data/test-unit/test/collector/test-load.rb +442 -0
  136. data/test-unit/test/collector/test_dir.rb +406 -0
  137. data/test-unit/test/collector/test_objectspace.rb +100 -0
  138. data/test-unit/test/run-test.rb +15 -0
  139. data/test-unit/test/test-attribute.rb +86 -0
  140. data/test-unit/test/test-color-scheme.rb +67 -0
  141. data/test-unit/test/test-color.rb +47 -0
  142. data/test-unit/test/test-diff.rb +518 -0
  143. data/test-unit/test/test-emacs-runner.rb +60 -0
  144. data/test-unit/test/test-fixture.rb +287 -0
  145. data/test-unit/test/test-notification.rb +33 -0
  146. data/test-unit/test/test-omission.rb +81 -0
  147. data/test-unit/test/test-pending.rb +70 -0
  148. data/test-unit/test/test-priority.rb +119 -0
  149. data/test-unit/test/test-testcase.rb +544 -0
  150. data/test-unit/test/test_assertions.rb +1151 -0
  151. data/test-unit/test/test_error.rb +26 -0
  152. data/test-unit/test/test_failure.rb +33 -0
  153. data/test-unit/test/test_testresult.rb +113 -0
  154. data/test-unit/test/test_testsuite.rb +129 -0
  155. data/test-unit/test/testunit-test-util.rb +14 -0
  156. data/test-unit/test/ui/test_testrunmediator.rb +20 -0
  157. data/test-unit/test/util/test-method-owner-finder.rb +38 -0
  158. data/test-unit/test/util/test_backtracefilter.rb +41 -0
  159. data/test-unit/test/util/test_observable.rb +102 -0
  160. data/test-unit/test/util/test_procwrapper.rb +36 -0
  161. data/test/.gitignore +1 -0
  162. data/test/groonga-test-utils.rb +134 -0
  163. data/test/run-test.rb +58 -0
  164. data/test/test-array.rb +90 -0
  165. data/test/test-column.rb +316 -0
  166. data/test/test-context-select.rb +93 -0
  167. data/test/test-context.rb +73 -0
  168. data/test/test-database.rb +113 -0
  169. data/test/test-encoding.rb +33 -0
  170. data/test/test-exception.rb +93 -0
  171. data/test/test-expression-builder.rb +217 -0
  172. data/test/test-expression.rb +134 -0
  173. data/test/test-fix-size-column.rb +65 -0
  174. data/test/test-gqtp.rb +72 -0
  175. data/test/test-hash.rb +305 -0
  176. data/test/test-index-column.rb +81 -0
  177. data/test/test-logger.rb +37 -0
  178. data/test/test-patricia-trie.rb +205 -0
  179. data/test/test-procedure.rb +37 -0
  180. data/test/test-query.rb +22 -0
  181. data/test/test-record.rb +243 -0
  182. data/test/test-remote.rb +54 -0
  183. data/test/test-schema-view.rb +90 -0
  184. data/test/test-schema.rb +459 -0
  185. data/test/test-snippet.rb +130 -0
  186. data/test/test-table-cursor.rb +153 -0
  187. data/test/test-table-offset-and-limit.rb +102 -0
  188. data/test/test-table-select-normalize.rb +53 -0
  189. data/test/test-table-select.rb +150 -0
  190. data/test/test-table.rb +594 -0
  191. data/test/test-type.rb +71 -0
  192. data/test/test-variable-size-column.rb +98 -0
  193. data/test/test-variable.rb +28 -0
  194. data/test/test-vector-column.rb +76 -0
  195. data/test/test-version.rb +31 -0
  196. data/test/test-view.rb +72 -0
  197. data/text/TUTORIAL.ja.rdoc +392 -0
  198. data/text/expression.rdoc +284 -0
  199. metadata +276 -0
@@ -0,0 +1,594 @@
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_define_column
39
+ table_path = @tables_dir + "bookmarks"
40
+ table = Groonga::Hash.create(:name => "Bookmarks",
41
+ :path => table_path.to_s)
42
+ column = table.define_column("name", "Text")
43
+ assert_equal("Bookmarks.name", column.name)
44
+ assert_equal(column, table.column("name"))
45
+ end
46
+
47
+ def test_temporary_table_define_column_default_persistent
48
+ table = Groonga::Hash.create
49
+ name = table.define_column("name", "ShortText")
50
+ assert_predicate(name, :temporary?)
51
+ end
52
+
53
+ def test_temporary_table_define_index_column_default_persistent
54
+ bookmarks = Groonga::Hash.create(:name => "Bookmarks")
55
+ terms = Groonga::Hash.create
56
+ url = terms.define_index_column("url", bookmarks)
57
+ assert_predicate(url, :temporary?)
58
+ end
59
+
60
+ def test_define_column_default_persistent
61
+ bookmarks = Groonga::Hash.create(:name => "Bookmarks")
62
+ real_name = bookmarks.define_column("real_name", "ShortText")
63
+ assert_predicate(real_name, :persistent?)
64
+ end
65
+
66
+ def test_define_column_not_persistent
67
+ bookmarks = Groonga::Hash.create(:name => "Bookmarks")
68
+ real_name = bookmarks.define_column("real_name", "ShortText",
69
+ :persistent => false)
70
+ assert_predicate(real_name, :temporary?)
71
+ end
72
+
73
+ def test_define_column_not_persistent_and_path
74
+ column_path = @tables_dir + "bookmakrs.real_name.column"
75
+
76
+ bookmarks = Groonga::Hash.create(:name => "Bookmarks")
77
+ message = "should not pass :path if :persistent is false: <#{column_path}>"
78
+ assert_raise(ArgumentError.new(message)) do
79
+ real_name = bookmarks.define_column("real_name", "ShortText",
80
+ :path => column_path.to_s,
81
+ :persistent => false)
82
+ end
83
+ end
84
+
85
+ def test_define_index_column_default_persistent
86
+ bookmarks = Groonga::Hash.create(:name => "Bookmarks")
87
+ terms = Groonga::Hash.create(:name => "Terms")
88
+ real_name = terms.define_index_column("real_name", bookmarks)
89
+ assert_predicate(real_name, :persistent?)
90
+ end
91
+
92
+ def test_define_index_column_not_persistent
93
+ bookmarks = Groonga::Hash.create(:name => "Bookmarks")
94
+ terms = Groonga::Hash.create(:name => "Terms")
95
+ real_name = terms.define_index_column("real_name", bookmarks,
96
+ :persistent => false)
97
+ assert_predicate(real_name, :temporary?)
98
+ end
99
+
100
+ def test_define_index_column_not_persistent_and_path
101
+ column_path = @tables_dir + "bookmakrs.real_name.column"
102
+
103
+ bookmarks = Groonga::Hash.create(:name => "Bookmarks")
104
+ terms = Groonga::Hash.create(:name => "Terms")
105
+ message = "should not pass :path if :persistent is false: <#{column_path}>"
106
+ assert_raise(ArgumentError.new(message)) do
107
+ real_name = terms.define_index_column("real_name", bookmarks,
108
+ :path => column_path.to_s,
109
+ :persistent => false)
110
+ end
111
+ end
112
+
113
+ def test_define_index_column
114
+ bookmarks = Groonga::Hash.create(:name => "Bookmarks")
115
+ bookmarks.define_column("content", "Text")
116
+ terms = Groonga::Hash.create(:name => "Terms")
117
+ terms.default_tokenizer = "TokenBigram"
118
+ index = terms.define_index_column("content_index", bookmarks,
119
+ :with_section => true,
120
+ :source => "Bookmarks.content")
121
+ bookmarks.add("google", :content => "Search engine")
122
+ assert_equal(["google"],
123
+ index.search("engine").collect {|record| record.key.key})
124
+ end
125
+
126
+ def test_column_nonexistent
127
+ table_path = @tables_dir + "bookmarks"
128
+ table = Groonga::Hash.create(:name => "Bookmarks",
129
+ :path => table_path.to_s)
130
+ assert_nil(table.column("nonexistent"))
131
+ end
132
+
133
+ def test_set_value
134
+ table_path = @tables_dir + "bookmarks"
135
+ bookmarks = Groonga::Hash.create(:name => "Bookmarks",
136
+ :value_type => "Int32",
137
+ :path => table_path.to_s)
138
+ comment_column_path = @columns_dir + "comment"
139
+ bookmarks_comment =
140
+ bookmarks.define_column("comment", "ShortText",
141
+ :type => "scalar",
142
+ :path => comment_column_path.to_s)
143
+ groonga = bookmarks.add("groonga")
144
+ groonga.value = 29
145
+ bookmarks_comment[groonga.id] = "fulltext search engine"
146
+
147
+ assert_equal([29, "fulltext search engine"],
148
+ [groonga.value, bookmarks_comment[groonga.id]])
149
+ end
150
+
151
+ def test_array_set
152
+ bookmarks = Groonga::Hash.create(:name => "Bookmarks",
153
+ :value_type => "Int32")
154
+ bookmarks.set_value("groonga", 29)
155
+
156
+ values = bookmarks.records.collect do |record|
157
+ record.value
158
+ end
159
+ assert_equal([29], values)
160
+ end
161
+
162
+ def test_add_without_name
163
+ users_path = @tables_dir + "users"
164
+ users = Groonga::Array.create(:name => "Users",
165
+ :path => users_path.to_s)
166
+ name_column_path = @columns_dir + "name"
167
+ users_name = users.define_column("name", "ShortText",
168
+ :path => name_column_path.to_s)
169
+ morita = users.add
170
+ users_name[morita.id] = "morita"
171
+ assert_equal("morita", users_name[morita.id])
172
+ end
173
+
174
+ def test_add_by_id
175
+ users_path = @tables_dir + "users"
176
+ users = Groonga::Hash.create(:name => "Users",
177
+ :path => users_path.to_s)
178
+
179
+ bookmarks_path = @tables_dir + "bookmarks"
180
+ bookmarks = Groonga::Hash.create(:name => "Bookmarks",
181
+ :key_type => users,
182
+ :value_type => "Int32",
183
+ :path => bookmarks_path.to_s)
184
+ morita = users.add("morita")
185
+ groonga = bookmarks.add(morita.id)
186
+ groonga.value = 29
187
+ assert_equal(29, groonga.value)
188
+ end
189
+
190
+ def test_columns
191
+ bookmarks_path = @tables_dir + "bookmarks"
192
+ bookmarks = Groonga::Array.create(:name => "Bookmarks",
193
+ :path => bookmarks_path.to_s)
194
+
195
+ uri_column = bookmarks.define_column("uri", "ShortText")
196
+ comment_column = bookmarks.define_column("comment", "Text")
197
+ assert_equal([uri_column.name, comment_column.name].sort,
198
+ bookmarks.columns.collect {|column| column.name}.sort)
199
+ end
200
+
201
+ def test_column_by_symbol
202
+ bookmarks_path = @tables_dir + "bookmarks"
203
+ bookmarks = Groonga::Array.create(:name => "Bookmarks",
204
+ :path => bookmarks_path.to_s)
205
+
206
+ uri_column = bookmarks.define_column("uri", "Text")
207
+ assert_equal(uri_column, bookmarks.column(:uri))
208
+ end
209
+
210
+ def test_size
211
+ bookmarks_path = @tables_dir + "bookmarks"
212
+ bookmarks = Groonga::Array.create(:name => "Bookmarks",
213
+ :path => bookmarks_path.to_s)
214
+
215
+ assert_equal(0, bookmarks.size)
216
+
217
+ bookmarks.add
218
+ bookmarks.add
219
+ bookmarks.add
220
+
221
+ assert_equal(3, bookmarks.size)
222
+ end
223
+
224
+ def test_empty?
225
+ bookmarks_path = @tables_dir + "bookmarks"
226
+ bookmarks = Groonga::Array.create(:name => "Bookmarks",
227
+ :path => bookmarks_path.to_s)
228
+
229
+ assert_predicate(bookmarks, :empty?)
230
+ bookmarks.add
231
+ assert_not_predicate(bookmarks, :empty?)
232
+ end
233
+
234
+ def test_path
235
+ bookmarks_path = @tables_dir + "bookmarks"
236
+ bookmarks = Groonga::Array.create(:name => "Bookmarks",
237
+ :path => bookmarks_path.to_s)
238
+ assert_equal(bookmarks_path.to_s, bookmarks.path)
239
+ end
240
+
241
+ def test_time_column
242
+ bookmarks_path = @tables_dir + "bookmarks"
243
+ bookmarks = Groonga::Array.create(:name => "Bookmarks",
244
+ :path => bookmarks_path.to_s)
245
+ column = bookmarks.define_column("created_at", "Time")
246
+
247
+ bookmark = bookmarks.add
248
+ now = Time.now
249
+ bookmark["created_at"] = now
250
+ assert_equal(now.to_a,
251
+ bookmark["created_at"].to_a)
252
+ end
253
+
254
+ def test_delete
255
+ bookmarks_path = @tables_dir + "bookmarks"
256
+ bookmarks = Groonga::Array.create(:name => "Bookmarks",
257
+ :path => bookmarks_path.to_s)
258
+
259
+ bookmark1 = bookmarks.add
260
+ bookmark2 = bookmarks.add
261
+ bookmark3 = bookmarks.add
262
+
263
+ assert_equal(3, bookmarks.size)
264
+ bookmarks.delete(bookmark2.id)
265
+ assert_equal(2, bookmarks.size)
266
+ end
267
+
268
+ def test_remove
269
+ bookmarks_path = @tables_dir + "bookmarks"
270
+ bookmarks = Groonga::Array.create(:name => "Bookmarks",
271
+ :path => bookmarks_path.to_s)
272
+ assert_predicate(bookmarks_path, :exist?)
273
+ bookmarks.remove
274
+ assert_not_predicate(bookmarks_path, :exist?)
275
+ end
276
+
277
+ def test_temporary_add
278
+ table = Groonga::Hash.create(:key_type => "ShortText")
279
+ assert_equal(0, table.size)
280
+ table.add("key")
281
+ assert_equal(1, table.size)
282
+ end
283
+
284
+ def test_each
285
+ users = Groonga::Array.create(:name => "Users")
286
+ user_name = users.define_column("name", "ShortText")
287
+
288
+ names = ["daijiro", "gunyarakun", "yu"]
289
+ names.each do |name|
290
+ user = users.add
291
+ user["name"] = name
292
+ end
293
+
294
+ assert_equal(names.sort, users.collect {|user| user["name"]}.sort)
295
+ end
296
+
297
+ def test_truncate
298
+ users = Groonga::Array.create(:name => "Users")
299
+ users.add
300
+ users.add
301
+ users.add
302
+ assert_equal(3, users.size)
303
+ assert_nothing_raised do
304
+ users.truncate
305
+ end
306
+ assert_equal(0, users.size)
307
+ end
308
+
309
+ def test_sort
310
+ bookmarks = create_bookmarks
311
+ add_shuffled_ids(bookmarks)
312
+
313
+ results = bookmarks.sort([
314
+ {
315
+ :key => "id",
316
+ :order => :descending,
317
+ },
318
+ ],
319
+ :limit => 20)
320
+ assert_equal((180..199).to_a.reverse,
321
+ results.collect {|record| record["id"]})
322
+ end
323
+
324
+ def test_sort_simple
325
+ bookmarks = create_bookmarks
326
+ add_shuffled_ids(bookmarks)
327
+
328
+ results = bookmarks.sort(["id"], :limit => 20)
329
+ assert_equal((100..119).to_a,
330
+ results.collect {|record| record["id"]})
331
+ end
332
+
333
+ def test_sort_by_array
334
+ bookmarks = create_bookmarks
335
+ add_shuffled_ids(bookmarks)
336
+
337
+ results = bookmarks.sort([["id", "descending"]], :limit => 20)
338
+ assert_equal((180..199).to_a.reverse,
339
+ results.collect {|record| record["id"]})
340
+ end
341
+
342
+ def test_sort_without_limit_and_offset
343
+ bookmarks = create_bookmarks
344
+ add_shuffled_ids(bookmarks)
345
+
346
+ results = bookmarks.sort([{:key => "id", :order => :descending}])
347
+ assert_equal((100..199).to_a.reverse,
348
+ results.collect {|record| record["id"]})
349
+ end
350
+
351
+ def test_sort_with_limit
352
+ bookmarks = create_bookmarks
353
+ add_shuffled_ids(bookmarks)
354
+
355
+ results = bookmarks.sort([{:key => "id", :order => :descending}],
356
+ :limit => 20)
357
+ assert_equal((180..199).to_a.reverse,
358
+ results.collect {|record| record["id"]})
359
+ end
360
+
361
+ def test_sort_with_offset
362
+ bookmarks = create_bookmarks
363
+ add_shuffled_ids(bookmarks)
364
+
365
+ results = bookmarks.sort([{:key => "id", :order => :descending}],
366
+ :offset => 20)
367
+ assert_equal((100..179).to_a.reverse,
368
+ results.collect {|record| record["id"]})
369
+ end
370
+
371
+ def test_sort_with_limit_and_offset
372
+ bookmarks = create_bookmarks
373
+ add_shuffled_ids(bookmarks)
374
+
375
+ results = bookmarks.sort([{:key => "id", :order => :descending}],
376
+ :limit => 20, :offset => 20)
377
+ assert_equal((160..179).to_a.reverse,
378
+ results.collect {|record| record["id"]})
379
+ end
380
+
381
+ def test_sort_with_nonexistent_key
382
+ bookmarks = create_bookmarks
383
+ add_shuffled_ids(bookmarks)
384
+ message = "no such column: <\"nonexistent\">: <#{bookmarks.inspect}>"
385
+ assert_raise(Groonga::NoSuchColumn.new(message)) do
386
+ bookmarks.sort([{:key => "nonexistent", :order => :descending}])
387
+ end
388
+ end
389
+
390
+ def test_sort_with_nonexistent_value
391
+ bookmarks = create_bookmarks
392
+ bookmarks.define_column("uri", "ShortText")
393
+ empty1 = bookmarks.add
394
+ groonga = bookmarks.add(:uri => "http://groonga.org/")
395
+ empty2 = bookmarks.add
396
+ assert_equal([groonga, empty1, empty2],
397
+ bookmarks.sort([{:key => "uri", :order => :descending}]))
398
+ end
399
+
400
+ def test_group
401
+ bookmarks = Groonga::Hash.create(:name => "Bookmarks")
402
+ bookmarks.define_column("title", "Text")
403
+ comments = Groonga::Array.create(:name => "Comments")
404
+ comments.define_column("bookmark", bookmarks)
405
+ comments.define_column("content", "Text")
406
+ comments.define_column("issued", "Int32")
407
+
408
+ groonga = bookmarks.add("http://groonga.org/", :title => "groonga")
409
+ ruby = bookmarks.add("http://ruby-lang.org/", :title => "Ruby")
410
+
411
+ now = Time.now.to_i
412
+ comments.add(:bookmark => groonga,
413
+ :content => "full-text search",
414
+ :issued => now)
415
+ comments.add(:bookmark => groonga,
416
+ :content => "column store",
417
+ :issued => now)
418
+ comments.add(:bookmark => ruby,
419
+ :content => "object oriented script language",
420
+ :issued => now)
421
+
422
+ records = comments.select do |record|
423
+ record["issued"] > 0
424
+ end
425
+ assert_equal([[2, "groonga", "http://groonga.org/"],
426
+ [1, "Ruby", "http://ruby-lang.org/"]],
427
+ records.group(".bookmark").collect do |record|
428
+ bookmark = record.key
429
+ [record.n_sub_records,
430
+ bookmark["title"],
431
+ bookmark.key]
432
+ end)
433
+ end
434
+
435
+ def test_union!
436
+ bookmarks = Groonga::Hash.create(:name => "Bookmarks")
437
+ bookmarks.define_column("title", "ShortText")
438
+
439
+ groonga = bookmarks.add("http://groonga.org/", :title => "groonga")
440
+ ruby = bookmarks.add("http://ruby-lang.org/", :title => "Ruby")
441
+
442
+ ruby_bookmarks = bookmarks.select {|record| record["title"] == "Ruby"}
443
+ groonga_bookmarks = bookmarks.select {|record| record["title"] == "groonga"}
444
+ assert_equal(["Ruby", "groonga"],
445
+ ruby_bookmarks.union!(groonga_bookmarks).collect do |record|
446
+ record[".title"]
447
+ end)
448
+ end
449
+
450
+ def test_intersection!
451
+ bookmarks = Groonga::Hash.create(:name => "bookmarks")
452
+ bookmarks.define_column("title", "ShortText")
453
+
454
+ bookmarks.add("http://groonga.org/", :title => "groonga")
455
+ bookmarks.add("http://ruby-lang.org/", :title => "Ruby")
456
+
457
+ ruby_bookmarks = bookmarks.select {|record| record["title"] == "Ruby"}
458
+ all_bookmarks = bookmarks.select
459
+ assert_equal(["Ruby"],
460
+ ruby_bookmarks.intersection!(all_bookmarks).collect do |record|
461
+ record[".title"]
462
+ end)
463
+ end
464
+
465
+ def test_difference!
466
+ bookmarks = Groonga::Hash.create(:name => "Bookmarks")
467
+ bookmarks.define_column("title", "ShortText")
468
+
469
+ bookmarks.add("http://groonga.org/", :title => "groonga")
470
+ bookmarks.add("http://ruby-lang.org/", :title => "Ruby")
471
+
472
+ ruby_bookmarks = bookmarks.select {|record| record["title"] == "Ruby"}
473
+ all_bookmarks = bookmarks.select
474
+ assert_equal(["groonga"],
475
+ all_bookmarks.difference!(ruby_bookmarks).collect do |record|
476
+ record[".title"]
477
+ end)
478
+ end
479
+
480
+ def test_merge!
481
+ bookmarks = Groonga::Hash.create(:name => "Bookmarks")
482
+ bookmarks.define_column("title", "ShortText")
483
+
484
+ bookmarks.add("http://groonga.org/", :title => "groonga")
485
+ bookmarks.add("http://ruby-lang.org/", :title => "Ruby")
486
+
487
+ ruby_bookmarks = bookmarks.select {|record| (record["title"] == "Ruby") &
488
+ (record["title"] == "Ruby") }
489
+ all_bookmarks = bookmarks.select
490
+ assert_equal([["groonga", 1], ["Ruby", 2]],
491
+ all_bookmarks.merge!(ruby_bookmarks).collect do |record|
492
+ [record[".title"], record.score]
493
+ end)
494
+ end
495
+
496
+ def test_lock
497
+ bookmarks = Groonga::Array.create(:name => "Bookmarks")
498
+ bookmark = bookmarks.add
499
+
500
+ assert_not_predicate(bookmarks, :locked?)
501
+ bookmarks.lock
502
+ assert_predicate(bookmarks, :locked?)
503
+ bookmarks.unlock
504
+ assert_not_predicate(bookmarks, :locked?)
505
+ end
506
+
507
+ def test_lock_failed
508
+ bookmarks = Groonga::Array.create(:name => "Bookmarks")
509
+ bookmark = bookmarks.add
510
+
511
+ bookmarks.lock
512
+ assert_raise(Groonga::ResourceDeadlockAvoided) do
513
+ bookmarks.lock
514
+ end
515
+ end
516
+
517
+ def test_lock_block
518
+ bookmarks = Groonga::Array.create(:name => "Bookmarks")
519
+ bookmark = bookmarks.add
520
+
521
+ assert_not_predicate(bookmarks, :locked?)
522
+ bookmarks.lock do
523
+ assert_predicate(bookmarks, :locked?)
524
+ end
525
+ assert_not_predicate(bookmarks, :locked?)
526
+ end
527
+
528
+ def test_clear_lock
529
+ bookmarks = Groonga::Array.create(:name => "Bookmarks")
530
+ bookmark = bookmarks.add
531
+
532
+ assert_not_predicate(bookmarks, :locked?)
533
+ bookmarks.lock
534
+ assert_predicate(bookmarks, :locked?)
535
+ bookmarks.clear_lock
536
+ assert_not_predicate(bookmarks, :locked?)
537
+ end
538
+
539
+ def test_auto_record_register
540
+ users = Groonga::Hash.create(:name => "Users",
541
+ :key_type => "ShortText")
542
+ books = Groonga::Hash.create(:name => "Books",
543
+ :key_type => "ShortText")
544
+ users.define_column("book", "Books")
545
+
546
+ assert_equal([], books.select.collect {|book| book.key})
547
+ users.add("ryoqun", :book => "XP")
548
+ assert_equal([books["XP"]],
549
+ books.select.collect {|book| book.key})
550
+ end
551
+
552
+ def test_get_common_prefix_column
553
+ users = Groonga::Array.create(:name => "Users")
554
+ name_kana = users.define_column("name_kana", "ShortText")
555
+ name = users.define_column("name", "ShortText")
556
+
557
+ assert_equal(name, users.column("name"))
558
+ end
559
+
560
+ def test_empty_reference_column_value
561
+ users = Groonga::Hash.create(:name => "Users",
562
+ :key_type => "ShortText")
563
+ books = Groonga::Hash.create(:name => "Books",
564
+ :key_type => "ShortText")
565
+ book = users.define_column("book", books)
566
+ users.add("morita", :book => "")
567
+ assert_equal({"id" => 1, "key" => "morita", "book" => nil},
568
+ users["morita"].attributes)
569
+ end
570
+
571
+ def test_have_column
572
+ users = Groonga::Hash.create(:name => "Users",
573
+ :key_type => "ShortText")
574
+ users.define_column("name", "ShortText")
575
+ assert_true(users.have_column?("name"), "name")
576
+ assert_false(users.have_column?("description"), "description")
577
+ end
578
+
579
+ private
580
+ def create_bookmarks
581
+ bookmarks = Groonga::Array.create(:name => "Bookmarks")
582
+ bookmarks.define_column("id", "Int32")
583
+ bookmarks
584
+ end
585
+
586
+ def add_shuffled_ids(bookmarks)
587
+ srand(Time.now.to_i)
588
+ (0...100).to_a.shuffle.each do |i|
589
+ bookmark = bookmarks.add
590
+ bookmark["id"] = i + 100
591
+ end
592
+ bookmarks
593
+ end
594
+ end