From c7dc9dd7df6a505597a337a3a4c92386b4e6c9e6 Mon Sep 17 00:00:00 2001 From: Anton Berezhnyi Date: Tue, 14 Nov 2023 16:49:05 +0200 Subject: [PATCH] Add json path optional --- TelegramHelpers.go | 19 ++++++++++--------- Test2EnsureAuthFlow.go | 7 ++++--- expectjson/jsonpathoptional.go | 25 +++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 12 deletions(-) create mode 100644 expectjson/jsonpathoptional.go diff --git a/TelegramHelpers.go b/TelegramHelpers.go index ab8df13..587c423 100644 --- a/TelegramHelpers.go +++ b/TelegramHelpers.go @@ -7,6 +7,7 @@ import ( "github.com/vitorsalgado/mocha/v3" "github.com/vitorsalgado/mocha/v3/expect" "github.com/vitorsalgado/mocha/v3/reply" + "integration-testing/expectjson" "mvdan.cc/xurls/v2" "net/url" "regexp" @@ -18,7 +19,7 @@ import ( var sendMessageLastId = 100 -var expectMarkdownV2 = expect.JSONPath("parse_mode", expect.ToEqual("MarkdownV2")) +var expectMarkdownV2 = expectjson.JSONPathOptional("parse_mode", expect.ToEqual("MarkdownV2")) func initTelegramHelpers(server *TelegramMockServer) { var err error @@ -141,14 +142,14 @@ func loginUser(t *testing.T, chatId int, fakeUser *FakeUser, sender *User) { } func expectChatId(chatId int) expect.Matcher { - return expect.JSONPath( + return expectjson.JSONPathOptional( "chat_id", expect.ToEqual(strconv.Itoa(chatId)), ) } func expectMessageId(messageId int) expect.Matcher { - return expect.JSONPath( + return expectjson.JSONPathOptional( "message_id", expect.ToEqual(strconv.Itoa(messageId)), ) @@ -169,9 +170,9 @@ func expectAuthorizationMessage(chatId int) *mocha.MockBuilder { Repeat(1). Body( expectMarkdownV2, expectChatId(chatId), - expect.JSONPath("text", expect.ToContain("авториз")), - expect.JSONPath("text", expect.ToContain(mocks.TelegramMockServer.authUrl)), - expect.JSONPath("text", expect.ToMatchExpr(mocks.TelegramMockServer.authUrlRegexp)), + expectjson.JSONPathOptional("text", expect.ToContain("авториз")), + expectjson.JSONPathOptional("text", expect.ToContain(mocks.TelegramMockServer.authUrl)), + expectjson.JSONPathOptional("text", expect.ToMatchExpr(mocks.TelegramMockServer.authUrlRegexp)), ). Reply(getSendMessageSuccessResponse()) } @@ -181,11 +182,11 @@ func expectWelcomeMessage(chatId int) *mocha.MockBuilder { Repeat(1). Body( expectMarkdownV2, expectChatId(chatId), - expect.JSONPath("text", expect.ToContain("будете отримувати сповіщення")), - expect.JSONPath( + expectjson.JSONPathOptional("text", expect.ToContain("будете отримувати сповіщення")), + expectjson.JSONPathOptional( "reply_markup", expectJsonPayload( - expect.JSONPath("keyboard.[0][0].text", expect.ToHavePrefix("/list")), + expectjson.JSONPathOptional("keyboard.[0][0].text", expect.ToHavePrefix("/list")), ), ), ). diff --git a/Test2EnsureAuthFlow.go b/Test2EnsureAuthFlow.go index 48e3150..039f20e 100644 --- a/Test2EnsureAuthFlow.go +++ b/Test2EnsureAuthFlow.go @@ -5,6 +5,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/vitorsalgado/mocha/v3" "github.com/vitorsalgado/mocha/v3/expect" + "integration-testing/expectjson" "strings" "testing" "time" @@ -37,7 +38,7 @@ func Test2EnsureAuthFlow(t *testing.T) { Repeat(1). Body( expectMarkdownV2, expectChatId(userId), - expect.JSONPath("text", expect.ToContain("Ваша загальна успішність у навчанні")), + expectjson.JSONPathOptional("text", expect.ToContain("Ваша загальна успішність у навчанні")), ). Reply(getSendMessageSuccessResponse()). PostAction(catchMessage) @@ -89,7 +90,7 @@ func Test2EnsureAuthFlow(t *testing.T) { Repeat(1). Body( expectMarkdownV2, expectChatId(userId), - expect.JSONPath("text", expect.ToHavePrefix("*Системи управління знаннями*")), + expectjson.JSONPathOptional("text", expect.ToHavePrefix("*Системи управління знаннями*")), ). Reply(getSendMessageSuccessResponse()). PostAction(catchMessage) @@ -133,7 +134,7 @@ func Test2EnsureAuthFlow(t *testing.T) { Repeat(1). Body( expectMarkdownV2, expectChatId(userId), - expect.JSONPath("text", expect.ToHavePrefix("Відтепер надсилання сповіщень зупинено")), + expectjson.JSONPathOptional("text", expect.ToHavePrefix("Відтепер надсилання сповіщень зупинено")), ). Reply(getSendMessageSuccessResponse()). PostAction(catchMessage), diff --git a/expectjson/jsonpathoptional.go b/expectjson/jsonpathoptional.go new file mode 100644 index 0000000..820b1f2 --- /dev/null +++ b/expectjson/jsonpathoptional.go @@ -0,0 +1,25 @@ +package expectjson + +import ( + "fmt" + "github.com/vitorsalgado/mocha/v3/expect" +) + +func JSONPathOptional(p string, matcher expect.Matcher) expect.Matcher { + jsonMatcher := expect.JSONPath(p, matcher) + + m := expect.Matcher{} + m.Name = "JSONPathOptional" + m.DescribeMismatch = jsonMatcher.DescribeMismatch + m.Matches = func(v any, args expect.Args) (bool, error) { + matched, err := jsonMatcher.Matches(v, args) + if err != nil && err.Error() == "could not find a field using provided json path" { + fmt.Printf("JSONPathOptional: %s not found, but it's optional. Value: %v", p, v) + return false, err + } + + return matched, err + } + + return m +}