|
| 1 | +/* |
| 2 | +2019 © Postgres.ai |
| 3 | +*/ |
| 4 | + |
| 5 | +package models |
| 6 | + |
| 7 | +import ( |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | +) |
| 13 | + |
| 14 | +func TestClone_IsProtected(t *testing.T) { |
| 15 | + tests := []struct { |
| 16 | + name string |
| 17 | + clone Clone |
| 18 | + expected bool |
| 19 | + }{ |
| 20 | + {name: "not protected flag is false", clone: Clone{Protected: false}, expected: false}, |
| 21 | + {name: "protected with no expiry (infinite)", clone: Clone{Protected: true, ProtectedTill: nil}, expected: true}, |
| 22 | + {name: "protected with future expiry", clone: Clone{Protected: true, ProtectedTill: NewLocalTime(time.Now().Add(1 * time.Hour))}, expected: true}, |
| 23 | + {name: "protected with past expiry (expired)", clone: Clone{Protected: true, ProtectedTill: NewLocalTime(time.Now().Add(-1 * time.Hour))}, expected: false}, |
| 24 | + {name: "protected with expiry exactly now", clone: Clone{Protected: true, ProtectedTill: NewLocalTime(time.Now())}, expected: false}, |
| 25 | + {name: "protected with far future expiry", clone: Clone{Protected: true, ProtectedTill: NewLocalTime(time.Now().Add(365 * 24 * time.Hour))}, expected: true}, |
| 26 | + {name: "protected with 1 minute future", clone: Clone{Protected: true, ProtectedTill: NewLocalTime(time.Now().Add(1 * time.Minute))}, expected: true}, |
| 27 | + {name: "protected with 1 second past", clone: Clone{Protected: true, ProtectedTill: NewLocalTime(time.Now().Add(-1 * time.Second))}, expected: false}, |
| 28 | + {name: "not protected but has expiry time", clone: Clone{Protected: false, ProtectedTill: NewLocalTime(time.Now().Add(1 * time.Hour))}, expected: false}, |
| 29 | + } |
| 30 | + |
| 31 | + for _, tt := range tests { |
| 32 | + t.Run(tt.name, func(t *testing.T) { |
| 33 | + result := tt.clone.IsProtected() |
| 34 | + assert.Equal(t, tt.expected, result) |
| 35 | + }) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func TestClone_ProtectionExpiresIn(t *testing.T) { |
| 40 | + tests := []struct { |
| 41 | + name string |
| 42 | + clone Clone |
| 43 | + expectZero bool |
| 44 | + minDuration time.Duration |
| 45 | + maxDuration time.Duration |
| 46 | + }{ |
| 47 | + {name: "not protected returns zero", clone: Clone{Protected: false}, expectZero: true}, |
| 48 | + {name: "protected with no expiry returns zero", clone: Clone{Protected: true, ProtectedTill: nil}, expectZero: true}, |
| 49 | + {name: "protected with past expiry returns zero", clone: Clone{Protected: true, ProtectedTill: NewLocalTime(time.Now().Add(-1 * time.Hour))}, expectZero: true}, |
| 50 | + {name: "protected with future expiry", clone: Clone{Protected: true, ProtectedTill: NewLocalTime(time.Now().Add(1 * time.Hour))}, expectZero: false, minDuration: 59 * time.Minute, maxDuration: 61 * time.Minute}, |
| 51 | + {name: "protected with 30 minute expiry", clone: Clone{Protected: true, ProtectedTill: NewLocalTime(time.Now().Add(30 * time.Minute))}, expectZero: false, minDuration: 29 * time.Minute, maxDuration: 31 * time.Minute}, |
| 52 | + {name: "protected with 1 day expiry", clone: Clone{Protected: true, ProtectedTill: NewLocalTime(time.Now().Add(24 * time.Hour))}, expectZero: false, minDuration: 23 * time.Hour, maxDuration: 25 * time.Hour}, |
| 53 | + {name: "not protected but has expiry time", clone: Clone{Protected: false, ProtectedTill: NewLocalTime(time.Now().Add(1 * time.Hour))}, expectZero: true}, |
| 54 | + } |
| 55 | + |
| 56 | + for _, tt := range tests { |
| 57 | + t.Run(tt.name, func(t *testing.T) { |
| 58 | + result := tt.clone.ProtectionExpiresIn() |
| 59 | + if tt.expectZero { |
| 60 | + assert.Equal(t, time.Duration(0), result) |
| 61 | + } else { |
| 62 | + assert.GreaterOrEqual(t, result, tt.minDuration) |
| 63 | + assert.LessOrEqual(t, result, tt.maxDuration) |
| 64 | + } |
| 65 | + }) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func TestClone_IsProtected_EdgeCases(t *testing.T) { |
| 70 | + t.Run("rapid check near expiry time", func(t *testing.T) { |
| 71 | + expiryTime := time.Now().Add(100 * time.Millisecond) |
| 72 | + clone := Clone{Protected: true, ProtectedTill: NewLocalTime(expiryTime)} |
| 73 | + |
| 74 | + assert.True(t, clone.IsProtected()) |
| 75 | + |
| 76 | + time.Sleep(150 * time.Millisecond) |
| 77 | + |
| 78 | + assert.False(t, clone.IsProtected()) |
| 79 | + }) |
| 80 | + |
| 81 | + t.Run("zero time value", func(t *testing.T) { |
| 82 | + clone := Clone{Protected: true, ProtectedTill: NewLocalTime(time.Time{})} |
| 83 | + assert.False(t, clone.IsProtected()) |
| 84 | + }) |
| 85 | +} |
| 86 | + |
| 87 | +func TestClone_ProtectionExpiresIn_EdgeCases(t *testing.T) { |
| 88 | + t.Run("expiry in milliseconds", func(t *testing.T) { |
| 89 | + clone := Clone{Protected: true, ProtectedTill: NewLocalTime(time.Now().Add(500 * time.Millisecond))} |
| 90 | + |
| 91 | + result := clone.ProtectionExpiresIn() |
| 92 | + assert.Greater(t, result, time.Duration(0)) |
| 93 | + assert.Less(t, result, 1*time.Second) |
| 94 | + }) |
| 95 | + |
| 96 | + t.Run("very large expiry", func(t *testing.T) { |
| 97 | + clone := Clone{Protected: true, ProtectedTill: NewLocalTime(time.Now().Add(10 * 365 * 24 * time.Hour))} |
| 98 | + |
| 99 | + result := clone.ProtectionExpiresIn() |
| 100 | + assert.Greater(t, result, 9*365*24*time.Hour) |
| 101 | + }) |
| 102 | + |
| 103 | + t.Run("just expired returns zero", func(t *testing.T) { |
| 104 | + clone := Clone{Protected: true, ProtectedTill: NewLocalTime(time.Now().Add(-1 * time.Nanosecond))} |
| 105 | + |
| 106 | + result := clone.ProtectionExpiresIn() |
| 107 | + assert.Equal(t, time.Duration(0), result) |
| 108 | + }) |
| 109 | +} |
0 commit comments