diff --git a/dataswamp/namespace/repository_contract.go b/dataswamp/namespace/repository_contract.go new file mode 100644 index 0000000..dbb7151 --- /dev/null +++ b/dataswamp/namespace/repository_contract.go @@ -0,0 +1,53 @@ +package namespace + +import ( + "github.com/matryer/is" + "testing" + "time" +) + +func RepositoryContract(fac func() Repository, t *testing.T) { + basicNamespaceContract(fac, t) +} + +func basicNamespaceContract(fac func() Repository, t *testing.T) { + is := is.New(t) + r := fac() + + all, err := r.All() + + is.NoErr(err) + is.Equal(len(all), 0) + + ns := Namespace{ + 23, + "n1", + time.Now(), + time.Duration(time.Hour * 3), + FileSizeQuota{1000, 0}, + Usage{1, 2, 3, 4, 5}, + } + + ns1, _ := r.Create(ns) + ns.Name = "n2" + ns2, _ := r.Create(ns) + is.True(ns1 != ns2) + + all, err = r.All() + + is.NoErr(err) + is.Equal(len(all), 2) + + is.Equal(ns.ID, int64(23)) + + ns3, _ := r.GetByName("n2") + + is.Equal(ns3, ns2) + + is.NoErr(r.Delete(ns2.ID)) + + all, err = r.All() + + is.NoErr(err) + is.Equal(len(all), 1) +} diff --git a/dataswamp/namespace/valueobjects.go b/dataswamp/namespace/valueobjects.go index d47f04f..2675c36 100644 --- a/dataswamp/namespace/valueobjects.go +++ b/dataswamp/namespace/valueobjects.go @@ -34,6 +34,7 @@ func (f *FileSizeQuota) Remove(size int64) error { } type Usage struct { + Stored int64 Uploads int64 UploadB int64 Downloads int64 @@ -42,6 +43,7 @@ type Usage struct { func (u Usage) Uploaded(size int64) Usage { return Usage{ + u.Stored, u.Uploads + 1, u.UploadB + size, u.Downloads, @@ -51,6 +53,7 @@ func (u Usage) Uploaded(size int64) Usage { func (u Usage) Downloaded(size int64) Usage { return Usage{ + u.Stored, u.Uploads, u.UploadB, u.Downloads + 1, diff --git a/dataswamp/services.go b/dataswamp/services.go index a9fae27..c9ea65e 100644 --- a/dataswamp/services.go +++ b/dataswamp/services.go @@ -38,7 +38,7 @@ func (s SwampFileService) getOrCreateNs(namespace_in string) *namespace.Namespac time.Now(), s.default_allowance_duration, namespace.FileSizeQuota{s.default_allowance_bytes, 0}, - namespace.Usage{0, 0, 0, 0}, + namespace.Usage{0, 0, 0, 0, 0}, } created_ns, err := s.namespace_repo.Create(new_ns) @@ -93,6 +93,7 @@ func (s SwampFileService) SaveFile(ref swampfile.FileReference, src io.Reader, s f.Close() ns.FileQuota.Add(size) + ns.Usage = ns.Usage.Downloaded(size) s.namespace_repo.Update(ns.ID, *ns) return nil diff --git a/infrastructure/memory/namespace/repository.go b/infrastructure/memory/namespace/repository.go index 3f662c5..02128df 100644 --- a/infrastructure/memory/namespace/repository.go +++ b/infrastructure/memory/namespace/repository.go @@ -11,7 +11,7 @@ type Repository struct { NextId int64 } -func NewRepository() *Repository { +func NewRepository() namespace.Repository { r := new(Repository) r.NextId = 0 r.IdIdx = make(map[int64]*namespace.Namespace) diff --git a/infrastructure/memory/namespace/repository_test.go b/infrastructure/memory/namespace/repository_test.go index e68cc1a..8720769 100644 --- a/infrastructure/memory/namespace/repository_test.go +++ b/infrastructure/memory/namespace/repository_test.go @@ -2,49 +2,9 @@ package namespace import ( "caj-larsson/bog/dataswamp/namespace" - "github.com/matryer/is" "testing" - "time" ) -func TestUserAgentRepo(t *testing.T) { - is := is.New(t) - r := NewRepository() - - all, err := r.All() - - is.NoErr(err) - is.Equal(len(all), 0) - - ns := namespace.Namespace{ - 23, - "n1", - time.Now(), - time.Duration(time.Hour * 3), - namespace.FileSizeQuota{1000, 0}, - namespace.Usage{0, 0, 0, 0}, - } - - ns1, _ := r.Create(ns) - ns.Name = "n2" - ns2, _ := r.Create(ns) - is.True(ns1 != ns2) - - all, err = r.All() - - is.NoErr(err) - is.Equal(len(all), 2) - - is.Equal(ns.ID, int64(23)) - - ns3, _ := r.GetByName("n2") - - is.Equal(ns3, ns2) - - is.NoErr(r.Delete(ns2.ID)) - - all, err = r.All() - - is.NoErr(err) - is.Equal(len(all), 1) +func TestFileRepo(t *testing.T) { + namespace.RepositoryContract(NewRepository, t) } diff --git a/infrastructure/sqlite/namespace/repository.go b/infrastructure/sqlite/namespace/repository.go index 434b4ec..191e985 100644 --- a/infrastructure/sqlite/namespace/repository.go +++ b/infrastructure/sqlite/namespace/repository.go @@ -11,7 +11,7 @@ type Repository struct { db *sql.DB } -func NewRepository(filename string) *Repository { +func NewRepository(filename string) namespace.Repository { db, err := sql.Open("sqlite3", filename) if err != nil { panic(err) diff --git a/infrastructure/sqlite/namespace/repository_test.go b/infrastructure/sqlite/namespace/repository_test.go new file mode 100644 index 0000000..7477352 --- /dev/null +++ b/infrastructure/sqlite/namespace/repository_test.go @@ -0,0 +1,16 @@ +package namespace + +import ( + "caj-larsson/bog/dataswamp/namespace" + "path" + "testing" +) + +func TestFileRepo(t *testing.T) { + fac := func() namespace.Repository { + d := t.TempDir() + db_path := path.Join(d, "db.sql") + return NewRepository(db_path) + } + namespace.RepositoryContract(fac, t) +}