genofire/hs_monolith
genofire
/
hs_monolith
Archived
1
0
Fork 0

[TASK] add review model

This commit is contained in:
Martin Geno 2017-03-30 18:10:36 +02:00
parent 969bd7f618
commit 4e966a59df
No known key found for this signature in database
GPG Key ID: F0D39A37E925E941
2 changed files with 59 additions and 0 deletions

28
models/review.go Normal file
View File

@ -0,0 +1,28 @@
package models
import "github.com/genofire/hs_master-kss-monolith/lib/database"
type Review struct {
ID int64
ProductID int64
LocaleLanguage string
FirstName string
LastName string
RatingStars int64
Text string
}
func (r *Review) DisplayName() string {
if len(r.FirstName) > 0 {
if len(r.LastName) > 0 {
last := []byte(r.LastName)
return r.FirstName + " " + string(last[0]) + "."
}
return r.FirstName
}
return "Anonymous"
}
func init() {
database.AddModel(&Review{})
}

31
models/review_test.go Normal file
View File

@ -0,0 +1,31 @@
package models
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestDisplayName(t *testing.T) {
assert := assert.New(t)
r := Review{}
assert.Equal("", r.FirstName, "wrong firstname")
assert.Equal("", r.LastName, "wrong lastname")
assert.Equal("Anonymous", r.DisplayName(), "No name")
r.FirstName = "Max"
assert.Equal("Max", r.FirstName, "wrong firstname")
assert.Equal("", r.LastName, "wrong lastname")
assert.Equal("Max", r.DisplayName(), "Only Firstname")
r.LastName = "Mustermann"
assert.Equal("Max", r.FirstName, "wrong firstname")
assert.Equal("Mustermann", r.LastName, "wrong lastname")
assert.Equal("Max M.", r.DisplayName(), "Shorted Name")
r.FirstName = ""
assert.Equal("", r.FirstName, "wrong firstname")
assert.Equal("Mustermann", r.LastName, "wrong lastname")
assert.Equal("Anonymous", r.DisplayName(), "displayname: no firstname")
}