Skip to content

Commit 031a050

Browse files
committed
Unit tests
Signed-off-by: Zeynel Koca <[email protected]>
1 parent 9102a8e commit 031a050

File tree

1 file changed

+167
-0
lines changed

1 file changed

+167
-0
lines changed

state/aws/dynamodb/dynamodb_test.go

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,3 +1161,170 @@ func TestMultiTx(t *testing.T) {
11611161
require.NoError(t, err)
11621162
})
11631163
}
1164+
1165+
func TestParseTTLWithDefault(t *testing.T) {
1166+
t.Run("Use explicit TTL from request metadata", func(t *testing.T) {
1167+
defaultTTL := 600
1168+
s := StateStore{
1169+
ttlAttributeName: "expiresAt",
1170+
ttlInSeconds: &defaultTTL,
1171+
}
1172+
1173+
req := &state.SetRequest{
1174+
Key: "test-key",
1175+
Metadata: map[string]string{
1176+
"ttlInSeconds": "300",
1177+
},
1178+
}
1179+
1180+
ttl, err := s.parseTTL(req)
1181+
require.NoError(t, err)
1182+
require.NotNil(t, ttl)
1183+
1184+
// Should use explicit value (300), not default (600)
1185+
expectedTime := time.Now().Unix() + 300
1186+
assert.InDelta(t, expectedTime, *ttl, 2) // Allow 2 second tolerance
1187+
})
1188+
1189+
t.Run("Use default TTL when no explicit TTL in request", func(t *testing.T) {
1190+
defaultTTL := 600
1191+
s := StateStore{
1192+
ttlAttributeName: "expiresAt",
1193+
ttlInSeconds: &defaultTTL,
1194+
}
1195+
1196+
req := &state.SetRequest{
1197+
Key: "test-key",
1198+
Metadata: map[string]string{},
1199+
}
1200+
1201+
ttl, err := s.parseTTL(req)
1202+
require.NoError(t, err)
1203+
require.NotNil(t, ttl)
1204+
1205+
// Should use default value (600)
1206+
expectedTime := time.Now().Unix() + 600
1207+
assert.InDelta(t, expectedTime, *ttl, 2) // Allow 2 second tolerance
1208+
})
1209+
1210+
t.Run("No TTL when no default and no explicit TTL", func(t *testing.T) {
1211+
s := StateStore{
1212+
ttlAttributeName: "expiresAt",
1213+
ttlInSeconds: nil, // No default configured
1214+
}
1215+
1216+
req := &state.SetRequest{
1217+
Key: "test-key",
1218+
Metadata: map[string]string{},
1219+
}
1220+
1221+
ttl, err := s.parseTTL(req)
1222+
require.NoError(t, err)
1223+
assert.Nil(t, ttl)
1224+
})
1225+
1226+
t.Run("No TTL when ttlAttributeName is not set", func(t *testing.T) {
1227+
defaultTTL := 600
1228+
s := StateStore{
1229+
ttlAttributeName: "", // TTL not enabled in component
1230+
ttlInSeconds: &defaultTTL,
1231+
}
1232+
1233+
req := &state.SetRequest{
1234+
Key: "test-key",
1235+
Metadata: map[string]string{
1236+
"ttlInSeconds": "300",
1237+
},
1238+
}
1239+
1240+
ttl, err := s.parseTTL(req)
1241+
require.NoError(t, err)
1242+
assert.Nil(t, ttl) // Should return nil when TTL not enabled
1243+
})
1244+
1245+
t.Run("Explicit TTL with value -1", func(t *testing.T) {
1246+
defaultTTL := 600
1247+
s := StateStore{
1248+
ttlAttributeName: "expiresAt",
1249+
ttlInSeconds: &defaultTTL,
1250+
}
1251+
1252+
req := &state.SetRequest{
1253+
Key: "test-key",
1254+
Metadata: map[string]string{
1255+
"ttlInSeconds": "-1",
1256+
},
1257+
}
1258+
1259+
ttl, err := s.parseTTL(req)
1260+
require.NoError(t, err)
1261+
require.NotNil(t, ttl)
1262+
1263+
// -1 should result in immediate expiration (now + -1)
1264+
expectedTime := time.Now().Unix() - 1
1265+
assert.InDelta(t, expectedTime, *ttl, 2)
1266+
})
1267+
1268+
t.Run("Default TTL with large value", func(t *testing.T) {
1269+
defaultTTL := 86400 // 24 hours
1270+
s := StateStore{
1271+
ttlAttributeName: "expiresAt",
1272+
ttlInSeconds: &defaultTTL,
1273+
}
1274+
1275+
req := &state.SetRequest{
1276+
Key: "test-key",
1277+
Metadata: map[string]string{},
1278+
}
1279+
1280+
ttl, err := s.parseTTL(req)
1281+
require.NoError(t, err)
1282+
require.NotNil(t, ttl)
1283+
1284+
expectedTime := time.Now().Unix() + 86400
1285+
assert.InDelta(t, expectedTime, *ttl, 2)
1286+
})
1287+
1288+
t.Run("Error on invalid TTL value", func(t *testing.T) {
1289+
defaultTTL := 600
1290+
s := StateStore{
1291+
ttlAttributeName: "expiresAt",
1292+
ttlInSeconds: &defaultTTL,
1293+
}
1294+
1295+
req := &state.SetRequest{
1296+
Key: "test-key",
1297+
Metadata: map[string]string{
1298+
"ttlInSeconds": "invalid",
1299+
},
1300+
}
1301+
1302+
ttl, err := s.parseTTL(req)
1303+
require.Error(t, err)
1304+
assert.Nil(t, ttl)
1305+
assert.Contains(t, err.Error(), "invalid syntax")
1306+
})
1307+
1308+
t.Run("Explicit TTL overrides default in request with empty metadata", func(t *testing.T) {
1309+
defaultTTL := 1200
1310+
s := StateStore{
1311+
ttlAttributeName: "expiresAt",
1312+
ttlInSeconds: &defaultTTL,
1313+
}
1314+
1315+
req := &state.SetRequest{
1316+
Key: "test-key",
1317+
Metadata: map[string]string{
1318+
"ttlInSeconds": "0",
1319+
},
1320+
}
1321+
1322+
ttl, err := s.parseTTL(req)
1323+
require.NoError(t, err)
1324+
require.NotNil(t, ttl)
1325+
1326+
// Should use explicit value 0, not default
1327+
expectedTime := time.Now().Unix()
1328+
assert.InDelta(t, expectedTime, *ttl, 2)
1329+
})
1330+
}

0 commit comments

Comments
 (0)