// Verify if the items being read are as expected. func CheckOpsReader(t *testing.T, loader OpsReader) { expectedOpsRead := 0 const startingTs = 1396456709420 for op := loader.Next(); op != nil; op = loader.Next() { expectedOpsRead += 1 ensure.NotNil(t, op) ensure.DeepEqual(t, loader.OpsRead(), expectedOpsRead) // check the "ts" field CheckTime(t, float64(startingTs+loader.OpsRead()), op.Timestamp) // check the insert doc // certain key exists for i := 1; i <= 5; i++ { logTypeKey := fmt.Sprintf("logType%d", i) logType, ok := GetElem(op.InsertDoc, logTypeKey) if i != expectedOpsRead { ensure.False(t, ok) ensure.Nil(t, logType) } else { ensure.True(t, ok) ensure.NotNil(t, logType) } } // check the value for the shared key message := fmt.Sprintf("m%d", expectedOpsRead) actualMessage, ok := GetElem(op.InsertDoc, "message") ensure.True(t, ok) ensure.DeepEqual(t, actualMessage.(string), message) } ensure.DeepEqual(t, expectedOpsRead, 5) }
func TestCmdScoreLess(t *testing.T) { t.Parallel() scores := cmdScores([]cmdScore{ {levenshtein: 1, jaroWinkler: 0.9}, {levenshtein: 2, jaroWinkler: 0.8}, }) ensure.True(t, scores.Less(0, 1)) scores = cmdScores([]cmdScore{ {levenshtein: 1, jaroWinkler: 0.9}, {levenshtein: 1, jaroWinkler: 0.8}, }) ensure.True(t, scores.Less(0, 1)) scores = cmdScores([]cmdScore{ {levenshtein: 1, jaroWinkler: 0.8}, {levenshtein: 1, jaroWinkler: 0.9}, }) ensure.False(t, scores.Less(0, 1)) scores = cmdScores([]cmdScore{ {levenshtein: 2, jaroWinkler: 0.9}, {levenshtein: 1, jaroWinkler: 0.8}, }) ensure.False(t, scores.Less(0, 1)) }
func TestWriteBatchIterator(t *testing.T) { db := newTestDB(t, "TestWriteBatchIterator", nil) defer db.Close() var ( givenKey1 = []byte("key1") givenVal1 = []byte("val1") givenKey2 = []byte("key2") ) // create and fill the write batch wb := NewWriteBatch() defer wb.Destroy() wb.Put(givenKey1, givenVal1) wb.Delete(givenKey2) ensure.DeepEqual(t, wb.Count(), 2) // iterate over the batch iter := wb.NewIterator() ensure.True(t, iter.Next()) record := iter.Record() ensure.DeepEqual(t, record.Type, WriteBatchRecordTypeValue) ensure.DeepEqual(t, record.Key, givenKey1) ensure.DeepEqual(t, record.Value, givenVal1) ensure.True(t, iter.Next()) record = iter.Record() ensure.DeepEqual(t, record.Type, WriteBatchRecordTypeDeletion) ensure.DeepEqual(t, record.Key, givenKey2) // there shouldn't be any left ensure.False(t, iter.Next()) }
func TestCounters(t *testing.T) { coll := NewCounters() coll.C("get").Incr(1) coll.C("set").Decr(-1) ensure.True(t, coll.C("get").Count() == 1) ensure.True(t, coll.C("del").Count() == 0) }
func TestNewContainerError(t *testing.T) { givenErr := errors.New("") c, err := NewContainer(func(*Container) error { return givenErr }) ensure.True(t, err == givenErr) ensure.True(t, c == nil) }
func TestShouldFilterOp(t *testing.T) { t.Parallel() ensure.True(t, shouldFilterOp(&Op{Type: Update}, []OpType{Update})) ensure.True(t, shouldFilterOp(&Op{Type: Update}, []OpType{Update, Query})) ensure.True(t, shouldFilterOp(&Op{Type: Query}, []OpType{Update, Query})) ensure.False(t, shouldFilterOp(&Op{Type: Command}, []OpType{Update, Query})) ensure.False(t, shouldFilterOp(&Op{Type: Command}, []OpType{})) }
func TestDBTypeOf(t *testing.T) { db := New(newRocksDB(t)) defer db.Close() db.Set([]byte("name"), []byte("latermoon")) e := db.TypeOf([]byte("name")) ensure.True(t, e == STRING) e = db.TypeOf([]byte("age")) ensure.True(t, e == NONE) }
func TestIsFileName(t *testing.T) { t.Parallel() ensure.True(t, isFilename("file")) ensure.True(t, isFilename("star\\*")) ensure.True(t, isFilename("bracket\\[")) ensure.True(t, isFilename("question\\?")) ensure.False(t, isFilename("*")) ensure.False(t, isFilename("?")) ensure.False(t, isFilename("[")) }
func TestApplyMakesNew(t *testing.T) { const givenName = "x" const givenID = "y" givenContainerConfig := &dockerclient.ContainerConfig{Image: "foo"} givenHostConfig := &dockerclient.HostConfig{} var inspectCalls, createCalls, startCalls, afterCreateCalls int container, err := NewContainer( ContainerName(givenName), ContainerConfig(givenContainerConfig), ContainerHostConfig(givenHostConfig), ContainerAfterCreate(func(containerID string) error { afterCreateCalls++ ensure.DeepEqual(t, containerID, givenID) return nil }), ) ensure.Nil(t, err) client := &mockClient{ inspectContainer: func(name string) (*dockerclient.ContainerInfo, error) { inspectCalls++ switch inspectCalls { case 1: ensure.DeepEqual(t, name, givenName) return nil, dockerclient.ErrNotFound case 2: return &dockerclient.ContainerInfo{Id: givenID}, nil } panic("not reached") }, createContainer: func(config *dockerclient.ContainerConfig, name string) (string, error) { createCalls++ ensure.True(t, config == givenContainerConfig) ensure.DeepEqual(t, name, givenName) return "", nil }, startContainer: func(id string, config *dockerclient.HostConfig) error { startCalls++ ensure.DeepEqual(t, id, givenID) ensure.True(t, config == givenHostConfig) return nil }, } ensure.Nil(t, container.Apply(client)) ensure.DeepEqual(t, inspectCalls, 2) ensure.DeepEqual(t, createCalls, 1) ensure.DeepEqual(t, afterCreateCalls, 1) ensure.DeepEqual(t, startCalls, 1) }
func TestApplyWithExistingRemoveError(t *testing.T) { const image = "x" givenErr := errors.New("") container := &Container{ containerConfig: &dockerclient.ContainerConfig{ Image: image, }, removeExisting: true, } client := &mockClient{ inspectContainer: func(name string) (*dockerclient.ContainerInfo, error) { return &dockerclient.ContainerInfo{ Id: "y", Image: "z", }, nil }, listImages: func() ([]*dockerclient.Image, error) { return []*dockerclient.Image{ { RepoTags: []string{image}, Id: "y", }, }, nil }, removeContainer: func(id string, force, volumes bool) error { return givenErr }, } err := container.Apply(client) ensure.True(t, stackerr.HasUnderlying(err, stackerr.Equals(givenErr))) }
func TestCheckExistingWithDesiredImage(t *testing.T) { const image = "x" const id = "y" container := &Container{ containerConfig: &dockerclient.ContainerConfig{ Image: image, }, } client := &mockClient{ listImages: func() ([]*dockerclient.Image, error) { return []*dockerclient.Image{ { RepoTags: []string{image}, Id: id, }, }, nil }, } ci := &dockerclient.ContainerInfo{ Image: id, Config: &dockerclient.ContainerConfig{}, } ok, err := container.checkExisting(client, ci) ensure.Nil(t, err) ensure.True(t, ok) }
func TestApplyStartError(t *testing.T) { givenErr := errors.New("") const image = "x" const id = "y" container := &Container{ containerConfig: &dockerclient.ContainerConfig{ Image: image, }, } client := &mockClient{ inspectContainer: func(name string) (*dockerclient.ContainerInfo, error) { return &dockerclient.ContainerInfo{ Id: "a", Image: id, Config: &dockerclient.ContainerConfig{}, }, nil }, listImages: func() ([]*dockerclient.Image, error) { return []*dockerclient.Image{ { RepoTags: []string{image}, Id: id, }, }, nil }, startContainer: func(id string, config *dockerclient.HostConfig) error { return givenErr }, } err := container.Apply(client) ensure.True(t, stackerr.HasUnderlying(err, stackerr.Equals(givenErr))) }
func TestWriteBatch(t *testing.T) { db := newTestDB(t, "TestWriteBatch", nil) defer db.Close() var ( givenKey1 = []byte("key1") givenVal1 = []byte("val1") givenKey2 = []byte("key2") ) wo := NewDefaultWriteOptions() ensure.Nil(t, db.Put(wo, givenKey2, []byte("foo"))) // create and fill the write batch wb := NewWriteBatch() defer wb.Destroy() wb.Put(givenKey1, givenVal1) wb.Delete(givenKey2) ensure.DeepEqual(t, wb.Count(), 2) // perform the batch ensure.Nil(t, db.Write(wo, wb)) // check changes ro := NewDefaultReadOptions() v1, err := db.Get(ro, givenKey1) defer v1.Free() ensure.Nil(t, err) ensure.DeepEqual(t, v1.Data(), givenVal1) v2, err := db.Get(ro, givenKey2) defer v2.Free() ensure.Nil(t, err) ensure.True(t, v2.Data() == nil) }
func TestDBCRUD(t *testing.T) { db := newTestDB(t, "TestDBGet", nil) defer db.Close() var ( givenKey = []byte("hello") givenVal1 = []byte("world1") givenVal2 = []byte("world2") wo = NewDefaultWriteOptions() ro = NewDefaultReadOptions() ) // create ensure.Nil(t, db.Put(wo, givenKey, givenVal1)) // retrieve v1, err := db.Get(ro, givenKey) defer v1.Free() ensure.Nil(t, err) ensure.DeepEqual(t, v1.Data(), givenVal1) // update ensure.Nil(t, db.Put(wo, givenKey, givenVal2)) v2, err := db.Get(ro, givenKey) defer v2.Free() ensure.Nil(t, err) ensure.DeepEqual(t, v2.Data(), givenVal2) // delete ensure.Nil(t, db.Delete(wo, givenKey)) v3, err := db.Get(ro, givenKey) ensure.Nil(t, err) ensure.True(t, v3.Data() == nil) }
func TestAcceptsPath(t *testing.T) { t.Parallel() i := iosSymbolUploader{Path: testDwarfPath} ensure.True(t, i.acceptsPath()) i.Path = "" ensure.False(t, i.acceptsPath()) }
func TestConfigAppDefaultNotFound(t *testing.T) { t.Parallel() c := parseConfig{Applications: map[string]*parseAppConfig{}} ac, err := c.app(defaultKey) ensure.True(t, ac == nil) ensure.Err(t, err, regexp.MustCompile("No default app configured")) }
func TestExistingConnState(t *testing.T) { t.Parallel() hello := []byte("hello") fin := make(chan struct{}) okHandler := func(w http.ResponseWriter, r *http.Request) { defer close(fin) w.Write(hello) } var called int32 listener, err := net.Listen("tcp", "127.0.0.1:0") ensure.Nil(t, err) server := &http.Server{ Handler: http.HandlerFunc(okHandler), ConnState: func(c net.Conn, s http.ConnState) { atomic.AddInt32(&called, 1) }, } transport := &http.Transport{} client := &http.Client{Transport: transport} down := &httpdown.HTTP{} s := down.Serve(server, listener) res, err := client.Get(fmt.Sprintf("http://%s/", listener.Addr().String())) ensure.Nil(t, err) actualBody, err := ioutil.ReadAll(res.Body) ensure.Nil(t, err) ensure.DeepEqual(t, actualBody, hello) ensure.Nil(t, res.Body.Close()) ensure.Nil(t, s.Stop()) <-fin ensure.True(t, atomic.LoadInt32(&called) > 0) }
func TestOneError(t *testing.T) { t.Parallel() e := errors.New("") var g errgroup.Group g.Error(e) ensure.True(t, g.Wait() == e) }
func TestSetupAndConfigure(t *testing.T) { t.Parallel() h := parsecli.NewHarness(t) h.MakeEmptyRoot() defer h.Stop() n := &newCmd{} h.Env.Type = parsecli.ParseFormat h.Env.In = ioutil.NopCloser(strings.NewReader("\n")) code, err := n.setupSample(h.Env, "myapp", &parsecli.ParseAppConfig{ApplicationID: "a"}, true, false) ensure.Nil(t, err) ensure.True(t, code) type jsSDKVersion struct { JS []string `json:"js"` } ht := parsecli.TransportFunc(func(r *http.Request) (*http.Response, error) { ensure.DeepEqual(t, r.URL.Path, "/1/jsVersions") rows := jsSDKVersion{JS: []string{"1.5.0", "1.6.0"}} return &http.Response{ StatusCode: http.StatusOK, Body: ioutil.NopCloser(strings.NewReader(jsonStr(t, rows))), }, nil }) h.Env.ParseAPIClient = &parsecli.ParseAPIClient{APIClient: &parse.Client{Transport: ht}} err = n.configureSample( &addCmd{MakeDefault: true}, "yolo", (&parsecli.ParseAppConfig{ApplicationID: "a"}).WithInternalMasterKey("m"), nil, h.Env, ) ensure.Nil(t, err) }
func TestAndroidAcceptsPath(t *testing.T) { t.Parallel() a := androidSymbolUploader{ Path: filepath.Join("Resources", "mapping.txt"), } ensure.True(t, a.acceptsPath()) }
func TestConfigGlobalEmpty(t *testing.T) { t.Parallel() dir := makeDirWithConfig(t, "") defer os.RemoveAll(dir) c, err := configFromDir(dir) ensure.True(t, c == nil) ensure.Err(t, err, regexp.MustCompile("is not valid JSON")) }
func TestContainerForceRemoveExisting(t *testing.T) { c, err := NewContainer( ContainerName("x"), ContainerForceRemoveExisting(), ) ensure.Nil(t, err) ensure.True(t, c.forceRemoveExisting) }
func TestConfigAppNotFound(t *testing.T) { t.Parallel() const name = "foo" c := parseConfig{Applications: map[string]*parseAppConfig{}} ac, err := c.app(name) ensure.True(t, ac == nil) ensure.Err(t, err, regexp.MustCompile(`App "foo" wasn't found`)) }
func TestFilterPolicy(t *testing.T) { var ( givenKeys = [][]byte{[]byte("key1"), []byte("key2"), []byte("key3")} givenFilter = []byte("key") createFilterCalled = false keyMayMatchCalled = false ) policy := &mockFilterPolicy{ createFilter: func(keys [][]byte) []byte { createFilterCalled = true ensure.DeepEqual(&fatalAsError{t}, keys, givenKeys) return givenFilter }, keyMayMatch: func(key, filter []byte) bool { keyMayMatchCalled = true ensure.DeepEqual(&fatalAsError{t}, key, givenKeys[0]) ensure.DeepEqual(&fatalAsError{t}, filter, givenFilter) return true }, } db := newTestDB(t, "TestFilterPolicy", func(opts *Options) { blockOpts := NewDefaultBlockBasedTableOptions() blockOpts.SetFilterPolicy(policy) opts.SetBlockBasedTableFactory(blockOpts) }) defer db.Close() // insert keys wo := NewDefaultWriteOptions() for _, k := range givenKeys { ensure.Nil(t, db.Put(wo, k, []byte("val"))) } // flush to trigger the filter creation ensure.Nil(t, db.Flush(NewDefaultFlushOptions())) ensure.True(t, createFilterCalled) // test key may match call ro := NewDefaultReadOptions() v1, err := db.Get(ro, givenKeys[0]) defer v1.Free() ensure.Nil(t, err) ensure.True(t, keyMayMatchCalled) }
func TestHash(t *testing.T) { db := New(newRocksDB(t)) defer db.Close() h := db.Hash([]byte("user")) ensure.Nil(t, h.Set([]byte("name"), []byte("latermoon"))) ensure.Nil(t, h.Set([]byte("age"), []byte("28"))) ensure.Nil(t, h.Set([]byte("sex"), []byte("Male"))) val, err := h.Get([]byte("name")) ensure.Nil(t, err) ensure.DeepEqual(t, val, []byte("latermoon")) val, err = h.Get([]byte("age")) ensure.Nil(t, err) ensure.DeepEqual(t, val, []byte("28")) val, err = h.Get([]byte("sex")) ensure.Nil(t, err) ensure.DeepEqual(t, val, []byte("Male")) // Exist and remove exist, err := h.Exist([]byte("age")) ensure.Nil(t, err) ensure.True(t, exist) err = h.Remove([]byte("name")) ensure.Nil(t, err) exist, err = h.Exist([]byte("name")) ensure.Nil(t, err) ensure.False(t, exist) vals, err := h.MGet([]byte("age"), []byte("sex")) ensure.Nil(t, err) ensure.True(t, len(vals) == 2) ensure.DeepEqual(t, vals[0], []byte("28"), vals[1], []byte("Male")) h.drop() db.RangeEnumerate([]byte{0}, []byte{'z'}, IterForward, func(i int, key, value []byte, quit *bool) { t.Log(i, string(key), string(value)) t.Fail() }) }
func TestContainerAuthConfig(t *testing.T) { config := &dockerclient.AuthConfig{} c, err := NewContainer( ContainerName("x"), ContainerAuthConfig(config), ) ensure.Nil(t, err) ensure.True(t, c.authConfig == config) }
func TestConfigMissing(t *testing.T) { t.Parallel() dir, err := ioutil.TempDir("", "parse-cli-config-") ensure.Nil(t, err) defer os.RemoveAll(dir) c, err := configFromDir(dir) ensure.True(t, c == nil) ensure.Err(t, err, regexp.MustCompile("Command must be run in a directory containing a Parse project.")) }
func TestGetElem(t *testing.T) { doc := bson.D{{"a", 1}} value, exists := GetElem(doc, "a") ensure.True(t, exists) ensure.DeepEqual(t, value.(int), 1) value, exists = GetElem(doc, "b") ensure.False(t, exists) ensure.Nil(t, value) }
func TestNewRequestBodyReadError(t *testing.T) { givenErr := errors.New("") _, err := newRequest(&http.Request{ URL: &url.URL{}, Body: ioutil.NopCloser(fReader(func([]byte) (int, error) { return 0, givenErr })), }) ensure.True(t, err == givenErr, err) }
func TestBatchDoTransportError(t *testing.T) { givenErr := errors.New("") c := &fbapi.Client{ Transport: fTransport(func(*http.Request) (*http.Response, error) { return nil, givenErr }), } _, err := BatchDo(c, &Batch{}) ensure.True(t, err == givenErr, err) }