diff --git a/operation/operation.go b/operation/operation.go index d2b572d..e246b32 100644 --- a/operation/operation.go +++ b/operation/operation.go @@ -5,12 +5,65 @@ package operation // Operation represents a storage operation to be executed. // This is used within transactions and other operation contexts. type Operation struct { - // Type specifies the operation type (Get, Put, Delete). - Type Type - // Key is the target key for the operation. - Key []byte - // Value contains the data for put operations, nil for get/delete. - Value []byte - // Options contains additional operation configuration. - Options []Option + // tp specifies the operation type (Get, Put, Delete). + tp Type + // key is the target key for the operation. + key []byte + // value contains the data for put operations, nil for get/delete. + value []byte + // options contains additional operation configuration. + options []Option +} + +// Type returns the operation type (Get, Put, or Delete). +func (o Operation) Type() Type { + return o.tp +} + +// Key returns the key associated with the operation. +func (o Operation) Key() []byte { + return o.key +} + +// Value returns the value associated with the operation. +func (o Operation) Value() []byte { + return o.value +} + +// Options returns the configuration options for the operation. +func (o Operation) Options() []Option { + return o.options +} + +// Get creates a new read operation for the specified key. +// Returns an Operation configured for reading data from storage. +func Get(key []byte, options ...Option) Operation { + return Operation{ + tp: TypeGet, + key: key, + value: nil, + options: options, + } +} + +// Put creates a new write operation for the specified key-value pair. +// Returns an Operation configured for writing data to storage. +func Put(key, value []byte, options ...Option) Operation { + return Operation{ + tp: TypePut, + key: key, + value: value, + options: options, + } +} + +// Delete creates a new delete operation for the specified key. +// Returns an Operation configured for removing data from storage. +func Delete(key []byte, options ...Option) Operation { + return Operation{ + tp: TypeDelete, + key: key, + value: nil, + options: options, + } } diff --git a/operation/operation_test.go b/operation/operation_test.go new file mode 100644 index 0000000..39aa50e --- /dev/null +++ b/operation/operation_test.go @@ -0,0 +1,83 @@ +package operation_test + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/tarantool/go-storage/operation" +) + +func TestGet(t *testing.T) { + t.Parallel() + + key := []byte("test-key") + op := operation.Get(key) + + assert.Equal(t, operation.TypeGet, op.Type()) + assert.Equal(t, key, op.Key()) + assert.Nil(t, op.Value()) + assert.Empty(t, op.Options()) +} + +func TestGetWithOptions(t *testing.T) { + t.Parallel() + + key := []byte("test-key") + op := operation.Get(key, operation.Option{}, operation.Option{}) + + assert.Equal(t, operation.TypeGet, op.Type()) + assert.Equal(t, key, op.Key()) + assert.Nil(t, op.Value()) + assert.Len(t, op.Options(), 2) +} + +func TestPut(t *testing.T) { + t.Parallel() + + key := []byte("test-key") + value := []byte("test-value") + op := operation.Put(key, value) + + assert.Equal(t, operation.TypePut, op.Type()) + assert.Equal(t, key, op.Key()) + assert.Equal(t, value, op.Value()) + assert.Empty(t, op.Options()) +} + +func TestPutWithOptions(t *testing.T) { + t.Parallel() + + key := []byte("test-key") + value := []byte("test-value") + op := operation.Put(key, value, operation.Option{}, operation.Option{}) + + assert.Equal(t, operation.TypePut, op.Type()) + assert.Equal(t, key, op.Key()) + assert.Equal(t, value, op.Value()) + assert.Len(t, op.Options(), 2) +} + +func TestDelete(t *testing.T) { + t.Parallel() + + key := []byte("test-key") + op := operation.Delete(key) + + assert.Equal(t, operation.TypeDelete, op.Type()) + assert.Equal(t, key, op.Key()) + assert.Nil(t, op.Value()) + assert.Empty(t, op.Options()) +} + +func TestDeleteWithOptions(t *testing.T) { + t.Parallel() + + key := []byte("test-key") + op := operation.Delete(key, operation.Option{}, operation.Option{}) + + assert.Equal(t, operation.TypeDelete, op.Type()) + assert.Equal(t, key, op.Key()) + assert.Nil(t, op.Value()) + assert.Len(t, op.Options(), 2) +} diff --git a/operation/type_test.go b/operation/type_test.go new file mode 100644 index 0000000..0b15064 --- /dev/null +++ b/operation/type_test.go @@ -0,0 +1,33 @@ +package operation_test + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/tarantool/go-storage/operation" +) + +func TestTypeString(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + typ operation.Type + expected string + }{ + {"TypeGet", operation.TypeGet, "Get"}, + {"TypePut", operation.TypePut, "Put"}, + {"TypeDelete", operation.TypeDelete, "Delete"}, + {"UnknownType", operation.Type(99), "Unknown"}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + result := tt.typ.String() + assert.Equal(t, tt.expected, result) + }) + } +}