Backend: Add unit tests for pkg

This commit is contained in:
Theresa Gresch 2020-07-07 16:29:55 +02:00
parent 0b7d8a6897
commit 0faf7dbb7f
6 changed files with 55 additions and 1 deletions

View file

@ -15,7 +15,7 @@ func TestNonCanonical(t *testing.T) {
assert.Equal(t, true, NonCanonical("short/short/short/test1_"))
})
t.Run("short?", func(t *testing.T) {
assert.Equal(t, true, NonCanonical("short/short/short/test1?"))
assert.Equal(t, true, NonCanonical("short#short_short_test1?"))
})
t.Run("short/test_test_test1234", func(t *testing.T) {
assert.Equal(t, false, NonCanonical("hort/test_test_test12345"))

View file

@ -99,6 +99,11 @@ func TestS2(t *testing.T) {
t.Run("empty code", func(t *testing.T) {
token := S2("")
assert.Equal(t, "", token)
})
t.Run("invalid code", func(t *testing.T) {
token := S2("xxx")
assert.Equal(t, "", token)
})
}

View file

@ -29,6 +29,10 @@ func TestIsPPID(t *testing.T) {
}
assert.True(t, IsPPID("lt9k3pw1wowuy3c2", 'l'))
assert.False(t, IsPPID("lt9k3pw1wowuy3c2123", 'l'))
assert.False(t, IsPPID("lt9k3pw1wowuy3c2123", 'l'))
assert.False(t, IsPPID("", 'l'))
assert.False(t, IsPPID("lt9k3pw1w ?owuy 3c2123", 'l'))
}
func TestIsHex(t *testing.T) {
@ -37,6 +41,8 @@ func TestIsHex(t *testing.T) {
assert.False(t, IsHex("55785BAC-9A4B-4747-B090-GE123FFEE437"))
assert.False(t, IsHex("550e8400-e29b-11d4-a716_446655440000"))
assert.True(t, IsHex("4B1FEF2D1CF4A5BE38B263E0637EDEAD"))
assert.False(t, IsHex(""))
}
func TestIsUUID(t *testing.T) {

View file

@ -1,6 +1,7 @@
package txt
import (
"fmt"
"regexp"
"strconv"
"strings"
@ -113,6 +114,7 @@ func Time(s string) (result time.Time) {
time.UTC)
} else if found := DatePathRegexp.Find(b); len(found) > 0 { // Is it a date path like "2020/01/03"?
n := DateIntRegexp.FindAll(found, -1)
fmt.Println(n)
if len(n) < 2 || len(n) > 3 {
return result

View file

@ -162,6 +162,30 @@ func TestTime(t *testing.T) {
assert.False(t, result.IsZero())
assert.Equal(t, "2019-01-03 00:00:00 +0000 UTC", result.String())
})
t.Run("fo.jpg", func(t *testing.T) {
result := Time("fo.jpg")
assert.True(t, result.IsZero())
assert.Equal(t, "0001-01-01 00:00:00 +0000 UTC", result.String())
})
t.Run("n >6", func(t *testing.T) {
result := Time("2020-01-30_09-87-18-23.jpg")
assert.Equal(t, "0001-01-01 00:00:00 +0000 UTC", result.String())
})
t.Run("year < yearmin", func(t *testing.T) {
result := Time("1020-01-30_09-57-18.jpg")
assert.Equal(t, "0001-01-01 00:00:00 +0000 UTC", result.String())
})
t.Run("hour > hourmax", func(t *testing.T) {
result := Time("1020-01-30_25-57-18.jpg")
assert.Equal(t, "0001-01-01 00:00:00 +0000 UTC", result.String())
})
t.Run("invalid days", func(t *testing.T) {
result := Time("2020-01-00.jpg")
assert.Equal(t, "0001-01-01 00:00:00 +0000 UTC", result.String())
})
}
func TestInt(t *testing.T) {
@ -271,6 +295,11 @@ func TestCountryCode(t *testing.T) {
result := CountryCode("")
assert.Equal(t, "zz", result)
})
t.Run("zz", func(t *testing.T) {
result := CountryCode("zz")
assert.Equal(t, "zz", result)
})
}
func TestYear(t *testing.T) {
@ -304,3 +333,9 @@ func TestYear(t *testing.T) {
assert.Equal(t, 0, result)
})
}
func TestIsUInt(t *testing.T) {
assert.False(t, IsUInt(""))
assert.False(t, IsUInt("12 3"))
assert.True(t, IsUInt("123"))
}

View file

@ -74,4 +74,10 @@ func TestFileTitle(t *testing.T) {
t.Run("du,-ich,-er, Sie und es", func(t *testing.T) {
assert.Equal(t, "Du, Ich, Er, Sie und Es", FileTitle("du,-ich,-er, Sie und es"))
})
t.Run("Title too short", func(t *testing.T) {
assert.Equal(t, "", FileTitle("jg.jpg"))
})
t.Run("invalid words", func(t *testing.T) {
assert.Equal(t, "", FileTitle("jg hg "))
})
}