...

Source file src/github.com/go-kivik/kivik/v4/x/memorydb/store_test.go

Documentation: github.com/go-kivik/kivik/v4/x/memorydb

     1  // Licensed under the Apache License, Version 2.0 (the "License"); you may not
     2  // use this file except in compliance with the License. You may obtain a copy of
     3  // the License at
     4  //
     5  //  http://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
     9  // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    10  // License for the specific language governing permissions and limitations under
    11  // the License.
    12  
    13  package memorydb
    14  
    15  import (
    16  	"strings"
    17  	"testing"
    18  
    19  	"gitlab.com/flimzy/testy"
    20  )
    21  
    22  func TestRandStr(t *testing.T) {
    23  	str := randStr()
    24  	if len(str) != 32 {
    25  		t.Errorf("Expected 32-char string, got %d", len(str))
    26  	}
    27  }
    28  
    29  func TestToCouchDoc(t *testing.T) {
    30  	type tcdTest struct {
    31  		Name     string
    32  		Input    interface{}
    33  		Expected couchDoc
    34  		Error    string
    35  	}
    36  	tests := []tcdTest{
    37  		{
    38  			Name:     "Map",
    39  			Input:    map[string]interface{}{"foo": "bar"},
    40  			Expected: couchDoc{"foo": "bar"},
    41  		},
    42  		{
    43  			Name:     "CouchDoc",
    44  			Input:    couchDoc{"foo": "bar"},
    45  			Expected: couchDoc{"foo": "bar"},
    46  		},
    47  		{
    48  			Name:  "Unmarshalable",
    49  			Input: make(chan int),
    50  			Error: "json: unsupported type: chan int",
    51  		},
    52  		{
    53  			Name:     "Marshalable",
    54  			Input:    map[string]string{"foo": "bar"},
    55  			Expected: couchDoc{"foo": "bar"},
    56  		},
    57  	}
    58  	for _, test := range tests {
    59  		func(test tcdTest) {
    60  			t.Run(test.Name, func(t *testing.T) {
    61  				result, err := toCouchDoc(test.Input)
    62  				var msg string
    63  				if err != nil {
    64  					msg = err.Error()
    65  				}
    66  				if msg != test.Error {
    67  					t.Errorf("Unexpected error: %s", msg)
    68  				}
    69  				if d := testy.DiffInterface(test.Expected, result); d != nil {
    70  					t.Error(d)
    71  				}
    72  			})
    73  		}(test)
    74  	}
    75  }
    76  
    77  func TestAddRevision(t *testing.T) {
    78  	d := &database{
    79  		docs: make(map[string]*document),
    80  	}
    81  	r := d.addRevision(couchDoc{"_id": "bar"})
    82  	if !strings.HasPrefix(r, "1-") {
    83  		t.Errorf("Expected initial revision to start with '1-', but got '%s'", r)
    84  	}
    85  	if len(r) != 34 {
    86  		t.Errorf("rev (%s) is %d chars long, expected 34", r, len(r))
    87  	}
    88  	r = d.addRevision(couchDoc{"_id": "bar"})
    89  	if !strings.HasPrefix(r, "2-") {
    90  		t.Errorf("Expected second revision to start with '2-', but got '%s'", r)
    91  	}
    92  	if len(r) != 34 {
    93  		t.Errorf("rev (%s) is %d chars long, expected 34", r, len(r))
    94  	}
    95  	t.Run("NoID", func(t *testing.T) {
    96  		r := func() (i interface{}) {
    97  			defer func() {
    98  				i = recover()
    99  			}()
   100  			d.addRevision(nil)
   101  			return nil
   102  		}()
   103  		if r == nil {
   104  			t.Errorf("addRevision without ID should panic")
   105  		}
   106  	})
   107  	t.Run("InvalidJSON", func(t *testing.T) {
   108  		r := func() (i interface{}) {
   109  			defer func() {
   110  				i = recover()
   111  			}()
   112  			d.addRevision(couchDoc{"_id": "foo", "invalid": make(chan int)})
   113  			return nil
   114  		}()
   115  		if r == nil {
   116  			t.Errorf("unmarshalable objects should panic")
   117  		}
   118  	})
   119  }
   120  
   121  func TestAddLocalRevision(t *testing.T) {
   122  	d := &database{
   123  		docs: make(map[string]*document),
   124  	}
   125  	r := d.addRevision(couchDoc{"_id": "_local/foo"})
   126  	if r != "1-0" {
   127  		t.Errorf("Expected local revision, got %s", r)
   128  	}
   129  	r = d.addRevision(couchDoc{"_id": "_local/foo"})
   130  	if r != "1-0" {
   131  		t.Errorf("Expected local revision, got %s", r)
   132  	}
   133  }
   134  
   135  func TestGetRevisionMissing(t *testing.T) {
   136  	d := &database{
   137  		docs: make(map[string]*document),
   138  	}
   139  	_, found := d.getRevision("foo", "bar")
   140  	if found {
   141  		t.Errorf("Should not have found missing revision")
   142  	}
   143  }
   144  
   145  func TestGetRevisionFound(t *testing.T) {
   146  	d := &database{
   147  		docs: make(map[string]*document),
   148  	}
   149  	r := d.addRevision(map[string]interface{}{"_id": "foo", "a": 1})
   150  	_ = d.addRevision(map[string]interface{}{"_id": "foo", "a": 2})
   151  	result, found := d.getRevision("foo", r)
   152  	if !found {
   153  		t.Errorf("Should have found revision")
   154  	}
   155  	expected := map[string]interface{}{"_id": "foo", "a": 1, "_rev": r}
   156  	if d := testy.DiffAsJSON(expected, result.data); d != nil {
   157  		t.Error(d)
   158  	}
   159  }
   160  
   161  func TestRev(t *testing.T) {
   162  	t.Run("Missing", func(t *testing.T) {
   163  		d := couchDoc{}
   164  		if d.Rev() != "" {
   165  			t.Errorf("Rev should be missing, but got %s", d.Rev())
   166  		}
   167  	})
   168  	t.Run("Set", func(t *testing.T) {
   169  		d := couchDoc{"_rev": "foo"}
   170  		if d.Rev() != "foo" {
   171  			t.Errorf("Rev should be foo, but got %s", d.Rev())
   172  		}
   173  	})
   174  	t.Run("NonString", func(t *testing.T) {
   175  		d := couchDoc{"_rev": true}
   176  		if d.Rev() != "" {
   177  			t.Errorf("Rev should be missing, but got %s", d.Rev())
   178  		}
   179  	})
   180  }
   181  
   182  func TestID(t *testing.T) {
   183  	t.Run("Missing", func(t *testing.T) {
   184  		d := couchDoc{}
   185  		if d.ID() != "" {
   186  			t.Errorf("ID should be missing, but got %s", d.ID())
   187  		}
   188  	})
   189  	t.Run("Set", func(t *testing.T) {
   190  		d := couchDoc{"_id": "foo"}
   191  		if d.ID() != "foo" {
   192  			t.Errorf("ID should be foo, but got %s", d.ID())
   193  		}
   194  	})
   195  	t.Run("NonString", func(t *testing.T) {
   196  		d := couchDoc{"_id": true}
   197  		if d.ID() != "" {
   198  			t.Errorf("ID should be missing, but got %s", d.ID())
   199  		}
   200  	})
   201  }
   202  

View as plain text