Skip to content

Commit

Permalink
Merge pull request wal-g#1462 from IvanSibirtsev/master
Browse files Browse the repository at this point in the history
add tests to LSN Regex
  • Loading branch information
serprex authored May 3, 2023
2 parents b81fb43 + 94c3747 commit c50db95
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
5 changes: 2 additions & 3 deletions utility/utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,13 +232,12 @@ func StripPrefixName(path string) string {
return name
}

// TODO : unit tests
var patternLSN = "[0-9A-F]{24}"
var regexpLSN = regexp.MustCompile(patternLSN)
var RegexpLSN = regexp.MustCompile(patternLSN)

// Strips the backup WAL file name.
func StripWalFileName(path string) string {
foundLsn := regexpLSN.FindAllString(path, 2)
foundLsn := RegexpLSN.FindAllString(path, 2)
if len(foundLsn) > 0 {
return foundLsn[0]
}
Expand Down
54 changes: 52 additions & 2 deletions utility/utility_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package utility_test

import (
"bytes"
"fmt"
"io"
"math/rand"
"os"
Expand Down Expand Up @@ -415,6 +416,52 @@ func TestStripWalFileName_ValidLsn(t *testing.T) {
assert.Equal(t, path, result)
}

func TestLsnRegex(t *testing.T) {
lsns := []string{RandomLsn(), RandomLsn()}
tests := []struct {
name string
lsn string
expected []string
expectedLen int
}{
{
name: "LsnRegex_ReturnLsnFromString",
lsn: lsns[0],
expected: []string{lsns[0]},
},
{
name: "LsnRegex_ReturnLsnFromStringWithAnotherText",
lsn: fmt.Sprintf("some text %s or 43567", lsns[0]),
expected: []string{lsns[0]},
},
{
name: "LsnRegex_ReturnEmptyArrayWhenLsnIsIncorrect",
lsn: GetRandomizedHexString(23),
expected: nil,
},
{
name: "LsnRegex_ReturnLsnWhenItIsAllF",
lsn: strings.Repeat("F", 24),
expected: []string{strings.Repeat("F", 24)},
},
{
name: "LsnRegex_ReturnAllLsnWhenHasSeparator",
lsn: strings.Join(lsns[:], "-"),
expected: lsns,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

result := utility.RegexpLSN.FindAllString(tt.lsn, -1)

assert.Equalf(t, len(tt.expected), len(result), "Expected different array length")
assert.Equalf(t, tt.expected, result, "Expected different result")
})
}
}

func TestStripWalFileName_ReturnFirstLsn(t *testing.T) {
var paths = [3]string{RandomLsn(), RandomLsn(), RandomLsn()}
var path = strings.Join(paths[:], "-")
Expand All @@ -425,10 +472,13 @@ func TestStripWalFileName_ReturnFirstLsn(t *testing.T) {
}

func RandomLsn() string {
var letter = []rune("ABCDEF0123456789")
const LSNLength = 24
return GetRandomizedHexString(LSNLength)
}

b := make([]rune, LSNLength)
func GetRandomizedHexString(length int) string {
var letter = []rune("ABCDEF0123456789")
b := make([]rune, length)
for i := range b {
b[i] = letter[rand.Intn(len(letter))]
}
Expand Down

0 comments on commit c50db95

Please sign in to comment.