Progress on testing the domain and MockUserAgentRepository

master
Caj Larsson 3 years ago
parent 54fe6d011e
commit 9ae0ab7b50

@ -23,6 +23,7 @@ type BogFile struct {
var ( var (
ErrDuplicate = errors.New("record already exists") ErrDuplicate = errors.New("record already exists")
ErrExceedQuota = errors.New("file too large") ErrExceedQuota = errors.New("file too large")
ErrQuotaInvalid = errors.New("quota invalid")
ErrNotExists = errors.New("row not exists") ErrNotExists = errors.New("row not exists")
ErrUpdateFailed = errors.New("update failed") ErrUpdateFailed = errors.New("update failed")
ErrDeleteFailed = errors.New("delete failed") ErrDeleteFailed = errors.New("delete failed")

@ -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")
}
}

@ -7,7 +7,6 @@ var (
) )
type UserAgentRepository interface{ type UserAgentRepository interface{
Migrate() error
Create(useragent UserAgent) (*UserAgent, error) Create(useragent UserAgent) (*UserAgent, error)
All() ([]UserAgent, error) All() ([]UserAgent, error)
GetByName(name string) (*UserAgent, error) GetByName(name string) (*UserAgent, error)

@ -14,12 +14,24 @@ func (f *FileSizeQuota) Allows(size int64) bool {
return f.CurrentUsage + size <= f.AllowanceKB return f.CurrentUsage + size <= f.AllowanceKB
} }
func (f *FileSizeQuota) Add(size int64) { func (f *FileSizeQuota) Add(size int64) error {
if !f.Allows(size) {
return ErrExceedQuota
}
f.CurrentUsage += size f.CurrentUsage += size
return nil
} }
func (f *FileSizeQuota) Remove(size int64) { func (f *FileSizeQuota) Remove(size int64) error {
if size > f.CurrentUsage {
return ErrQuotaInvalid
}
f.CurrentUsage -= size f.CurrentUsage -= size
return nil
} }
type BogOutFile interface { type BogOutFile interface {

@ -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")
}
}

@ -19,11 +19,11 @@ func NewSQLiteUserAgentRepository(filename string) *SQLiteUserAgentRepository {
repo := SQLiteUserAgentRepository{ repo := SQLiteUserAgentRepository{
db: db, db: db,
} }
repo.Migrate() repo.migrate()
return &repo return &repo
} }
func (r SQLiteUserAgentRepository) Migrate() error { func (r SQLiteUserAgentRepository) migrate() error {
query := ` query := `
CREATE TABLE IF NOT EXISTS useragent( CREATE TABLE IF NOT EXISTS useragent(
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,

Loading…
Cancel
Save