git.frostfs.info/TrueCloudLab/frostfs-sdk-go@v0.0.0-20241022124111-5361f0ecebd3/object/patch_test.go (about)

     1  package object
     2  
     3  import (
     4  	"testing"
     5  
     6  	v2object "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/object"
     7  	"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/refs"
     8  	oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
     9  	oidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id/test"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestPatch(t *testing.T) {
    14  	t.Run("to v2", func(t *testing.T) {
    15  		t.Run("only attributes", func(t *testing.T) {
    16  			var p Patch
    17  
    18  			var attr1, attr2 Attribute
    19  			attr1.SetKey("key1")
    20  			attr1.SetValue("val1")
    21  			attr2.SetKey("key2")
    22  			attr2.SetValue("val2")
    23  
    24  			p.Address = oidtest.Address()
    25  			p.NewAttributes = []Attribute{attr1, attr2}
    26  			p.ReplaceAttributes = true
    27  
    28  			v2patch := p.ToV2()
    29  
    30  			addr := new(oid.Address)
    31  			require.NotNil(t, v2patch.GetAddress())
    32  			addr.ReadFromV2(*v2patch.GetAddress())
    33  			require.True(t, p.Address.Equals(*addr))
    34  
    35  			require.Equal(t, p.ReplaceAttributes, v2patch.GetReplaceAttributes())
    36  
    37  			require.Nil(t, v2patch.GetPatch())
    38  
    39  			require.Len(t, v2patch.GetNewAttributes(), 2)
    40  			require.Equal(t, attr1.Key(), v2patch.GetNewAttributes()[0].GetKey())
    41  			require.Equal(t, attr2.Key(), v2patch.GetNewAttributes()[1].GetKey())
    42  			require.Equal(t, attr1.Value(), v2patch.GetNewAttributes()[0].GetValue())
    43  			require.Equal(t, attr2.Value(), v2patch.GetNewAttributes()[1].GetValue())
    44  		})
    45  
    46  		t.Run("with payload patch", func(t *testing.T) {
    47  			var p Patch
    48  
    49  			var attr1, attr2 Attribute
    50  			attr1.SetKey("key1")
    51  			attr1.SetValue("val1")
    52  			attr2.SetKey("key2")
    53  			attr2.SetValue("val2")
    54  
    55  			p.Address = oidtest.Address()
    56  			p.NewAttributes = []Attribute{attr1, attr2}
    57  			p.ReplaceAttributes = true
    58  
    59  			rng := &Range{}
    60  			rng.SetOffset(100)
    61  			rng.SetLength(10)
    62  			p.PayloadPatch = &PayloadPatch{
    63  				Range: rng,
    64  				Chunk: []byte("payload_patch_chunk"),
    65  			}
    66  
    67  			v2patch := p.ToV2()
    68  
    69  			addr := new(oid.Address)
    70  			require.NotNil(t, v2patch.GetAddress())
    71  			addr.ReadFromV2(*v2patch.GetAddress())
    72  			require.True(t, p.Address.Equals(*addr))
    73  
    74  			require.Equal(t, p.ReplaceAttributes, v2patch.GetReplaceAttributes())
    75  
    76  			require.Len(t, v2patch.GetNewAttributes(), 2)
    77  			require.Equal(t, attr1.Key(), v2patch.GetNewAttributes()[0].GetKey())
    78  			require.Equal(t, attr2.Key(), v2patch.GetNewAttributes()[1].GetKey())
    79  			require.Equal(t, attr1.Value(), v2patch.GetNewAttributes()[0].GetValue())
    80  			require.Equal(t, attr2.Value(), v2patch.GetNewAttributes()[1].GetValue())
    81  
    82  			require.NotNil(t, v2patch.GetPatch())
    83  			require.NotNil(t, v2patch.GetPatch().Range)
    84  			require.Equal(t, uint64(100), v2patch.GetPatch().Range.GetOffset())
    85  			require.Equal(t, uint64(10), v2patch.GetPatch().Range.GetLength())
    86  			require.Equal(t, []byte("payload_patch_chunk"), v2patch.GetPatch().Chunk)
    87  		})
    88  
    89  	})
    90  
    91  	t.Run("from v2", func(t *testing.T) {
    92  		t.Run("only attributes", func(t *testing.T) {
    93  			v2patch := new(v2object.PatchRequestBody)
    94  
    95  			address := oidtest.Address()
    96  			v2addr := new(refs.Address)
    97  			address.WriteToV2(v2addr)
    98  			v2patch.SetAddress(v2addr)
    99  
   100  			var attr1, attr2 Attribute
   101  			attr1.SetKey("key1")
   102  			attr1.SetValue("val1")
   103  			attr2.SetKey("key2")
   104  			attr2.SetValue("val2")
   105  
   106  			v2patch.SetNewAttributes([]v2object.Attribute{
   107  				*attr1.ToV2(), *attr2.ToV2(),
   108  			})
   109  			v2patch.SetReplaceAttributes(true)
   110  
   111  			var p Patch
   112  			p.FromV2(v2patch)
   113  
   114  			require.Equal(t, address, p.Address)
   115  			require.Equal(t, []Attribute{attr1, attr2}, p.NewAttributes)
   116  			require.Equal(t, true, p.ReplaceAttributes)
   117  			require.Nil(t, p.PayloadPatch)
   118  		})
   119  
   120  		t.Run("with payload patch", func(t *testing.T) {
   121  			v2patchReqBody := new(v2object.PatchRequestBody)
   122  
   123  			address := oidtest.Address()
   124  			v2addr := new(refs.Address)
   125  			address.WriteToV2(v2addr)
   126  			v2patchReqBody.SetAddress(v2addr)
   127  
   128  			var attr1, attr2 Attribute
   129  			attr1.SetKey("key1")
   130  			attr1.SetValue("val1")
   131  			attr2.SetKey("key2")
   132  			attr2.SetValue("val2")
   133  
   134  			v2patchReqBody.SetNewAttributes([]v2object.Attribute{
   135  				*attr1.ToV2(), *attr2.ToV2(),
   136  			})
   137  			v2patchReqBody.SetReplaceAttributes(true)
   138  
   139  			v2Rng := &v2object.Range{}
   140  			v2Rng.SetOffset(13)
   141  			v2Rng.SetLength(10)
   142  			v2Patch := &v2object.PatchRequestBodyPatch{
   143  				Range: v2Rng,
   144  				Chunk: []byte("payload_patch_chunk"),
   145  			}
   146  			v2patchReqBody.SetPatch(v2Patch)
   147  
   148  			var p Patch
   149  			p.FromV2(v2patchReqBody)
   150  
   151  			require.Equal(t, address, p.Address)
   152  			require.Equal(t, []Attribute{attr1, attr2}, p.NewAttributes)
   153  			require.Equal(t, true, p.ReplaceAttributes)
   154  			require.NotNil(t, p.PayloadPatch)
   155  			require.NotNil(t, p.PayloadPatch.Range)
   156  			require.Equal(t, uint64(13), p.PayloadPatch.Range.GetOffset())
   157  			require.Equal(t, uint64(10), p.PayloadPatch.Range.GetLength())
   158  			require.Equal(t, []byte("payload_patch_chunk"), p.PayloadPatch.Chunk)
   159  		})
   160  	})
   161  }