示例#1
0
func testTreeStore_Units_ByFile(t *testing.T, ts TreeStoreImporter) {
	want := []*unit.SourceUnit{
		{Type: "t1", Name: "u1", Files: []string{"f1"}},
		{Type: "t2", Name: "u2", Files: []string{"f1", "f2"}},
		{Type: "t3", Name: "u3", 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 = 0
	units, err := ts.Units(ByFiles("f1"))
	if err != nil {
		t.Errorf("%s: Units(ByFiles f1): %s", ts, err)
	}
	sort.Sort(unit.SourceUnits(units))
	sort.Sort(unit.SourceUnits(want))
	if !reflect.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 != want {
			t.Errorf("%s: Units(ByFiles f1): got %d index hits, want %d", ts, c_unitFilesIndex_getByPath, want)
		}
	}

	c_unitFilesIndex_getByPath = 0
	units2, err := ts.Units(ByFiles("f2"))
	if err != nil {
		t.Errorf("%s: Units(ByFiles f2): %s", ts, err)
	}
	want2 := []*unit.SourceUnit{
		{Type: "t2", Name: "u2", Files: []string{"f1", "f2"}},
	}
	if !reflect.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 != want {
			t.Errorf("%s: Units(ByFiles f1): got %d index hits, want %d", ts, c_unitFilesIndex_getByPath, want)
		}
	}
}
示例#2
0
func testMultiRepoStore_Units(t *testing.T, mrs MultiRepoStoreImporter) {
	units := []*unit.SourceUnit{
		{Type: "t1", Name: "u1"},
		{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)
		}
	}

	want := []*unit.SourceUnit{
		{Repo: "r", CommitID: "c", Type: "t1", Name: "u1"},
		{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 !reflect.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{{Repo: "r", CommitID: "c", Type: "t2", Name: "u2"}}; !reflect.DeepEqual(units2, want) {
		t.Errorf("%s: Units(t2 u2): got %v, want %v", mrs, units2, want)
	}
}
示例#3
0
func testTreeStore_Units(t *testing.T, ts TreeStoreImporter) {
	want := []*unit.SourceUnit{
		{Type: "t1", Name: "u1"},
		{Type: "t2", Name: "u2"},
		{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 = 0
		c_unitsIndex_listUnits = 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 !reflect.DeepEqual(units, want) {
			t.Errorf("%s: Units(): got %v, want %v", ts, units, want)
		}
		if isIndexedStore(ts) {
			if want := 1; c_unitsIndex_listUnits != want {
				t.Errorf("%s: Units: listed unitsIndex %dx, want %dx", ts, c_unitsIndex_listUnits, want)
			}
			if want := 0; c_fsTreeStore_unitsOpened != want {
				t.Errorf("%s: Units: got %d units opened, want %d (should use unitsIndex)", ts, c_fsTreeStore_unitsOpened, want)
			}
		}
	}

	{
		c_fsTreeStore_unitsOpened = 0
		c_unitsIndex_listUnits = 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{
			{Type: "t1", Name: "u1"},
			{Type: "t3", Name: "u3"},
		}
		sort.Sort(unit.SourceUnits(units))
		sort.Sort(unit.SourceUnits(want))
		if !reflect.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 != want {
				t.Errorf("%s: Units: listed unitsIndex %dx, want %dx", ts, c_unitsIndex_listUnits, want)
			}
			if want := 0; c_fsTreeStore_unitsOpened != want {
				t.Errorf("%s: Units: got %d units opened, want %d (should use unitsIndex)", ts, c_fsTreeStore_unitsOpened, want)
			}
		}
	}
}