mirror of
https://github.com/Eggbertx/gochan.git
synced 2025-08-03 07:36:23 -07:00
Start adding template function tests
This commit is contained in:
parent
e5bbcc6493
commit
34e6887490
2 changed files with 71 additions and 6 deletions
|
@ -12,12 +12,9 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func banMaskTmplFunc(ban gcsql.IPBan) string {
|
func banMaskTmplFunc(ban gcsql.IPBan) string {
|
||||||
if ban.ID < 1 {
|
|
||||||
if ban.RangeStart == ban.RangeEnd {
|
if ban.RangeStart == ban.RangeEnd {
|
||||||
return ban.RangeStart
|
return ban.RangeStart
|
||||||
}
|
}
|
||||||
return ""
|
|
||||||
}
|
|
||||||
ipn, err := gcutil.GetIPRangeSubnet(ban.RangeStart, ban.RangeEnd)
|
ipn, err := gcutil.GetIPRangeSubnet(ban.RangeStart, ban.RangeEnd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "?"
|
return "?"
|
||||||
|
@ -69,7 +66,7 @@ func getTopPostIDTmplFunc(post *gcsql.Post) int {
|
||||||
return id
|
return id
|
||||||
}
|
}
|
||||||
|
|
||||||
func numRepliesTmplFunc(boardid, opID int) int {
|
func numRepliesTmplFunc(_, opID int) int {
|
||||||
num, err := gcsql.GetThreadReplyCountFromOP(opID)
|
num, err := gcsql.GetThreadReplyCountFromOP(opID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0
|
return 0
|
||||||
|
|
68
pkg/gcsql/initsql/init_test.go
Normal file
68
pkg/gcsql/initsql/init_test.go
Normal file
|
@ -0,0 +1,68 @@
|
||||||
|
package initsql
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/gochan-org/gochan/pkg/gcsql"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestBanMaskTmplFunc(t *testing.T) {
|
||||||
|
testCases := []struct {
|
||||||
|
desc string
|
||||||
|
rangeStart string
|
||||||
|
rangeEnd string
|
||||||
|
expects string
|
||||||
|
banID int
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
desc: "expect empty string if either value is enpty",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "expect rangeStart if banID is 0 and rangeStart == rangEnd",
|
||||||
|
rangeStart: "192.168.56.1",
|
||||||
|
rangeEnd: "192.168.56.1",
|
||||||
|
expects: "192.168.56.1",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: `expect "?" if an error is received and banID > 0`,
|
||||||
|
banID: 1,
|
||||||
|
rangeStart: "lol",
|
||||||
|
rangeEnd: "lmao",
|
||||||
|
expects: "?",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "expect CIDR if ban exists, comparison is valid, and IPs differ (IPv4)",
|
||||||
|
banID: 1,
|
||||||
|
rangeStart: "192.168.56.0",
|
||||||
|
rangeEnd: "192.168.56.255",
|
||||||
|
expects: "192.168.56.0/24",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "expect CIDR if ban exists, comparison is valid, and IPs differ (IPv6)",
|
||||||
|
banID: 1,
|
||||||
|
rangeStart: "2801::",
|
||||||
|
rangeEnd: "2801::ffff",
|
||||||
|
expects: "2801::/112",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "expect IP if ban exists, comparison is valid, and IPs are the same (IPv4)",
|
||||||
|
banID: 1,
|
||||||
|
rangeStart: "192.168.56.1",
|
||||||
|
rangeEnd: "192.168.56.1",
|
||||||
|
expects: "192.168.56.1",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
var ban gcsql.IPBan
|
||||||
|
for _, tC := range testCases {
|
||||||
|
t.Run(tC.desc, func(tr *testing.T) {
|
||||||
|
ban = gcsql.IPBan{
|
||||||
|
ID: tC.banID,
|
||||||
|
RangeStart: tC.rangeStart,
|
||||||
|
RangeEnd: tC.rangeEnd,
|
||||||
|
}
|
||||||
|
result := banMaskTmplFunc(ban)
|
||||||
|
assert.Equal(tr, tC.expects, result)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue