From 9ae0ab7b501c8108911d0e4afe4a2e20d9b9d30e Mon Sep 17 00:00:00 2001 From: Caj Larsson Date: Sat, 23 Apr 2022 11:05:36 +0800 Subject: [PATCH] Progress on testing the domain and MockUserAgentRepository --- domain/entities.go | 1 + domain/entities_test.go | 10 ------- domain/mock.go | 59 +++++++++++++++++++++++++++++++++++++ domain/mock_test.go | 33 +++++++++++++++++++++ domain/repository.go | 1 - domain/valueobjects.go | 16 ++++++++-- domain/valueobjects_test.go | 55 ++++++++++++++++++++++++++++++++++ integration/sqliterepo.go | 4 +-- 8 files changed, 164 insertions(+), 15 deletions(-) delete mode 100644 domain/entities_test.go create mode 100644 domain/mock.go create mode 100644 domain/mock_test.go create mode 100644 domain/valueobjects_test.go diff --git a/domain/entities.go b/domain/entities.go index fc65034..68238b5 100644 --- a/domain/entities.go +++ b/domain/entities.go @@ -23,6 +23,7 @@ type BogFile struct { var ( ErrDuplicate = errors.New("record already exists") ErrExceedQuota = errors.New("file too large") + ErrQuotaInvalid = errors.New("quota invalid") ErrNotExists = errors.New("row not exists") ErrUpdateFailed = errors.New("update failed") ErrDeleteFailed = errors.New("delete failed") diff --git a/domain/entities_test.go b/domain/entities_test.go deleted file mode 100644 index 688b02b..0000000 --- a/domain/entities_test.go +++ /dev/null @@ -1,10 +0,0 @@ -package domain - -import "testing" - -func TestSum(t *testing.T) { - total := 3 - if total != 10 { - t.Errorf("It failed ok") - } -} diff --git a/domain/mock.go b/domain/mock.go new file mode 100644 index 0000000..2025968 --- /dev/null +++ b/domain/mock.go @@ -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 { + +} diff --git a/domain/mock_test.go b/domain/mock_test.go new file mode 100644 index 0000000..f75160a --- /dev/null +++ b/domain/mock_test.go @@ -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") + } +} diff --git a/domain/repository.go b/domain/repository.go index 01a0454..005f852 100644 --- a/domain/repository.go +++ b/domain/repository.go @@ -7,7 +7,6 @@ var ( ) type UserAgentRepository interface{ - Migrate() error Create(useragent UserAgent) (*UserAgent, error) All() ([]UserAgent, error) GetByName(name string) (*UserAgent, error) diff --git a/domain/valueobjects.go b/domain/valueobjects.go index b7a71a7..7a4ff39 100644 --- a/domain/valueobjects.go +++ b/domain/valueobjects.go @@ -14,12 +14,24 @@ func (f *FileSizeQuota) Allows(size int64) bool { 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 + + return nil } -func (f *FileSizeQuota) Remove(size int64) { +func (f *FileSizeQuota) Remove(size int64) error { + if size > f.CurrentUsage { + return ErrQuotaInvalid + } + f.CurrentUsage -= size + + return nil } type BogOutFile interface { diff --git a/domain/valueobjects_test.go b/domain/valueobjects_test.go new file mode 100644 index 0000000..b476cb2 --- /dev/null +++ b/domain/valueobjects_test.go @@ -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") + } +} diff --git a/integration/sqliterepo.go b/integration/sqliterepo.go index 5df55c4..de419b6 100644 --- a/integration/sqliterepo.go +++ b/integration/sqliterepo.go @@ -19,11 +19,11 @@ func NewSQLiteUserAgentRepository(filename string) *SQLiteUserAgentRepository { repo := SQLiteUserAgentRepository{ db: db, } - repo.Migrate() + repo.migrate() return &repo } -func (r SQLiteUserAgentRepository) Migrate() error { +func (r SQLiteUserAgentRepository) migrate() error { query := ` CREATE TABLE IF NOT EXISTS useragent( id INTEGER PRIMARY KEY AUTOINCREMENT,