Fix UA mock id persistance bug

master
Caj Larsson 3 years ago
parent 199d4ed6f3
commit 0a569805ea

@ -38,7 +38,7 @@ func (r SQLiteUserAgentRepository) migrate() error {
return err return err
} }
func (r SQLiteUserAgentRepository) Create(useragent domain.UserAgent) (*domain.UserAgent, error) { func (r *SQLiteUserAgentRepository) Create(useragent domain.UserAgent) (*domain.UserAgent, error) {
var record, err = fromEntity(useragent) var record, err = fromEntity(useragent)
if err != nil { if err != nil {

@ -10,7 +10,7 @@ import (
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" }
@ -61,7 +61,6 @@ func TestFileIsReadBack(t *testing.T) {
_ = s.SaveFile(file_ref1, infile, int64(infile.Len())) _ = s.SaveFile(file_ref1, infile, int64(infile.Len()))
outbogfile, _ := s.OpenOutFile(file_ref1) outbogfile, _ := s.OpenOutFile(file_ref1)
outfile := bytes.NewBufferString("") outfile := bytes.NewBufferString("")
@ -71,3 +70,25 @@ func TestFileIsReadBack(t *testing.T) {
t.Errorf("file corrupted") 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")
}
}

@ -12,7 +12,7 @@ type MockUserAgentRepository struct {
NextId int64 NextId int64
} }
func NewMockUserAgentRepository() domain.UserAgentRepository{ func NewMockUserAgentRepository() *MockUserAgentRepository {
r := new(MockUserAgentRepository) r := new(MockUserAgentRepository)
r.NextId = 0 r.NextId = 0
r.IdIdx = make(map[int64]*domain.UserAgent) r.IdIdx = make(map[int64]*domain.UserAgent)
@ -20,7 +20,7 @@ func NewMockUserAgentRepository() domain.UserAgentRepository{
return r return r
} }
func (r MockUserAgentRepository) Create(useragent domain.UserAgent) (*domain.UserAgent, error) { func (r *MockUserAgentRepository) Create(useragent domain.UserAgent) (*domain.UserAgent, error) {
r.NextId += 1 r.NextId += 1
useragent.ID = r.NextId useragent.ID = r.NextId
@ -30,7 +30,7 @@ func (r MockUserAgentRepository) Create(useragent domain.UserAgent) (*domain.Use
} }
func (r MockUserAgentRepository) All() ([]domain.UserAgent, error) { func (r *MockUserAgentRepository) All() ([]domain.UserAgent, error) {
v := make([]domain.UserAgent, 0, len(r.IdIdx)) v := make([]domain.UserAgent, 0, len(r.IdIdx))
for _, value := range r.IdIdx { for _, value := range r.IdIdx {
@ -40,7 +40,7 @@ func (r MockUserAgentRepository) All() ([]domain.UserAgent, error) {
} }
func (r MockUserAgentRepository) GetByName(name string) (*domain.UserAgent, error) { func (r *MockUserAgentRepository) GetByName(name string) (*domain.UserAgent, error) {
useragent, exists := r.NameIdx[name] useragent, exists := r.NameIdx[name]
if exists { if exists {
return useragent, nil return useragent, nil
@ -49,7 +49,7 @@ func (r MockUserAgentRepository) GetByName(name string) (*domain.UserAgent, erro
} }
func (r MockUserAgentRepository) Update(id int64, useragent domain.UserAgent) (*domain.UserAgent, error) { func (r *MockUserAgentRepository) Update(id int64, useragent domain.UserAgent) (*domain.UserAgent, error) {
original := *r.IdIdx[id] original := *r.IdIdx[id]
useragent.ID = id useragent.ID = id
r.IdIdx[id] = &useragent r.IdIdx[id] = &useragent
@ -58,7 +58,7 @@ func (r MockUserAgentRepository) Update(id int64, useragent domain.UserAgent) (*
} }
func (r MockUserAgentRepository) Delete(id int64) error { func (r *MockUserAgentRepository) Delete(id int64) error {
original := *r.IdIdx[id] original := *r.IdIdx[id]
delete(r.NameIdx, original.Name) delete(r.NameIdx, original.Name)
delete(r.IdIdx, original.ID) delete(r.IdIdx, original.ID)

Loading…
Cancel
Save