func TestFindDoc(t *testing.T) {
	type fdTest struct {
		name     string
		db       *db
		query    interface{}
		expected interface{}
	}
	tests := []fdTest{
		{
			name:  "simple selector",
			query: `{"selector":{}}`,
			db: func() *db {
				db := setupDB(t, nil)
				id := "chicken"
				if _, err := db.Put(context.Background(), id, map[string]string{"value": id}); err != nil {
					t.Fatal(err)
				}
				return db
			}(),
			expected: map[string]interface{}{
				"_id":   "chicken",
				"_rev":  "1-xxx",
				"value": "chicken",
			},
		},
		{
			name:  "fields",
			query: `{"selector":{}, "fields":["value","_rev"]}`,
			db: func() *db {
				db := setupDB(t, nil)
				if _, _, err := db.CreateDoc(context.Background(), map[string]string{"value": "foo"}); err != nil {
					t.Fatal(err)
				}
				return db
			}(),
			expected: map[string]interface{}{
				"value": "foo",
				"_rev":  "1-xxx",
			},
		},
	}
	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			db := test.db
			if db == nil {
				db = setupDB(t, nil)
			}
			rows, err := db.Find(context.Background(), test.query)
			if err != nil {
				t.Fatal(err)
			}
			var row driver.Row
			if e := rows.Next(&row); e != nil {
				t.Fatal(e)
			}
			_ = rows.Close()
			var result map[string]interface{}
			if e := json.Unmarshal(row.Doc, &result); e != nil {
				t.Fatal(e)
			}
			parts := strings.Split(result["_rev"].(string), "-")
			result["_rev"] = parts[0] + "-xxx"
			if d := diff.AsJSON(test.expected, result); d != nil {
				t.Error(d)
			}
		})
	}
}
// TestFindDoc is the same as Testfind, but assumes only a single result
// (ignores any others), and compares the entire document.
TestFindDoc is the same as Testfind, but assumes only a single result
(ignores any others), and compares the entire document.
// TestFindDoc is the same as Testfind but assumes only a single result // ( ignores any others ) and compares the entire document .
func TestFindDoc(t *testing.T) {
	type fdTest struct {
		name     string
		db       *db
		query    interface{}
		expected interface{}
	}
	tests := []fdTest{
		{
			name:  "simple selector",
			query: `{"selector":{}}`,
			db: func() *db {
				db := setupDB(t, nil)
				id := "chicken"
				if _, err := db.Put(context.Background(), id, map[string]string{"value": id}); err != nil {
					t.Fatal(err)
				}
				return db
			}(),
			expected: map[string]interface{}{
				"_id":   "chicken",
				"_rev":  "1-xxx",
				"value": "chicken",
			},
		},
		{
			name:  "fields",
			query: `{"selector":{}, "fields":["value","_rev"]}`,
			db: func() *db {
				db := setupDB(t, nil)
				if _, _, err := db.CreateDoc(context.Background(), map[string]string{"value": "foo"}); err != nil {
					t.Fatal(err)
				}
				return db
			}(),
			expected: map[string]interface{}{
				"value": "foo",
				"_rev":  "1-xxx",
			},
		},
	}
	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			db := test.db
			if db == nil {
				db = setupDB(t, nil)
			}
			rows, err := db.Find(context.Background(), test.query)
			if err != nil {
				t.Fatal(err)
			}
			var row driver.Row
			if e := rows.Next(&row); e != nil {
				t.Fatal(e)
			}
			_ = rows.Close()
			var result map[string]interface{}
			if e := json.Unmarshal(row.Doc, &result); e != nil {
				t.Fatal(e)
			}
			parts := strings.Split(result["_rev"].(string), "-")
			result["_rev"] = parts[0] + "-xxx"
			if d := diff.AsJSON(test.expected, result); d != nil {
				t.Error(d)
			}
		})
	}
}
func TestFindDoc ( t * testing . T ) { type fdTest struct { name string db * db query interface { } expected interface { } } tests := [ ] fdTest { { name : " " , query : `{"selector":{}}` , db : func ( ) * db { db := setupDB ( t , nil ) id := " " if _ , err := db . Put ( context . Background ( ) , id , map [ string ] string { " " : id } ) ; err != nil { t . Fatal ( err ) } return db } ( ) , expected : map [ string ] interface { } { " " : " " , " " : " " , " " : " " , } , } , { name : " " , query : `{"selector":{}, "fields":["value","_rev"]}` , db : func ( ) * db { db := setupDB ( t , nil ) if _ , _ , err := db . CreateDoc ( context . Background ( ) , map [ string ] string { " " : " " } ) ; err != nil { t . Fatal ( err ) } return db } ( ) , expected : map [ string ] interface { } { " " : " " , " " : " " , } , } , } for _ , test := range tests { t . Run ( test . name , func ( t * testing . T ) { db := test . db if db == nil { db = setupDB ( t , nil ) } rows , err := db . Find ( context . Background ( ) , test . query ) if err != nil { t . Fatal ( err ) } var row driver . Row if e := rows . Next ( & row ) ; e != nil { t . Fatal ( e ) } _ = rows . Close ( ) var result map [ string ] interface { } if e := json . Unmarshal ( row . Doc , & result ) ; e != nil { t . Fatal ( e ) } parts := strings . Split ( result [ " " ] . ( string ) , " " ) result [ " " ] = parts [ 0 ] + " " if d := diff . AsJSON ( test . expected , result ) ; d != nil { t . Error ( d ) } } ) } }
TestFindDoc is the same as Testfind, but assumes only a single result
(ignores any others), and compares the entire document.
TestFindDoc is the same as Testfind but assumes only a single result ( ignores any others ) and compares the entire document .