func TestMap(t *testing.T) {
	bx := NewTestDB()
	defer bx.Close()

	// Create a new bucket.
	letters, err := bx.New([]byte("letters"))
	if err != nil {
		t.Error(err.Error())
	}

	// Setup items to insert.
	items := []struct {
		Key, Value []byte
	}{
		{[]byte("A"), []byte("alpha")},
		{[]byte("B"), []byte("beta")},
		{[]byte("C"), []byte("gamma")},
	}

	// Insert items into `letters` bucket.
	if err := letters.Insert(items); err != nil {
		fmt.Println("could not insert items!")
	}

	// Setup slice of items to collect results.
	type item struct {
		Key, Value []byte
	}
	results := []item{}

	// Anon func to apply to each item in bucket.
	// Here, we're just going to collect the items just inserted.
	do := func(k, v []byte) error {
		results = append(results, item{k, v})
		return nil
	}

	// Now map the `do` function over each item.
	if err := letters.Map(do); err != nil {
		t.Error(err.Error())
	}

	// Finally, check to see if our results match the originally
	// inserted items.
	for i, want := range items {
		got := results[i]
		if !bytes.Equal(got.Key, want.Key) {
			t.Errorf("got %v, want %v", got.Key, want.Key)
		}
		if !bytes.Equal(got.Value, want.Value) {
			t.Errorf("got %v, want %v", got.Value, want.Value)
		}
	}
}
// Ensure that we can apply functions to each k/v pair.
Ensure that we can apply functions to each k/v pair.
// Ensure that we can apply functions to each k / v pair .
func TestMap(t *testing.T) {
	bx := NewTestDB()
	defer bx.Close()
	letters, err := bx.New([]byte("letters"))
	if err != nil {
		t.Error(err.Error())
	}
	items := []struct {
		Key, Value []byte
	}{
		{[]byte("A"), []byte("alpha")},
		{[]byte("B"), []byte("beta")},
		{[]byte("C"), []byte("gamma")},
	}
	if err := letters.Insert(items); err != nil {
		fmt.Println("could not insert items!")
	}
	type item struct {
		Key, Value []byte
	}
	results := []item{}
	do := func(k, v []byte) error {
		results = append(results, item{k, v})
		return nil
	}
	if err := letters.Map(do); err != nil {
		t.Error(err.Error())
	}
	for i, want := range items {
		got := results[i]
		if !bytes.Equal(got.Key, want.Key) {
			t.Errorf("got %v, want %v", got.Key, want.Key)
		}
		if !bytes.Equal(got.Value, want.Value) {
			t.Errorf("got %v, want %v", got.Value, want.Value)
		}
	}
}
func TestMap ( t * testing . T ) { bx := NewTestDB ( ) defer bx . Close ( ) letters , err := bx . New ( [ ] byte ( " " ) ) if err != nil { t . Error ( err . Error ( ) ) } items := [ ] struct { Key , Value [ ] byte } { { [ ] byte ( " " ) , [ ] byte ( " " ) } , { [ ] byte ( " " ) , [ ] byte ( " " ) } , { [ ] byte ( " " ) , [ ] byte ( " " ) } , } if err := letters . Insert ( items ) ; err != nil { fmt . Println ( " " ) } type item struct { Key , Value [ ] byte } results := [ ] item { } do := func ( k , v [ ] byte ) error { results = append ( results , item { k , v } ) return nil } if err := letters . Map ( do ) ; err != nil { t . Error ( err . Error ( ) ) } for i , want := range items { got := results [ i ] if ! bytes . Equal ( got . Key , want . Key ) { t . Errorf ( " " , got . Key , want . Key ) } if ! bytes . Equal ( got . Value , want . Value ) { t . Errorf ( " " , got . Value , want . Value ) } } }
Ensure that we can apply functions to each k/v pair.
Ensure that we can apply functions to each k / v pair .
// Create a new bucket. // Setup items to insert. // Insert items into `letters` bucket. // Setup slice of items to collect results. // Anon func to apply to each item in bucket. // Here, we're just going to collect the items just inserted. // Now map the `do` function over each item. // Finally, check to see if our results match the originally // inserted items.