Namespace repo contract

master
Caj Larsson 3 years ago
parent 443c9b9376
commit 1c15742710

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

@ -34,6 +34,7 @@ func (f *FileSizeQuota) Remove(size int64) error {
} }
type Usage struct { type Usage struct {
Stored int64
Uploads int64 Uploads int64
UploadB int64 UploadB int64
Downloads int64 Downloads int64
@ -42,6 +43,7 @@ type Usage struct {
func (u Usage) Uploaded(size int64) Usage { func (u Usage) Uploaded(size int64) Usage {
return Usage{ return Usage{
u.Stored,
u.Uploads + 1, u.Uploads + 1,
u.UploadB + size, u.UploadB + size,
u.Downloads, u.Downloads,
@ -51,6 +53,7 @@ func (u Usage) Uploaded(size int64) Usage {
func (u Usage) Downloaded(size int64) Usage { func (u Usage) Downloaded(size int64) Usage {
return Usage{ return Usage{
u.Stored,
u.Uploads, u.Uploads,
u.UploadB, u.UploadB,
u.Downloads + 1, u.Downloads + 1,

@ -38,7 +38,7 @@ func (s SwampFileService) getOrCreateNs(namespace_in string) *namespace.Namespac
time.Now(), time.Now(),
s.default_allowance_duration, s.default_allowance_duration,
namespace.FileSizeQuota{s.default_allowance_bytes, 0}, 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) 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() f.Close()
ns.FileQuota.Add(size) ns.FileQuota.Add(size)
ns.Usage = ns.Usage.Downloaded(size)
s.namespace_repo.Update(ns.ID, *ns) s.namespace_repo.Update(ns.ID, *ns)
return nil return nil

@ -11,7 +11,7 @@ type Repository struct {
NextId int64 NextId int64
} }
func NewRepository() *Repository { func NewRepository() namespace.Repository {
r := new(Repository) r := new(Repository)
r.NextId = 0 r.NextId = 0
r.IdIdx = make(map[int64]*namespace.Namespace) r.IdIdx = make(map[int64]*namespace.Namespace)

@ -2,49 +2,9 @@ package namespace
import ( import (
"caj-larsson/bog/dataswamp/namespace" "caj-larsson/bog/dataswamp/namespace"
"github.com/matryer/is"
"testing" "testing"
"time"
) )
func TestUserAgentRepo(t *testing.T) { func TestFileRepo(t *testing.T) {
is := is.New(t) namespace.RepositoryContract(NewRepository, 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)
} }

@ -11,7 +11,7 @@ type Repository struct {
db *sql.DB db *sql.DB
} }
func NewRepository(filename string) *Repository { func NewRepository(filename string) namespace.Repository {
db, err := sql.Open("sqlite3", filename) db, err := sql.Open("sqlite3", filename)
if err != nil { if err != nil {
panic(err) panic(err)

@ -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)
}
Loading…
Cancel
Save