From e986d3f871c1fd9317afcc090ccf2035a63aa220 Mon Sep 17 00:00:00 2001 From: edercarloscosta Date: Tue, 27 Feb 2024 00:07:13 +0000 Subject: [PATCH 1/2] increasing the code coverage --- auth_test.go | 31 ++++++++++++++++++++++ types_test.go | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 auth_test.go create mode 100644 types_test.go diff --git a/auth_test.go b/auth_test.go new file mode 100644 index 0000000..e7a0400 --- /dev/null +++ b/auth_test.go @@ -0,0 +1,31 @@ +package amqp091 + +import "testing" + +func TestPlainAuth(t *testing.T) { + auth := &PlainAuth{ + Username: "user", + Password: "pass", + } + + if auth.Mechanism() != "PLAIN" { + t.Errorf("Expected PLAIN, got %s", auth.Mechanism()) + } + + expectedResponse := "\000user\000pass" + if auth.Response() != expectedResponse { + t.Errorf("Expected %s, got %s", expectedResponse, auth.Response()) + } +} + +func TestExternalAuth(t *testing.T) { + auth := &ExternalAuth{} + + if auth.Mechanism() != "EXTERNAL" { + t.Errorf("Expected EXTERNAL, got %s", auth.Mechanism()) + } + + if auth.Response() != "\000*\000*" { + t.Errorf("Expected \000*\000*, got %s", auth.Response()) + } +} \ No newline at end of file diff --git a/types_test.go b/types_test.go new file mode 100644 index 0000000..0138e4b --- /dev/null +++ b/types_test.go @@ -0,0 +1,71 @@ +package amqp091 + +import ( + "testing" + "time" +) + +func TestNewError(t *testing.T) { + testCases := []struct { + code uint16 + text string + expectedServer bool + }{ + // Just three basics samples + {404, "Not Found", true}, + {500, "Internal Server Error", true}, + {403, "Forbidden", true}, + } + + for _, tc := range testCases { + err := newError(tc.code, tc.text) + if err.Code != int(tc.code) { + t.Errorf("expected Code %d, got %d", tc.code, err.Code) + } + if err.Reason != tc.text { + t.Errorf("expected Reason %s, got %s", tc.text, err.Reason) + } + if err.Server != tc.expectedServer { + t.Errorf("expected Server to be %v", tc.expectedServer) + } + } +} + +func TestValidateField(t *testing.T) { + // Test case for simple types + simpleTypes := []interface{}{ + nil, true, byte(1), int8(1), 10, int16(10), int32(10), int64(10), + float32(1.0), float64(1.0), "string", []byte("byte slice"), + Decimal{Scale: 2, Value: 12345}, time.Now(), + } + for _, v := range simpleTypes { + if err := validateField(v); err != nil { + t.Errorf("validateField failed for simple type %T: %s", v, err) + } + } + + // Test case for []interface{} + sliceTypes := []interface{}{ + "string", 10, float64(1.0), Decimal{Scale: 2, Value: 12345}, + } + if err := validateField(sliceTypes); err != nil { + t.Errorf("validateField failed for []interface{}: %s", err) + } + + // Test case for Table + tableType := Table{ + "key1": "value1", + "key2": 10, + "key3": []interface{}{"nested string", 20}, + } + if err := validateField(tableType); err != nil { + t.Errorf("validateField failed for Table: %s", err) + } + + // Test case for unsupported type + unsupportedType := struct{}{} + if err := validateField(unsupportedType); err == nil { + t.Error("validateField should fail for unsupported type but it didn't") + } +} + From 715a1d104ddcb0adb302ef6a8fd5718a79fdb2ed Mon Sep 17 00:00:00 2001 From: edercarloscosta Date: Tue, 27 Feb 2024 09:54:14 +0000 Subject: [PATCH 2/2] gofmt fixes --- auth_test.go | 38 +++++++++++++++++++------------------- types_test.go | 43 +++++++++++++++++++++---------------------- 2 files changed, 40 insertions(+), 41 deletions(-) diff --git a/auth_test.go b/auth_test.go index e7a0400..b489f87 100644 --- a/auth_test.go +++ b/auth_test.go @@ -3,29 +3,29 @@ package amqp091 import "testing" func TestPlainAuth(t *testing.T) { - auth := &PlainAuth{ - Username: "user", - Password: "pass", - } + auth := &PlainAuth{ + Username: "user", + Password: "pass", + } - if auth.Mechanism() != "PLAIN" { - t.Errorf("Expected PLAIN, got %s", auth.Mechanism()) - } + if auth.Mechanism() != "PLAIN" { + t.Errorf("Expected PLAIN, got %s", auth.Mechanism()) + } - expectedResponse := "\000user\000pass" - if auth.Response() != expectedResponse { - t.Errorf("Expected %s, got %s", expectedResponse, auth.Response()) - } + expectedResponse := "\000user\000pass" + if auth.Response() != expectedResponse { + t.Errorf("Expected %s, got %s", expectedResponse, auth.Response()) + } } func TestExternalAuth(t *testing.T) { - auth := &ExternalAuth{} + auth := &ExternalAuth{} - if auth.Mechanism() != "EXTERNAL" { - t.Errorf("Expected EXTERNAL, got %s", auth.Mechanism()) - } + if auth.Mechanism() != "EXTERNAL" { + t.Errorf("Expected EXTERNAL, got %s", auth.Mechanism()) + } - if auth.Response() != "\000*\000*" { - t.Errorf("Expected \000*\000*, got %s", auth.Response()) - } -} \ No newline at end of file + if auth.Response() != "\000*\000*" { + t.Errorf("Expected \000*\000*, got %s", auth.Response()) + } +} diff --git a/types_test.go b/types_test.go index 0138e4b..9bba811 100644 --- a/types_test.go +++ b/types_test.go @@ -6,29 +6,29 @@ import ( ) func TestNewError(t *testing.T) { - testCases := []struct { - code uint16 - text string - expectedServer bool - }{ + testCases := []struct { + code uint16 + text string + expectedServer bool + }{ // Just three basics samples - {404, "Not Found", true}, - {500, "Internal Server Error", true}, - {403, "Forbidden", true}, - } + {404, "Not Found", true}, + {500, "Internal Server Error", true}, + {403, "Forbidden", true}, + } - for _, tc := range testCases { - err := newError(tc.code, tc.text) - if err.Code != int(tc.code) { - t.Errorf("expected Code %d, got %d", tc.code, err.Code) - } - if err.Reason != tc.text { - t.Errorf("expected Reason %s, got %s", tc.text, err.Reason) - } - if err.Server != tc.expectedServer { - t.Errorf("expected Server to be %v", tc.expectedServer) - } - } + for _, tc := range testCases { + err := newError(tc.code, tc.text) + if err.Code != int(tc.code) { + t.Errorf("expected Code %d, got %d", tc.code, err.Code) + } + if err.Reason != tc.text { + t.Errorf("expected Reason %s, got %s", tc.text, err.Reason) + } + if err.Server != tc.expectedServer { + t.Errorf("expected Server to be %v", tc.expectedServer) + } + } } func TestValidateField(t *testing.T) { @@ -68,4 +68,3 @@ func TestValidateField(t *testing.T) { t.Error("validateField should fail for unsupported type but it didn't") } } -