From 1faed6d0791e1e0d945ab5e17fdfb314dbadfbda Mon Sep 17 00:00:00 2001 From: cyuankuo Date: Mon, 27 May 2024 22:16:04 +0930 Subject: [PATCH 1/8] minor tests for command.go under rueidis.compat --- rueidiscompat/command_test.go | 118 ++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) diff --git a/rueidiscompat/command_test.go b/rueidiscompat/command_test.go index 07220def..fc98852c 100644 --- a/rueidiscompat/command_test.go +++ b/rueidiscompat/command_test.go @@ -28,6 +28,8 @@ package rueidiscompat import ( "errors" + "fmt" + "testing" "time" . "github.com/onsi/ginkgo/v2" @@ -672,3 +674,119 @@ func testCmd(resp3 bool) { Expect(tm2).To(BeTemporally("==", tm)) }) } +func TestGeoSearchQueryArgs(t *testing.T) { + tests := []struct { + name string + query GeoSearchQuery + expected []string + }{ + { + name: "Radius with default unit", + query: GeoSearchQuery{ + Radius: 5.0, + }, + expected: []string{"FROMLONLAT", "0", "0", "BYRADIUS", "5", "KM"}, + }, + { + name: "Radius with specified unit", + query: GeoSearchQuery{ + Radius: 5.0, + RadiusUnit: "M", + }, + expected: []string{"FROMLONLAT", "0", "0", "BYRADIUS", "5", "M"}, + }, + { + name: "Box with default unit", + query: GeoSearchQuery{ + BoxWidth: 10.0, + BoxHeight: 20.0, + }, + expected: []string{"FROMLONLAT", "0", "0", "BYBOX", "10", "20", "KM"}, + }, + { + name: "Box with specified unit", + query: GeoSearchQuery{ + BoxWidth: 10.0, + BoxHeight: 20.0, + BoxUnit: "M", + }, + expected: []string{"FROMLONLAT", "0", "0", "BYBOX", "10", "20", "M"}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + result := tt.query.args() + if len(result) != len(tt.expected) { + t.Errorf("Expected %v args, got %v", len(tt.expected), len(result)) + } + for i, v := range result { + if v != tt.expected[i] { + t.Errorf("Expected %v at position %d, got %v", tt.expected[i], i, v) + } + } + }) + } +} + +type MockRedisResult struct { + arr []MockRedisResult + str string + i64 int64 + hasErr bool +} + +func (m MockRedisResult) ToArray() ([]MockRedisResult, error) { + if m.hasErr { + return nil, fmt.Errorf("mock error") + } + return m.arr, nil +} + +func (m MockRedisResult) ToString() (string, error) { + if m.hasErr { + return "", fmt.Errorf("mock error") + } + return m.str, nil +} + +func (m MockRedisResult) AsInt64() (int64, error) { + if m.hasErr { + return 0, fmt.Errorf("mock error") + } + return m.i64, nil +} + +func TestSetErr(t *testing.T) { + // Create a new XAutoClaimCmd instance + cmd := &XAutoClaimCmd{} + + // Set an error using the SetErr method + errMsg := "test error" + cmd.SetErr(fmt.Errorf(errMsg)) + + // Check if the error is properly set in the command object + if cmd.Err() == nil { + t.Error("expected non-nil error, got nil") + } + + // Check if the error message matches the expected error message + if cmd.Err().Error() != errMsg { + t.Errorf("expected error message: %s, got: %s", errMsg, cmd.Err().Error()) + } +} +func TestStringInvalid(t *testing.T) { + agg := Invalid + expected := "" + if result := agg.String(); result != expected { + t.Errorf("Invalid: expected %s, got %s", expected, result) + } +} + +func TestStringAvg(t *testing.T) { + agg := Avg + expected := "AVG" + if result := agg.String(); result != expected { + t.Errorf("Avg: expected %s, got %s", expected, result) + } +} From 1c5c9f10710e78a7e915292a0263152b08f27375 Mon Sep 17 00:00:00 2001 From: cyuankuo Date: Mon, 27 May 2024 22:25:47 +0930 Subject: [PATCH 2/8] minor test cases for command.go, removed redundant code --- rueidiscompat/command_test.go | 28 ---------------------------- 1 file changed, 28 deletions(-) diff --git a/rueidiscompat/command_test.go b/rueidiscompat/command_test.go index fc98852c..e6ebe2c5 100644 --- a/rueidiscompat/command_test.go +++ b/rueidiscompat/command_test.go @@ -729,34 +729,6 @@ func TestGeoSearchQueryArgs(t *testing.T) { } } -type MockRedisResult struct { - arr []MockRedisResult - str string - i64 int64 - hasErr bool -} - -func (m MockRedisResult) ToArray() ([]MockRedisResult, error) { - if m.hasErr { - return nil, fmt.Errorf("mock error") - } - return m.arr, nil -} - -func (m MockRedisResult) ToString() (string, error) { - if m.hasErr { - return "", fmt.Errorf("mock error") - } - return m.str, nil -} - -func (m MockRedisResult) AsInt64() (int64, error) { - if m.hasErr { - return 0, fmt.Errorf("mock error") - } - return m.i64, nil -} - func TestSetErr(t *testing.T) { // Create a new XAutoClaimCmd instance cmd := &XAutoClaimCmd{} From d54a8c186dd06c7accd615370afb91809bb37c6d Mon Sep 17 00:00:00 2001 From: cyuankuo Date: Mon, 27 May 2024 23:50:15 +0930 Subject: [PATCH 3/8] Minor test for command.go under rueidis compat to increase coverage --- rueidiscompat/command.go | 12 ++- rueidiscompat/command_test.go | 152 ++++++++++++++++++++++++++++++++++ 2 files changed, 162 insertions(+), 2 deletions(-) diff --git a/rueidiscompat/command.go b/rueidiscompat/command.go index 460f0526..dd530b01 100644 --- a/rueidiscompat/command.go +++ b/rueidiscompat/command.go @@ -2958,9 +2958,17 @@ func (cmd *JSONCmd) Val() string { } // https://github.com/redis/go-redis/blob/v9.3.0/json.go#L105 -func (cmd *JSONCmd) Expanded() (any, error) { +// +// func (cmd *JSONCmd) Expanded() (any, error) { +// if cmd.typ == TYP_STRING { +// return cmd.Val(), nil +// } +// // TYP_ARRAY +// return cmd.expanded, nil +// } +func (cmd *JSONCmd) Expanded() ([]interface{}, error) { if cmd.typ == TYP_STRING { - return cmd.Val(), nil + return []interface{}{cmd.val}, nil } // TYP_ARRAY return cmd.expanded, nil diff --git a/rueidiscompat/command_test.go b/rueidiscompat/command_test.go index e6ebe2c5..9303e758 100644 --- a/rueidiscompat/command_test.go +++ b/rueidiscompat/command_test.go @@ -762,3 +762,155 @@ func TestStringAvg(t *testing.T) { t.Errorf("Avg: expected %s, got %s", expected, result) } } + +func TestStringSum(t *testing.T) { + agg := Sum + expected := "SUM" + if result := agg.String(); result != expected { + t.Errorf("Sum: expected %s, got %s", expected, result) + } +} + +func TestStringMin(t *testing.T) { + agg := Min + expected := "MIN" + if result := agg.String(); result != expected { + t.Errorf("Min: expected %s, got %s", expected, result) + } +} + +func TestStringMax(t *testing.T) { + agg := Max + expected := "MAX" + if result := agg.String(); result != expected { + t.Errorf("MAX: expected %s, got %s", expected, result) + } +} + +func TestStringRange(t *testing.T) { + agg := Range + expected := "RANGE" + if result := agg.String(); result != expected { + t.Errorf("Range: expected %s, got %s", expected, result) + } +} + +func TestStringCount(t *testing.T) { + agg := Count + expected := "COUNT" + if result := agg.String(); result != expected { + t.Errorf("Count: expected %s, got %s", expected, result) + } +} + +func TestStringFirst(t *testing.T) { + agg := First + expected := "FIRST" + if result := agg.String(); result != expected { + t.Errorf("First: expected %s, got %s", expected, result) + } +} + +func TestStringLast(t *testing.T) { + agg := Last + expected := "LAST" + if result := agg.String(); result != expected { + t.Errorf("Last: expected %s, got %s", expected, result) + } +} + +func TestStringStdP(t *testing.T) { + agg := StdP + expected := "STD.P" + if result := agg.String(); result != expected { + t.Errorf("StdP: expected %s, got %s", expected, result) + } +} + +func TestStringStdS(t *testing.T) { + agg := StdS + expected := "STD.S" + if result := agg.String(); result != expected { + t.Errorf("StdS: expected %s, got %s", expected, result) + } +} + +func TestStringVarP(t *testing.T) { + agg := VarP + expected := "VAR.P" + if result := agg.String(); result != expected { + t.Errorf("VarP: expected %s, got %s", expected, result) + } +} + +func TestStringVarS(t *testing.T) { + agg := VarS + expected := "VAR.S" + if result := agg.String(); result != expected { + t.Errorf("VarS: expected %s, got %s", expected, result) + } +} + +func TestStringTwa(t *testing.T) { + agg := Aggregator(100) + expected := "" + if result := agg.String(); result != expected { + t.Errorf("Empty string: expected %s, got %s", expected, result) + } +} + +func TestStringDefault(t *testing.T) { + agg := Twa + expected := "TWA" + if result := agg.String(); result != expected { + t.Errorf("Twa: expected %s, got %s", expected, result) + } +} + +func TestExpandedString(t *testing.T) { + // Create a JSONCmd instance with type TYP_STRING + cmd := &JSONCmd{typ: TYP_STRING} + + // Call the Expanded method + result, err := cmd.Expanded() + + // Verify the result + if err != nil { + t.Errorf("Expected no error, got: %v", err) + } + expected := []interface{}{"test"} + if !isEqual(result, expected) { + t.Errorf("Expected %v, got: %v", expected, result) + } +} + +func TestExpandedArray(t *testing.T) { + // Create a JSONCmd instance with type TYP_ARRAY + expanded := []interface{}{1, "two", true} + cmd := &JSONCmd{typ: TYP_ARRAY, expanded: expanded} + + // Call the Expanded method + result, err := cmd.Expanded() + + // Verify the result + if err != nil { + t.Errorf("Expected no error, got: %v", err) + } + expected := expanded + if !isEqual(result, expected) { + t.Errorf("Expected %v, got: %v", expected, result) + } +} + +// Helper function to compare two slices of interfaces +func isEqual(slice1, slice2 []interface{}) bool { + if len(slice1) != len(slice2) { + return false + } + for i := range slice1 { + if slice1[i] != slice2[i] { + return false + } + } + return true +} From f9b9a245033f6e69aced5fd0952f5b7fd815a670 Mon Sep 17 00:00:00 2001 From: cyuankuo Date: Tue, 28 May 2024 00:17:52 +0930 Subject: [PATCH 4/8] minor test for command.go under rueidiscompat --- rueidiscompat/command.go | 13 ++---- rueidiscompat/command_test.go | 84 +++++++++++++++++++++-------------- 2 files changed, 53 insertions(+), 44 deletions(-) diff --git a/rueidiscompat/command.go b/rueidiscompat/command.go index dd530b01..4f11c8f1 100644 --- a/rueidiscompat/command.go +++ b/rueidiscompat/command.go @@ -2958,17 +2958,10 @@ func (cmd *JSONCmd) Val() string { } // https://github.com/redis/go-redis/blob/v9.3.0/json.go#L105 -// -// func (cmd *JSONCmd) Expanded() (any, error) { -// if cmd.typ == TYP_STRING { -// return cmd.Val(), nil -// } -// // TYP_ARRAY -// return cmd.expanded, nil -// } -func (cmd *JSONCmd) Expanded() ([]interface{}, error) { + +func (cmd *JSONCmd) Expanded() (any, error) { if cmd.typ == TYP_STRING { - return []interface{}{cmd.val}, nil + return cmd.Val(), nil } // TYP_ARRAY return cmd.expanded, nil diff --git a/rueidiscompat/command_test.go b/rueidiscompat/command_test.go index 9303e758..a5c073a7 100644 --- a/rueidiscompat/command_test.go +++ b/rueidiscompat/command_test.go @@ -867,40 +867,40 @@ func TestStringDefault(t *testing.T) { } } -func TestExpandedString(t *testing.T) { - // Create a JSONCmd instance with type TYP_STRING - cmd := &JSONCmd{typ: TYP_STRING} - - // Call the Expanded method - result, err := cmd.Expanded() - - // Verify the result - if err != nil { - t.Errorf("Expected no error, got: %v", err) - } - expected := []interface{}{"test"} - if !isEqual(result, expected) { - t.Errorf("Expected %v, got: %v", expected, result) - } -} - -func TestExpandedArray(t *testing.T) { - // Create a JSONCmd instance with type TYP_ARRAY - expanded := []interface{}{1, "two", true} - cmd := &JSONCmd{typ: TYP_ARRAY, expanded: expanded} - - // Call the Expanded method - result, err := cmd.Expanded() - - // Verify the result - if err != nil { - t.Errorf("Expected no error, got: %v", err) - } - expected := expanded - if !isEqual(result, expected) { - t.Errorf("Expected %v, got: %v", expected, result) - } -} +//func TestExpandedString(t *testing.T) { +// // Create a JSONCmd instance with type TYP_STRING +// cmd := &JSONCmd{typ: TYP_STRING} +// +// // Call the Expanded method +// result, err := cmd.Expanded() +// +// // Verify the result +// if err != nil { +// t.Errorf("Expected no error, got: %v", err) +// } +// expected := []interface{}{} // An empty slice since no value is set +// if !isEqual(result, expected) { +// t.Errorf("Expected %v, got: %v", expected, result) +// } +//} + +//func TestExpandedArray(t *testing.T) { +// // Create a JSONCmd instance with type TYP_ARRAY +// expanded := []interface{}{1, "two", true} +// cmd := &JSONCmd{typ: TYP_ARRAY, expanded: expanded} +// +// // Call the Expanded method +// result, err := cmd.Expanded() +// +// // Verify the result +// if err != nil { +// t.Errorf("Expected no error, got: %v", err) +// } +// expected := expanded +// if !isEqual(result, expected) { +// t.Errorf("Expected %v, got: %v", expected, result) +// } +//} // Helper function to compare two slices of interfaces func isEqual(slice1, slice2 []interface{}) bool { @@ -914,3 +914,19 @@ func isEqual(slice1, slice2 []interface{}) bool { } return true } + +func TestFormatMs(t *testing.T) { + // Test case 1: Duration greater than 0 and less than 1 millisecond + dur1 := time.Microsecond / 2 // Half a microsecond + expected1 := int64(1) + if result1 := formatMs(dur1); result1 != expected1 { + t.Errorf("Test case 1 failed: Expected %d, got %d", expected1, result1) + } + + // Test case 2: Duration equal to 1 millisecond + dur2 := time.Millisecond + expected2 := int64(1) + if result2 := formatMs(dur2); result2 != expected2 { + t.Errorf("Test case 2 failed: Expected %d, got %d", expected2, result2) + } +} From ccfb882ad28e4219cc65847f5ed6b8008fd1f1b0 Mon Sep 17 00:00:00 2001 From: cyuankuo Date: Tue, 28 May 2024 00:24:01 +0930 Subject: [PATCH 5/8] Remove redundant testing function --- rueidiscompat/command_test.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/rueidiscompat/command_test.go b/rueidiscompat/command_test.go index a5c073a7..79067b03 100644 --- a/rueidiscompat/command_test.go +++ b/rueidiscompat/command_test.go @@ -902,18 +902,18 @@ func TestStringDefault(t *testing.T) { // } //} -// Helper function to compare two slices of interfaces -func isEqual(slice1, slice2 []interface{}) bool { - if len(slice1) != len(slice2) { - return false - } - for i := range slice1 { - if slice1[i] != slice2[i] { - return false - } - } - return true -} +//// Helper function to compare two slices of interfaces +//func isEqual(slice1, slice2 []interface{}) bool { +// if len(slice1) != len(slice2) { +// return false +// } +// for i := range slice1 { +// if slice1[i] != slice2[i] { +// return false +// } +// } +// return true +//} func TestFormatMs(t *testing.T) { // Test case 1: Duration greater than 0 and less than 1 millisecond From 91e22b5710d15de9c39f8933a7da76f58e015bb2 Mon Sep 17 00:00:00 2001 From: cyuankuo Date: Tue, 28 May 2024 22:35:27 +0930 Subject: [PATCH 6/8] Fix conflict, add String() test case --- rueidiscompat/command_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/rueidiscompat/command_test.go b/rueidiscompat/command_test.go index 79067b03..c7308fd3 100644 --- a/rueidiscompat/command_test.go +++ b/rueidiscompat/command_test.go @@ -866,6 +866,7 @@ func TestStringDefault(t *testing.T) { t.Errorf("Twa: expected %s, got %s", expected, result) } } +<<<<<<< HEAD //func TestExpandedString(t *testing.T) { // // Create a JSONCmd instance with type TYP_STRING @@ -930,3 +931,5 @@ func TestFormatMs(t *testing.T) { t.Errorf("Test case 2 failed: Expected %d, got %d", expected2, result2) } } +======= +>>>>>>> f78ad19 (Fix conflict, add String() test case) From 1a80b1a935b86b7b9d68b2b413b55ab319d4a78f Mon Sep 17 00:00:00 2001 From: cyuankuo Date: Tue, 28 May 2024 22:55:25 +0930 Subject: [PATCH 7/8] Fix conflict --- rueidiscompat/command.go | 1 - rueidiscompat/command_test.go | 3 --- 2 files changed, 4 deletions(-) diff --git a/rueidiscompat/command.go b/rueidiscompat/command.go index 4f11c8f1..460f0526 100644 --- a/rueidiscompat/command.go +++ b/rueidiscompat/command.go @@ -2958,7 +2958,6 @@ func (cmd *JSONCmd) Val() string { } // https://github.com/redis/go-redis/blob/v9.3.0/json.go#L105 - func (cmd *JSONCmd) Expanded() (any, error) { if cmd.typ == TYP_STRING { return cmd.Val(), nil diff --git a/rueidiscompat/command_test.go b/rueidiscompat/command_test.go index c7308fd3..79067b03 100644 --- a/rueidiscompat/command_test.go +++ b/rueidiscompat/command_test.go @@ -866,7 +866,6 @@ func TestStringDefault(t *testing.T) { t.Errorf("Twa: expected %s, got %s", expected, result) } } -<<<<<<< HEAD //func TestExpandedString(t *testing.T) { // // Create a JSONCmd instance with type TYP_STRING @@ -931,5 +930,3 @@ func TestFormatMs(t *testing.T) { t.Errorf("Test case 2 failed: Expected %d, got %d", expected2, result2) } } -======= ->>>>>>> f78ad19 (Fix conflict, add String() test case) From 85d88083a166c7240323e1ca726c9266d81e6861 Mon Sep 17 00:00:00 2001 From: cyuankuo Date: Tue, 28 May 2024 23:14:59 +0930 Subject: [PATCH 8/8] remove comment --- rueidiscompat/command_test.go | 48 ----------------------------------- 1 file changed, 48 deletions(-) diff --git a/rueidiscompat/command_test.go b/rueidiscompat/command_test.go index 79067b03..7d914302 100644 --- a/rueidiscompat/command_test.go +++ b/rueidiscompat/command_test.go @@ -867,54 +867,6 @@ func TestStringDefault(t *testing.T) { } } -//func TestExpandedString(t *testing.T) { -// // Create a JSONCmd instance with type TYP_STRING -// cmd := &JSONCmd{typ: TYP_STRING} -// -// // Call the Expanded method -// result, err := cmd.Expanded() -// -// // Verify the result -// if err != nil { -// t.Errorf("Expected no error, got: %v", err) -// } -// expected := []interface{}{} // An empty slice since no value is set -// if !isEqual(result, expected) { -// t.Errorf("Expected %v, got: %v", expected, result) -// } -//} - -//func TestExpandedArray(t *testing.T) { -// // Create a JSONCmd instance with type TYP_ARRAY -// expanded := []interface{}{1, "two", true} -// cmd := &JSONCmd{typ: TYP_ARRAY, expanded: expanded} -// -// // Call the Expanded method -// result, err := cmd.Expanded() -// -// // Verify the result -// if err != nil { -// t.Errorf("Expected no error, got: %v", err) -// } -// expected := expanded -// if !isEqual(result, expected) { -// t.Errorf("Expected %v, got: %v", expected, result) -// } -//} - -//// Helper function to compare two slices of interfaces -//func isEqual(slice1, slice2 []interface{}) bool { -// if len(slice1) != len(slice2) { -// return false -// } -// for i := range slice1 { -// if slice1[i] != slice2[i] { -// return false -// } -// } -// return true -//} - func TestFormatMs(t *testing.T) { // Test case 1: Duration greater than 0 and less than 1 millisecond dur1 := time.Microsecond / 2 // Half a microsecond