Skip to content

Commit ff4d63e

Browse files
chore(test): Add comprehensive edge case tests for IncrByFloat command (#3477)
This commit adds extensive test coverage for the IncrByFloat Redis command, covering various edge cases and scenarios that were not previously tested. Test cases added: - Negative increment values - Zero increment (should return current value) - High precision floating point operations - Non-existent key behavior (should start from 0) - Integer values stored as strings - Scientific notation (both positive and negative) - Error handling for non-numeric values - Very large numbers (near float64 limits) - Very small numbers (near zero precision) These tests ensure robust behavior of the IncrByFloat command across different numeric formats and edge conditions, improving the overall reliability and test coverage of the go-redis library. The tests use Gomega's BeNumerically matcher for floating point comparisons to handle precision issues appropriately.
1 parent e07f55b commit ff4d63e

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

commands_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2412,6 +2412,77 @@ var _ = Describe("Commands", func() {
24122412

24132413
Expect(args).To(Equal(expectedArgs))
24142414
})
2415+
2416+
It("should IncrByFloat with edge cases", func() {
2417+
// Test with negative increment
2418+
set := client.Set(ctx, "key", "10.5", 0)
2419+
Expect(set.Err()).NotTo(HaveOccurred())
2420+
2421+
incrByFloat := client.IncrByFloat(ctx, "key", -2.3)
2422+
Expect(incrByFloat.Err()).NotTo(HaveOccurred())
2423+
Expect(incrByFloat.Val()).To(BeNumerically("~", 8.2, 0.0001))
2424+
2425+
// Test with zero increment (should return current value)
2426+
incrByFloat = client.IncrByFloat(ctx, "key", 0.0)
2427+
Expect(incrByFloat.Err()).NotTo(HaveOccurred())
2428+
Expect(incrByFloat.Val()).To(BeNumerically("~", 8.2, 0.0001))
2429+
2430+
// Test with very small increment (precision test)
2431+
incrByFloat = client.IncrByFloat(ctx, "key", 0.0001)
2432+
Expect(incrByFloat.Err()).NotTo(HaveOccurred())
2433+
Expect(incrByFloat.Val()).To(BeNumerically("~", 8.2001, 0.00001))
2434+
2435+
// Test with non-existent key (should start from 0)
2436+
incrByFloat = client.IncrByFloat(ctx, "nonexistent", 5.5)
2437+
Expect(incrByFloat.Err()).NotTo(HaveOccurred())
2438+
Expect(incrByFloat.Val()).To(Equal(5.5))
2439+
2440+
// Test with integer value stored as string
2441+
set = client.Set(ctx, "intkey", "42", 0)
2442+
Expect(set.Err()).NotTo(HaveOccurred())
2443+
2444+
incrByFloat = client.IncrByFloat(ctx, "intkey", 0.5)
2445+
Expect(incrByFloat.Err()).NotTo(HaveOccurred())
2446+
Expect(incrByFloat.Val()).To(Equal(42.5))
2447+
2448+
// Test with scientific notation
2449+
set = client.Set(ctx, "scikey", "1.5e2", 0)
2450+
Expect(set.Err()).NotTo(HaveOccurred())
2451+
2452+
incrByFloat = client.IncrByFloat(ctx, "scikey", 5.0)
2453+
Expect(incrByFloat.Err()).NotTo(HaveOccurred())
2454+
Expect(incrByFloat.Val()).To(Equal(155.0))
2455+
2456+
// Test with negative scientific notation
2457+
incrByFloat = client.IncrByFloat(ctx, "scikey", -1.5e1)
2458+
Expect(incrByFloat.Err()).NotTo(HaveOccurred())
2459+
Expect(incrByFloat.Val()).To(Equal(140.0))
2460+
2461+
// Test error case: non-numeric value
2462+
set = client.Set(ctx, "stringkey", "notanumber", 0)
2463+
Expect(set.Err()).NotTo(HaveOccurred())
2464+
2465+
incrByFloat = client.IncrByFloat(ctx, "stringkey", 1.0)
2466+
Expect(incrByFloat.Err()).To(HaveOccurred())
2467+
Expect(incrByFloat.Err().Error()).To(ContainSubstring("value is not a valid float"))
2468+
2469+
// Test with very large numbers
2470+
set = client.Set(ctx, "largekey", "1.7976931348623157e+308", 0)
2471+
Expect(set.Err()).NotTo(HaveOccurred())
2472+
2473+
// This should work as it's within float64 range
2474+
incrByFloat = client.IncrByFloat(ctx, "largekey", -1.0e+308)
2475+
Expect(incrByFloat.Err()).NotTo(HaveOccurred())
2476+
Expect(incrByFloat.Val()).To(BeNumerically("~", 7.976931348623157e+307, 1e+300))
2477+
2478+
// Test with very small numbers (near zero)
2479+
set = client.Set(ctx, "smallkey", "1e-10", 0)
2480+
Expect(set.Err()).NotTo(HaveOccurred())
2481+
2482+
incrByFloat = client.IncrByFloat(ctx, "smallkey", 1e-10)
2483+
Expect(incrByFloat.Err()).NotTo(HaveOccurred())
2484+
Expect(incrByFloat.Val()).To(BeNumerically("~", 2e-10, 1e-15))
2485+
})
24152486
})
24162487

24172488
Describe("hashes", func() {

0 commit comments

Comments
 (0)