Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(internal/utils): refine Get, Create, Update, Delete request regex #1422

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions rules/internal/utils/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ import (
)

var (
getReqMessageRegexp = regexp.MustCompile("^Get[A-Za-z0-9]*Request$")
getReqMessageRegexp = regexp.MustCompile("^Get[A-Z]+[A-Za-z0-9]*Request$")
listReqMessageRegexp = regexp.MustCompile("^List[A-Z]+[A-Za-z0-9]*Request$")
listRespMessageRegexp = regexp.MustCompile("^List([A-Z]+[A-Za-z0-9]*)Response$")
listRevisionsReqMessageRegexp = regexp.MustCompile(`^List(?:[A-Z]+[A-Za-z0-9]+)RevisionsRequest$`)
listRevisionsRespMessageRegexp = regexp.MustCompile(`^List(?:[A-Z]+[A-Za-z0-9]+)RevisionsResponse$`)
createReqMessageRegexp = regexp.MustCompile("^Create[A-Za-z0-9]*Request$")
updateReqMessageRegexp = regexp.MustCompile("^Update[A-Za-z0-9]*Request$")
deleteReqMessageRegexp = regexp.MustCompile("^Delete[A-Za-z0-9]*Request$")
createReqMessageRegexp = regexp.MustCompile("^Create[A-Z]+[A-Za-z0-9]*Request$")
updateReqMessageRegexp = regexp.MustCompile("^Update[A-Z]+[A-Za-z0-9]*Request$")
deleteReqMessageRegexp = regexp.MustCompile("^Delete[A-Z]+[A-Za-z0-9]*Request$")
)

// Returns true if this is an AIP-131 Get request message, false otherwise.
Expand Down
156 changes: 156 additions & 0 deletions rules/internal/utils/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,159 @@ func TestIsListRevisionsRequestMessage(t *testing.T) {
})
}
}

func TestIsGetRequestMessage(t *testing.T) {
for _, test := range []struct {
name string
RPC string
want bool
}{
{
name: "valid get request",
RPC: "GetBook",
want: true,
},
{
name: "not get request",
RPC: "ArchiveBook",
want: false,
},
{
name: "lookalike request",
RPC: "Getup",
want: false,
},
{
name: "multiword lookalike request",
RPC: "GetupFinder",
want: false,
},
} {
t.Run(test.name, func(t *testing.T) {
file := testutils.ParseProto3Tmpl(t, `
message {{.RPC}}Request {}
`, test)
m := file.GetMessageTypes()[0]
if got, want := IsGetRequestMessage(m), test.want; got != want {
t.Errorf("got %v, want %v", got, want)
}
})
}
}

func TestIsCreateRequestMessage(t *testing.T) {
for _, test := range []struct {
name string
RPC string
want bool
}{
{
name: "valid create request",
RPC: "CreateBook",
want: true,
},
{
name: "not create request",
RPC: "ArchiveBook",
want: false,
},
{
name: "lookalike request",
RPC: "Created",
want: false,
},
{
name: "multiword lookalike request",
RPC: "CreatedBook",
want: false,
},
} {
t.Run(test.name, func(t *testing.T) {
file := testutils.ParseProto3Tmpl(t, `
message {{.RPC}}Request {}
`, test)
m := file.GetMessageTypes()[0]
if got, want := IsCreateRequestMessage(m), test.want; got != want {
t.Errorf("got %v, want %v", got, want)
}
})
}
}

func TestIsUpdateRequestMessage(t *testing.T) {
for _, test := range []struct {
name string
RPC string
want bool
}{
{
name: "valid update request",
RPC: "UpdateBook",
want: true,
},
{
name: "not update request",
RPC: "ArchiveBook",
want: false,
},
{
name: "lookalike request",
RPC: "Updater",
want: false,
},
{
name: "multiword lookalike request",
RPC: "UpdaterAndCreator",
want: false,
},
} {
t.Run(test.name, func(t *testing.T) {
file := testutils.ParseProto3Tmpl(t, `
message {{.RPC}}Request {}
`, test)
m := file.GetMessageTypes()[0]
if got, want := IsUpdateRequestMessage(m), test.want; got != want {
t.Errorf("got %v, want %v", got, want)
}
})
}
}

func TestIsDeleteRequestMessage(t *testing.T) {
for _, test := range []struct {
name string
RPC string
want bool
}{
{
name: "valid delete request",
RPC: "DeleteBook",
want: true,
},
{
name: "not delete request",
RPC: "ArchiveBook",
want: false,
},
{
name: "lookalike request",
RPC: "Deleter",
want: false,
},
{
name: "multiword lookalike request",
RPC: "DeleterAndPurge",
want: false,
},
} {
t.Run(test.name, func(t *testing.T) {
file := testutils.ParseProto3Tmpl(t, `
message {{.RPC}}Request {}
`, test)
m := file.GetMessageTypes()[0]
if got, want := IsDeleteRequestMessage(m), test.want; got != want {
t.Errorf("got %v, want %v", got, want)
}
})
}
}
Loading