github.com/olivere/camlistore@v0.0.0-20140121221811-1b7ac2da0199/pkg/index/kvfile/kvfile_test.go (about)

     1  /*
     2  Copyright 2011 Google Inc.
     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 kvfile_test
    18  
    19  import (
    20  	"io/ioutil"
    21  	"os"
    22  	"path/filepath"
    23  	"testing"
    24  
    25  	"camlistore.org/pkg/index"
    26  	"camlistore.org/pkg/index/indextest"
    27  	"camlistore.org/pkg/sorted"
    28  	"camlistore.org/pkg/sorted/kvfile"
    29  	"camlistore.org/pkg/sorted/kvtest"
    30  	"camlistore.org/pkg/test"
    31  )
    32  
    33  func newSorted(t *testing.T) (kv sorted.KeyValue, cleanup func()) {
    34  	td, err := ioutil.TempDir("", "kvfile-test")
    35  	if err != nil {
    36  		t.Fatal(err)
    37  	}
    38  	kv, err = kvfile.NewStorage(filepath.Join(td, "kvfile"))
    39  	if err != nil {
    40  		os.RemoveAll(td)
    41  		t.Fatal(err)
    42  	}
    43  	return kv, func() {
    44  		kv.Close()
    45  		os.RemoveAll(td)
    46  	}
    47  }
    48  
    49  func TestSortedKV(t *testing.T) {
    50  	kv, cleanup := newSorted(t)
    51  	defer cleanup()
    52  	kvtest.TestSorted(t, kv)
    53  }
    54  
    55  type tester struct{}
    56  
    57  func (tester) test(t *testing.T, tfn func(*testing.T, func() *index.Index)) {
    58  	defer test.TLog(t)()
    59  	var cleanups []func()
    60  	defer func() {
    61  		for _, fn := range cleanups {
    62  			fn()
    63  		}
    64  	}()
    65  
    66  	initIndex := func() *index.Index {
    67  		kv, cleanup := newSorted(t)
    68  		cleanups = append(cleanups, cleanup)
    69  		return index.New(kv)
    70  	}
    71  
    72  	tfn(t, initIndex)
    73  }
    74  
    75  func TestIndex_KV(t *testing.T) {
    76  	tester{}.test(t, indextest.Index)
    77  }
    78  
    79  func TestPathsOfSignerTarget_KV(t *testing.T) {
    80  	tester{}.test(t, indextest.PathsOfSignerTarget)
    81  }
    82  
    83  func TestFiles_KV(t *testing.T) {
    84  	tester{}.test(t, indextest.Files)
    85  }
    86  
    87  func TestEdgesTo_KV(t *testing.T) {
    88  	tester{}.test(t, indextest.EdgesTo)
    89  }
    90  
    91  func TestDelete_KV(t *testing.T) {
    92  	tester{}.test(t, indextest.Delete)
    93  }