github.com/core-coin/go-core/v2@v2.1.9/xcbdb/dbtest/testsuite.go (about)

     1  // Copyright 2019 by the Authors
     2  // This file is part of the go-core library.
     3  //
     4  // The go-core library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-core library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-core library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package dbtest
    18  
    19  import (
    20  	"bytes"
    21  	"reflect"
    22  	"sort"
    23  	"testing"
    24  
    25  	"github.com/core-coin/go-core/v2/xcbdb"
    26  )
    27  
    28  // TestDatabaseSuite runs a suite of tests against a KeyValueStore database
    29  // implementation.
    30  func TestDatabaseSuite(t *testing.T, New func() xcbdb.KeyValueStore) {
    31  	t.Run("Iterator", func(t *testing.T) {
    32  		tests := []struct {
    33  			content map[string]string
    34  			prefix  string
    35  			start   string
    36  			order   []string
    37  		}{
    38  			// Empty databases should be iterable
    39  			{map[string]string{}, "", "", nil},
    40  			{map[string]string{}, "non-existent-prefix", "", nil},
    41  
    42  			// Single-item databases should be iterable
    43  			{map[string]string{"key": "val"}, "", "", []string{"key"}},
    44  			{map[string]string{"key": "val"}, "k", "", []string{"key"}},
    45  			{map[string]string{"key": "val"}, "l", "", nil},
    46  
    47  			// Multi-item databases should be fully iterable
    48  			{
    49  				map[string]string{"k1": "v1", "k5": "v5", "k2": "v2", "k4": "v4", "k3": "v3"},
    50  				"", "",
    51  				[]string{"k1", "k2", "k3", "k4", "k5"},
    52  			},
    53  			{
    54  				map[string]string{"k1": "v1", "k5": "v5", "k2": "v2", "k4": "v4", "k3": "v3"},
    55  				"k", "",
    56  				[]string{"k1", "k2", "k3", "k4", "k5"},
    57  			},
    58  			{
    59  				map[string]string{"k1": "v1", "k5": "v5", "k2": "v2", "k4": "v4", "k3": "v3"},
    60  				"l", "",
    61  				nil,
    62  			},
    63  			// Multi-item databases should be prefix-iterable
    64  			{
    65  				map[string]string{
    66  					"ka1": "va1", "ka5": "va5", "ka2": "va2", "ka4": "va4", "ka3": "va3",
    67  					"kb1": "vb1", "kb5": "vb5", "kb2": "vb2", "kb4": "vb4", "kb3": "vb3",
    68  				},
    69  				"ka", "",
    70  				[]string{"ka1", "ka2", "ka3", "ka4", "ka5"},
    71  			},
    72  			{
    73  				map[string]string{
    74  					"ka1": "va1", "ka5": "va5", "ka2": "va2", "ka4": "va4", "ka3": "va3",
    75  					"kb1": "vb1", "kb5": "vb5", "kb2": "vb2", "kb4": "vb4", "kb3": "vb3",
    76  				},
    77  				"kc", "",
    78  				nil,
    79  			},
    80  			// Multi-item databases should be prefix-iterable with start position
    81  			{
    82  				map[string]string{
    83  					"ka1": "va1", "ka5": "va5", "ka2": "va2", "ka4": "va4", "ka3": "va3",
    84  					"kb1": "vb1", "kb5": "vb5", "kb2": "vb2", "kb4": "vb4", "kb3": "vb3",
    85  				},
    86  				"ka", "3",
    87  				[]string{"ka3", "ka4", "ka5"},
    88  			},
    89  			{
    90  				map[string]string{
    91  					"ka1": "va1", "ka5": "va5", "ka2": "va2", "ka4": "va4", "ka3": "va3",
    92  					"kb1": "vb1", "kb5": "vb5", "kb2": "vb2", "kb4": "vb4", "kb3": "vb3",
    93  				},
    94  				"ka", "8",
    95  				nil,
    96  			},
    97  		}
    98  		for i, tt := range tests {
    99  			// Create the key-value data store
   100  			db := New()
   101  			for key, val := range tt.content {
   102  				if err := db.Put([]byte(key), []byte(val)); err != nil {
   103  					t.Fatalf("test %d: failed to insert item %s:%s into database: %v", i, key, val, err)
   104  				}
   105  			}
   106  			// Iterate over the database with the given configs and verify the results
   107  			it, idx := db.NewIterator([]byte(tt.prefix), []byte(tt.start)), 0
   108  			for it.Next() {
   109  				if len(tt.order) <= idx {
   110  					t.Errorf("test %d: prefix=%q more items than expected: checking idx=%d (key %q), expecting len=%d", i, tt.prefix, idx, it.Key(), len(tt.order))
   111  					break
   112  				}
   113  				if !bytes.Equal(it.Key(), []byte(tt.order[idx])) {
   114  					t.Errorf("test %d: item %d: key mismatch: have %s, want %s", i, idx, string(it.Key()), tt.order[idx])
   115  				}
   116  				if !bytes.Equal(it.Value(), []byte(tt.content[tt.order[idx]])) {
   117  					t.Errorf("test %d: item %d: value mismatch: have %s, want %s", i, idx, string(it.Value()), tt.content[tt.order[idx]])
   118  				}
   119  				idx++
   120  			}
   121  			if err := it.Error(); err != nil {
   122  				t.Errorf("test %d: iteration failed: %v", i, err)
   123  			}
   124  			if idx != len(tt.order) {
   125  				t.Errorf("test %d: iteration terminated prematurely: have %d, want %d", i, idx, len(tt.order))
   126  			}
   127  			db.Close()
   128  		}
   129  	})
   130  
   131  	t.Run("IteratorWith", func(t *testing.T) {
   132  		db := New()
   133  		defer db.Close()
   134  
   135  		keys := []string{"1", "2", "3", "4", "6", "10", "11", "12", "20", "21", "22"}
   136  		sort.Strings(keys) // 1, 10, 11, etc
   137  
   138  		for _, k := range keys {
   139  			if err := db.Put([]byte(k), nil); err != nil {
   140  				t.Fatal(err)
   141  			}
   142  		}
   143  
   144  		{
   145  			it := db.NewIterator(nil, nil)
   146  			got, want := iterateKeys(it), keys
   147  			if err := it.Error(); err != nil {
   148  				t.Fatal(err)
   149  			}
   150  			if !reflect.DeepEqual(got, want) {
   151  				t.Errorf("Iterator: got: %s; want: %s", got, want)
   152  			}
   153  		}
   154  
   155  		{
   156  			it := db.NewIterator([]byte("1"), nil)
   157  			got, want := iterateKeys(it), []string{"1", "10", "11", "12"}
   158  			if err := it.Error(); err != nil {
   159  				t.Fatal(err)
   160  			}
   161  			if !reflect.DeepEqual(got, want) {
   162  				t.Errorf("IteratorWith(1,nil): got: %s; want: %s", got, want)
   163  			}
   164  		}
   165  
   166  		{
   167  			it := db.NewIterator([]byte("5"), nil)
   168  			got, want := iterateKeys(it), []string{}
   169  			if err := it.Error(); err != nil {
   170  				t.Fatal(err)
   171  			}
   172  			if !reflect.DeepEqual(got, want) {
   173  				t.Errorf("IteratorWith(5,nil): got: %s; want: %s", got, want)
   174  			}
   175  		}
   176  
   177  		{
   178  			it := db.NewIterator(nil, []byte("2"))
   179  			got, want := iterateKeys(it), []string{"2", "20", "21", "22", "3", "4", "6"}
   180  			if err := it.Error(); err != nil {
   181  				t.Fatal(err)
   182  			}
   183  			if !reflect.DeepEqual(got, want) {
   184  				t.Errorf("IteratorWith(nil,2): got: %s; want: %s", got, want)
   185  			}
   186  		}
   187  
   188  		{
   189  			it := db.NewIterator(nil, []byte("5"))
   190  			got, want := iterateKeys(it), []string{"6"}
   191  			if err := it.Error(); err != nil {
   192  				t.Fatal(err)
   193  			}
   194  			if !reflect.DeepEqual(got, want) {
   195  				t.Errorf("IteratorWith(nil,5): got: %s; want: %s", got, want)
   196  			}
   197  		}
   198  	})
   199  
   200  	t.Run("KeyValueOperations", func(t *testing.T) {
   201  		db := New()
   202  		defer db.Close()
   203  
   204  		key := []byte("foo")
   205  
   206  		if got, err := db.Has(key); err != nil {
   207  			t.Error(err)
   208  		} else if got {
   209  			t.Errorf("wrong value: %t", got)
   210  		}
   211  
   212  		value := []byte("hello world")
   213  		if err := db.Put(key, value); err != nil {
   214  			t.Error(err)
   215  		}
   216  
   217  		if got, err := db.Has(key); err != nil {
   218  			t.Error(err)
   219  		} else if !got {
   220  			t.Errorf("wrong value: %t", got)
   221  		}
   222  
   223  		if got, err := db.Get(key); err != nil {
   224  			t.Error(err)
   225  		} else if !bytes.Equal(got, value) {
   226  			t.Errorf("wrong value: %q", got)
   227  		}
   228  
   229  		if err := db.Delete(key); err != nil {
   230  			t.Error(err)
   231  		}
   232  
   233  		if got, err := db.Has(key); err != nil {
   234  			t.Error(err)
   235  		} else if got {
   236  			t.Errorf("wrong value: %t", got)
   237  		}
   238  	})
   239  
   240  	t.Run("Batch", func(t *testing.T) {
   241  		db := New()
   242  		defer db.Close()
   243  
   244  		b := db.NewBatch()
   245  		for _, k := range []string{"1", "2", "3", "4"} {
   246  			if err := b.Put([]byte(k), nil); err != nil {
   247  				t.Fatal(err)
   248  			}
   249  		}
   250  
   251  		if has, err := db.Has([]byte("1")); err != nil {
   252  			t.Fatal(err)
   253  		} else if has {
   254  			t.Error("db contains element before batch write")
   255  		}
   256  
   257  		if err := b.Write(); err != nil {
   258  			t.Fatal(err)
   259  		}
   260  
   261  		{
   262  			it := db.NewIterator(nil, nil)
   263  			if got, want := iterateKeys(it), []string{"1", "2", "3", "4"}; !reflect.DeepEqual(got, want) {
   264  				t.Errorf("got: %s; want: %s", got, want)
   265  			}
   266  		}
   267  
   268  		b.Reset()
   269  
   270  		// Mix writes and deletes in batch
   271  		b.Put([]byte("5"), nil)
   272  		b.Delete([]byte("1"))
   273  		b.Put([]byte("6"), nil)
   274  		b.Delete([]byte("3"))
   275  		b.Put([]byte("3"), nil)
   276  
   277  		if err := b.Write(); err != nil {
   278  			t.Fatal(err)
   279  		}
   280  
   281  		{
   282  			it := db.NewIterator(nil, nil)
   283  			if got, want := iterateKeys(it), []string{"2", "3", "4", "5", "6"}; !reflect.DeepEqual(got, want) {
   284  				t.Errorf("got: %s; want: %s", got, want)
   285  			}
   286  		}
   287  	})
   288  
   289  	t.Run("BatchReplay", func(t *testing.T) {
   290  		db := New()
   291  		defer db.Close()
   292  
   293  		want := []string{"1", "2", "3", "4"}
   294  		b := db.NewBatch()
   295  		for _, k := range want {
   296  			if err := b.Put([]byte(k), nil); err != nil {
   297  				t.Fatal(err)
   298  			}
   299  		}
   300  
   301  		b2 := db.NewBatch()
   302  		if err := b.Replay(b2); err != nil {
   303  			t.Fatal(err)
   304  		}
   305  
   306  		if err := b2.Replay(db); err != nil {
   307  			t.Fatal(err)
   308  		}
   309  
   310  		it := db.NewIterator(nil, nil)
   311  		if got := iterateKeys(it); !reflect.DeepEqual(got, want) {
   312  			t.Errorf("got: %s; want: %s", got, want)
   313  		}
   314  	})
   315  
   316  }
   317  
   318  func iterateKeys(it xcbdb.Iterator) []string {
   319  	keys := []string{}
   320  	for it.Next() {
   321  		keys = append(keys, string(it.Key()))
   322  	}
   323  	sort.Strings(keys)
   324  	it.Release()
   325  	return keys
   326  }