You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
bog/test/domain/services_test.go

95 lines
2.2 KiB
Go

package test
import (
"time"
"bytes"
"testing"
"caj-larsson/bog/domain"
"caj-larsson/bog/test/mock"
)
var file_ref1 = domain.FileReference { "path1", "ua1" }
var file_ref2 = domain.FileReference { "path1", "ua2" }
var file_ref3 = domain.FileReference { "path2", "ua1" }
func NewTestBogFileService() domain.BogFileService {
file_repo := mock.NewMockFileRepository()
ua_repo := mock.NewMockUserAgentRepository()
return domain.NewBogFileService(ua_repo, file_repo, 1024, time.Hour)
}
func TestFileDontExist(t *testing.T) {
s := NewTestBogFileService()
outfile, err := s.OpenOutFile(file_ref1)
if outfile != nil && err != domain.ErrNotExists {
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")
}
}
func TestUAIsolation(t *testing.T) {
file_repo := mock.NewMockFileRepository()
ua_repo := mock.NewMockUserAgentRepository()
s := domain.NewBogFileService(ua_repo, file_repo, 1024, time.Hour)
ua1_file := bytes.NewBufferString("My bog data ua1")
ua2_file := bytes.NewBufferString("My bog data ua2")
_ = s.SaveFile(file_ref1, ua1_file, int64(ua1_file.Len()))
_ = s.SaveFile(file_ref2, ua2_file, int64(ua2_file.Len()))
outbogfile, _ := s.OpenOutFile(file_ref1)
outfile := bytes.NewBufferString("")
_, _ = outfile.ReadFrom(outbogfile)
if outfile.String() != "My bog data ua1" {
t.Errorf("file corrupted")
}
}