Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 124 lines (94 sloc) 3.092 kb
1a12022 @mrb Initial
authored
1 package riakpbc
2
3 import (
17823c1 @mrb Add test setup/teardown data methods, new tests
authored
4 "encoding/json"
27a3831 @mrb Add some basic tests, need running server
authored
5 "fmt"
6 "github.com/bmizerany/assert"
3448773 @mrb Fix links in tests
authored
7 "github.com/mrb/riakpbc"
27a3831 @mrb Add some basic tests, need running server
authored
8 "strings"
1a12022 @mrb Initial
authored
9 "testing"
10 )
11
27a3831 @mrb Add some basic tests, need running server
authored
12 func setupConnection(t *testing.T) (conn *riakpbc.Conn) {
112dd3f @mrb Slightly alter API in favor of New()
authored
13 conn, err := riakpbc.New("127.0.0.1:8087", 1e8, 1e8)
14 conn.Dial()
27a3831 @mrb Add some basic tests, need running server
authored
15 assert.T(t, err == nil)
16 assert.T(t, conn != nil)
17
112dd3f @mrb Slightly alter API in favor of New()
authored
18 return conn
27a3831 @mrb Add some basic tests, need running server
authored
19 }
20
17823c1 @mrb Add test setup/teardown data methods, new tests
authored
21 func setupData(t *testing.T, conn *riakpbc.Conn) {
22 ok, err := conn.StoreObject("riakpbctestbucket", "testkey", "{\"data\":\"is awesome!\"}")
23 assert.T(t, err == nil)
24 assert.T(t, string(ok) == "Success")
25 }
26
27 func teardownData(t *testing.T, conn *riakpbc.Conn) {
28 ok, err := conn.DeleteObject("riakpbctestbucket", "testkey")
29 assert.T(t, err == nil)
30 assert.T(t, string(ok) == "Success")
31 }
32
27a3831 @mrb Add some basic tests, need running server
authored
33 func TestClientId(t *testing.T) {
34 riak := setupConnection(t)
17823c1 @mrb Add test setup/teardown data methods, new tests
authored
35 ok, err := riak.SetClientId("riakpbctestclientid")
27a3831 @mrb Add some basic tests, need running server
authored
36 assert.T(t, err == nil)
37 assert.T(t, string(ok) == "Success")
38
39 clientId, err := riak.GetClientId()
40 assert.T(t, err == nil)
17823c1 @mrb Add test setup/teardown data methods, new tests
authored
41 assert.T(t, string(clientId) == "riakpbctestclientid")
27a3831 @mrb Add some basic tests, need running server
authored
42 }
43
44 func TestListBuckets(t *testing.T) {
45 riak := setupConnection(t)
17823c1 @mrb Add test setup/teardown data methods, new tests
authored
46
47 setupData(t, riak)
48
49 buckets, err := riak.ListBuckets()
50 assert.T(t, err == nil)
51
27a3831 @mrb Add some basic tests, need running server
authored
52 bucketString := fmt.Sprintf("%s", buckets)
53 assert.T(t, strings.Contains(bucketString, "riakpbctestbucket"))
17823c1 @mrb Add test setup/teardown data methods, new tests
authored
54
55 teardownData(t, riak)
56 }
57
58 func TestFetchObject(t *testing.T) {
59 riak := setupConnection(t)
60 setupData(t, riak)
61
62 object, err := riak.FetchObject("riakpbctestbucket", "testkey")
63 assert.T(t, err == nil)
64 stringObject := string(object)
65
66 jsonD, err := json.Marshal("{\"data\":\"is awesome!\"}")
67 assert.T(t, err == nil)
68 assert.T(t, stringObject == string(jsonD))
69
70 teardownData(t, riak)
71 }
72
73 func TestDeleteObject(t *testing.T) {
74 riak := setupConnection(t)
75 setupData(t, riak)
76
77 object, err := riak.DeleteObject("riakpbctestbucket", "testkey")
78 assert.T(t, err == nil)
79 assert.T(t, string(object) == "Success")
80
81 object, err = riak.FetchObject("riakpbctestbucket", "testkey")
82 assert.T(t, err.Error() == "object not found")
83
84 teardownData(t, riak)
27a3831 @mrb Add some basic tests, need running server
authored
85 }
86
87 func TestGetAndSetBuckets(t *testing.T) {
88 riak := setupConnection(t)
17823c1 @mrb Add test setup/teardown data methods, new tests
authored
89 setupData(t, riak)
27a3831 @mrb Add some basic tests, need running server
authored
90
91 nval := uint32(1)
92 allowmult := false
93 ok, err := riak.SetBucket("riakpbctestbucket", &nval, &allowmult)
94 assert.T(t, err == nil)
95 assert.T(t, string(ok) == "Success")
96
97 bucket, err := riak.GetBucket("riakpbctestbucket")
98 assert.T(t, err == nil)
17823c1 @mrb Add test setup/teardown data methods, new tests
authored
99 assert.T(t, strings.Contains(string(bucket), "false"))
100
101 teardownData(t, riak)
102 }
103
104 func TestPing(t *testing.T) {
105 riak := setupConnection(t)
106
107 pong, err := riak.Ping()
108 assert.T(t, string(pong) == "Pong")
109 assert.T(t, err == nil)
27a3831 @mrb Add some basic tests, need running server
authored
110 }
111
b154ef0 @mrb Kill bullshit example, add MapReduce test
authored
112 func TestMapReduce(t *testing.T) {
113 riak := setupConnection(t)
114 setupData(t, riak)
115
116 twoLevelQuery := "{\"inputs\":[[\"riakpbctestbucket\",\"testkey\"]],\"query\":[{\"map\":{\"language\":\"javascript\",\"keep\":false,\"name\":\"Riak.mapValuesJson\"}},{\"reduce\":{\"language\":\"javascript\",\"keep\":true,\"name\":\"Riak.reduceMax\"}}]}"
117 reduced, err := riak.MapReduce(twoLevelQuery)
118 assert.T(t, err == nil)
119 assert.T(t, reduced != nil)
120 assert.T(t, len(reduced) == 2)
121
122 teardownData(t, riak)
123 }
Something went wrong with that request. Please try again.