github.com/10XDev/rclone@v1.52.3-0.20200626220027-16af9ab76b2a/backend/b2/api/types_test.go (about) 1 package api_test 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/rclone/rclone/backend/b2/api" 8 "github.com/rclone/rclone/fstest" 9 "github.com/stretchr/testify/assert" 10 "github.com/stretchr/testify/require" 11 ) 12 13 var ( 14 emptyT api.Timestamp 15 t0 = api.Timestamp(fstest.Time("1970-01-01T01:01:01.123456789Z")) 16 t0r = api.Timestamp(fstest.Time("1970-01-01T01:01:01.123000000Z")) 17 t1 = api.Timestamp(fstest.Time("2001-02-03T04:05:06.123000000Z")) 18 ) 19 20 func TestTimestampMarshalJSON(t *testing.T) { 21 resB, err := t0.MarshalJSON() 22 res := string(resB) 23 require.NoError(t, err) 24 assert.Equal(t, "3661123", res) 25 26 resB, err = t1.MarshalJSON() 27 res = string(resB) 28 require.NoError(t, err) 29 assert.Equal(t, "981173106123", res) 30 } 31 32 func TestTimestampUnmarshalJSON(t *testing.T) { 33 var tActual api.Timestamp 34 err := tActual.UnmarshalJSON([]byte("981173106123")) 35 require.NoError(t, err) 36 assert.Equal(t, (time.Time)(t1), (time.Time)(tActual)) 37 } 38 39 func TestTimestampAddVersion(t *testing.T) { 40 for _, test := range []struct { 41 t api.Timestamp 42 in string 43 expected string 44 }{ 45 {t0, "potato.txt", "potato-v1970-01-01-010101-123.txt"}, 46 {t1, "potato", "potato-v2001-02-03-040506-123"}, 47 {t1, "", "-v2001-02-03-040506-123"}, 48 } { 49 actual := test.t.AddVersion(test.in) 50 assert.Equal(t, test.expected, actual, test.in) 51 } 52 } 53 54 func TestTimestampRemoveVersion(t *testing.T) { 55 for _, test := range []struct { 56 in string 57 expectedT api.Timestamp 58 expectedRemote string 59 }{ 60 {"potato.txt", emptyT, "potato.txt"}, 61 {"potato-v1970-01-01-010101-123.txt", t0r, "potato.txt"}, 62 {"potato-v2001-02-03-040506-123", t1, "potato"}, 63 {"-v2001-02-03-040506-123", t1, ""}, 64 {"potato-v2A01-02-03-040506-123", emptyT, "potato-v2A01-02-03-040506-123"}, 65 {"potato-v2001-02-03-040506=123", emptyT, "potato-v2001-02-03-040506=123"}, 66 } { 67 actualT, actualRemote := api.RemoveVersion(test.in) 68 assert.Equal(t, test.expectedT, actualT, test.in) 69 assert.Equal(t, test.expectedRemote, actualRemote, test.in) 70 } 71 } 72 73 func TestTimestampIsZero(t *testing.T) { 74 assert.True(t, emptyT.IsZero()) 75 assert.False(t, t0.IsZero()) 76 assert.False(t, t1.IsZero()) 77 } 78 79 func TestTimestampEqual(t *testing.T) { 80 assert.False(t, emptyT.Equal(emptyT)) 81 assert.False(t, t0.Equal(emptyT)) 82 assert.False(t, emptyT.Equal(t0)) 83 assert.False(t, t0.Equal(t1)) 84 assert.False(t, t1.Equal(t0)) 85 assert.True(t, t0.Equal(t0)) 86 assert.True(t, t1.Equal(t1)) 87 }