Skip to content

Commit 3c34561

Browse files
committed
feat: added WithBotIDs and validation for WithBotUserIDs
1 parent 2c1997c commit 3c34561

File tree

5 files changed

+261
-1
lines changed

5 files changed

+261
-1
lines changed

botsfwmodels/platform_user_data.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ var _ PlatformUserData = (*PlatformUserBaseDbo)(nil)
66
type PlatformUserBaseDbo struct {
77
BotBaseData
88

9-
BotIDs []string `json:"botIDs,omitempty" dalgo:"botIDs,omitempty,noindex" firestore:"botIDs,omitempty"`
9+
WithRequiredBotIDs
1010

1111
// FirstName is the first name of a user
1212
FirstName string `json:"firstName,omitempty" dalgo:"firstName,omitempty,noindex" firestore:"firstName,omitempty"`
@@ -22,3 +22,13 @@ type PlatformUserBaseDbo struct {
2222
func (v *PlatformUserBaseDbo) BaseData() *PlatformUserBaseDbo {
2323
return v
2424
}
25+
26+
func (v *PlatformUserBaseDbo) Validate() error {
27+
if err := v.BotBaseData.Validate(); err != nil {
28+
return err
29+
}
30+
if err := v.WithRequiredBotIDs.Validate(); err != nil {
31+
return err
32+
}
33+
return nil
34+
}

botsfwmodels/with_bot_ids.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package botsfwmodels
2+
3+
import (
4+
"fmt"
5+
"github.com/strongo/validation"
6+
"strings"
7+
)
8+
9+
type WithBotIDs struct {
10+
BotIDs []string `json:"botIDs,omitempty" dalgo:"botIDs,omitempty,noindex" firestore:"botIDs,omitempty"`
11+
}
12+
13+
func (v WithBotIDs) Validate() error {
14+
for i, botID := range v.BotIDs {
15+
if strings.TrimSpace(botID) == "" {
16+
return validation.NewErrBadRecordFieldValue(fmt.Sprintf("botIDs[%d]", i), "is empty")
17+
}
18+
}
19+
return nil
20+
}
21+
22+
type WithRequiredBotIDs WithBotIDs
23+
24+
func (v WithRequiredBotIDs) Validate() error {
25+
if len(v.BotIDs) == 0 {
26+
return validation.NewErrRecordIsMissingRequiredField("botIDs")
27+
}
28+
return WithBotIDs(v).Validate()
29+
}

botsfwmodels/with_bot_ids_test.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package botsfwmodels
2+
3+
import (
4+
"github.com/strongo/validation"
5+
"testing"
6+
)
7+
8+
func TestWithBotIDs_Validate(t *testing.T) {
9+
type fields struct {
10+
BotIDs []string
11+
}
12+
tests := []struct {
13+
name string
14+
fields fields
15+
wantErr bool
16+
}{
17+
{
18+
name: "Should pass for good records",
19+
fields: fields{
20+
BotIDs: []string{"bot1", "bot2", "bot3"},
21+
},
22+
wantErr: false,
23+
},
24+
{
25+
name: "Should pass for nil",
26+
fields: fields{
27+
BotIDs: []string{"bot1"},
28+
},
29+
wantErr: false,
30+
},
31+
{
32+
name: "Should pass for empty",
33+
fields: fields{
34+
BotIDs: []string{"bot1"},
35+
},
36+
wantErr: false,
37+
},
38+
{
39+
name: "Should fail for non trimmed",
40+
fields: fields{
41+
BotIDs: []string{" bot1"},
42+
},
43+
wantErr: false,
44+
},
45+
}
46+
for _, tt := range tests {
47+
t.Run(tt.name, func(t *testing.T) {
48+
v := WithBotIDs{
49+
BotIDs: tt.fields.BotIDs,
50+
}
51+
if err := v.Validate(); (err != nil) != tt.wantErr {
52+
t.Errorf("Validate() error = %v, wantErr %v", err, tt.wantErr)
53+
}
54+
})
55+
}
56+
}
57+
58+
func TestWithRequiredBotIDs_Validate(t *testing.T) {
59+
tests := []struct {
60+
name string
61+
v WithRequiredBotIDs
62+
wantErr bool
63+
}{
64+
{
65+
name: "Should pass",
66+
v: WithRequiredBotIDs{
67+
BotIDs: []string{"bot1"},
68+
},
69+
wantErr: false,
70+
},
71+
{
72+
name: "Should fail on nil",
73+
v: WithRequiredBotIDs{BotIDs: []string{}},
74+
wantErr: true,
75+
},
76+
{
77+
name: "Should fail on empty",
78+
v: WithRequiredBotIDs{BotIDs: []string{}},
79+
wantErr: true,
80+
},
81+
}
82+
for _, tt := range tests {
83+
t.Run(tt.name, func(t *testing.T) {
84+
err := tt.v.Validate()
85+
if err == nil && tt.wantErr || err != nil && !tt.wantErr || tt.wantErr && !validation.IsBadFieldValueError(err) {
86+
t.Errorf("Validate() error = %v, wantErr %v", err, tt.wantErr)
87+
}
88+
})
89+
}
90+
}

botsfwmodels/with_bot_user_ids.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,41 @@ package botsfwmodels
22

33
import (
44
"fmt"
5+
"github.com/strongo/validation"
56
"slices"
7+
"strings"
68
)
79

810
type WithBotUserIDs struct {
911
BotUserIDs []string `json:"botUserIDs,omitempty" firestore:"botUserIDs,omitempty" dalgo:"botUserIDs,omitempty"`
1012
}
1113

1214
func (v *WithBotUserIDs) SetBotUserID(platform, bot, userID string) {
15+
if pid := strings.TrimSpace(platform); pid == "" {
16+
panic("value of `platform` argument is empty")
17+
} else if pid != platform {
18+
panic("value of `platform` argument is not trimmed")
19+
}
20+
if bid := strings.TrimSpace(bot); bid != bot {
21+
panic("value of `bot` argument is not trimmed")
22+
}
23+
if uid := strings.TrimSpace(userID); uid == "" {
24+
panic("value of `platform` argument is empty")
25+
} else if uid != userID {
26+
panic("value of `userID` argument is not trimmed")
27+
}
1328
botUserID := fmt.Sprintf("%s:%s:%s", platform, bot, userID)
1429
if slices.Contains(v.BotUserIDs, botUserID) {
1530
return
1631
}
1732
v.BotUserIDs = append(v.BotUserIDs, botUserID)
1833
}
34+
35+
func (v *WithBotUserIDs) Validate() error {
36+
for i, botUserID := range v.BotUserIDs {
37+
if strings.TrimSpace(botUserID) == "" {
38+
return validation.NewErrBadRecordFieldValue(fmt.Sprintf("botUserIDs[%d]", i), "is empty")
39+
}
40+
}
41+
return nil
42+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package botsfwmodels
2+
3+
import (
4+
"errors"
5+
"testing"
6+
)
7+
8+
func TestWithBotUserIDs_SetBotUserID(t *testing.T) {
9+
type fields struct {
10+
BotUserIDs []string
11+
}
12+
type args struct {
13+
platform string
14+
bot string
15+
userID string
16+
}
17+
tests := []struct {
18+
name string
19+
fields fields
20+
args args
21+
expectedPanic string
22+
}{
23+
{
24+
name: "Should pass",
25+
fields: fields{BotUserIDs: []string{"platform1:bot1:user1"}},
26+
args: args{platform: "platform2", bot: "bot2", userID: "user2"},
27+
},
28+
{
29+
name: "Should panic when platform is empty",
30+
args: args{platform: " ", bot: "bot2", userID: "user2"},
31+
expectedPanic: "value of `platform` argument is empty",
32+
},
33+
{
34+
name: "Should panic when platform has space at start",
35+
args: args{platform: " p1", bot: "bot2", userID: "user2"},
36+
expectedPanic: "value of `platform` argument is not trimmed",
37+
},
38+
{
39+
name: "Should panic when platform has space at end",
40+
args: args{platform: "p1 ", bot: "bot2", userID: "user2"},
41+
expectedPanic: "value of `platform` argument is not trimmed",
42+
},
43+
{
44+
name: "Should pass when bot is empty",
45+
args: args{platform: "platform", bot: "", userID: "user2"},
46+
},
47+
{
48+
name: "Should panic when bot has only spaces",
49+
args: args{platform: "platform", bot: " ", userID: "user2"},
50+
expectedPanic: "value of `bot` argument is not trimmed",
51+
},
52+
{
53+
name: "Should panic when bot has space at start",
54+
args: args{platform: "platform", bot: " bot2", userID: "user2"},
55+
expectedPanic: "value of `bot` argument is not trimmed",
56+
},
57+
{
58+
name: "Should panic when bot has space at end",
59+
args: args{platform: "bot", bot: "bot2 ", userID: "user2"},
60+
expectedPanic: "value of `bot` argument is not trimmed",
61+
},
62+
}
63+
for _, tt := range tests {
64+
t.Run(tt.name, func(t *testing.T) {
65+
v := &WithBotUserIDs{
66+
BotUserIDs: tt.fields.BotUserIDs,
67+
}
68+
if tt.expectedPanic != "" {
69+
defer func() {
70+
if r := recover(); r == nil || r.(string) != tt.expectedPanic {
71+
t.Errorf("SetBotUserID() panic = %v, expected: %v", r, tt.expectedPanic)
72+
}
73+
}()
74+
}
75+
v.SetBotUserID(tt.args.platform, tt.args.bot, tt.args.userID)
76+
})
77+
}
78+
}
79+
80+
func TestWithBotUserIDs_Validate(t *testing.T) {
81+
type fields struct {
82+
BotUserIDs []string
83+
}
84+
tests := []struct {
85+
name string
86+
fields fields
87+
wantErr error
88+
}{
89+
{
90+
name: "Should pass",
91+
fields: fields{
92+
BotUserIDs: []string{"platform1:bot1:user1"},
93+
},
94+
wantErr: nil,
95+
},
96+
}
97+
for _, tt := range tests {
98+
t.Run(tt.name, func(t *testing.T) {
99+
v := &WithBotUserIDs{
100+
BotUserIDs: tt.fields.BotUserIDs,
101+
}
102+
if err := v.Validate(); err == nil && tt.wantErr != nil || tt.wantErr == nil && err != nil || !errors.Is(err, tt.wantErr) {
103+
t.Errorf("Validate() error = %v, wantErr %v", err, tt.wantErr)
104+
}
105+
})
106+
}
107+
}

0 commit comments

Comments
 (0)