use testify's assert on model/license's tests (#12679)

This commit is contained in:
Alfian Dhimas Nur Marita
2019-10-11 16:57:14 +07:00
committed by Jesús Espino
parent 68b5c5896b
commit def68212a8

View File

@@ -6,6 +6,8 @@ package model
import ( import (
"strings" "strings"
"testing" "testing"
"github.com/stretchr/testify/assert"
) )
func TestLicenseFeaturesToMap(t *testing.T) { func TestLicenseFeaturesToMap(t *testing.T) {
@@ -102,27 +104,20 @@ func TestLicenseFeaturesSetDefaults(t *testing.T) {
func TestLicenseIsExpired(t *testing.T) { func TestLicenseIsExpired(t *testing.T) {
l1 := License{} l1 := License{}
l1.ExpiresAt = GetMillis() - 1000 l1.ExpiresAt = GetMillis() - 1000
if !l1.IsExpired() { assert.True(t, l1.IsExpired())
t.Fatal("license should be expired")
}
l1.ExpiresAt = GetMillis() + 10000 l1.ExpiresAt = GetMillis() + 10000
if l1.IsExpired() { assert.False(t, l1.IsExpired())
t.Fatal("license should not be expired")
}
} }
func TestLicenseIsStarted(t *testing.T) { func TestLicenseIsStarted(t *testing.T) {
l1 := License{} l1 := License{}
l1.StartsAt = GetMillis() - 1000 l1.StartsAt = GetMillis() - 1000
if !l1.IsStarted() {
t.Fatal("license should be started") assert.True(t, l1.IsStarted())
}
l1.StartsAt = GetMillis() + 10000 l1.StartsAt = GetMillis() + 10000
if l1.IsStarted() { assert.False(t, l1.IsStarted())
t.Fatal("license should not be started")
}
} }
func TestLicenseToFromJson(t *testing.T) { func TestLicenseToFromJson(t *testing.T) {
@@ -147,9 +142,7 @@ func TestLicenseToFromJson(t *testing.T) {
j := l.ToJson() j := l.ToJson()
l1 := LicenseFromJson(strings.NewReader(j)) l1 := LicenseFromJson(strings.NewReader(j))
if l1 == nil { assert.NotNil(t, l1)
t.Fatalf("Decoding failed but should have passed.")
}
CheckString(t, l1.Id, l.Id) CheckString(t, l1.Id, l.Id)
CheckInt64(t, l1.IssuedAt, l.IssuedAt) CheckInt64(t, l1.IssuedAt, l.IssuedAt)
@@ -184,9 +177,7 @@ func TestLicenseToFromJson(t *testing.T) {
invalid := `{"asdf` invalid := `{"asdf`
l2 := LicenseFromJson(strings.NewReader(invalid)) l2 := LicenseFromJson(strings.NewReader(invalid))
if l2 != nil { assert.Nil(t, l2)
t.Fatalf("Should have failed but didn't")
}
} }
func TestLicenseRecordIsValid(t *testing.T) { func TestLicenseRecordIsValid(t *testing.T) {
@@ -195,38 +186,31 @@ func TestLicenseRecordIsValid(t *testing.T) {
Bytes: "asdfghjkl;", Bytes: "asdfghjkl;",
} }
if err := lr.IsValid(); err == nil { err := lr.IsValid()
t.Fatalf("Should have been invalid") assert.NotNil(t, err)
}
lr.Id = NewId() lr.Id = NewId()
lr.CreateAt = 0 lr.CreateAt = 0
if err := lr.IsValid(); err == nil { err = lr.IsValid()
t.Fatalf("Should have been invalid") assert.NotNil(t, err)
}
lr.CreateAt = GetMillis() lr.CreateAt = GetMillis()
lr.Bytes = "" lr.Bytes = ""
if err := lr.IsValid(); err == nil { err = lr.IsValid()
t.Fatalf("Should have been invalid") assert.NotNil(t, err)
}
lr.Bytes = strings.Repeat("0123456789", 1001) lr.Bytes = strings.Repeat("0123456789", 1001)
if err := lr.IsValid(); err == nil { err = lr.IsValid()
t.Fatalf("Should have been invalid") assert.NotNil(t, err)
}
lr.Bytes = "ASDFGHJKL;" lr.Bytes = "ASDFGHJKL;"
if err := lr.IsValid(); err != nil { err = lr.IsValid()
t.Fatal(err) assert.Nil(t, err)
}
} }
func TestLicenseRecordPreSave(t *testing.T) { func TestLicenseRecordPreSave(t *testing.T) {
lr := LicenseRecord{} lr := LicenseRecord{}
lr.PreSave() lr.PreSave()
if lr.CreateAt == 0 { assert.NotZero(t, lr.CreateAt)
t.Fatal("CreateAt should not be zero")
}
} }