Exemple #1
0
func DB() *repo.DB {
	db := pouchdb.NewWithOpts("db", pouchdb.Options{DB: memdown})
	return &repo.DB{
		PouchDB:         db,
		PouchPluginFind: find.New(db),
		DBName:          "db",
	}
}
Exemple #2
0
func newDB(name string) *DB {
	pdb := pouchdb.NewWithOpts(name, PouchDBOptions)
	return &DB{
		PouchDB:         pdb,
		PouchPluginFind: find.New(pdb),
		DBName:          name,
	}
}
Exemple #3
0
func TestFind(t *testing.T) {
	mainDB := pouchdb.NewWithOpts("finddb", pouchdb.Options{
		DB: memdown,
	})
	mainDB.Destroy(pouchdb.Options{}) // to ensure a clean slate
	mainDB = pouchdb.NewWithOpts("finddb", pouchdb.Options{
		DB: memdown,
	})

	db := myDB{
		mainDB,
		find.New(mainDB),
	}

	ferr := db.CreateIndex(find.Index{
		Fields: []string{"name", "size"},
	})
	if ferr != nil {
		t.Fatalf("Error from CreateIndex: %s\n", ferr)
	}

	// Create the same index again; we should be notified it exists
	ferr = db.CreateIndex(find.Index{
		Fields: []string{"name", "size"},
	})
	if ferr != nil && !find.IsIndexExists(ferr) {
		t.Fatalf("Error re-creating index: %s\n", ferr)
	}
	if !find.IsIndexExists(ferr) {
		t.Fatalf("We were not notified that the index already existed\n")
	}

	expected := []*find.IndexDef{
		&find.IndexDef{
			Ddoc: "",
			Name: "_all_docs",
			Type: "special",
			Def: struct {
				Fields []map[string]string "json:\"fields\""
			}{
				Fields: []map[string]string{
					map[string]string{"_id": "asc"},
				},
			},
		},
		&find.IndexDef{
			Ddoc: "_design/idx-1c1850c82e1b5105c94a267ec61322ce",
			Name: "idx-1c1850c82e1b5105c94a267ec61322ce",
			Type: "json",
			Def: struct {
				Fields []map[string]string "json:\"fields\""
			}{
				Fields: []map[string]string{
					map[string]string{"name": "asc"},
					map[string]string{"size": "asc"},
				},
			},
		},
	}

	idxs, err := db.GetIndexes()
	if err != nil {
		t.Fatalf("Error running GetIndexes: %s", err)
	}
	if !reflect.DeepEqual(idxs, expected) {
		DumpDiff(expected, idxs)
		t.Fatal()
	}

	// Retrieval
	doc := map[string]interface{}{
		"_id":  "12345",
		"name": "Bob",
		"size": 48,
	}
	_, err = db.Put(doc)
	if err != nil {
		t.Fatalf("Error calling Put(): %s", err)
	}
	doc = map[string]interface{}{
		"_id":  "23456",
		"name": "Alice",
	}
	_, err = db.Put(doc)
	if err != nil {
		t.Fatalf("Error calling Put(): %s", err)
	}

	var resultDoc map[string]interface{}
	req := map[string]interface{}{
		"selector": map[string]string{
			"name": "Bob",
		},
		"fields": []string{
			"_id",
			"name",
			"size",
		},
	}
	expectedResult := map[string]interface{}{
		"docs": []interface{}{
			map[string]interface{}{
				"_id":  "12345",
				"name": "Bob",
				"size": float64(48),
			},
		},
	}
	err = db.Find(req, &resultDoc)
	if err != nil {
		t.Fatalf("Error executing Find(): %s", err)
	}
	if !reflect.DeepEqual(expectedResult, resultDoc) {
		DumpDiff(expectedResult, resultDoc)
		t.Fatal()
	}

	err = db.DeleteIndex(idxs[1])
	if err != nil {
		t.Fatalf("Error running DeleteIndex: %s", err)
	}

	expected = []*find.IndexDef{
		&find.IndexDef{
			Ddoc: "",
			Name: "_all_docs",
			Type: "special",
			Def: struct {
				Fields []map[string]string "json:\"fields\""
			}{
				Fields: []map[string]string{
					map[string]string{"_id": "asc"},
				},
			},
		},
	}
	idxs, err = db.GetIndexes()
	if err != nil {
		t.Fatalf("Error running GetIndexes: %s", err)
	}
	if !reflect.DeepEqual(idxs, expected) {
		DumpDiff(expected, idxs)
		t.Fatal()
	}
}