I have some Go tests, and I am trying to use Null*s correctly, but go test is giving errors and I am not sure what is wrong.
Here is the error message:
go test ./...
utils/db.go:1:1: expected 'package', found 'EOF'
? mobifit [no test files]
# mobifit/app/entities
app/entities/user_test.go:34: cannot use dateOfBirth (type func() time.Time) as type time.Time in assignment
app/entities/user_test.go:58: User.Gender undefined (type User has no method Gender)
app/entities/user_test.go:59: User.DateOfBirth undefined (type User has no method DateOfBirth)
app/entities/user_test.go:60: User.Height undefined (type User has no method Height)
app/entities/user_test.go:61: User.CurrentWeight undefined (type User has no method CurrentWeight)
app/entities/user_test.go:65: User.Email undefined (type User has no method Email)
app/entities/user_test.go:66: User.HashedPassword undefined (type User has no method HashedPassword)
app/entities/user_test.go:67: User.FirstName undefined (type User has no method FirstName)
app/entities/user_test.go:68: User.LastName undefined (type User has no method LastName)
app/entities/user_test.go:69: User.CreatedAt undefined (type User has no method CreatedAt)
app/entities/user_test.go:69: too many errors
FAIL mobifit/app/entities [build failed]
? mobifit/app/handlers [no test files]
Here are the tests:
package entities
import (
// "database/sql"
// "github.com/go-sql-driver/mysql"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
var (
email = "leebrooks0@gmail.com"
hashedPassword = "password"
firstName = "Lee"
lastName = "Brooks"
gender = Male
dateOfBirth = time.Now
height = 1.85
currentWeight = 101.3
)
func privacyConcernedUser() *User {
user := new(User)
user.Email.String = email
user.HashedPassword.String = hashedPassword
user.FirstName.String = firstName
user.LastName.String = lastName
return user
}
func normalUser() *User {
user := privacyConcernedUser()
user.Gender.String = gender
user.DateOfBirth.Time = dateOfBirth
user.Height.Float64 = height
user.CurrentWeight.Float64 = currentWeight
return user
}
func TestSignUpPrivacyConcernedUser(t *testing.T) {
user := privacyConcernedUser()
user.SignUp()
checkMandatoryValues(t, user)
assert.Nil(t, user.Gender.String)
assert.Nil(t, user.DateOfBirth.Time)
assert.Nil(t, user.Height.Float64)
assert.Nil(t, user.CurrentWeight.Float64)
}
func TestSignUpNormalUser(t *testing.T) {
user := normalUser()
user.SignUp()
checkMandatoryValues(t, user)
assert.Equal(t, User.Gender.String, gender)
assert.Equal(t, User.DateOfBirth.Time, dateOfBirth)
assert.Equal(t, User.Height.Float64, height)
assert.Equal(t, User.CurrentWeight.Float64, currentWeight)
}
func checkMandatoryValues(t *testing.T, user *User) {
assert.Equal(t, User.Email.String, email)
assert.Equal(t, User.HashedPassword.String, hashedPassword)
assert.Equal(t, User.FirstName.String, firstName)
assert.Equal(t, User.LastName.String, lastName)
assert.NotNil(t, User.CreatedAt.Time) // TODO: Should rather check for not empty
}
UPDATE: Where is the User type?
type User struct {
Id sql.NullInt64
Email sql.NullString
HashedPassword sql.NullString
RoleId sql.NullInt64
FirstName sql.NullString
LastName sql.NullString
Gender sql.NullString
DateOfBirth mysql.NullTime
Height sql.NullFloat64
CurrentWeight sql.NullFloat64
CreatedAt mysql.NullTime
ConfirmedAt mysql.NullTime
LastActivityAt mysql.NullTime
DeletedAt mysql.NullTime
}
func (u *User) SignUp() {
}
For this error:
app/entities/user_test.go:34: cannot use dateOfBirth (type func() time.Time) as type time.Time in assignment
You seem to be trying to assign the function time.Now
to a time.Time
field. Presumably you want to call the function?
For these errors:
app/entities/user_test.go:58: User.Gender undefined (type User has no method Gender)
app/entities/user_test.go:59: User.DateOfBirth undefined (type User has no method DateOfBirth)
...
It looks like you're trying to access fields on the type User
, when you really want to be checking the variable user
(note the case difference).
Lastly, if you are creating sample data with sql.Null*
fields and want it to resemble real data, you should probably set the Valid
field to true
so that you don't trip up code that is actually checking for NULL
values.