Exemplo n.º 1
0
func testTreeStore_Units_ByFile(t *testing.T, ts TreeStoreImporter) {
	want := []*unit.SourceUnit{
		{Key: unit.Key{Type: "t1", Name: "u1"}, Info: unit.Info{Files: []string{"f1"}}},
		{Key: unit.Key{Type: "t2", Name: "u2"}, Info: unit.Info{Files: []string{"f1", "f2"}}},
		{Key: unit.Key{Type: "t3", Name: "u3"}, Info: unit.Info{Files: []string{"f1", "f3"}}},
	}
	for _, unit := range want {
		if err := ts.Import(unit, graph.Output{}); err != nil {
			t.Errorf("%s: Import(%v, empty data): %s", ts, unit, err)
		}
	}
	if ts, ok := ts.(TreeIndexer); ok {
		if err := ts.Index(); err != nil {
			t.Fatalf("%s: Index: %s", ts, err)
		}
	}

	c_unitFilesIndex_getByPath.set(0)
	units, err := ts.Units(ByFiles(false, "f1"))
	if err != nil {
		t.Errorf("%s: Units(ByFiles f1): %s", ts, err)
	}
	sort.Sort(unit.SourceUnits(units))
	sort.Sort(unit.SourceUnits(want))
	if !deepEqual(units, want) {
		t.Errorf("%s: Units(ByFiles f1): got %v, want %v", ts, units, want)
	}
	if isIndexedStore(ts) {
		if want := 1; c_unitFilesIndex_getByPath.get() != want {
			t.Errorf("%s: Units(ByFiles f1): got %d index hits, want %d", ts, c_unitFilesIndex_getByPath.get(), want)
		}
	}

	c_unitFilesIndex_getByPath.set(0)
	units2, err := ts.Units(ByFiles(false, "f2"))
	if err != nil {
		t.Errorf("%s: Units(ByFiles f2): %s", ts, err)
	}
	want2 := []*unit.SourceUnit{
		{Key: unit.Key{Type: "t2", Name: "u2"}, Info: unit.Info{Files: []string{"f1", "f2"}}},
	}
	if !deepEqual(units2, want2) {
		t.Errorf("%s: Units(ByFiles f2): got %v, want %v", ts, units2, want2)
	}
	if isIndexedStore(ts) {
		if want := 1; c_unitFilesIndex_getByPath.get() != want {
			t.Errorf("%s: Units(ByFiles f1): got %d index hits, want %d", ts, c_unitFilesIndex_getByPath.get(), want)
		}
	}
}
Exemplo n.º 2
0
func testMultiRepoStore_Units(t *testing.T, mrs MultiRepoStoreImporter) {
	units := []*unit.SourceUnit{
		{Key: unit.Key{Type: "t1", Name: "u1"}},
		{Key: unit.Key{Type: "t2", Name: "u2"}},
	}
	for _, unit := range units {
		if err := mrs.Import("r", "c", unit, graph.Output{}); err != nil {
			t.Errorf("%s: Import(c, %v, empty data): %s", mrs, unit, err)
		}
	}
	if mrs, ok := mrs.(MultiRepoIndexer); ok {
		if err := mrs.Index("r", "c"); err != nil {
			t.Fatalf("%s: Index: %s", mrs, err)
		}
	}
	if err := mrs.CreateVersion("r", "c"); err != nil {
		t.Errorf("%s: CreateVersion(c): %s", mrs, err)
	}

	want := []*unit.SourceUnit{
		{Key: unit.Key{Repo: "r", CommitID: "c", Type: "t1", Name: "u1"}},
		{Key: unit.Key{Repo: "r", CommitID: "c", Type: "t2", Name: "u2"}},
	}

	units, err := mrs.Units()
	if err != nil {
		t.Errorf("%s: Units(): %s", mrs, err)
	}
	sort.Sort(unit.SourceUnits(units))
	sort.Sort(unit.SourceUnits(want))
	if !deepEqual(units, want) {
		t.Errorf("%s: Units(): got %v, want %v", mrs, units, want)
	}

	units2, err := mrs.Units(ByUnits(unit.ID2{Type: "t2", Name: "u2"}))
	if err != nil {
		t.Errorf("%s: Units(t2 u2): %s", mrs, err)
	}
	if want := []*unit.SourceUnit{{Key: unit.Key{Repo: "r", CommitID: "c", Type: "t2", Name: "u2"}}}; !deepEqual(units2, want) {
		t.Errorf("%s: Units(t2 u2): got %v, want %v", mrs, units2, want)
	}
}
Exemplo n.º 3
0
func testTreeStore_Units(t *testing.T, ts TreeStoreImporter) {
	want := []*unit.SourceUnit{
		{Key: unit.Key{Type: "t1", Name: "u1"}},
		{Key: unit.Key{Type: "t2", Name: "u2"}},
		{Key: unit.Key{Type: "t3", Name: "u3"}},
	}
	for _, unit := range want {
		if err := ts.Import(unit, graph.Output{}); err != nil {
			t.Errorf("%s: Import(%v, empty data): %s", ts, unit, err)
		}
	}
	if ts, ok := ts.(TreeIndexer); ok {
		if err := ts.Index(); err != nil {
			t.Fatalf("%s: Index: %s", ts, err)
		}
	}

	{
		c_fsTreeStore_unitsOpened.set(0)
		c_unitsIndex_listUnits.set(0)
		units, err := ts.Units()
		if err != nil {
			t.Errorf("%s: Units(): %s", ts, err)
		}
		sort.Sort(unit.SourceUnits(units))
		sort.Sort(unit.SourceUnits(want))
		if !deepEqual(units, want) {
			t.Errorf("%s: Units(): got %v, want %v", ts, units, want)
		}
		if isIndexedStore(ts) {
			if want := 1; c_unitsIndex_listUnits.get() != want {
				t.Errorf("%s: Units: listed unitsIndex %dx, want %dx", ts, c_unitsIndex_listUnits.get(), want)
			}
			if want := 0; c_fsTreeStore_unitsOpened.get() != want {
				t.Errorf("%s: Units: got %d units opened, want %d (should use unitsIndex)", ts, c_fsTreeStore_unitsOpened.get(), want)
			}
		}
	}

	{
		c_fsTreeStore_unitsOpened.set(0)
		c_unitsIndex_listUnits.set(0)

		origMaxIndividualFetches := maxIndividualFetches
		maxIndividualFetches = 1
		defer func() {
			maxIndividualFetches = origMaxIndividualFetches
		}()

		units, err := ts.Units(ByUnits(unit.ID2{Type: "t3", Name: "u3"}, unit.ID2{Type: "t1", Name: "u1"}))
		if err != nil {
			t.Errorf("%s: Units(3 and 1): %s", ts, err)
		}
		want := []*unit.SourceUnit{
			{Key: unit.Key{Type: "t1", Name: "u1"}},
			{Key: unit.Key{Type: "t3", Name: "u3"}},
		}
		sort.Sort(unit.SourceUnits(units))
		sort.Sort(unit.SourceUnits(want))
		if !deepEqual(units, want) {
			t.Errorf("%s: Units(3 and 1): got %v, want %v", ts, units, want)
		}
		if isIndexedStore(ts) {
			if want := 1; c_unitsIndex_listUnits.get() != want {
				t.Errorf("%s: Units: listed unitsIndex %dx, want %dx", ts, c_unitsIndex_listUnits.get(), want)
			}
			if want := 0; c_fsTreeStore_unitsOpened.get() != want {
				t.Errorf("%s: Units: got %d units opened, want %d (should use unitsIndex)", ts, c_fsTreeStore_unitsOpened.get(), want)
			}
		}
	}
}