Use is. tests and fmt

master
Caj Larsson 3 years ago
parent 8a0e6b80e3
commit e6e2f372dd

@ -1,8 +1,8 @@
package namespace package namespace
import ( import (
"time"
"errors" "errors"
"time"
) )
type Namespace struct { type Namespace struct {

@ -1,6 +1,5 @@
package namespace package namespace
type FileSizeQuota struct { type FileSizeQuota struct {
AllowanceKB int64 AllowanceKB int64
CurrentUsage int64 CurrentUsage int64

@ -1,57 +1,37 @@
package namespace package namespace
import ( import (
"github.com/matryer/is"
"testing" "testing"
) )
func TestQuota(t *testing.T) { func TestQuota(t *testing.T) {
is := is.New(t)
quota := FileSizeQuota{1000, 0} quota := FileSizeQuota{1000, 0}
if !quota.Allows(1000) { is.True(quota.Allows(1000))
t.Errorf("It should allow filling completely") is.True(!quota.Allows(1001))
}
if quota.Allows(1001) {
t.Errorf("It should not allow filling completely")
}
} }
func TestQuotaManipulation(t *testing.T) { func TestQuotaManipulation(t *testing.T) {
is := is.New(t)
quota := FileSizeQuota{1000, 0} quota := FileSizeQuota{1000, 0}
if quota.Add(500) != nil { is.NoErr(quota.Add(500))
t.Errorf("It should allow adding")
}
if quota.CurrentUsage != 500 { is.Equal(quota.CurrentUsage, int64(500))
t.Errorf("It should add the usage correctly")
}
if quota.Add(500) != nil { is.NoErr(quota.Add(500))
t.Errorf("It should allow adding up to the limit")
}
if quota.Add(1) != ErrExceedQuota { is.Equal(quota.Add(1), ErrExceedQuota)
t.Errorf("It should not allow adding beyond limit")
}
if quota.CurrentUsage != 1000 { is.Equal(quota.CurrentUsage, int64(1000))
t.Errorf("It should not overtaxed after failure to add")
}
if quota.Remove(1001) != ErrQuotaInvalid { is.Equal(quota.Remove(1001), ErrQuotaInvalid)
t.Errorf("It should not allow reducing further than 0")
}
if quota.CurrentUsage != 1000 { is.Equal(quota.CurrentUsage, int64(1000))
t.Errorf("It should not overtaxed after failure to remove")
}
if quota.Remove(1000) != nil { is.NoErr(quota.Remove(1000))
t.Errorf("It should allow reducing to 0")
}
if quota.CurrentUsage != 0 { is.Equal(quota.CurrentUsage, int64(0))
t.Errorf("It should reduce accurately")
}
} }

@ -1,17 +1,16 @@
package dataswamp package dataswamp
import ( import (
"caj-larsson/bog/dataswamp/namespace"
"caj-larsson/bog/dataswamp/swampfile"
"io" "io"
"time"
"strconv"
"strings"
"path" "path"
"path/filepath" "path/filepath"
"caj-larsson/bog/dataswamp/namespace" "strconv"
"caj-larsson/bog/dataswamp/swampfile" "strings"
"time"
) )
type SwampFileService struct { type SwampFileService struct {
namespace_repo namespace.Repository namespace_repo namespace.Repository
swamp_file_repo swampfile.Repository swamp_file_repo swampfile.Repository
@ -77,11 +76,11 @@ func (s SwampFileService) SaveFile(ref swampfile.FileReference, src io.Reader, s
} }
func (s SwampFileService) OpenOutFile(ref swampfile.FileReference) (swampfile.SwampOutFile, error) { func (s SwampFileService) OpenOutFile(ref swampfile.FileReference) (swampfile.SwampOutFile, error) {
ns, err := s.namespace_repo.GetByName(ref.UserAgent) ns := s.getOrCreateNs(ref.UserAgent)
if err == namespace.ErrNotExists { // if err == namespace.ErrNotExists {
return nil, err // return nil, err
} // }
f, err := s.swamp_file_repo.Open(ref.Path, strconv.FormatInt(ns.ID, 10)) f, err := s.swamp_file_repo.Open(ref.Path, strconv.FormatInt(ns.ID, 10))

@ -1,12 +1,12 @@
package dataswamp package dataswamp
import ( import (
"time"
"bytes" "bytes"
"testing"
"github.com/matryer/is"
"caj-larsson/bog/dataswamp/swampfile" "caj-larsson/bog/dataswamp/swampfile"
"caj-larsson/bog/dataswamp/namespace" "github.com/matryer/is"
"testing"
"time"
// "caj-larsson/bog/dataswamp/namespace"
m_namespace "caj-larsson/bog/infrastructure/memory/namespace" m_namespace "caj-larsson/bog/infrastructure/memory/namespace"
m_swampfile "caj-larsson/bog/infrastructure/memory/swampfile" m_swampfile "caj-larsson/bog/infrastructure/memory/swampfile"
) )
@ -15,7 +15,6 @@ var file_ref1 = swampfile.FileReference { "ptah1", "ua1" }
var file_ref2 = swampfile.FileReference{"path1", "ua2"} var file_ref2 = swampfile.FileReference{"path1", "ua2"}
var file_ref3 = swampfile.FileReference{"path2", "ua1"} var file_ref3 = swampfile.FileReference{"path2", "ua1"}
func NewTestSwampFileService() SwampFileService { func NewTestSwampFileService() SwampFileService {
file_repo := m_swampfile.NewRepository() file_repo := m_swampfile.NewRepository()
ns_repo := m_namespace.NewRepository() ns_repo := m_namespace.NewRepository()
@ -23,24 +22,23 @@ func NewTestSwampFileService() SwampFileService {
} }
func TestFileDontExist(t *testing.T) { func TestFileDontExist(t *testing.T) {
is := is.New(t)
s := NewTestSwampFileService() s := NewTestSwampFileService()
outfile, err := s.OpenOutFile(file_ref1) outfile, err := s.OpenOutFile(file_ref1)
if outfile != nil && err != swampfile.ErrNotExists { is.True(err == swampfile.ErrNotExists)
t.Errorf("File shall not exist by default") is.True(outfile == nil)
}
} }
func TestFileIsStored(t *testing.T) { func TestFileIsStored(t *testing.T) {
is := is.New(t)
s := NewTestSwampFileService() s := NewTestSwampFileService()
fakefile := bytes.NewBufferString("My bog data") fakefile := bytes.NewBufferString("My bog data")
err := s.SaveFile(file_ref1, fakefile, int64(fakefile.Len())) err := s.SaveFile(file_ref1, fakefile, int64(fakefile.Len()))
if err != nil { is.NoErr(err)
t.Errorf("A small file should be writable %s", err)
}
largefakefile := bytes.NewBufferString("") largefakefile := bytes.NewBufferString("")
@ -50,14 +48,11 @@ func TestFileIsStored(t *testing.T) {
err = s.SaveFile(file_ref3, largefakefile, int64(largefakefile.Len())) err = s.SaveFile(file_ref3, largefakefile, int64(largefakefile.Len()))
if err != namespace.ErrExceedQuota { is.Equal(err, swampfile.ErrExceedQuota)
t.Errorf("too large files should not be excepted")
}
} }
func TestFileIsReadBack(t *testing.T) { func TestFileIsReadBack(t *testing.T) {
is := is.New(t)
s := NewTestSwampFileService() s := NewTestSwampFileService()
infile := bytes.NewBufferString("My bog data") infile := bytes.NewBufferString("My bog data")
@ -69,13 +64,11 @@ func TestFileIsReadBack(t *testing.T) {
outfile := bytes.NewBufferString("") outfile := bytes.NewBufferString("")
_, _ = outfile.ReadFrom(outswampfile) _, _ = outfile.ReadFrom(outswampfile)
if outfile.String() != "My bog data" { is.Equal(outfile.String(), "My bog data")
t.Errorf("file corrupted")
} }
}
func TestUAIsolation(t *testing.T) { func TestUAIsolation(t *testing.T) {
is := is.New(t)
s := NewTestSwampFileService() s := NewTestSwampFileService()
ns1_file := bytes.NewBufferString("My bog data ua1") ns1_file := bytes.NewBufferString("My bog data ua1")
@ -89,11 +82,8 @@ func TestUAIsolation(t *testing.T) {
outfile := bytes.NewBufferString("") outfile := bytes.NewBufferString("")
_, _ = outfile.ReadFrom(outswampfile) _, _ = outfile.ReadFrom(outswampfile)
if outfile.String() != "My bog data ua1" { is.Equal(outfile.String(), "My bog data ua1")
t.Errorf("file corrupted")
} }
}
func TestCleanPath(t *testing.T) { func TestCleanPath(t *testing.T) {
is := is.New(t) is := is.New(t)

@ -1,8 +1,8 @@
package swampfile package swampfile
import ( import (
"time"
"errors" "errors"
"time"
) )
type SwampFile struct { type SwampFile struct {

@ -1,29 +1,27 @@
package swampfile package swampfile
import ( import (
"github.com/matryer/is"
"testing" "testing"
) )
func RepositoryContract(fac func() Repository, t *testing.T) { func RepositoryContract(fac func() Repository, t *testing.T) {
basicFileOperationContract(fac, t) basicFileOperationContract(fac, t)
} }
func basicFileOperationContract(fac func() Repository, t *testing.T) { func basicFileOperationContract(fac func() Repository, t *testing.T) {
is := is.New(t)
repo := fac() repo := fac()
not_file, err := repo.Open("doesnot", "exist") not_file, err := repo.Open("doesnot", "exist")
if err != ErrNotExists || not_file != nil{ is.Equal(err, ErrNotExists)
t.Errorf("Must raise not exists and file must not open") is.Equal(not_file, nil)
}
new_file, err := repo.Create("newfile.new", "ua1") new_file, err := repo.Create("newfile.new", "ua1")
if err != nil || new_file == nil { is.NoErr(err)
t.Errorf("Create ") is.True(new_file != nil)
}
var testdata = "testingdata" var testdata = "testingdata"
@ -32,17 +30,14 @@ func basicFileOperationContract(fac func() Repository, t *testing.T) {
reopened_file, err := repo.Open("newfile.new", "ua1") reopened_file, err := repo.Open("newfile.new", "ua1")
if err != nil || reopened_file == nil{ is.NoErr(err)
t.Errorf("Must open existing files") is.True(reopened_file != nil)
}
readback := make([]byte, 128) readback := make([]byte, 128)
size, err := reopened_file.Read(readback) size, err := reopened_file.Read(readback)
if string(readback[0:size]) != testdata { is.Equal(string(readback[0:size]), testdata)
t.Errorf("Must contain previously stored data '%s' '%s'", string(readback), testdata)
}
reopened_file.Close() reopened_file.Close()
@ -50,7 +45,6 @@ func basicFileOperationContract(fac func() Repository, t *testing.T) {
deleted_file, err := repo.Open("newfile.new", "ua1") deleted_file, err := repo.Open("newfile.new", "ua1")
if err != ErrNotExists || deleted_file != nil{ is.Equal(err, ErrNotExists)
t.Errorf("Musn't open deleted files") is.True(deleted_file == nil)
}
} }

@ -1,10 +1,10 @@
package swampfile package swampfile
import ( import (
"time" "caj-larsson/bog/dataswamp/swampfile"
"os" "os"
"path" "path"
"caj-larsson/bog/dataswamp/swampfile" "time"
) )
type FileSystemSwampFileData struct { type FileSystemSwampFileData struct {

@ -1,9 +1,9 @@
package swampfile package swampfile
import ( import (
"caj-larsson/bog/dataswamp/swampfile"
"path" "path"
"testing" "testing"
"caj-larsson/bog/dataswamp/swampfile"
) )
func TestFsFileRepo(t *testing.T) { func TestFsFileRepo(t *testing.T) {

@ -1,11 +1,10 @@
package memory package namespace
import ( import (
// "time" // "time"
"caj-larsson/bog/dataswamp/namespace" "caj-larsson/bog/dataswamp/namespace"
) )
type Repository struct { type Repository struct {
IdIdx map[int64]*namespace.Namespace IdIdx map[int64]*namespace.Namespace
NameIdx map[string]*namespace.Namespace NameIdx map[string]*namespace.Namespace
@ -29,7 +28,6 @@ func (r *Repository) Create(ns namespace.Namespace) (*namespace.Namespace, error
return &ns, nil return &ns, nil
} }
func (r *Repository) All() ([]namespace.Namespace, error) { func (r *Repository) All() ([]namespace.Namespace, error) {
ns := make([]namespace.Namespace, 0, len(r.IdIdx)) ns := make([]namespace.Namespace, 0, len(r.IdIdx))
@ -39,7 +37,6 @@ func (r *Repository) All() ([]namespace.Namespace, error) {
return ns, nil return ns, nil
} }
func (r *Repository) GetByName(name string) (*namespace.Namespace, error) { func (r *Repository) GetByName(name string) (*namespace.Namespace, error) {
ns, exists := r.NameIdx[name] ns, exists := r.NameIdx[name]
if exists { if exists {
@ -48,7 +45,6 @@ func (r *Repository) GetByName(name string) (*namespace.Namespace, error) {
return nil, namespace.ErrNotExists return nil, namespace.ErrNotExists
} }
func (r *Repository) Update(id int64, ns namespace.Namespace) (*namespace.Namespace, error) { func (r *Repository) Update(id int64, ns namespace.Namespace) (*namespace.Namespace, error) {
original := *r.IdIdx[id] original := *r.IdIdx[id]
ns.ID = id ns.ID = id
@ -57,7 +53,6 @@ func (r *Repository) Update(id int64, ns namespace.Namespace) (*namespace.Namesp
return &ns, nil return &ns, nil
} }
func (r *Repository) Delete(id int64) error { func (r *Repository) Delete(id int64) error {
original := *r.IdIdx[id] original := *r.IdIdx[id]
delete(r.NameIdx, original.Name) delete(r.NameIdx, original.Name)

@ -1,52 +1,43 @@
package memory package namespace
import ( import (
"caj-larsson/bog/dataswamp/namespace"
"github.com/matryer/is"
"testing" "testing"
"time" "time"
"caj-larsson/bog/dataswamp/namespace"
) )
func TestUserAgentRepo(t *testing.T) { func TestUserAgentRepo(t *testing.T) {
is := is.New(t)
r := NewRepository() r := NewRepository()
all, err := r.All() all, err := r.All()
if len(all) != 0 && err != nil { is.NoErr(err)
t.Errorf("New repo should be empty") is.Equal(len(all), 0)
}
ns := namespace.Namespace{23, "n1", time.Now(), time.Duration(time.Hour * 3), namespace.FileSizeQuota{1000, 0}} ns := namespace.Namespace{23, "n1", time.Now(), time.Duration(time.Hour * 3), namespace.FileSizeQuota{1000, 0}}
ns1, _ := r.Create(ns) ns1, _ := r.Create(ns)
ns.Name = "n2" ns.Name = "n2"
ns2, _ := r.Create(ns) ns2, _ := r.Create(ns)
if ns1 == ns2 { is.True(ns1 != ns2)
t.Errorf("Must create unique items")
}
all, err = r.All() all, err = r.All()
if len(all) != 2 && err != nil { is.NoErr(err)
t.Errorf("After adding there should be two Useragent") is.Equal(len(all), 2)
}
if ns.ID != 23 { is.Equal(ns.ID, int64(23))
t.Errorf("It does not change the original UserAgent")
}
ns3, _ := r.GetByName("n2") ns3, _ := r.GetByName("n2")
if ns3 != ns2 { is.Equal(ns3, ns2)
t.Errorf("It the correct ns is acquired")
}
if r.Delete(ns2.ID) != nil { is.NoErr(r.Delete(ns2.ID))
t.Errorf("Must delete without error")
}
all, err = r.All() all, err = r.All()
if len(all) != 1 && err != nil {
t.Errorf("After deleting one there should be one NS ") is.NoErr(err)
} is.Equal(len(all), 1)
} }

@ -1,15 +1,14 @@
package swampfile package swampfile
import ( import (
"time"
"path"
"os" "os"
"path"
"time"
// "io" // "io"
"github.com/spf13/afero"
"caj-larsson/bog/dataswamp/swampfile" "caj-larsson/bog/dataswamp/swampfile"
"github.com/spf13/afero"
) )
type SwampFile struct { type SwampFile struct {
filename string filename string
file afero.File file afero.File

@ -1,11 +1,10 @@
package swampfile package swampfile
import ( import (
"testing"
"caj-larsson/bog/dataswamp/swampfile" "caj-larsson/bog/dataswamp/swampfile"
"testing"
) )
func TestFileRepo(t *testing.T) { func TestFileRepo(t *testing.T) {
swampfile.RepositoryContract(NewRepository, t) swampfile.RepositoryContract(NewRepository, t)
} }

@ -1,9 +1,9 @@
package namespace package namespace
import ( import (
"caj-larsson/bog/dataswamp/namespace"
"errors" "errors"
"time" "time"
"caj-larsson/bog/dataswamp/namespace"
) )
type NamespaceRecord struct { type NamespaceRecord struct {

@ -2,8 +2,8 @@ package main
import ( import (
"caj-larsson/bog/server" "caj-larsson/bog/server"
"io/ioutil"
"fmt" "fmt"
"io/ioutil"
) )
func main() { func main() {

@ -1,15 +1,15 @@
package server package server
import ( import (
"net/http"
"fmt" "fmt"
"net/http"
"strconv" "strconv"
// "io" // "io"
"caj-larsson/bog/dataswamp" "caj-larsson/bog/dataswamp"
"caj-larsson/bog/dataswamp/namespace" "caj-larsson/bog/dataswamp/namespace"
"caj-larsson/bog/dataswamp/swampfile" "caj-larsson/bog/dataswamp/swampfile"
sql_namespace "caj-larsson/bog/infrastructure/sqlite/namespace"
fs_swampfile "caj-larsson/bog/infrastructure/fs/swampfile" fs_swampfile "caj-larsson/bog/infrastructure/fs/swampfile"
sql_namespace "caj-larsson/bog/infrastructure/sqlite/namespace"
) )
type Router interface { type Router interface {
@ -82,7 +82,6 @@ func (b *Bog) routes() {
b.router.HandleFunc("/", b.fileHandler) b.router.HandleFunc("/", b.fileHandler)
} }
func New(config *Configuration) *Bog { func New(config *Configuration) *Bog {
b := new(Bog) b := new(Bog)
b.address = config.bindAddress() b.address = config.bindAddress()

@ -3,38 +3,36 @@ package server
import ( import (
"testing" "testing"
// "fmt" // "fmt"
// "net/http/httptest" "caj-larsson/bog/dataswamp"
// "net/http" "caj-larsson/bog/infrastructure/memory/namespace"
"caj-larsson/bog/infrastructure/memory/swampfile"
// "strings" "github.com/matryer/is"
// "time" "net/http"
// "caj-larsson/bog/domain_dataswamp" "net/http/httptest"
"strings"
"time"
) )
func TestApplication(t *testing.T) { func TestApplication(t *testing.T) {
is := is.New(t)
file_service := dataswamp.NewSwampFileService(
namespace.NewRepository(),
swampfile.NewRepository(),
1000,
time.Hour,
)
// file_service := domain_dataswamp.NewBogFileService( bog := Bog{
// mock.NewMockUserAgentRepository(), router: new(http.ServeMux),
// mock.NewMockFileRepository(), file_service: file_service,
// 1000, address: "fake",
// time.Hour, }
// ) bog.routes()
req := httptest.NewRequest("POST", "/apath", strings.NewReader("testdata"))
// bog := Bog { req.Header.Add("User-Agent", "testingclient")
// router: new(http.ServeMux), req.Header.Add("Content-Length", "8")
// file_service: file_service, w := httptest.NewRecorder()
// address: "fake", bog.router.ServeHTTP(w, req)
// }
// bog.routes()
// req := httptest.NewRequest("POST", "/apath", strings.NewReader("testdata"))
// req.Header.Add("User-Agent", "testingclient")
// req.Header.Add("Content-Length", "8")
// w := httptest.NewRecorder()
// bog.router.ServeHTTP(w, req)
// if (w.Code != 200){ is.Equal(w.Code, 200)
// fmt.Printf("%v", w)
// t.Error("not ok")
// }
} }

@ -2,13 +2,11 @@ package server
import ( import (
"fmt" "fmt"
"time"
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
"github.com/c2h5oh/datasize" "github.com/c2h5oh/datasize"
"time"
) )
func (qc QuotaConfig) ParsedSizeBytes() int64 { func (qc QuotaConfig) ParsedSizeBytes() int64 {
var v datasize.ByteSize var v datasize.ByteSize
@ -42,24 +40,20 @@ type QuotaConfig struct {
DefaultDuration string `toml:"default_duration"` DefaultDuration string `toml:"default_duration"`
} }
type ServerConfig struct { type ServerConfig struct {
Port int64 Port int64
Host string Host string
} }
type FileConfig struct { type FileConfig struct {
Path string Path string
} }
type DatabaseConfig struct { type DatabaseConfig struct {
Backend string Backend string
Connection string Connection string
} }
type Configuration struct { type Configuration struct {
Server ServerConfig Server ServerConfig
File FileConfig File FileConfig
@ -71,7 +65,6 @@ func (c *Configuration) bindAddress() string {
return fmt.Sprintf("%s:%d", c.Server.Host, c.Server.Port) return fmt.Sprintf("%s:%d", c.Server.Host, c.Server.Port)
} }
func ConfigFromToml(toml_data string) (*Configuration, error) { func ConfigFromToml(toml_data string) (*Configuration, error) {
var config Configuration var config Configuration

@ -1,13 +1,13 @@
package server package server
import ( import (
"time" "github.com/matryer/is"
"testing" "testing"
"time"
) )
func TestConfiguration(t *testing.T) { func TestConfiguration(t *testing.T) {
is := is.New(t)
c, _ := ConfigFromToml( c, _ := ConfigFromToml(
`[server] `[server]
port = 8002 port = 8002
@ -25,19 +25,8 @@ func TestConfiguration(t *testing.T) {
default_duration = "72h"`, default_duration = "72h"`,
) )
if c.Server.Port != 8002 { is.Equal(c.Server.Port, int64(8002))
t.Errorf("port parsing failed") is.Equal(c.Server.Host, "127.0.0.1")
} is.Equal(c.Quota.ParsedSizeBytes(), int64(1024*1024))
is.Equal(c.Quota.ParsedDuration(), time.Duration(time.Hour*72))
if c.Server.Host != "127.0.0.1" {
t.Errorf("host parsing failed")
}
if c.Quota.ParsedSizeBytes() != 1024 * 1024 {
t.Errorf("quota size parsing failed")
}
if c.Quota.ParsedDuration() != time.Duration(time.Hour * 72) {
t.Errorf("quota size parsing failed")
}
} }

Loading…
Cancel
Save