Ejemplo n.º 1
0
// expectCount iterates through the cursor results and logs a fatal error if
// the number of documents is not equal to n.
func expectCount(cursor mongo.Cursor, n int) {
	defer cursor.Close()
	i := 0
	for cursor.HasNext() {
		i += 1
		var m mongo.M
		err := cursor.Next(&m)
		if err != nil {
			log.Fatal(err)
		}
	}
	if i != n {
		log.Fatalf("Got result count=%d, want %d", i, n)
	}
}
Ejemplo n.º 2
0
func traverseQueryResult(cursor mongo.Cursor, limit int) []mongo.M {
	foods := make([]mongo.M, 0)
	i := 0

	for cursor.HasNext() && i < limit {
		var m mongo.M
		err := cursor.Next(&m)
		if nil != err {
			log.Println("decode mongo map fail|", err)
			continue
		}

		foods = append(foods, m)
		i++
	}
	return foods
}
Ejemplo n.º 3
0
// expectFieldValues iterates through the cursor and logs a fatal error if
// the a document does not have field in values or if a value in values was not
// found in a document.
func expectFieldValues(cursor mongo.Cursor, field string, values ...interface{}) {
	defer cursor.Close()
	found := map[interface{}]bool{}
	for cursor.HasNext() {
		var m mongo.M
		err := cursor.Next(&m)
		if err != nil {
			log.Fatal(err)
		}
		found[m["name"]] = true
	}
	for _, value := range values {
		if !found[value] {
			log.Fatalf("Expected result %v not found\n", value)
		} else {
			delete(found, value)
		}
	}
	for value, _ := range found {
		log.Fatalf("Unexpected result %v found\n", value)
	}
}