Exemplo n.º 1
0
func TestSaveSnapshot(t *testing.T) {
	const (
		site     = "TestUpdateReadDeletePackageHistory.com"
		path     = "gcse"
		foundWay = "testing"
	)
	assert.NoError(t, UpdatePackageHistory(site, path, func(info *sppb.HistoryInfo) error {
		assert.Equal(t, "info", info, &sppb.HistoryInfo{})
		info.FoundWay = foundWay
		return nil
	}))
	h, err := ReadPackageHistory(site, path)
	assert.NoError(t, err)
	assert.Equal(t, "h", h, &sppb.HistoryInfo{FoundWay: foundWay})

	outPath := villa.Path(os.TempDir()).Join("TestSaveSnapshot").S()
	assert.NoError(t, SaveSnapshot(outPath))
	box := &bh.RefCountBox{
		DataPath: func() string {
			return outPath
		},
	}
	h, err = ReadPackageHistoryOf(box, site, path)
	assert.NoError(t, err)
	assert.Equal(t, "h", h, &sppb.HistoryInfo{FoundWay: foundWay})
}
Exemplo n.º 2
0
func TestInc(t *testing.T) {
	DataPath = "./tmp.bolt"
	FlushPeriod = time.Second
	err := os.RemoveAll(DataPath)
	if err != nil && err != os.ErrNotExist {
		assert.NoError(t, err)
		return
	}

	now := time.Now()
	Inc("abc")
	Flush()
	Process()

	names, err := ReadNames()
	assert.NoError(t, err)
	assert.Equal(t, "names", names, []string{"abc"})

	data, err := ReadDataOfName("daily", "abc")
	assert.NoError(t, err)
	assert.Equal(t, "data", data, []LabeledCounter{{
		Counter{
			Sum: 1,
			Div: 1,
		},
		now.Format("2006-01-02"),
	}})
}
Exemplo n.º 3
0
func TestUnmarshalFile(t *testing.T) {
	fn := path.Join(os.TempDir(), "TestMarshalToFile.json")
	assert.NoError(t, ioutil.WriteFile(fn, []byte(`{"S":"abc"}`), 0644))
	var s T
	assert.NoError(t, UnmarshalFile(fn, &s))
	assert.Equal(t, "s.S", s.S, "abc")
}
Exemplo n.º 4
0
func TestForEachPackageOfSite(t *testing.T) {
	// Clear the database
	configs.StoreBoltPath().RemoveAll()

	const (
		site  = "TestForEachPackageOfSite.com"
		path1 = "gcse"
		name1 = "pkgname"
		path2 = "gcse2"
		name2 = "TestForEachPackageOfSite"
	)
	assert.NoError(t, UpdatePackage(site, path1, func(info *stpb.PackageInfo) error {
		info.Name = name1
		return nil
	}))
	assert.NoError(t, UpdatePackage(site, path2, func(info *stpb.PackageInfo) error {
		info.Name = name2
		return nil
	}))
	var paths, names []string
	assert.NoError(t, ForEachPackageOfSite(site, func(path string, info *stpb.PackageInfo) error {
		paths = append(paths, path)
		names = append(names, info.Name)
		return nil
	}))
	assert.Equal(t, "paths", paths, []string{path1, path2})
	assert.Equal(t, "names", names, []string{name1, name2})
}
Exemplo n.º 5
0
func TestIndex_DescNotIndexedBug(t *testing.T) {
	const (
		description = "description"
		readme      = "readme"
	)
	hits := []HitInfo{{
		DocInfo: DocInfo{
			Package:     "github.com/daviddengcn/gcse",
			Name:        "gcse",
			Description: description,
			ReadmeData:  readme,
		},
	}}
	idxs := []int{0}
	fullHitSaved := 0
	ts := &index.TokenSetSearcher{}
	assert.NoError(t, indexAndSaveHits(ts, hits, idxs, func(hit *HitInfo) error {
		fullHitSaved++
		assert.Equal(t, "Description", hit.Description, description)
		assert.Equal(t, "Readme", hit.ReadmeData, readme)
		return nil
	}))
	assert.Equal(t, "fullHitSaved", fullHitSaved, 1)
	results, err := search(ts, IndexTextField, description)
	assert.NoError(t, err)
	assert.Equal(t, "results", results, hits)

	results, err = search(ts, IndexTextField, readme)
	assert.NoError(t, err)
	assert.Equal(t, "results", results, hits)
}
Exemplo n.º 6
0
func TestMarshalToFile(t *testing.T) {
	fn := path.Join(os.TempDir(), "TestMarshalToFile.json")
	s := T{
		S: "abc",
	}
	assert.NoError(t, MarshalToFile(s, fn))
	bs, err := ioutil.ReadFile(fn)
	assert.NoError(t, err)
	assert.Equal(t, "bs", strings.TrimSpace(string(bs)), `{"S":"abc"}`)
}
Exemplo n.º 7
0
func TestMoveData(t *testing.T) {
	DataPath = "./tmp.bolt"
	FlushPeriod = time.Second
	err := os.RemoveAll(DataPath)
	if err != nil && err != os.ErrNotExist {
		assert.NoError(t, err)
		return
	}

	now := time.Now()
	checkValue := func(tp, name string, sum, div int) {
		data, err := ReadDataOfName(tp, name)
		assert.NoError(t, err)
		if sum == 0 && div == 0 {
			assert.Equal(t, fmt.Sprintf("%v:%v", name, tp), len(data), 0)
			return
		}
		assert.Equal(t, fmt.Sprintf("%v:%v", name, tp), data, []LabeledCounter{{
			Counter{Sum: sum, Div: div},
			now.Format("2006-01-02"),
		}})
	}
	checkNames := func(exp []string) {
		names, err := ReadNames()
		assert.NoError(t, err)
		assert.Equal(t, "names", names, exp)
	}

	AddValue(Average, "TestMoveData-abc", 123)
	AddValue(Average, "TestMoveData-def", 456)
	Flush()
	Process()

	t.Log("before move")
	checkNames([]string{"TestMoveData-abc", "TestMoveData-def"})
	checkValue(Daily, "TestMoveData-abc", 123, 1)
	checkValue(Daily, "TestMoveData-def", 456, 1)

	assert.NoError(t, MoveData("TestMoveData-abc", "TestMoveData-def", Average))
	t.Log("after move from abc to def")
	checkNames([]string{"TestMoveData-def"})
	checkValue(Daily, "TestMoveData-abc", 0, 0)
	checkValue(Daily, "TestMoveData-def", 579, 2)

	assert.NoError(t, MoveData("TestMoveData-def", "TestMoveData-none", Average))
	t.Log("after move from def to none")
	checkNames([]string{"TestMoveData-none"})
	checkValue(Daily, "TestMoveData-abc", 0, 0)
	checkValue(Daily, "TestMoveData-def", 0, 0)
	checkValue(Daily, "TestMoveData-none", 579, 2)
}
Exemplo n.º 8
0
func TestCrawlingEntry(t *testing.T) {
	src := CrawlingEntry{
		ScheduleTime: time.Now(),
		Version:      19,
		Etag:         "Hello",
	}

	var buf bytesp.Slice
	assert.NoError(t, src.WriteTo(&buf))

	var dst CrawlingEntry
	assert.NoError(t, dst.ReadFrom(&buf, -1))

	assert.StringEqual(t, "dst", dst, src)
}
Exemplo n.º 9
0
func TestSlice_ReadFromEOF(t *testing.T) {
	r1 := readerReturningEOFLastRead(1)
	var s Slice
	n, err := s.ReadFrom(&r1)
	assert.Equal(t, "n", n, int64(1))
	assert.NoError(t, err)
	assert.Equal(t, "s", s, Slice([]byte{1}))

	r2 := readerReturningEOFAfterLastRead(1)
	s = nil
	n, err = s.ReadFrom(&r2)
	assert.Equal(t, "n", n, int64(1))
	assert.NoError(t, err)
	assert.Equal(t, "s", s, Slice([]byte{2}))
}
Exemplo n.º 10
0
func TestNodeToLines_Literal(t *testing.T) {
	info, err := parse("", `
package main

func main() {
	a := Da {
		A: 10,
		B: 20,
	}
}

	`)
	if !assert.NoError(t, err) {
		return
	}

	lines := info.funcs.sourceLines("")
	assert.StringEqual(t, "lines", lines, strings.Split(
		`func main() {
    a := Da{
        A: 10,
        B: 20,
    }
}`, "\n"))
}
Exemplo n.º 11
0
func TestRefCountBox_UpdateView(t *testing.T) {
	fn := path.Join(os.TempDir(), "TestRefCountBox_UpdateView.bolt")
	assert.NoError(t, os.RemoveAll(fn))

	b := RefCountBox{
		DataPath: func() string { return fn },
	}
	k := bytes.Split([]byte("a.b"), []byte("."))
	v := "hello"
	assert.NoErrorOrDie(t, b.Update(func(tx Tx) error {
		return tx.Put(k, []byte(v))
	}))
	assert.Equal(t, "b.count", b.count, 0)
	assert.ValueShould(t, "b.db.DB", b.db.DB, b.db.DB == nil, "is not nil")

	found := false
	assert.NoErrorOrDie(t, b.View(func(tx Tx) error {
		return tx.Value(k, func(bs bytesp.Slice) error {
			found = true
			assert.Equal(t, "bs", string(v), string(bs))
			return nil
		})
	}))
	assert.Equal(t, "b.count", b.count, 0)
	assert.ValueShould(t, "b.db.DB", b.db.DB, b.db.DB == nil, "is not nil")
}
Exemplo n.º 12
0
func TestRefCountBox_Basic(t *testing.T) {
	fn := path.Join(os.TempDir(), "TestRefCountBox.bolt")
	assert.NoError(t, os.RemoveAll(fn))

	b := RefCountBox{
		DataPath: func() string { return fn },
	}
	db, err := b.Alloc()
	assert.NoError(t, err)
	assert.ValueShould(t, "db.DB", db.DB, db.DB != nil, "is nil")
	assert.Equal(t, "b.count", b.count, 1)
	assert.ValueShould(t, "b.db.DB", b.db.DB, b.db.DB != nil, "is nil")

	b.Free()
	assert.Equal(t, "b.count", b.count, 0)
	assert.ValueShould(t, "b.db.DB", b.db.DB, b.db.DB == nil, "is not nil")
}
Exemplo n.º 13
0
func TestDocInfo(t *testing.T) {
	src := DocInfo{
		Name:        "gcse",
		Package:     "github.com/daviddengcn/gcse",
		Author:      "github.com/daviddengcn",
		LastUpdated: time.Now(),
		StarCount:   10,
		Synopsis:    "Go Package Search Engine",
		Description: "More details about GCSE",
		ProjectURL:  "http://github.com/daviddengcn/gcse",
		ReadmeFn:    "readme.txt",
		ReadmeData:  "Just read me",
		Imports: []string{
			"github.com/daviddengcn/go-villa",
			"github.com/daviddengcn/sophie",
		},
		TestImports: []string{
			"github.com/daviddengcn/go-check",
		},
		Exported: []string{
			"DocInfo", "CheckRuneType",
		},
	}
	var buf bytesp.Slice
	assert.NoError(t, src.WriteTo(&buf))

	var dst DocInfo
	assert.NoError(t, dst.ReadFrom(&buf, -1))

	assert.StringEqual(t, "dst", dst, src)

	// checking the bug introduced by reusing slice
	dst2 := dst
	assert.StringEqual(t, "dst2.Imports[0]", dst2.Imports[0],
		"github.com/daviddengcn/go-villa")

	src.Imports[0] = "github.com/daviddengcn/go-assert"
	buf = nil
	assert.NoError(t, src.WriteTo(&buf))
	assert.NoError(t, dst.ReadFrom(&buf, -1))
	assert.StringEqual(t, "dst", dst, src)

	assert.StringEqual(t, "dst2.Imports[0]", dst2.Imports[0],
		"github.com/daviddengcn/go-villa")
}
Exemplo n.º 14
0
func TestPlusone(t *testing.T) {
	url := "http://www.google.com/"
	cnt, err := Plusone(http.DefaultClient, url)
	assert.NoError(t, err)
	t.Logf("Plusone of %s: %d", url, cnt)
	if cnt <= 0 {
		t.Errorf("Zero Plusone count for %s", url)
	}
}
Exemplo n.º 15
0
func TestRefCountBox_UnsetDataPath(t *testing.T) {
	fn := path.Join(os.TempDir(), "TestRefCountBox.bolt")
	assert.NoError(t, os.RemoveAll(fn))

	b := RefCountBox{}
	db, err := b.Alloc()
	assert.Equal(t, "err", errorsp.Cause(err), ErrBoxDataPathNotSpecified)
	assert.ValueShould(t, "db.DB", db.DB, db.DB == nil, "is not nil")
	assert.ValueShould(t, "b.db.DB", b.db.DB, b.db.DB == nil, "is not nil")
	assert.Equal(t, "b.count", b.count, 0)
}
Exemplo n.º 16
0
func TestUpdateReadDeletePackage(t *testing.T) {
	const (
		site = "TestUpdateReadPackage.com"
		path = "gcse"
		name = "pkgname"
	)
	assert.NoError(t, UpdatePackage(site, path, func(info *stpb.PackageInfo) error {
		assert.Equal(t, "info", info, &stpb.PackageInfo{})
		info.Name = name
		return nil
	}))
	pkg, err := ReadPackage(site, path)
	assert.NoError(t, err)
	assert.Equal(t, "pkg", pkg, &stpb.PackageInfo{Name: name})

	assert.NoError(t, DeletePackage(site, path))

	pkg, err = ReadPackage(site, path)
	assert.NoError(t, err)
	assert.Equal(t, "pkg", pkg, &stpb.PackageInfo{})
}
Exemplo n.º 17
0
func TestSlice_ReadFrom(t *testing.T) {
	src := make([]byte, 63*1024)
	for i := range src {
		src[i] = byte(i)
	}
	b := bytes.NewBuffer(src)
	var s Slice
	n, err := s.ReadFrom(b)
	assert.Equal(t, "n", n, int64(len(src)))
	assert.NoError(t, err)
	assert.Equal(t, "s", s, Slice(src))
}
Exemplo n.º 18
0
func TestUpdateReadDeletePackageHistory(t *testing.T) {
	const (
		site     = "TestUpdateReadDeletePackageHistory.com"
		path     = "gcse"
		foundWay = "testing"
	)
	assert.NoError(t, UpdatePackageHistory(site, path, func(info *sppb.HistoryInfo) error {
		assert.Equal(t, "info", info, &sppb.HistoryInfo{})
		info.FoundWay = foundWay
		return nil
	}))
	h, err := ReadPackageHistory(site, path)
	assert.NoError(t, err)
	assert.Equal(t, "h", h, &sppb.HistoryInfo{FoundWay: foundWay})

	assert.NoError(t, DeletePackageHistory(site, path))

	h, err = ReadPackageHistory(site, path)
	assert.NoError(t, err)
	assert.Equal(t, "h", h, &sppb.HistoryInfo{})
}
Exemplo n.º 19
0
func TestUpdateReadDeletePersonHistory(t *testing.T) {
	const (
		site     = "TestUpdateReadDeletePersonHistory.com"
		id       = "daviddengcn"
		foundWay = "testing"
	)
	assert.NoError(t, UpdatePersonHistory(site, id, func(info *sppb.HistoryInfo) error {
		assert.Equal(t, "info", info, &sppb.HistoryInfo{})
		info.FoundWay = foundWay
		return nil
	}))
	h, err := ReadPersonHistory(site, id)
	assert.NoError(t, err)
	assert.Equal(t, "h", h, &sppb.HistoryInfo{FoundWay: foundWay})

	assert.NoError(t, DeletePersonHistory(site, id))

	h, err = ReadPersonHistory(site, id)
	assert.NoError(t, err)
	assert.Equal(t, "h", h, &sppb.HistoryInfo{})
}
Exemplo n.º 20
0
func TestDoFill(t *testing.T) {
	const (
		site = "github.com"
		path = "daviddengcn/gcse"
	)
	tm := time.Now().Add(-20 * timep.Day)
	cDB := gcse.LoadCrawlerDB()
	cDB.PackageDB.Put(site+"/"+path, gcse.CrawlingEntry{
		ScheduleTime: tm.Add(10 * timep.Day),
	})
	assert.NoError(t, cDB.Sync())

	assert.NoError(t, doFill())

	h, err := store.ReadPackageHistory(site, path)
	assert.NoError(t, err)
	ts, _ := ptypes.TimestampProto(tm)
	assert.Equal(t, "h", h, &sppb.HistoryInfo{
		FoundTime: ts,
		FoundWay:  "unknown",
	})
}
Exemplo n.º 21
0
func TestUpdateReadDeletePerson(t *testing.T) {
	const (
		site = "TestUpdateReadDeletePerson.com"
		id   = "daviddengcn"
		etag = "tag"
	)
	assert.NoError(t, UpdatePerson(site, id, func(info *stpb.PersonInfo) error {
		assert.Equal(t, "info", info, &stpb.PersonInfo{})
		info.CrawlingInfo = &sppb.CrawlingInfo{
			Etag: etag,
		}
		return nil
	}))
	p, err := ReadPerson(site, id)
	assert.NoError(t, err)
	assert.Equal(t, "p", p, &stpb.PersonInfo{CrawlingInfo: &sppb.CrawlingInfo{Etag: etag}})

	assert.NoError(t, DeletePerson(site, id))

	p, err = ReadPerson(site, id)
	assert.NoError(t, err)
	assert.Equal(t, "p", p, &stpb.PersonInfo{})
}
Exemplo n.º 22
0
func TestForEachPackageSite(t *testing.T) {
	// Clear the database
	configs.StoreBoltPath().RemoveAll()

	const (
		site1 = "TestForEachPackageSite1.com"
		site2 = "github.com"
		path  = "gcse"
		name  = "pkgname"
	)
	assert.NoError(t, UpdatePackage(site1, path, func(info *stpb.PackageInfo) error {
		return nil
	}))
	assert.NoError(t, UpdatePackage(site2, path, func(info *stpb.PackageInfo) error {
		return nil
	}))
	var sites []string
	assert.NoError(t, ForEachPackageSite(func(site string) error {
		sites = append(sites, site)
		return nil
	}))
	assert.Equal(t, "sites", sites, []string{site1, site2})
}
Exemplo n.º 23
0
func TestRefCountBox_OpenFunc(t *testing.T) {
	fn := path.Join(os.TempDir(), "TestRefCountBox.bolt")
	assert.NoError(t, os.RemoveAll(fn))

	openFuncCalled := false

	b := RefCountBox{
		DataPath: func() string { return fn },
		OpenFunc: func(path string) (DB, error) {
			openFuncCalled = true
			return Open(path, 0644, nil)
		},
	}
	db, err := b.Alloc()
	assert.NoError(t, err)
	assert.ValueShould(t, "db.DB", db.DB, db.DB != nil, "is nil")
	assert.Equal(t, "b.count", b.count, 1)
	assert.ValueShould(t, "b.db.DB", b.db.DB, b.db.DB != nil, "is nil")
	assert.True(t, "openFuncCalled", openFuncCalled)

	b.Free()
	assert.Equal(t, "b.count", b.count, 0)
	assert.ValueShould(t, "b.db.DB", b.db.DB, b.db.DB == nil, "is not nil")
}
Exemplo n.º 24
0
func TestAppendPackageEvent(t *testing.T) {
	const (
		site     = "TestAppendPackageEvent.com"
		path     = "gcse"
		foundWay = "test"
	)
	// Insert a found only event, no action.
	foundTm := time.Now()
	foundTs, _ := ptypes.TimestampProto(foundTm)
	assert.NoError(t, AppendPackageEvent(site, path, "test", foundTm, sppb.HistoryEvent_Action_None))
	h, err := ReadPackageHistory(site, path)
	assert.NoError(t, err)
	assert.Equal(t, "h", h, &sppb.HistoryInfo{FoundWay: foundWay, FoundTime: foundTs})

	// Inser a Success action
	succTm := foundTm.Add(time.Hour)
	succTs, _ := ptypes.TimestampProto(succTm)
	assert.NoError(t, AppendPackageEvent(site, path, "non-test", succTm, sppb.HistoryEvent_Action_Success))
	h, err = ReadPackageHistory(site, path)
	assert.NoError(t, err)
	assert.Equal(t, "h", h, &sppb.HistoryInfo{
		FoundWay:  foundWay,
		FoundTime: foundTs,
		Events: []*sppb.HistoryEvent{{
			Timestamp: succTs,
			Action:    sppb.HistoryEvent_Action_Success,
		}},
		LatestSuccess: succTs,
	})
	// Inser a Failed action
	failedTm := succTm.Add(time.Hour)
	failedTs, _ := ptypes.TimestampProto(failedTm)
	assert.NoError(t, AppendPackageEvent(site, path, "", failedTm, sppb.HistoryEvent_Action_Failed))
	h, err = ReadPackageHistory(site, path)
	assert.NoError(t, err)
	assert.Equal(t, "h", h, &sppb.HistoryInfo{
		FoundWay:  foundWay,
		FoundTime: foundTs,
		Events: []*sppb.HistoryEvent{{
			Timestamp: failedTs,
			Action:    sppb.HistoryEvent_Action_Failed,
		}, {
			Timestamp: succTs,
			Action:    sppb.HistoryEvent_Action_Success,
		}},
		LatestSuccess: succTs,
		LatestFailed:  failedTs,
	})
}
Exemplo n.º 25
0
func TestRefCountBox_OpenFuncFailed(t *testing.T) {
	fn := path.Join(os.TempDir(), "TestRefCountBox.bolt")
	assert.NoError(t, os.RemoveAll(fn))

	openFuncCalled := false
	failedErr := errors.New("failed")

	b := RefCountBox{
		DataPath: func() string { return fn },
		OpenFunc: func(path string) (DB, error) {
			openFuncCalled = true
			return DB{}, failedErr
		},
	}
	db, err := b.Alloc()
	assert.Equal(t, "err", errorsp.Cause(err), failedErr)
	assert.ValueShould(t, "db.DB", db.DB, db.DB == nil, "is not nil")
	assert.ValueShould(t, "b.db.DB", b.db.DB, b.db.DB == nil, "is not nil")
	assert.Equal(t, "b.count", b.count, 0)
	assert.True(t, "openFuncCalled", openFuncCalled)
}
Exemplo n.º 26
0
func TestSimpleFlow(t *testing.T) {
	DataPath = "./tmp.bolt"
	FlushPeriod = time.Second
	err := os.RemoveAll(DataPath)
	if err != nil && err != os.ErrNotExist {
		assert.NoError(t, err)
		return
	}

	now := time.Now()
	AddValue(Average, "abc", 123)
	Flush()
	Process()

	names, err := ReadNames()
	assert.NoError(t, err)
	assert.Equal(t, "names", names, []string{"abc"})

	data, err := ReadDataOfName("daily", "abc")
	assert.NoError(t, err)
	assert.Equal(t, "data", data, []LabeledCounter{{
		Counter{
			Sum: 123,
			Div: 1,
		},
		now.Format("2006-01-02"),
	}})

	// Try update
	AddValue(Max, "abc", 1000)
	Flush()
	Process()
	data, err = ReadDataOfName("daily", "abc")
	assert.NoError(t, err)
	assert.Equal(t, "data", data, []LabeledCounter{{
		Counter{
			Sum: 1000,
			Div: 1,
		},
		now.Format("2006-01-02"),
	}})

	// Test of auto flusing
	AddValue(Average, "def", 456)
	time.Sleep(time.Second * 2)
	Process()

	names, err = ReadNames()
	assert.NoError(t, err)
	assert.Equal(t, "names", names, []string{"abc", "def"})

	data, err = ReadDataOfName("monthly", "def")
	assert.NoError(t, err)
	assert.Equal(t, "data", data, []LabeledCounter{{
		Counter: Counter{
			Sum: 456,
			Div: 1,
		},
		Label: now.Format("2006-01"),
	}})
}
Exemplo n.º 27
0
func TestSlice(t *testing.T) {
	var bs Slice
	assert.Equal(t, "len(bs)", len(bs), 0)
	assert.StringEqual(t, "bs", bs, "[]")

	n, err := bs.Skip(1)
	assert.Equal(t, "err", err, io.EOF)
	assert.Equal(t, "n", n, int64(0))

	c, err := bs.ReadByte()
	assert.Equal(t, "err", err, io.EOF)

	bs.Write([]byte{1})
	c, err = bs.ReadByte()
	assert.NoError(t, err)
	assert.Equal(t, "c", c, byte(1))
	assert.Equal(t, "len(bs)", len(bs), 0)

	bs.Write([]byte{1, 2, 3})
	n, err = bs.Skip(4)
	assert.NoError(t, err)
	assert.Equal(t, "n", n, int64(3))

	bs.Write([]byte{1, 2, 3})
	assert.Equal(t, "len(bs)", len(bs), 3)
	assert.StringEqual(t, "bs", bs, "[1 2 3]")

	cnt, err := bs.Read(nil)
	assert.NoError(t, err)
	assert.Equal(t, "cnt", cnt, 0)
	assert.Equal(t, "len(bs)", len(bs), 3)
	assert.Equal(t, "bs", bs, Slice([]byte{1, 2, 3}))

	n, err = bs.Skip(0)
	assert.NoError(t, err)
	assert.Equal(t, "n", n, int64(0))
	assert.Equal(t, "len(bs)", len(bs), 3)
	assert.Equal(t, "bs", bs, Slice([]byte{1, 2, 3}))

	p := make([]byte, 2)
	bs.Read(p)
	assert.Equal(t, "len(bs)", len(bs), 1)
	assert.StringEqual(t, "bs", bs, "[3]")
	assert.StringEqual(t, "p", p, "[1 2]")

	bs.Read(make([]byte, 1))
	assert.Equal(t, "len(bs)", len(bs), 0)
	assert.StringEqual(t, "bs", bs, "[]")

	bs.Write([]byte{4, 5})
	assert.Equal(t, "len(bs)", len(bs), 2)
	assert.StringEqual(t, "bs", bs, "[4 5]")

	bs.WriteByte(6)

	c, err = bs.ReadByte()
	assert.NoError(t, err)
	assert.Equal(t, "c", c, byte(4))
	assert.StringEqual(t, "bs", bs, "[5 6]")

	bs.WriteRune('A')
	assert.Equal(t, "len(bs)", len(bs), 3)
	assert.StringEqual(t, "bs", bs, "[5 6 65]")
	bs.WriteRune('中')
	assert.Equal(t, "len(bs)", len(bs), 6)
	assert.StringEqual(t, "bs", bs, "[5 6 65 228 184 173]")

	bs.WriteString("世界")
	assert.Equal(t, "len(bs)", len(bs), 12)
	assert.StringEqual(t, "bs", bs, "[5 6 65 228 184 173 228 184 150 231 149 140]")

	bs.Skip(1)
	assert.StringEqual(t, "bs", bs, "[6 65 228 184 173 228 184 150 231 149 140]")

	bs.Close()

	bs.Reset()
	fmt.Fprint(&bs, "ABC")
	assert.StringEqual(t, "bs", bs, "[65 66 67]")

	data := make([]byte, 35*1024)
	io.ReadFull(rand.Reader, data)
	bs = nil
	n, err = bs.ReadFrom(bytes.NewReader(data))
	assert.NoError(t, err)
	assert.Equal(t, "n", n, int64(len(data)))
	assert.Equal(t, "bs == data", bytes.Equal(bs, data), true)

	bs = nil
	n, err = Slice(data).WriteTo(&bs)
	assert.NoError(t, err)
	assert.Equal(t, "n", n, int64(len(data)))
	assert.Equal(t, "bs == data", bytes.Equal(bs, data), true)

	bs = []byte("A中文")
	r, size, err := bs.ReadRune()
	assert.NoError(t, err)
	assert.Equal(t, "size", size, 1)
	assert.Equal(t, "r", r, 'A')
	r, size, err = bs.ReadRune()
	assert.NoError(t, err)
	assert.Equal(t, "size", size, len([]byte("中")))
	assert.Equal(t, "r", r, '中')
	r, size, err = bs.ReadRune()
	assert.NoError(t, err)
	assert.Equal(t, "size", size, len([]byte("文")))
	assert.Equal(t, "r", r, '文')
}
Exemplo n.º 28
0
func TestIndex(t *testing.T) {
	const (
		package0 = "github.com/daviddengcn/gcse"
		package1 = "github.com/daviddengcn/gcse/indexer"
		package2 = "github.com/daviddengcn/go-villa"
	)

	docs := []DocInfo{
		{
			Package: package0,
			Name:    "gcse",
			TestImports: []string{
				package2, package0,
			},
		}, {
			Package: package1,
			Name:    "main",
			Imports: []string{
				package0,
				package2,
				package1,
			},
		}, {
			Package: package2,
			Name:    "villa",
		},
	}
	ts, err := Index(&mr.InputStruct{
		PartCountF: func() (int, error) {
			return 1, nil
		},
		IteratorF: func(int) (sophie.IterateCloser, error) {
			index := 0
			return &sophie.IterateCloserStruct{
				NextF: func(key, val sophie.SophieReader) error {
					if index >= len(docs) {
						return io.EOF
					}
					*key.(*sophie.RawString) = sophie.RawString(
						docs[index].Package)
					*val.(*DocInfo) = docs[index]
					val.(*DocInfo).Imports = append([]string{}, docs[index].Imports...)
					val.(*DocInfo).TestImports = append([]string{}, docs[index].TestImports...)

					index++
					return nil
				},
			}, nil
		},
	}, "./tmp")
	assert.NoErrorOrDie(t, err)

	hitsArr, err := index.OpenConstArray(path.Join("./tmp", HitsArrFn))
	for _, doc := range docs {
		idx := -1
		ts.Search(index.SingleFieldQuery(IndexPkgField, doc.Package), func(docID int32, data interface{}) error {
			idx = int(docID)
			return nil
		})
		d, err := hitsArr.GetGob(idx)
		assert.NoError(t, err)
		assert.Equal(t, "d.Package", d.(HitInfo).Package, doc.Package)
	}
	numDocs := ts.DocCount()
	assert.Equal(t, "DocCount", numDocs, 3)

	var pkgs []string
	if err := ts.Search(map[string]stringsp.Set{IndexTextField: nil},
		func(docID int32, data interface{}) error {
			hit := data.(HitInfo)
			pkgs = append(pkgs, hit.Package)
			return nil
		},
	); err != nil {
		t.Error(err)
		return
	}
	assert.StringEqual(t, "all", pkgs,
		[]string{
			"github.com/daviddengcn/gcse",
			"github.com/daviddengcn/go-villa",
			"github.com/daviddengcn/gcse/indexer",
		})

	var gcseInfo HitInfo
	if err := ts.Search(map[string]stringsp.Set{
		IndexPkgField: stringsp.NewSet("github.com/daviddengcn/gcse"),
	}, func(docID int32, data interface{}) error {
		gcseInfo = data.(HitInfo)
		return nil
	}); err != nil {
		t.Errorf("ts.Search: %v", err)
		return
	}
	assert.Equal(t, "gcseInfo.Imported", gcseInfo.Imported, []string(nil))
	assert.Equal(t, "gcseInfo.ImportedLen", gcseInfo.ImportedLen, 1)
	assert.Equal(t, "gcseInfo.TestImports", gcseInfo.TestImports, []string{"github.com/daviddengcn/go-villa"})

	var indexerInfo HitInfo
	if err := ts.Search(map[string]stringsp.Set{
		IndexPkgField: stringsp.NewSet("github.com/daviddengcn/gcse/indexer"),
	}, func(docID int32, data interface{}) error {
		gcseInfo = data.(HitInfo)
		return nil
	}); err != nil {
		t.Errorf("ts.Search: %v", err)
		return
	}
	assert.StringEqual(t, "indexerInfo.Imported",
		indexerInfo.Imported, []string{})
	assert.StringEqual(t, "indexerInfo.Imports",
		indexerInfo.Imports, []string{})

	if err := ts.Search(map[string]stringsp.Set{
		IndexPkgField: stringsp.NewSet("github.com/daviddengcn/go-villa"),
	}, func(docID int32, data interface{}) error {
		gcseInfo = data.(HitInfo)
		return nil
	}); err != nil {
		t.Errorf("ts.Search: %v", err)
		return
	}
	assert.Equal(t, "indexerInfo.Imported", indexerInfo.Imported, []string(nil))
	assert.Equal(t, "gcseInfo.TestImportedLen", gcseInfo.TestImportedLen, 1)
	assert.Equal(t, "gcseInfo.TestImported", gcseInfo.TestImported, []string(nil))
}
Exemplo n.º 29
0
func TestGobReadWrite(t *testing.T) {
	db := open(t, "TestGobReadWrite")
	defer db.Close()

	k := [][]byte{[]byte("a"), []byte("b")}

	// Testing Tx.PutGob and Tx.GobValue
	assert.NoErrorOrDie(t, db.Update(func(tx Tx) error {
		return tx.PutGob(k, &Da{S: "hello"})
	}))
	found1 := false
	assert.NoError(t, db.View(func(tx Tx) error {
		return tx.GobValue(k, func(v interface{}) error {
			found1 = true
			da, ok := v.(Da)
			assert.True(t, "ok", ok)
			assert.Equal(t, "da", da, Da{S: "hello"})
			return nil
		})
	}))
	assert.True(t, "found1", found1)
	found2 := false
	assert.NoError(t, db.View(func(tx Tx) error {
		return tx.ForEachGob(k[:len(k)-1], func(_ Bucket, k bytesp.Slice, v interface{}) error {
			found2 = true
			da, ok := v.(Da)
			assert.True(t, "ok", ok)
			assert.Equal(t, "da", da, Da{S: "hello"})
			return nil
		})
	}))
	assert.True(t, "found2", found2)

	// Testing Bucket.PutGob, Bucket.GobValue, Bucket.ForEachGob
	bk := [][]byte{[]byte("bbb")}
	assert.NoErrorOrDie(t, db.Update(func(tx Tx) error {
		b, err := tx.CreateBucketIfNotExists(bk)
		assert.NoErrorOrDie(t, err)
		return b.PutGob(k, &Da{S: "world"})
	}))
	found3 := false
	assert.NoError(t, db.View(func(tx Tx) error {
		return tx.Bucket(bk, func(b Bucket) error {
			return b.GobValue(k, func(v interface{}) error {
				found3 = true
				da, ok := v.(Da)
				assert.True(t, "ok", ok)
				assert.Equal(t, "da", da, Da{S: "world"})
				return nil
			})
		})
	}))
	assert.True(t, "found3", found3)
	found4 := false
	assert.NoError(t, db.View(func(tx Tx) error {
		return tx.Bucket(bk, func(b Bucket) error {
			return b.ForEachGob(k[:len(k)-1], func(k bytesp.Slice, v interface{}) error {
				found4 = true
				da, ok := v.(Da)
				assert.True(t, "ok", ok)
				assert.Equal(t, "da", da, Da{S: "world"})
				return nil
			})
		})
	}))
	assert.True(t, "found4", found4)
}