Use is. tests and fmt
parent
8a0e6b80e3
commit
e6e2f372dd
@ -1,57 +1,37 @@
|
||||
package namespace
|
||||
|
||||
import (
|
||||
"github.com/matryer/is"
|
||||
"testing"
|
||||
)
|
||||
|
||||
|
||||
func TestQuota(t *testing.T) {
|
||||
quota := FileSizeQuota { 1000, 0 }
|
||||
|
||||
if !quota.Allows(1000) {
|
||||
t.Errorf("It should allow filling completely")
|
||||
}
|
||||
if quota.Allows(1001) {
|
||||
t.Errorf("It should not allow filling completely")
|
||||
}
|
||||
is := is.New(t)
|
||||
quota := FileSizeQuota{1000, 0}
|
||||
|
||||
is.True(quota.Allows(1000))
|
||||
is.True(!quota.Allows(1001))
|
||||
}
|
||||
|
||||
func TestQuotaManipulation(t *testing.T) {
|
||||
quota := FileSizeQuota { 1000, 0 }
|
||||
is := is.New(t)
|
||||
quota := FileSizeQuota{1000, 0}
|
||||
|
||||
if quota.Add(500) != nil {
|
||||
t.Errorf("It should allow adding")
|
||||
}
|
||||
is.NoErr(quota.Add(500))
|
||||
|
||||
if quota.CurrentUsage != 500 {
|
||||
t.Errorf("It should add the usage correctly")
|
||||
}
|
||||
is.Equal(quota.CurrentUsage, int64(500))
|
||||
|
||||
if quota.Add(500) != nil {
|
||||
t.Errorf("It should allow adding up to the limit")
|
||||
}
|
||||
is.NoErr(quota.Add(500))
|
||||
|
||||
if quota.Add(1) != ErrExceedQuota {
|
||||
t.Errorf("It should not allow adding beyond limit")
|
||||
}
|
||||
is.Equal(quota.Add(1), ErrExceedQuota)
|
||||
|
||||
if quota.CurrentUsage != 1000 {
|
||||
t.Errorf("It should not overtaxed after failure to add")
|
||||
}
|
||||
is.Equal(quota.CurrentUsage, int64(1000))
|
||||
|
||||
if quota.Remove(1001) != ErrQuotaInvalid {
|
||||
t.Errorf("It should not allow reducing further than 0")
|
||||
}
|
||||
is.Equal(quota.Remove(1001), ErrQuotaInvalid)
|
||||
|
||||
if quota.CurrentUsage != 1000 {
|
||||
t.Errorf("It should not overtaxed after failure to remove")
|
||||
}
|
||||
is.Equal(quota.CurrentUsage, int64(1000))
|
||||
|
||||
if quota.Remove(1000) != nil {
|
||||
t.Errorf("It should allow reducing to 0")
|
||||
}
|
||||
is.NoErr(quota.Remove(1000))
|
||||
|
||||
if quota.CurrentUsage != 0 {
|
||||
t.Errorf("It should reduce accurately")
|
||||
}
|
||||
is.Equal(quota.CurrentUsage, int64(0))
|
||||
}
|
||||
|
@ -1,52 +1,43 @@
|
||||
package memory
|
||||
package namespace
|
||||
|
||||
import (
|
||||
"caj-larsson/bog/dataswamp/namespace"
|
||||
"github.com/matryer/is"
|
||||
"testing"
|
||||
"time"
|
||||
"caj-larsson/bog/dataswamp/namespace"
|
||||
)
|
||||
|
||||
func TestUserAgentRepo(t *testing.T) {
|
||||
is := is.New(t)
|
||||
r := NewRepository()
|
||||
|
||||
all, err := r.All()
|
||||
|
||||
if len(all) != 0 && err != nil {
|
||||
t.Errorf("New repo should be empty")
|
||||
}
|
||||
|
||||
ns := namespace.Namespace {23, "n1", time.Now(), time.Duration(time.Hour * 3), namespace.FileSizeQuota {1000, 0} }
|
||||
is.NoErr(err)
|
||||
is.Equal(len(all), 0)
|
||||
|
||||
ns := namespace.Namespace{23, "n1", time.Now(), time.Duration(time.Hour * 3), namespace.FileSizeQuota{1000, 0}}
|
||||
|
||||
ns1, _ := r.Create(ns)
|
||||
ns.Name = "n2"
|
||||
ns2, _ := r.Create(ns)
|
||||
if ns1 == ns2 {
|
||||
t.Errorf("Must create unique items")
|
||||
}
|
||||
is.True(ns1 != ns2)
|
||||
|
||||
all, err = r.All()
|
||||
|
||||
if len(all) != 2 && err != nil {
|
||||
t.Errorf("After adding there should be two Useragent")
|
||||
}
|
||||
is.NoErr(err)
|
||||
is.Equal(len(all), 2)
|
||||
|
||||
if ns.ID != 23 {
|
||||
t.Errorf("It does not change the original UserAgent")
|
||||
}
|
||||
is.Equal(ns.ID, int64(23))
|
||||
|
||||
ns3, _ := r.GetByName("n2")
|
||||
|
||||
if ns3 != ns2 {
|
||||
t.Errorf("It the correct ns is acquired")
|
||||
}
|
||||
is.Equal(ns3, ns2)
|
||||
|
||||
if r.Delete(ns2.ID) != nil {
|
||||
t.Errorf("Must delete without error")
|
||||
}
|
||||
is.NoErr(r.Delete(ns2.ID))
|
||||
|
||||
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,11 +1,10 @@
|
||||
package swampfile
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"caj-larsson/bog/dataswamp/swampfile"
|
||||
"testing"
|
||||
)
|
||||
|
||||
|
||||
func TestFileRepo(t *testing.T) {
|
||||
swampfile.RepositoryContract(NewRepository, t)
|
||||
}
|
||||
|
Loading…
Reference in New Issue