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

	things, err := bx.New([]byte("things"))
	if err != nil {
		t.Error(err.Error())
	}

	key := []byte("A")
	a, b := []byte("alpha"), []byte("beta")

	// Put key/a-value into the `things` bucket.
	if err := things.PutNX(key, a); err != nil {
		t.Error(err.Error())
	}

	// Read value back in a different read-only transaction.
	got, err := things.Get(key)
	if err != nil && !bytes.Equal(got, a) {
		t.Error(err.Error())
	}

	// Try putting key/b-value into the `things` bucket.
	if err := things.PutNX(key, b); err != nil {
		t.Error(err.Error())
	}

	// Value for key should still be a, not b.
	got, err = things.Get(key)
	if err != nil && !bytes.Equal(got, a) {
		t.Error(err.Error())
	}
}
// Ensure we don't overwrite existing items when using PutNX.
Ensure we don't overwrite existing items when using PutNX.
// Ensure we don ' t overwrite existing items when using PutNX .
func TestPutNX(t *testing.T) {
	bx := NewTestDB()
	defer bx.Close()
	things, err := bx.New([]byte("things"))
	if err != nil {
		t.Error(err.Error())
	}
	key := []byte("A")
	a, b := []byte("alpha"), []byte("beta")
	if err := things.PutNX(key, a); err != nil {
		t.Error(err.Error())
	}
	got, err := things.Get(key)
	if err != nil && !bytes.Equal(got, a) {
		t.Error(err.Error())
	}
	if err := things.PutNX(key, b); err != nil {
		t.Error(err.Error())
	}
	got, err = things.Get(key)
	if err != nil && !bytes.Equal(got, a) {
		t.Error(err.Error())
	}
}
func TestPutNX ( t * testing . T ) { bx := NewTestDB ( ) defer bx . Close ( ) things , err := bx . New ( [ ] byte ( " " ) ) if err != nil { t . Error ( err . Error ( ) ) } key := [ ] byte ( " " ) a , b := [ ] byte ( " " ) , [ ] byte ( " " ) if err := things . PutNX ( key , a ) ; err != nil { t . Error ( err . Error ( ) ) } got , err := things . Get ( key ) if err != nil && ! bytes . Equal ( got , a ) { t . Error ( err . Error ( ) ) } if err := things . PutNX ( key , b ) ; err != nil { t . Error ( err . Error ( ) ) } got , err = things . Get ( key ) if err != nil && ! bytes . Equal ( got , a ) { t . Error ( err . Error ( ) ) } }
Ensure we don't overwrite existing items when using PutNX.
Ensure we don ' t overwrite existing items when using PutNX .
// Put key/a-value into the `things` bucket. // Read value back in a different read-only transaction. // Try putting key/b-value into the `things` bucket. // Value for key should still be a, not b.