github.com/tenywen/fabric@v1.0.0-beta.0.20170620030522-a5b1ed380643/common/ledger/util/leveldbhelper/leveldb_helper_test.go (about)

     1  /*
     2  Copyright IBM Corp. 2016 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  		 http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package leveldbhelper
    18  
    19  import (
    20  	"os"
    21  	"path/filepath"
    22  	"testing"
    23  
    24  	"github.com/hyperledger/fabric/common/ledger/testutil"
    25  	"github.com/syndtr/goleveldb/leveldb"
    26  )
    27  
    28  func TestLevelDBHelperWriteWithoutOpen(t *testing.T) {
    29  	env := newTestDBEnv(t, testDBPath)
    30  	defer env.cleanup()
    31  	db := env.db
    32  	defer func() {
    33  		if recover() == nil {
    34  			t.Fatalf("A panic is expected when writing to db before opening")
    35  		}
    36  	}()
    37  	db.Put([]byte("key"), []byte("value"), false)
    38  }
    39  
    40  func TestLevelDBHelperReadWithoutOpen(t *testing.T) {
    41  	env := newTestDBEnv(t, testDBPath)
    42  	defer env.cleanup()
    43  	db := env.db
    44  	defer func() {
    45  		if recover() == nil {
    46  			t.Fatalf("A panic is expected when writing to db before opening")
    47  		}
    48  	}()
    49  	db.Get([]byte("key"))
    50  }
    51  
    52  func TestLevelDBHelper(t *testing.T) {
    53  	env := newTestDBEnv(t, testDBPath)
    54  	//defer env.cleanup()
    55  	db := env.db
    56  
    57  	db.Open()
    58  	// second time open should not have any side effect
    59  	db.Open()
    60  	db.Put([]byte("key1"), []byte("value1"), false)
    61  	db.Put([]byte("key2"), []byte("value2"), true)
    62  	db.Put([]byte("key3"), []byte("value3"), true)
    63  
    64  	val, _ := db.Get([]byte("key2"))
    65  	testutil.AssertEquals(t, string(val), "value2")
    66  
    67  	db.Delete([]byte("key1"), false)
    68  	db.Delete([]byte("key2"), true)
    69  
    70  	val1, err1 := db.Get([]byte("key1"))
    71  	testutil.AssertNoError(t, err1, "")
    72  	testutil.AssertEquals(t, string(val1), "")
    73  
    74  	val2, err2 := db.Get([]byte("key2"))
    75  	testutil.AssertNoError(t, err2, "")
    76  	testutil.AssertEquals(t, string(val2), "")
    77  
    78  	db.Close()
    79  	// second time open should not have any side effect
    80  	db.Close()
    81  
    82  	val3, err3 := db.Get([]byte("key3"))
    83  	testutil.AssertError(t, err3, "")
    84  
    85  	db.Open()
    86  	batch := &leveldb.Batch{}
    87  	batch.Put([]byte("key1"), []byte("value1"))
    88  	batch.Put([]byte("key2"), []byte("value2"))
    89  	batch.Delete([]byte("key3"))
    90  	db.WriteBatch(batch, true)
    91  
    92  	val1, err1 = db.Get([]byte("key1"))
    93  	testutil.AssertNoError(t, err1, "")
    94  	testutil.AssertEquals(t, string(val1), "value1")
    95  
    96  	val2, err2 = db.Get([]byte("key2"))
    97  	testutil.AssertNoError(t, err2, "")
    98  	testutil.AssertEquals(t, string(val2), "value2")
    99  
   100  	val3, err3 = db.Get([]byte("key3"))
   101  	testutil.AssertNoError(t, err3, "")
   102  	testutil.AssertEquals(t, string(val3), "")
   103  
   104  	keys := []string{}
   105  	itr := db.GetIterator(nil, nil)
   106  	for itr.Next() {
   107  		keys = append(keys, string(itr.Key()))
   108  	}
   109  	testutil.AssertEquals(t, keys, []string{"key1", "key2"})
   110  }
   111  
   112  func TestCreateDBInEmptyDir(t *testing.T) {
   113  	testutil.AssertNoError(t, os.RemoveAll(testDBPath), "")
   114  	testutil.AssertNoError(t, os.MkdirAll(testDBPath, 0775), "")
   115  	db := CreateDB(&Conf{testDBPath})
   116  	defer db.Close()
   117  	defer func() {
   118  		if r := recover(); r != nil {
   119  			t.Fatalf("Panic is not expected when opening db in an existing empty dir. %s", r)
   120  		}
   121  	}()
   122  	db.Open()
   123  }
   124  
   125  func TestCreateDBInNonEmptyDir(t *testing.T) {
   126  	testutil.AssertNoError(t, os.RemoveAll(testDBPath), "")
   127  	testutil.AssertNoError(t, os.MkdirAll(testDBPath, 0775), "")
   128  	file, err := os.Create(filepath.Join(testDBPath, "dummyfile.txt"))
   129  	testutil.AssertNoError(t, err, "")
   130  	file.Close()
   131  	db := CreateDB(&Conf{testDBPath})
   132  	defer db.Close()
   133  	defer func() {
   134  		if r := recover(); r == nil {
   135  			t.Fatalf("A panic is expected when opening db in an existing non-empty dir. %s", r)
   136  		}
   137  	}()
   138  	db.Open()
   139  }