示例#1
0
文件: http_test.go 项目: BobbWu/degdb
func TestInsertAndRetreiveTriples(t *testing.T) {
	t.Parallel()

	s := testServer(t)
	go s.network.Listen()

	time.Sleep(10 * time.Millisecond)
	base := fmt.Sprintf("http://localhost:%d", s.network.Port)

	testTriples := testTriplesKeyspace(s.network.LocalKeyspace())

	triples, err := json.Marshal(testTriples)
	if err != nil {
		t.Error(err)
	}

	buf := bytes.NewBuffer(triples)

	resp, err := http.Post(base+"/api/v1/insert", "application/json", buf)
	if err != nil {
		t.Error(err)
	}
	out, _ := ioutil.ReadAll(resp.Body)
	if !bytes.Contains(out, []byte(strconv.Itoa(len(testTriples)))) {
		t.Errorf("http.Post(/api/v1/insert) = %+v; missing %+v", string(out), len(testTriples))
	}

	// Takes a bit of time to write
	time.Sleep(100 * time.Millisecond)

	var signedTriples []*protocol.Triple

	resp, err = http.Get(base + "/api/v1/triples")
	if err != nil {
		t.Error(err)
	}
	err = json.NewDecoder(resp.Body).Decode(&signedTriples)
	if err != nil {
		t.Error(err)
	}
	strippedTriples := stripCreated(stripSigning(signedTriples))
	protocol.SortTriples(strippedTriples)

	if diff, equal := messagediff.PrettyDiff(testTriples, strippedTriples); !equal {
		t.Errorf("http.Get(/api/v1/insert) = %+v\n;not %+v\n%s", strippedTriples, testTriples, diff)
	}
}
示例#2
0
文件: http_test.go 项目: BobbWu/degdb
// testTriplesKeyspcae returns a set of triples in the current keyspace.
func testTriplesKeyspace(keyspace *protocol.Keyspace) []*protocol.Triple {
	subjs := make(map[string]string)
	var triples []*protocol.Triple
	for _, triple := range testTriples {
		triple := *triple
		if subj, ok := subjs[triple.Subj]; ok {
			triple.Subj = subj
		} else {
			subj = subjInKeyspace(keyspace, triple.Subj)
			subjs[triple.Subj] = subj
			triple.Subj = subj
		}
		triples = append(triples, &triple)
	}
	protocol.SortTriples(triples)
	return triples
}
示例#3
0
func TestBloom(t *testing.T) {
	t.Parallel()

	file, err := ioutil.TempFile(os.TempDir(), "triplestore.db")
	if err != nil {
		t.Fatal(err)
	}
	defer os.Remove(file.Name())
	db, err := NewTripleStore(file.Name(), log.New(os.Stdout, "", log.Flags()))
	if err != nil {
		t.Fatal(err)
	}

	tripleCount := 5000

	additionalTriples := make([]*protocol.Triple, 0, tripleCount+len(testTriples))
	for i := 0; i < tripleCount; i++ {
		additionalTriples = append(additionalTriples, &protocol.Triple{
			Subj: "/m/0test",
			Pred: "/type/object/name",
			Obj:  "Bloom " + strconv.Itoa(i),
		})
	}
	additionalTriples = append(additionalTriples, testTriples...)
	protocol.SortTriples(additionalTriples)

	db.Insert(additionalTriples)

	filter, err := db.Bloom(nil)
	if err != nil {
		t.Fatal(err)
	}
	for _, triple := range additionalTriples {
		data, err := triple.Marshal()
		if err != nil {
			t.Error(err)
			continue
		}
		if !filter.Test(data) {
			t.Errorf("Bloom filter missing triple %+v", triple)
		}
	}

	filter2, err := db.Bloom(&protocol.Keyspace{})
	if err != nil {
		t.Fatal(err)
	}

	for _, triple := range additionalTriples {
		data, err := triple.Marshal()
		if err != nil {
			t.Error(err)
			continue
		}
		if filter2.Test(data) {
			t.Errorf("Bloom filter incorrectly has %+v", triple)
		}
	}

	var resultTriples []*protocol.Triple
	results, errs := db.TriplesMatchingBloom(filter)
	for triples := range results {
		resultTriples = append(resultTriples, triples...)
	}
	for err := range errs {
		t.Error(err)
	}
	protocol.SortTriples(resultTriples)
	if diff, ok := messagediff.PrettyDiff(additionalTriples, resultTriples); !ok {
		t.Errorf("TriplesMatchingBloom(filter) = %#v; diff %s", resultTriples, diff)
	}

	resultTriples = nil
	results, errs = db.TriplesMatchingBloom(filter2)
	for triples := range results {
		resultTriples = append(resultTriples, triples...)
	}
	for err := range errs {
		t.Error(err)
	}
	for _, triple := range resultTriples {
		t.Errorf("TriplesMatchingBLoom(nil) incorrectly has %+v", triple)
	}
}
示例#4
0
文件: http_test.go 项目: BobbWu/degdb
func init() {
	newTmpDir()
	protocol.SortTriples(testTriples)
}