1//  Copyright (c) 2014 Couchbase, Inc.
2//  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
3//  except in compliance with the License. You may obtain a copy of the License at
4//    http://www.apache.org/licenses/LICENSE-2.0
5//  Unless required by applicable law or agreed to in writing, software distributed under the
6//  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
7//  either express or implied. See the License for the specific language governing permissions
8//  and limitations under the License.
9package forestdb
10
11import (
12	"os"
13	"testing"
14)
15
16func TestForestDBKVCrud(t *testing.T) {
17	defer os.RemoveAll("test")
18
19	dbfile, err := Open("test", nil)
20	if err != nil {
21		t.Fatal(err)
22	}
23	defer dbfile.Close()
24
25	kvstore, err := dbfile.OpenKVStoreDefault(nil)
26	if err != nil {
27		t.Fatal(err)
28	}
29	defer kvstore.Close()
30
31	// get a non-existant key
32	var val []byte
33	val, err = kvstore.GetKV([]byte("doesnotexist"))
34	if val != nil {
35		t.Error("expected nil value")
36	}
37	if err != RESULT_KEY_NOT_FOUND {
38		t.Errorf("expected %v, got %v", RESULT_KEY_NOT_FOUND, err)
39	}
40
41	// put a new key
42	err = kvstore.SetKV([]byte("key1"), []byte("value1"))
43	if err != nil {
44		t.Error(err)
45	}
46
47	// lookup that key
48	val, err = kvstore.GetKV([]byte("key1"))
49	if err != nil {
50		t.Error(err)
51	}
52	if string(val) != "value1" {
53		t.Errorf("expected value1, got %s", val)
54	}
55
56	// update it
57	err = kvstore.SetKV([]byte("key1"), []byte("value1-updated"))
58	if err != nil {
59		t.Error(err)
60	}
61
62	// look it up again
63	val, err = kvstore.GetKV([]byte("key1"))
64	if err != nil {
65		t.Error(err)
66	}
67	if string(val) != "value1-updated" {
68		t.Errorf("expected value1-updated, got %s", val)
69	}
70
71	// delete it
72	err = kvstore.DeleteKV([]byte("key1"))
73	if err != nil {
74		t.Error(err)
75	}
76
77	// look it up again
78	val, err = kvstore.GetKV([]byte("key1"))
79	if err != RESULT_KEY_NOT_FOUND {
80		t.Error(err)
81	}
82	if val != nil {
83		t.Errorf("expected nil value, got %#v", val)
84	}
85
86	// delete it again
87	err = kvstore.DeleteKV([]byte("key1"))
88	if err != nil {
89		t.Error(err)
90	}
91
92	// dete non-existant key
93	err = kvstore.DeleteKV([]byte("doesnotext"))
94	if err != nil {
95		t.Error(err)
96	}
97}
98