Exemple #1
0
func TestTry2(t *testing.T) {
	assert := assert.New(t)

	assert.Panics(func() {
		Try(func() {
			panic(te)
		})
	})

	assert.Panics(func() {
		Try(func() {
			PanicIfError(te)
		}, te2)
	})

	assert.Error(func() error {
		return Try(func() {
			PanicIfError(te)
		})
	}())

	assert.Error(func() error {
		return Try(func() {
			PanicIfError(te)
		}, testError{})
	}())

	assert.Nil(func() error {
		return Try(func() {
			PanicIfError(nil)
		})
	}())
}
Exemple #2
0
func TestDatabaseSpecs(t *testing.T) {
	assert := assert.New(t)

	badSpecs := []string{"mem:stuff", "mem:", "http:", "https:", "random:", "random:random", "http://some.com/wi::rd/path", "/file/ba:d"}
	for _, spec := range badSpecs {
		_, err := ParseDatabaseSpec(spec)
		assert.Error(err)
	}

	testCases := []map[string]string{
		map[string]string{"spec": "http://localhost:8000", "scheme": "http", "path": "//localhost:8000"},
		map[string]string{"spec": "http://localhost:8000/fff", "scheme": "http", "path": "//localhost:8000/fff"},
		map[string]string{"spec": "https://local.attic.io/john/doe", "scheme": "https", "path": "//local.attic.io/john/doe"},
		map[string]string{"spec": "ldb:/filesys/john/doe", "scheme": "ldb", "path": "/filesys/john/doe"},
		map[string]string{"spec": "./john/doe", "scheme": "ldb", "path": "./john/doe"},
		map[string]string{"spec": "john/doe", "scheme": "ldb", "path": "john/doe"},
		map[string]string{"spec": "/john/doe", "scheme": "ldb", "path": "/john/doe"},
		map[string]string{"spec": "mem", "scheme": "mem", "path": ""},
		map[string]string{"spec": "http://server.com/john/doe?access_token=jane", "scheme": "http", "path": "//server.com/john/doe?access_token=jane", "accessToken": "jane"},
		map[string]string{"spec": "https://server.com/john/doe/?arg=2&qp1=true&access_token=jane", "scheme": "https", "path": "//server.com/john/doe/?arg=2&qp1=true&access_token=jane", "accessToken": "jane"},
	}

	for _, tc := range testCases {
		dbSpec, err := ParseDatabaseSpec(tc["spec"])
		assert.NoError(err)
		assert.Equal(DatabaseSpec{Protocol: tc["scheme"], Path: tc["path"], accessToken: tc["accessToken"]}, dbSpec)
	}
}
Exemple #3
0
func TestPathSpec(t *testing.T) {
	assert := assert.New(t)

	badSpecs := []string{"mem::#", "mem::#s", "mem::#foobarbaz", "mem::#wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww"}
	for _, bs := range badSpecs {
		_, err := parsePathSpec(bs)
		assert.Error(err)
	}

	type testCase struct {
		spec, scheme, dbPath, pathStr string
	}

	testCases := []testCase{
		testCase{"http://local.attic.io/john/doe::#0123456789abcdefghijklmnopqrstuv", "http", "//local.attic.io/john/doe", "#0123456789abcdefghijklmnopqrstuv"},
		testCase{"ldb:/filesys/john/doe::#0123456789abcdefghijklmnopqrstuv", "ldb", "/filesys/john/doe", "#0123456789abcdefghijklmnopqrstuv"},
		testCase{"mem::#0123456789abcdefghijklmnopqrstuv", "mem", "", "#0123456789abcdefghijklmnopqrstuv"},
		testCase{"http://local.attic.io/john/doe::#0123456789abcdefghijklmnopqrstuv", "http", "//local.attic.io/john/doe", "#0123456789abcdefghijklmnopqrstuv"},
		testCase{"http://localhost:8000/john/doe/::ds1", "http", "//localhost:8000/john/doe/", "ds1"},
	}

	for _, tc := range testCases {
		dbSpec := databaseSpec{Protocol: tc.scheme, Path: tc.dbPath, accessToken: ""}
		path, err := NewAbsolutePath(tc.pathStr)
		assert.NoError(err)
		expected := pathSpec{dbSpec, path}
		actual, err := parsePathSpec(tc.spec)
		assert.NoError(err)
		assert.Equal(expected, actual)
	}
}
Exemple #4
0
func TestDatabaseSpecs(t *testing.T) {
	assert := assert.New(t)

	badSpecs := []string{"mem:stuff", "mem:", "http:", "https:", "random:", "random:random", "/file/ba:d"}
	for _, spec := range badSpecs {
		_, err := parseDatabaseSpec(spec)
		assert.Error(err, spec)
	}

	type testCase struct {
		spec, scheme, path, accessToken string
	}

	testCases := []testCase{
		testCase{"http://localhost:8000", "http", "//localhost:8000", ""},
		testCase{"http://localhost:8000/fff", "http", "//localhost:8000/fff", ""},
		testCase{"https://local.attic.io/john/doe", "https", "//local.attic.io/john/doe", ""},
		testCase{"ldb:/filesys/john/doe", "ldb", "/filesys/john/doe", ""},
		testCase{"./john/doe", "ldb", "./john/doe", ""},
		testCase{"john/doe", "ldb", "john/doe", ""},
		testCase{"/john/doe", "ldb", "/john/doe", ""},
		testCase{"mem", "mem", "", ""},
		testCase{"http://server.com/john/doe?access_token=jane", "http", "//server.com/john/doe?access_token=jane", "jane"},
		testCase{"https://server.com/john/doe/?arg=2&qp1=true&access_token=jane", "https", "//server.com/john/doe/?arg=2&qp1=true&access_token=jane", "jane"},
	}

	for _, tc := range testCases {
		dbSpec, err := parseDatabaseSpec(tc.spec)
		assert.NoError(err)
		assert.Equal(databaseSpec{Protocol: tc.scheme, Path: tc.path, accessToken: tc.accessToken}, dbSpec)
	}
}
Exemple #5
0
func TestTwoClientsWithEmptyDataset(t *testing.T) {
	assert := assert.New(t)
	id1 := "testdataset"
	cs := chunks.NewMemoryStore()

	dsx := newDS(id1, cs)
	dsy := newDS(id1, cs)

	// dsx: || -> |a|
	a := types.String("a")
	dsx, err := dsx.CommitValue(a)
	assert.NoError(err)
	assert.True(dsx.Head().Get(datas.ValueField).Equals(a))

	// dsy: || -> |b|
	_, ok := dsy.MaybeHead()
	assert.False(ok)
	b := types.String("b")
	dsy, err = dsy.CommitValue(b)
	assert.Error(err)
	// Commit failed, but ds1 now has latest head, so we should be able to just try again.
	// dsy: |a| -> |b|
	dsy, err = dsy.CommitValue(b)
	assert.NoError(err)
	assert.True(dsy.Head().Get(datas.ValueField).Equals(b))
}
Exemple #6
0
func TestDatasetSpecs(t *testing.T) {
	assert := assert.New(t)
	badSpecs := []string{"mem", "mem:", "mem:::ds", "http", "http:", "http://foo", "monkey", "monkey:balls", "mem:/a/bogus/path:dsname", "http://localhost:8000/one"}

	for _, spec := range badSpecs {
		_, err := parseDatasetSpec(spec)
		assert.Error(err, spec)
	}

	invalidDatasetNames := []string{" ", "", "$", "#", ":", "\n", "💩"}
	for _, s := range invalidDatasetNames {
		_, err := parseDatasetSpec("mem::" + s)
		assert.Error(err)
	}

	validDatasetNames := []string{"a", "Z", "0", "/", "-", "_"}
	for _, s := range validDatasetNames {
		_, err := parseDatasetSpec("mem::" + s)
		assert.NoError(err)
	}

	type testCase struct {
		spec, scheme, path, ds, accessToken string
	}

	testCases := []testCase{
		testCase{"http://localhost:8000::ds1", "http", "//localhost:8000", "ds1", ""},
		testCase{"http://localhost:8000/john/doe/::ds2", "http", "//localhost:8000/john/doe/", "ds2", ""},
		testCase{"https://local.attic.io/john/doe::ds3", "https", "//local.attic.io/john/doe", "ds3", ""},
		testCase{"http://local.attic.io/john/doe::ds1", "http", "//local.attic.io/john/doe", "ds1", ""},
		testCase{"ldb:/filesys/john/doe::ds/one", "ldb", "/filesys/john/doe", "ds/one", ""},
		testCase{"http://localhost:8000/john/doe?access_token=abc::ds/one", "http", "//localhost:8000/john/doe?access_token=abc", "ds/one", "abc"},
		testCase{"https://localhost:8000?qp1=x&access_token=abc&qp2=y::ds/one", "https", "//localhost:8000?qp1=x&access_token=abc&qp2=y", "ds/one", "abc"},
	}

	for _, tc := range testCases {
		dsSpec, err := parseDatasetSpec(tc.spec)
		assert.NoError(err)
		dbSpec1 := databaseSpec{Protocol: tc.scheme, Path: tc.path, accessToken: tc.accessToken}
		assert.Equal(datasetSpec{DbSpec: dbSpec1, DatasetName: tc.ds}, dsSpec)
	}
}
Exemple #7
0
func TestDatasetSpecs(t *testing.T) {
	assert := assert.New(t)
	badSpecs := []string{"mem", "mem:", "mem:::ds", "http", "http:", "http://foo", "monkey", "monkey:balls", "http::dsname", "mem:/a/bogus/path:dsname", "http://localhost:8000/one"}

	for _, spec := range badSpecs {
		_, err := ParseDatasetSpec(spec)
		assert.Error(err)
	}

	invalidDatasetNames := []string{" ", "", "$", "#", ":", "\n", "💩"}
	for _, s := range invalidDatasetNames {
		_, err := ParseDatasetSpec("mem::" + s)
		assert.Error(err)
	}

	validDatasetNames := []string{"a", "Z", "0", "/", "-", "_"}
	for _, s := range validDatasetNames {
		_, err := ParseDatasetSpec("mem::" + s)
		assert.NoError(err)
	}

	testCases := []map[string]string{
		map[string]string{"spec": "http://localhost:8000::ds1", "scheme": "http", "path": "//localhost:8000", "ds": "ds1"},
		map[string]string{"spec": "http://localhost:8000/john/doe/::ds2", "scheme": "http", "path": "//localhost:8000/john/doe/", "ds": "ds2"},
		map[string]string{"spec": "https://local.attic.io/john/doe::ds3", "scheme": "https", "path": "//local.attic.io/john/doe", "ds": "ds3"},
		map[string]string{"spec": "http://local.attic.io/john/doe::ds1", "scheme": "http", "path": "//local.attic.io/john/doe", "ds": "ds1"},
		map[string]string{"spec": "ldb:/filesys/john/doe::ds/one", "scheme": "ldb", "path": "/filesys/john/doe", "ds": "ds/one"},
		map[string]string{"spec": "http://localhost:8000/john/doe?access_token=abc::ds/one", "scheme": "http", "path": "//localhost:8000/john/doe?access_token=abc", "accessToken": "abc", "ds": "ds/one"},
		map[string]string{"spec": "https://localhost:8000?qp1=x&access_token=abc&qp2=y::ds/one", "scheme": "https", "path": "//localhost:8000?qp1=x&access_token=abc&qp2=y", "accessToken": "abc", "ds": "ds/one"},
	}

	for _, tc := range testCases {
		dsSpec, err := ParseDatasetSpec(tc["spec"])
		assert.NoError(err)
		dbSpec1 := DatabaseSpec{Protocol: tc["scheme"], Path: tc["path"], accessToken: tc["accessToken"]}
		assert.Equal(DatasetSpec{DbSpec: dbSpec1, DatasetName: tc["ds"]}, dsSpec)
	}
}
Exemple #8
0
func TestTryCatch(t *testing.T) {
	assert := assert.New(t)

	assert.Panics(func() {
		TryCatch(func() {
			panic(Wrap(te))
		},
			func(err error) error {
				if !causeInTypes(err, testError2{}) {
					panic(err)
				}
				return Unwrap(err)
			})
	})

	assert.Panics(func() {
		TryCatch(func() {
			panic(te)
		},
			func(err error) error {
				if !causeInTypes(err, testError{}) {
					panic(err)
				}
				return Unwrap(err)
			})
	})

	assert.IsType(wrappedError{}, func() error {
		return TryCatch(func() {
			panic(Wrap(te))
		},
			func(err error) error {
				return err
			})
	}())

	assert.Error(func() error {
		return TryCatch(func() {
			panic(Wrap(te))
		},
			func(err error) error {
				if !causeInTypes(err, testError2{}, testError{}) {
					panic(err)
				}
				return Unwrap(err)
			})
	}())
}
func TestAbsolutePathParseErrors(t *testing.T) {
	assert := assert.New(t)

	test := func(path, errMsg string) {
		p, err := NewAbsolutePath(path)
		assert.Equal(AbsolutePath{}, p)
		assert.Error(err)
		assert.Equal(errMsg, err.Error())
	}

	test("", "Empty path")
	test(".foo", "Invalid dataset name: .foo")
	test(".foo.bar.baz", "Invalid dataset name: .foo.bar.baz")
	test("#", "Invalid hash: ")
	test("#abc", "Invalid hash: abc")
	invHash := strings.Repeat("z", hash.StringLen)
	test("#"+invHash, "Invalid hash: "+invHash)
}
Exemple #10
0
func TestPathSpec(t *testing.T) {
	assert := assert.New(t)

	testCases := []map[string]string{
		map[string]string{"spec": "http://local.attic.io/john/doe::sha1-0123456789012345678901234567890123456789", "scheme": "http", "path": "//local.attic.io/john/doe", "ref": "sha1-0123456789012345678901234567890123456789"},
		map[string]string{"spec": "http://localhost:8000/john/doe/::ds1", "scheme": "http", "path": "//localhost:8000/john/doe/", "ds": "ds1"},
	}

	for _, tc := range testCases {
		pathSpec, err := ParsePathSpec(tc["spec"])
		assert.NoError(err)
		dbSpec1 := DatabaseSpec{Protocol: tc["scheme"], Path: tc["path"], accessToken: tc["accessToken"]}
		if tc["ref"] != "" {
			assert.Equal(&RefSpec{DbSpec: dbSpec1, Ref: hash.Parse(tc["ref"])}, pathSpec.(*RefSpec))
		} else {
			assert.Equal(&DatasetSpec{DbSpec: dbSpec1, DatasetName: tc["ds"]}, pathSpec.(*DatasetSpec))
		}
	}

	_, err := ParsePathSpec("http://local.attic.io")
	assert.Error(err)

}
Exemple #11
0
func TestTwoClientsWithNonEmptyDataset(t *testing.T) {
	assert := assert.New(t)
	id1 := "testdataset"
	cs := chunks.NewMemoryStore()

	a := types.String("a")
	{
		// ds1: || -> |a|
		ds1 := newDS(id1, cs)
		ds1, err := ds1.Commit(a)
		assert.NoError(err)
		assert.True(ds1.Head().Get(datas.ValueField).Equals(a))
	}

	dsx := newDS(id1, cs)
	dsy := newDS(id1, cs)

	// dsx: |a| -> |b|
	assert.True(dsx.Head().Get(datas.ValueField).Equals(a))
	b := types.String("b")
	dsx, err := dsx.Commit(b)
	assert.NoError(err)
	assert.True(dsx.Head().Get(datas.ValueField).Equals(b))

	// dsy: |a| -> |c|
	assert.True(dsy.Head().Get(datas.ValueField).Equals(a))
	c := types.String("c")
	dsy, err = dsy.Commit(c)
	assert.Error(err)
	assert.True(dsy.Head().Get(datas.ValueField).Equals(b))
	// Commit failed, but dsy now has latest head, so we should be able to just try again.
	// dsy: |b| -> |c|
	dsy, err = dsy.Commit(c)
	assert.NoError(err)
	assert.True(dsy.Head().Get(datas.ValueField).Equals(c))
}
Exemple #12
0
// Error asserts that a function returned an error (i.e. not `nil`).
//
//   actualObj, err := SomeFunction()
//   if assert.Error(t, err, "An error was expected") {
// 	   assert.Equal(t, err, expectedError)
//   }
//
// Returns whether the assertion was successful (true) or not (false).
func Error(t TestingT, err error, msgAndArgs ...interface{}) {
	if !assert.Error(t, err, msgAndArgs...) {
		t.FailNow()
	}
}