Domain services tested, first pass of domain testing done

master
Caj Larsson 3 years ago
parent b0d4820659
commit 199d4ed6f3

@ -15,7 +15,7 @@ it, you get the same bits back.
## TODO ## TODO
Alpha Alpha
- [ ] Test domain - [x] Test domain
- [ ] Test integration - [ ] Test integration
- [ ] Test application - [ ] Test application
- [ ] Concurrent access safety - [ ] Concurrent access safety

@ -10,34 +10,49 @@ import (
type BogFileService struct { type BogFileService struct {
user_agent_repo UserAgentRepository user_agent_repo UserAgentRepository
file_data_repo FileDataRepository file_data_repo FileDataRepository
default_allowance_bytes int64
default_allowance_duration time.Duration
} }
func NewBogFileService(user_agent_repo UserAgentRepository, file_data_repo FileDataRepository) BogFileService { func NewBogFileService(
return BogFileService {user_agent_repo, file_data_repo} user_agent_repo UserAgentRepository,
file_data_repo FileDataRepository,
da_bytes int64,
da_duration time.Duration,
) BogFileService {
return BogFileService {user_agent_repo, file_data_repo, da_bytes, da_duration}
} }
func (b BogFileService) SaveFile(ref FileReference, src io.Reader, size int64) error { func (b BogFileService) getOrCreateUA(useragent_in string) *UserAgent{
user_agent, err := b.user_agent_repo.GetByName(ref.UserAgent) ua, err := b.user_agent_repo.GetByName(useragent_in)
if !user_agent.FileQuota.Allows(size) {
return ErrExceedQuota
}
if err == ErrNotExists { if err == ErrNotExists {
// TODO make this into a factory method?
new_ua := UserAgent { new_ua := UserAgent {
0, ref.UserAgent, time.Now(), time.Duration(time.Second * 400), FileSizeQuota {10, 10 }, 0, useragent_in, time.Now(), b.default_allowance_duration, FileSizeQuota { b.default_allowance_bytes, 0 },
} }
user_agent, err = b.user_agent_repo.Create(new_ua) created_ua, err := b.user_agent_repo.Create(new_ua)
if err != nil { if err != nil {
panic(err) panic(err)
} }
return created_ua
} }
if err != nil { if err != nil {
panic(err) panic(err)
} }
return ua
}
func (b BogFileService) SaveFile(ref FileReference, src io.Reader, size int64) error {
user_agent := b.getOrCreateUA(ref.UserAgent)
if !user_agent.FileQuota.Allows(size) {
return ErrExceedQuota
}
f, err := b.file_data_repo.Create(ref.Path, strconv.FormatInt(user_agent.ID, 10)) f, err := b.file_data_repo.Create(ref.Path, strconv.FormatInt(user_agent.ID, 10))
if err != nil { if err != nil {

@ -1,30 +1,73 @@
package test package test
import ( import (
"time"
"bytes"
"testing" "testing"
"caj-larsson/bog/domain" "caj-larsson/bog/domain"
"caj-larsson/bog/test/mock" "caj-larsson/bog/test/mock"
) )
var file_ref1 = domain.FileReference { "path1", "ua1" } var file_ref1 = domain.FileReference { "path1", "ua1" }
//var file_ref2 = domain.FileReference { "path1", "ua2" } //var file_ref2 = domain.FileReference { "path1", "ua2" }
//var file_ref3 = domain.FileReference { "path2", "ua1" } var file_ref3 = domain.FileReference { "path2", "ua1" }
func TestFileService(t *testing.T) { func NewTestBogFileService() domain.BogFileService {
file_repo := mock.NewMockFileRepository() file_repo := mock.NewMockFileRepository()
ua_repo := mock.NewMockUserAgentRepository() ua_repo := mock.NewMockUserAgentRepository()
service := domain.NewBogFileService(ua_repo, file_repo) return domain.NewBogFileService(ua_repo, file_repo, 1024, time.Hour)
}
outfile, err := service.OpenOutFile(file_ref1) func TestFileDontExist(t *testing.T) {
s := NewTestBogFileService()
outfile, err := s.OpenOutFile(file_ref1)
if outfile != nil && err != domain.ErrNotExists { if outfile != nil && err != domain.ErrNotExists {
t.Errorf("File shall not exist by default") t.Errorf("File shall not exist by default")
} }
}
func TestFileIsStored(t *testing.T) {
s := NewTestBogFileService()
fakefile := bytes.NewBufferString("My bog data")
err := s.SaveFile(file_ref1, fakefile, int64(fakefile.Len()))
if err != nil {
t.Errorf("A small file should be writable %s", err)
}
largefakefile := bytes.NewBufferString("")
for largefakefile.Len() < 64000 {
_, err = largefakefile.WriteString("A very repetitive file")
}
err = s.SaveFile(file_ref3, largefakefile, int64(largefakefile.Len()))
if err != domain.ErrExceedQuota {
t.Errorf("too large files should not be excepted")
}
}
func TestFileIsReadBack(t *testing.T) {
s := NewTestBogFileService()
infile := bytes.NewBufferString("My bog data")
_ = s.SaveFile(file_ref1, infile, int64(infile.Len()))
outbogfile, _ := s.OpenOutFile(file_ref1)
outfile := bytes.NewBufferString("")
_, _ = outfile.ReadFrom(outbogfile)
if outfile.String() != "My bog data" {
t.Errorf("file corrupted")
}
} }

Loading…
Cancel
Save