From ff4d63e06b2063680aaae5c0850c7f2f688e649b Mon Sep 17 00:00:00 2001 From: cybersmeashish Date: Mon, 18 Aug 2025 22:35:47 +0530 Subject: [PATCH] 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. --- commands_test.go | 71 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/commands_test.go b/commands_test.go index 9e130089..c110d582 100644 --- a/commands_test.go +++ b/commands_test.go @@ -2412,6 +2412,77 @@ var _ = Describe("Commands", func() { Expect(args).To(Equal(expectedArgs)) }) + + It("should IncrByFloat with edge cases", func() { + // Test with negative increment + set := client.Set(ctx, "key", "10.5", 0) + Expect(set.Err()).NotTo(HaveOccurred()) + + incrByFloat := client.IncrByFloat(ctx, "key", -2.3) + Expect(incrByFloat.Err()).NotTo(HaveOccurred()) + Expect(incrByFloat.Val()).To(BeNumerically("~", 8.2, 0.0001)) + + // Test with zero increment (should return current value) + incrByFloat = client.IncrByFloat(ctx, "key", 0.0) + Expect(incrByFloat.Err()).NotTo(HaveOccurred()) + Expect(incrByFloat.Val()).To(BeNumerically("~", 8.2, 0.0001)) + + // Test with very small increment (precision test) + incrByFloat = client.IncrByFloat(ctx, "key", 0.0001) + Expect(incrByFloat.Err()).NotTo(HaveOccurred()) + Expect(incrByFloat.Val()).To(BeNumerically("~", 8.2001, 0.00001)) + + // Test with non-existent key (should start from 0) + incrByFloat = client.IncrByFloat(ctx, "nonexistent", 5.5) + Expect(incrByFloat.Err()).NotTo(HaveOccurred()) + Expect(incrByFloat.Val()).To(Equal(5.5)) + + // Test with integer value stored as string + set = client.Set(ctx, "intkey", "42", 0) + Expect(set.Err()).NotTo(HaveOccurred()) + + incrByFloat = client.IncrByFloat(ctx, "intkey", 0.5) + Expect(incrByFloat.Err()).NotTo(HaveOccurred()) + Expect(incrByFloat.Val()).To(Equal(42.5)) + + // Test with scientific notation + set = client.Set(ctx, "scikey", "1.5e2", 0) + Expect(set.Err()).NotTo(HaveOccurred()) + + incrByFloat = client.IncrByFloat(ctx, "scikey", 5.0) + Expect(incrByFloat.Err()).NotTo(HaveOccurred()) + Expect(incrByFloat.Val()).To(Equal(155.0)) + + // Test with negative scientific notation + incrByFloat = client.IncrByFloat(ctx, "scikey", -1.5e1) + Expect(incrByFloat.Err()).NotTo(HaveOccurred()) + Expect(incrByFloat.Val()).To(Equal(140.0)) + + // Test error case: non-numeric value + set = client.Set(ctx, "stringkey", "notanumber", 0) + Expect(set.Err()).NotTo(HaveOccurred()) + + incrByFloat = client.IncrByFloat(ctx, "stringkey", 1.0) + Expect(incrByFloat.Err()).To(HaveOccurred()) + Expect(incrByFloat.Err().Error()).To(ContainSubstring("value is not a valid float")) + + // Test with very large numbers + set = client.Set(ctx, "largekey", "1.7976931348623157e+308", 0) + Expect(set.Err()).NotTo(HaveOccurred()) + + // This should work as it's within float64 range + incrByFloat = client.IncrByFloat(ctx, "largekey", -1.0e+308) + Expect(incrByFloat.Err()).NotTo(HaveOccurred()) + Expect(incrByFloat.Val()).To(BeNumerically("~", 7.976931348623157e+307, 1e+300)) + + // Test with very small numbers (near zero) + set = client.Set(ctx, "smallkey", "1e-10", 0) + Expect(set.Err()).NotTo(HaveOccurred()) + + incrByFloat = client.IncrByFloat(ctx, "smallkey", 1e-10) + Expect(incrByFloat.Err()).NotTo(HaveOccurred()) + Expect(incrByFloat.Val()).To(BeNumerically("~", 2e-10, 1e-15)) + }) }) Describe("hashes", func() {