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