Progress on testing the domain and MockUserAgentRepository
parent
54fe6d011e
commit
9ae0ab7b50
@ -1,10 +0,0 @@
|
||||
package domain
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestSum(t *testing.T) {
|
||||
total := 3
|
||||
if total != 10 {
|
||||
t.Errorf("It failed ok")
|
||||
}
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
package domain
|
||||
|
||||
|
||||
type MockFileRepository struct {
|
||||
IdIdx map[int64]*UserAgent
|
||||
NameIdx map[string]*UserAgent
|
||||
NextId int64
|
||||
}
|
||||
|
||||
|
||||
func (r MockFileRepository) Create(useragent UserAgent) (*UserAgent, error) {
|
||||
r.NextId += 1
|
||||
useragent.ID = r.NextId
|
||||
|
||||
r.IdIdx[useragent.ID] = &useragent
|
||||
r.NameIdx[useragent.Name] = &useragent
|
||||
return &useragent, nil
|
||||
}
|
||||
|
||||
|
||||
func (r MockFileRepository) All() ([]UserAgent, error) {
|
||||
v := make([]UserAgent, 0, len(r.IdIdx))
|
||||
|
||||
for _, value := range r.IdIdx {
|
||||
v = append(v, *value)
|
||||
}
|
||||
return v, nil
|
||||
}
|
||||
|
||||
|
||||
func (r MockFileRepository) GetByName(name string) (*UserAgent, error) {
|
||||
useragent, exists := r.NameIdx[name]
|
||||
if exists {
|
||||
return useragent, nil
|
||||
}
|
||||
return nil, ErrNotExists
|
||||
}
|
||||
|
||||
|
||||
func (r MockFileRepository) Update(id int64, useragent UserAgent) (*UserAgent, error) {
|
||||
original := *r.IdIdx[id]
|
||||
useragent.ID = id
|
||||
r.IdIdx[id] = &useragent
|
||||
r.NameIdx[original.Name] = &useragent
|
||||
return &useragent, nil
|
||||
}
|
||||
|
||||
|
||||
func (r MockFileRepository) Delete(id int64) error {
|
||||
original := *r.IdIdx[id]
|
||||
delete(r.NameIdx, original.Name)
|
||||
delete(r.IdIdx, original.ID)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
type MockUserAgentRepository struct {
|
||||
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestMockFileRepo(t *testing.T) {
|
||||
r := new(MockFileRepository)
|
||||
r.NextId = 0
|
||||
r.IdIdx = make(map[int64]*UserAgent)
|
||||
r.NameIdx = make(map[string]*UserAgent)
|
||||
|
||||
all, err := r.All()
|
||||
|
||||
if len(all) != 0 && err != nil {
|
||||
t.Errorf("New repo should be empty")
|
||||
}
|
||||
|
||||
ua := UserAgent {23, "", time.Now(), time.Duration(time.Hour * 3), FileSizeQuota {1000, 0} }
|
||||
|
||||
r.Create(ua)
|
||||
|
||||
all, err = r.All()
|
||||
|
||||
if len(all) != 1 && err != nil {
|
||||
t.Errorf("After adding there should be a Useragent")
|
||||
}
|
||||
|
||||
if ua.ID != 23 {
|
||||
t.Errorf("It does not change the original UserAgent")
|
||||
}
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
package domain
|
||||
|
||||
import "testing"
|
||||
|
||||
|
||||
func TestQuota(t *testing.T) {
|
||||
quota := FileSizeQuota { 1000, 0 }
|
||||
|
||||
if !quota.Allows(1000) {
|
||||
t.Errorf("It should allow filling completely")
|
||||
}
|
||||
if quota.Allows(1001) {
|
||||
t.Errorf("It should not allow filling completely")
|
||||
}
|
||||
}
|
||||
|
||||
func TestQuotaManipulation(t *testing.T) {
|
||||
quota := FileSizeQuota { 1000, 0 }
|
||||
|
||||
if quota.Add(500) != nil {
|
||||
t.Errorf("It should allow adding")
|
||||
}
|
||||
|
||||
if quota.CurrentUsage != 500 {
|
||||
t.Errorf("It should add the usage correctly")
|
||||
}
|
||||
|
||||
if quota.Add(500) != nil {
|
||||
t.Errorf("It should allow adding up to the limit")
|
||||
}
|
||||
|
||||
if quota.Add(1) != ErrExceedQuota {
|
||||
t.Errorf("It should not allow adding beyond limit")
|
||||
}
|
||||
|
||||
if quota.CurrentUsage != 1000 {
|
||||
t.Errorf("It should not overtaxed after failure to add")
|
||||
}
|
||||
|
||||
if quota.Remove(1001) != ErrQuotaInvalid {
|
||||
t.Errorf("It should not allow reducing further than 0")
|
||||
}
|
||||
|
||||
if quota.CurrentUsage != 1000 {
|
||||
t.Errorf("It should not overtaxed after failure to remove")
|
||||
}
|
||||
|
||||
if quota.Remove(1000) != nil {
|
||||
t.Errorf("It should allow reducing to 0")
|
||||
}
|
||||
|
||||
if quota.CurrentUsage != 0 {
|
||||
t.Errorf("It should reduce accurately")
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue