forked from davidsantiago/clojure-hbase
/
core_test.clj
135 lines (124 loc) · 5.2 KB
/
core_test.clj
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
(ns clojure-hbase.core-test
(:refer-clojure :rename {get map-get})
(:use clojure.test
clojure.stacktrace
[clojure-hbase.core]
[clojure-hbase.admin :exclude [flush]])
(:import [org.apache.hadoop.hbase.util Bytes]
[java.util UUID]))
;; This file creates a table to do all its work in, and requires an already-
;; configured running instance of HBase. Obviously, make sure this is not a
;; production version of HBase you're testing on.
(def test-tbl-name (str "clojure-hbase-test-db" (UUID/randomUUID)))
(defn setup-tbl [] (create-table (table-descriptor test-tbl-name)))
(defn remove-tbl []
(disable-table test-tbl-name)
(delete-table test-tbl-name))
(defmacro as-test [& body]
`(do
(try
(setup-tbl)
~@body
(catch Throwable t# (print-cause-trace t#))
(finally
(remove-tbl)))))
(deftest create-delete-table
(as-test
(is (.contains (map #(Bytes/toString (.getName %)) (list-tables))
test-tbl-name)
"The table was created at the beginning of the as-test."))
(is (not (.contains (map #(Bytes/toString (.getName %)) (list-tables))
test-tbl-name))
"Now that we are out of the as-test, the table doesn't exist."))
(deftest add-delete-CF
(let [cf-name "test-cf-name"]
(as-test
(disable-table test-tbl-name)
(add-column-family test-tbl-name (column-descriptor cf-name))
(is (= (.getNameAsString (.getFamily
(get-table-descriptor test-tbl-name)
(to-bytes cf-name)))
cf-name)
"Created a new column family and retrieved its column descriptor.")
(delete-column-family test-tbl-name cf-name)
(is (= nil (.getFamily (get-table-descriptor test-tbl-name)
(to-bytes cf-name)))
"Deleted the column family successfully."))))
(deftest get-put-delete
(let [cf-name "test-cf-name"
row "testrow"
value "testval"]
(as-test
(disable-table test-tbl-name)
(add-column-family test-tbl-name (column-descriptor cf-name))
(enable-table test-tbl-name)
(with-table [test-tbl (table test-tbl-name)]
(put test-tbl row :value [cf-name :testqual value])
(is (= value (Bytes/toString (last (first
(as-vector
(get test-tbl row :column
[cf-name :testqual]))))))
"Successfully executed Put and Get.")
(delete test-tbl row :column [cf-name :testqual])
(is (= '() (as-vector (get test-tbl row :column
[cf-name :testqual])))
"Successfully executed Delete of the Put.")))))
(def scan-row-values (sort-by #(first %)
(for [k (range 10000)]
[(str (UUID/randomUUID))
(str (UUID/randomUUID))])))
(deftest scan-check
(let [cf-name "test-cf-name"]
(as-test
(disable-table test-tbl-name)
(add-column-family test-tbl-name (column-descriptor cf-name))
(enable-table test-tbl-name)
(with-table [test-tbl (table test-tbl-name)]
(doseq [[key value] scan-row-values]
(put test-tbl key :value [cf-name :value value]))
(is (= true
(reduce #(and %1 %2)
(with-scanner [scan-results (scan test-tbl)]
(map #(= (first %1)
(Bytes/toString (.getRow %2)))
scan-row-values (seq scan-results))))))))))
(deftest as-map-test
(let [cf-name "test-cf-name"
qual "testqual"
row "testrow"
value "testval"]
(as-test
(disable-table test-tbl-name)
(add-column-family test-tbl-name (column-descriptor cf-name))
(enable-table test-tbl-name)
(with-table [test-tbl (table test-tbl-name)]
(put test-tbl row :value [cf-name qual value])
(is (= value
(first (vals (get-in (as-map (get test-tbl row)
:map-family #(Bytes/toString %)
:map-qualifier #(Bytes/toString %)
:map-timestamp str
:map-value #(Bytes/toString %))
[cf-name qual]))))
"as-map works.")
(is (= {cf-name {qual value}}
(latest-as-map (get test-tbl row)
:map-family #(Bytes/toString %)
:map-qualifier #(Bytes/toString %)
:map-value #(Bytes/toString %)))
"latest-as-map works.")))))
;;;;;;(#'clojure-hbase.core/htable-pool))
(deftest test-set-config
(as-test
(is
(try (set-config
(make-config "hbase.zookeeper.quorum" "asdsa")) ;<- not valid
(table test-tbl-name) ;<- should raise exception
false #_"<- fail if we got here, it should have thrown"
(catch Exception e
true)))
(is
(do
(set-config
(make-config "hbase.zookeeper.quorum" "127.0.0.1")) ;<- valid
(table test-tbl-name)))))