From 6b3e2d900152aa6f2129fb0fdbec019c74958af5 Mon Sep 17 00:00:00 2001 From: Eric Date: Thu, 3 Oct 2024 11:16:49 -0500 Subject: [PATCH] modify GetPid() and GetRevenue() unit tests --- pkg/http_test.go | 87 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 83 insertions(+), 4 deletions(-) diff --git a/pkg/http_test.go b/pkg/http_test.go index 6f3c794..6ee6f82 100644 --- a/pkg/http_test.go +++ b/pkg/http_test.go @@ -53,13 +53,92 @@ func TestGetLanguage(t *testing.T) { } func TestGetPid(t *testing.T) { - url := url.URL{ - RawQuery: string(QueryParamPid) + "=" + "1234", - } - assert.Equal(t, "1234", GetPid(url)) + t.Run("Test missing or empty pid", func(t *testing.T) { + assert.Equal(t, "", GetPid(url.URL{})) + assert.Equal(t, "", GetPid(url.URL{ + RawQuery: string(QueryParamPid) + "=", + })) + }) + + t.Run("Test correct usage of pid", func(t *testing.T) { + assert.Equal(t, "1234", GetPid(url.URL{ + RawQuery: string(QueryParamPid) + "=" + "1234", + })) + assert.Equal(t, "1234-5678-90", GetPid(url.URL{ + RawQuery: string(QueryParamPid) + "=" + "1234-5678-90", + })) + assert.Equal(t, "1234_5678_90", GetPid(url.URL{ + RawQuery: string(QueryParamPid) + "=" + "1234_5678_90", + })) + assert.Equal(t, "1234-5678_90", GetPid(url.URL{ + RawQuery: string(QueryParamPid) + "=" + "1234-5678_90", + })) + }) } func TestGetRevenue(t *testing.T) { + t.Run("Test missing or empty payout", func(t *testing.T) { + assert.Equal(t, float64(0), GetRevenue( + url.URL{}, + )) + assert.Equal(t, float64(0), GetRevenue( + url.URL{ + RawQuery: "", + }, + )) + assert.Equal(t, float64(0), GetRevenue( + url.URL{ + RawQuery: "payout=", + }, + )) + }) + + t.Run("Test invalid payout", func(t *testing.T) { + assert.Equal(t, float64(0), GetRevenue( + url.URL{ + RawQuery: "payout=INVALID", + }, + )) + assert.Equal(t, float64(0), GetRevenue( + url.URL{ + RawQuery: "payout=NOT_A_NUMBER", + }, + )) + assert.Equal(t, float64(0), GetRevenue( + url.URL{ + RawQuery: "payout=-", + }, + )) + }) + + t.Run("Test negative payout", func(t *testing.T) { + assert.Equal(t, float64(0), GetRevenue( + url.URL{ + RawQuery: "payout=-0", + }, + )) + assert.Equal(t, float64(0), GetRevenue( + url.URL{ + RawQuery: "payout=-12", + }, + )) + assert.Equal(t, float64(0), GetRevenue( + url.URL{ + RawQuery: "payout=-12.5", + }, + )) + assert.Equal(t, float64(0), GetRevenue( + url.URL{ + RawQuery: "payout=-12.56789", + }, + )) + assert.Equal(t, float64(0), GetRevenue( + url.URL{ + RawQuery: "payout=-12.50000", + }, + )) + }) + t.Run("Test integer payout", func(t *testing.T) { assert.Equal(t, float64(12), GetRevenue( url.URL{