PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/test/ruby/hbase/table_test.rb

https://github.com/jy4618272/hindex
Ruby | 483 lines | 368 code | 77 blank | 38 comment | 9 complexity | c7a531cb6b2fdbf0380060edd7f9f198 MD5 | raw file
Possible License(s): Apache-2.0
  1. #
  2. # Copyright 2010 The Apache Software Foundation
  3. #
  4. # Licensed to the Apache Software Foundation (ASF) under one
  5. # or more contributor license agreements. See the NOTICE file
  6. # distributed with this work for additional information
  7. # regarding copyright ownership. The ASF licenses this file
  8. # to you under the Apache License, Version 2.0 (the
  9. # "License"); you may not use this file except in compliance
  10. # with the License. You may obtain a copy of the License at
  11. #
  12. # http://www.apache.org/licenses/LICENSE-2.0
  13. #
  14. # Unless required by applicable law or agreed to in writing, software
  15. # distributed under the License is distributed on an "AS IS" BASIS,
  16. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. # See the License for the specific language governing permissions and
  18. # limitations under the License.
  19. #
  20. require 'hbase'
  21. include HBaseConstants
  22. module Hbase
  23. # Constructor tests
  24. class TableConstructorTest < Test::Unit::TestCase
  25. include TestHelpers
  26. def setup
  27. setup_hbase
  28. end
  29. define_test "Hbase::Table constructor should fail for non-existent tables" do
  30. assert_raise(NativeException) do
  31. table('non-existent-table-name')
  32. end
  33. end
  34. define_test "Hbase::Table constructor should not fail for existent tables" do
  35. assert_nothing_raised do
  36. table('.META.')
  37. end
  38. end
  39. end
  40. # Helper methods tests
  41. class TableHelpersTest < Test::Unit::TestCase
  42. include TestHelpers
  43. def setup
  44. setup_hbase
  45. # Create test table if it does not exist
  46. @test_name = "hbase_shell_tests_table"
  47. create_test_table(@test_name)
  48. @test_table = table(@test_name)
  49. end
  50. define_test "is_meta_table? method should return true for the meta table" do
  51. assert(table('.META.').is_meta_table?)
  52. end
  53. define_test "is_meta_table? method should return true for the root table" do
  54. assert(table('-ROOT-').is_meta_table?)
  55. end
  56. define_test "is_meta_table? method should return false for a normal table" do
  57. assert(!@test_table.is_meta_table?)
  58. end
  59. #-------------------------------------------------------------------------------
  60. define_test "get_all_columns should return columns list" do
  61. cols = table('.META.').get_all_columns
  62. assert_kind_of(Array, cols)
  63. assert(cols.length > 0)
  64. end
  65. #-------------------------------------------------------------------------------
  66. define_test "parse_column_name should not return a qualifier for name-only column specifiers" do
  67. col, qual = table('.META.').parse_column_name('foo')
  68. assert_not_nil(col)
  69. assert_nil(qual)
  70. end
  71. define_test "parse_column_name should not return a qualifier for family-only column specifiers" do
  72. col, qual = table('.META.').parse_column_name('foo:')
  73. assert_not_nil(col)
  74. assert_nil(qual)
  75. end
  76. define_test "parse_column_name should return a qualifier for family:qualifier column specifiers" do
  77. col, qual = table('.META.').parse_column_name('foo:bar')
  78. assert_not_nil(col)
  79. assert_not_nil(qual)
  80. end
  81. end
  82. # Simple data management methods tests
  83. class TableSimpleMethodsTest < Test::Unit::TestCase
  84. include TestHelpers
  85. def setup
  86. setup_hbase
  87. # Create test table if it does not exist
  88. @test_name = "hbase_shell_tests_table"
  89. create_test_table(@test_name)
  90. @test_table = table(@test_name)
  91. # Insert data to perform delete operations
  92. @test_table.put("101", "x:a", "1")
  93. @test_table.put("101", "x:a", "2", Time.now.to_i)
  94. @test_table.put("102", "x:a", "1",1212)
  95. @test_table.put("102", "x:a", "2", 1213)
  96. @test_table.put(103, "x:a", "3")
  97. @test_table.put(103, "x:a", "4")
  98. @test_table.put("104", "x:a", 5)
  99. @test_table.put("104", "x:b", 6)
  100. @test_table.put(105, "x:a", "3")
  101. @test_table.put(105, "x:a", "4")
  102. end
  103. define_test "put should work without timestamp" do
  104. @test_table.put("123", "x:a", "1")
  105. end
  106. define_test "put should work with timestamp" do
  107. @test_table.put("123", "x:a", "2", Time.now.to_i)
  108. end
  109. define_test "put should work with integer keys" do
  110. @test_table.put(123, "x:a", "3")
  111. end
  112. define_test "put should work with integer values" do
  113. @test_table.put("123", "x:a", 4)
  114. end
  115. #-------------------------------------------------------------------------------
  116. define_test "delete should work without timestamp" do
  117. @test_table.delete("101", "x:a")
  118. res = @test_table.get('101', 'x:a')
  119. assert_nil(res)
  120. end
  121. define_test "delete should work with timestamp" do
  122. @test_table.delete("102", "x:a", 1214)
  123. res = @test_table.get('102', 'x:a')
  124. assert_nil(res)
  125. end
  126. define_test "delete should work with integer keys" do
  127. @test_table.delete(103, "x:a")
  128. res = @test_table.get('103', 'x:a')
  129. assert_nil(res)
  130. end
  131. #-------------------------------------------------------------------------------
  132. define_test "deleteall should work w/o columns and timestamps" do
  133. @test_table.deleteall("104")
  134. res = @test_table.get('104', 'x:a', 'x:b')
  135. assert_nil(res)
  136. end
  137. define_test "deleteall should work with integer keys" do
  138. @test_table.deleteall(105)
  139. res = @test_table.get('105', 'x:a')
  140. assert_nil(res)
  141. end
  142. #-------------------------------------------------------------------------------
  143. define_test "incr should work w/o value" do
  144. @test_table.incr("123", 'x:cnt1')
  145. end
  146. define_test "incr should work with value" do
  147. @test_table.incr("123", 'x:cnt2', 10)
  148. end
  149. define_test "incr should work with integer keys" do
  150. @test_table.incr(123, 'x:cnt3')
  151. end
  152. #-------------------------------------------------------------------------------
  153. define_test "get_counter should work with integer keys" do
  154. @test_table.incr(12345, 'x:cnt')
  155. assert_kind_of(Fixnum, @test_table.get_counter(12345, 'x:cnt'))
  156. end
  157. define_test "get_counter should return nil for non-existent counters" do
  158. assert_nil(@test_table.get_counter(12345, 'x:qqqq'))
  159. end
  160. end
  161. # Complex data management methods tests
  162. class TableComplexMethodsTest < Test::Unit::TestCase
  163. include TestHelpers
  164. def setup
  165. setup_hbase
  166. # Create test table if it does not exist
  167. @test_name = "hbase_shell_tests_table"
  168. create_test_table(@test_name)
  169. @test_table = table(@test_name)
  170. # Test data
  171. @test_ts = 12345678
  172. @test_table.put(1, "x:a", 1)
  173. @test_table.put(1, "x:b", 2, @test_ts)
  174. @test_table.put(2, "x:a", 11)
  175. @test_table.put(2, "x:b", 12, @test_ts)
  176. end
  177. define_test "count should work w/o a block passed" do
  178. assert(@test_table.count > 0)
  179. end
  180. define_test "count should work with a block passed (and yield)" do
  181. rows = []
  182. cnt = @test_table.count(1) do |cnt, row|
  183. rows << row
  184. end
  185. assert(cnt > 0)
  186. assert(!rows.empty?)
  187. end
  188. #-------------------------------------------------------------------------------
  189. define_test "get should work w/o columns specification" do
  190. res = @test_table.get('1')
  191. assert_not_nil(res)
  192. assert_kind_of(Hash, res)
  193. assert_not_nil(res['x:a'])
  194. assert_not_nil(res['x:b'])
  195. end
  196. define_test "get should work with integer keys" do
  197. res = @test_table.get(1)
  198. assert_not_nil(res)
  199. assert_kind_of(Hash, res)
  200. assert_not_nil(res['x:a'])
  201. assert_not_nil(res['x:b'])
  202. end
  203. define_test "get should work with hash columns spec and a single string COLUMN parameter" do
  204. res = @test_table.get('1', COLUMN => 'x:a')
  205. assert_not_nil(res)
  206. assert_kind_of(Hash, res)
  207. assert_not_nil(res['x:a'])
  208. assert_nil(res['x:b'])
  209. end
  210. define_test "get should work with hash columns spec and a single string COLUMNS parameter" do
  211. res = @test_table.get('1', COLUMNS => 'x:a')
  212. assert_not_nil(res)
  213. assert_kind_of(Hash, res)
  214. assert_not_nil(res['x:a'])
  215. assert_nil(res['x:b'])
  216. end
  217. define_test "get should work with hash columns spec and an array of strings COLUMN parameter" do
  218. res = @test_table.get('1', COLUMN => [ 'x:a', 'x:b' ])
  219. assert_not_nil(res)
  220. assert_kind_of(Hash, res)
  221. assert_not_nil(res['x:a'])
  222. assert_not_nil(res['x:b'])
  223. end
  224. define_test "get should work with hash columns spec and an array of strings COLUMNS parameter" do
  225. res = @test_table.get('1', COLUMNS => [ 'x:a', 'x:b' ])
  226. assert_not_nil(res)
  227. assert_kind_of(Hash, res)
  228. assert_not_nil(res['x:a'])
  229. assert_not_nil(res['x:b'])
  230. end
  231. define_test "get should work with hash columns spec and TIMESTAMP only" do
  232. res = @test_table.get('1', TIMESTAMP => @test_ts)
  233. assert_not_nil(res)
  234. assert_kind_of(Hash, res)
  235. assert_nil(res['x:a'])
  236. assert_not_nil(res['x:b'])
  237. end
  238. define_test "get should fail with hash columns spec and strange COLUMN value" do
  239. assert_raise(ArgumentError) do
  240. @test_table.get('1', COLUMN => {})
  241. end
  242. end
  243. define_test "get should fail with hash columns spec and strange COLUMNS value" do
  244. assert_raise(ArgumentError) do
  245. @test_table.get('1', COLUMN => {})
  246. end
  247. end
  248. define_test "get should fail with hash columns spec and no TIMESTAMP or COLUMN[S]" do
  249. assert_raise(ArgumentError) do
  250. @test_table.get('1', { :foo => :bar })
  251. end
  252. end
  253. define_test "get should work with a string column spec" do
  254. res = @test_table.get('1', 'x:b')
  255. assert_not_nil(res)
  256. assert_kind_of(Hash, res)
  257. assert_nil(res['x:a'])
  258. assert_not_nil(res['x:b'])
  259. end
  260. define_test "get should work with an array columns spec" do
  261. res = @test_table.get('1', 'x:a', 'x:b')
  262. assert_not_nil(res)
  263. assert_kind_of(Hash, res)
  264. assert_not_nil(res['x:a'])
  265. assert_not_nil(res['x:b'])
  266. end
  267. define_test "get should work with an array or arrays columns spec (yeah, crazy)" do
  268. res = @test_table.get('1', ['x:a'], ['x:b'])
  269. assert_not_nil(res)
  270. assert_kind_of(Hash, res)
  271. assert_not_nil(res['x:a'])
  272. assert_not_nil(res['x:b'])
  273. end
  274. define_test "get with a block should yield (column, value) pairs" do
  275. res = {}
  276. @test_table.get('1') { |col, val| res[col] = val }
  277. assert_equal(res.keys.sort, [ 'x:a', 'x:b' ])
  278. end
  279. define_test "get should support FILTER" do
  280. @test_table.put(1, "x:v", "thisvalue")
  281. begin
  282. res = @test_table.get('1', FILTER => "ValueFilter(=, 'binary:thisvalue')")
  283. assert_not_nil(res)
  284. assert_kind_of(Hash, res)
  285. assert_not_nil(res['x:v'])
  286. assert_nil(res['x:a'])
  287. res = @test_table.get('1', FILTER => "ValueFilter(=, 'binary:thatvalue')")
  288. assert_nil(res)
  289. ensure
  290. # clean up newly added columns for this test only.
  291. @test_table.delete(1, "x:v")
  292. end
  293. end
  294. #-------------------------------------------------------------------------------
  295. define_test "scan should work w/o any params" do
  296. res = @test_table.scan
  297. assert_not_nil(res)
  298. assert_kind_of(Hash, res)
  299. assert_not_nil(res['1'])
  300. assert_not_nil(res['1']['x:a'])
  301. assert_not_nil(res['1']['x:b'])
  302. assert_not_nil(res['2'])
  303. assert_not_nil(res['2']['x:a'])
  304. assert_not_nil(res['2']['x:b'])
  305. end
  306. define_test "scan should support STARTROW parameter" do
  307. res = @test_table.scan STARTROW => '2'
  308. assert_not_nil(res)
  309. assert_kind_of(Hash, res)
  310. assert_nil(res['1'])
  311. assert_not_nil(res['2'])
  312. assert_not_nil(res['2']['x:a'])
  313. assert_not_nil(res['2']['x:b'])
  314. end
  315. define_test "scan should support STOPROW parameter" do
  316. res = @test_table.scan STOPROW => '2'
  317. assert_not_nil(res)
  318. assert_kind_of(Hash, res)
  319. assert_not_nil(res['1'])
  320. assert_not_nil(res['1']['x:a'])
  321. assert_not_nil(res['1']['x:b'])
  322. assert_nil(res['2'])
  323. end
  324. define_test "scan should support LIMIT parameter" do
  325. res = @test_table.scan LIMIT => 1
  326. assert_not_nil(res)
  327. assert_kind_of(Hash, res)
  328. assert_not_nil(res['1'])
  329. assert_not_nil(res['1']['x:a'])
  330. assert_not_nil(res['1']['x:b'])
  331. assert_nil(res['2'])
  332. end
  333. define_test "scan should support TIMESTAMP parameter" do
  334. res = @test_table.scan TIMESTAMP => @test_ts
  335. assert_not_nil(res)
  336. assert_kind_of(Hash, res)
  337. assert_not_nil(res['1'])
  338. assert_nil(res['1']['x:a'])
  339. assert_not_nil(res['1']['x:b'])
  340. assert_not_nil(res['2'])
  341. assert_nil(res['2']['x:a'])
  342. assert_not_nil(res['2']['x:b'])
  343. end
  344. define_test "scan should support TIMERANGE parameter" do
  345. res = @test_table.scan TIMERANGE => [0, 1]
  346. assert_not_nil(res)
  347. assert_kind_of(Hash, res)
  348. assert_nil(res['1'])
  349. assert_nil(res['2'])
  350. end
  351. define_test "scan should support COLUMNS parameter with an array of columns" do
  352. res = @test_table.scan COLUMNS => [ 'x:a', 'x:b' ]
  353. assert_not_nil(res)
  354. assert_kind_of(Hash, res)
  355. assert_not_nil(res['1'])
  356. assert_not_nil(res['1']['x:a'])
  357. assert_not_nil(res['1']['x:b'])
  358. assert_not_nil(res['2'])
  359. assert_not_nil(res['2']['x:a'])
  360. assert_not_nil(res['2']['x:b'])
  361. end
  362. define_test "scan should support COLUMNS parameter with a single column name" do
  363. res = @test_table.scan COLUMNS => 'x:a'
  364. assert_not_nil(res)
  365. assert_kind_of(Hash, res)
  366. assert_not_nil(res['1'])
  367. assert_not_nil(res['1']['x:a'])
  368. assert_nil(res['1']['x:b'])
  369. assert_not_nil(res['2'])
  370. assert_not_nil(res['2']['x:a'])
  371. assert_nil(res['2']['x:b'])
  372. end
  373. define_test "scan should fail on invalid COLUMNS parameter types" do
  374. assert_raise(ArgumentError) do
  375. @test_table.scan COLUMNS => {}
  376. end
  377. end
  378. define_test "scan should fail on non-hash params" do
  379. assert_raise(ArgumentError) do
  380. @test_table.scan 123
  381. end
  382. end
  383. define_test "scan with a block should yield rows and return rows counter" do
  384. rows = {}
  385. res = @test_table.scan { |row, cells| rows[row] = cells }
  386. assert_equal(rows.keys.size, res)
  387. end
  388. define_test "scan should support FILTER" do
  389. @test_table.put(1, "x:v", "thisvalue")
  390. begin
  391. res = @test_table.scan FILTER => "ValueFilter(=, 'binary:thisvalue')"
  392. assert_not_equal(res, {}, "Result is empty")
  393. assert_kind_of(Hash, res)
  394. assert_not_nil(res['1'])
  395. assert_not_nil(res['1']['x:v'])
  396. assert_nil(res['1']['x:a'])
  397. assert_nil(res['2'])
  398. res = @test_table.scan FILTER => "ValueFilter(=, 'binary:thatvalue')"
  399. assert_equal(res, {}, "Result is not empty")
  400. ensure
  401. # clean up newly added columns for this test only.
  402. @test_table.delete(1, "x:v")
  403. end
  404. end
  405. end
  406. end