Tests: Add unit tests

This commit is contained in:
graciousgrey 2023-08-09 15:56:16 +02:00
parent b194188e91
commit e6679e6c50
3 changed files with 97 additions and 1 deletions

View file

@ -1,8 +1,9 @@
package entity
import (
"github.com/stretchr/testify/assert"
"testing"
"github.com/stretchr/testify/assert"
)
func TestSession_Report(t *testing.T) {

View file

@ -0,0 +1,26 @@
package entity
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestGenerateToken(t *testing.T) {
m := GenerateToken()
assert.Equal(t, 8, len(m))
}
func TestInvalidDownloadToken(t *testing.T) {
assert.True(t, InvalidDownloadToken("xxx"))
assert.True(t, InvalidDownloadToken("1ud3qfpu"))
DownloadToken.Set("1ud3qfpu", "1ud3qfpu")
assert.False(t, InvalidDownloadToken("1ud3qfpu"))
}
func TestInvalidPreviewToken(t *testing.T) {
assert.True(t, InvalidPreviewToken("xxx"))
assert.True(t, InvalidPreviewToken("2ud3qfpu"))
PreviewToken.Set("2ud3qfpu", "2ud3qfpu")
assert.False(t, InvalidPreviewToken("2ud3qfpu"))
}

View file

@ -0,0 +1,69 @@
package entity
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestCreateUserDetails(t *testing.T) {
t.Run("Empty UID", func(t *testing.T) {
m := &User{UserUID: ""}
assert.Error(t, CreateUserDetails(m))
assert.Nil(t, m.UserDetails)
})
t.Run("Success", func(t *testing.T) {
m := &User{UserUID: "1234"}
err := CreateUserDetails(m)
if err != nil {
t.Fatal(err)
}
assert.NotNil(t, m.UserDetails)
})
}
func TestUserDetails_HasID(t *testing.T) {
u := FindUserByName("alice")
assert.True(t, u.UserDetails.HasID())
}
func TestUserDetails_Updates(t *testing.T) {
m := &User{
UserUID: "1234",
UserDetails: &UserDetails{
BirthYear: 1999,
BirthMonth: 3,
NameTitle: "Dr.",
GivenName: "John",
MiddleName: "Wulfrick",
FamilyName: "Doe",
}}
m.UserDetails.Updates(UserDetails{GivenName: "Jane"})
assert.Equal(t, "Jane", m.UserDetails.GivenName)
}
func TestUserDetails_DisplayName(t *testing.T) {
t.Run("Dr. John Doe", func(t *testing.T) {
m := &User{
UserUID: "1234",
UserDetails: &UserDetails{
BirthYear: 1999,
BirthMonth: 3,
NameTitle: "Dr.",
GivenName: "John",
MiddleName: "Wulfrick",
FamilyName: "Doe",
}}
assert.Equal(t, "Dr. John Doe", m.UserDetails.DisplayName())
})
t.Run("Empty", func(t *testing.T) {
m := &User{}
assert.Equal(t, "", m.UserDetails.DisplayName())
})
}