Skip to content

Commit

Permalink
Add json path optional
Browse files Browse the repository at this point in the history
  • Loading branch information
berejant committed Nov 14, 2023
1 parent 6d1e0d0 commit c7dc9dd
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 12 deletions.
19 changes: 10 additions & 9 deletions TelegramHelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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)),
)
Expand All @@ -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())
}
Expand All @@ -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")),
),
),
).
Expand Down
7 changes: 4 additions & 3 deletions Test2EnsureAuthFlow.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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),
Expand Down
25 changes: 25 additions & 0 deletions expectjson/jsonpathoptional.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit c7dc9dd

Please sign in to comment.