first commit

way too late.
This commit is contained in:
didier amyot committed 2015-02-06 22:03:05 -08:00
commit d4f7dc57fc
53 files changed
+3518

No files matched your search

@@ -0,0 +1,106 @@
package inmemoryconnector
import (
"testing"
"github.com/didiercrunch/doormanserver/doormen"
"gopkg.in/mgo.v2/bson"
)
func createMockDoorman(name string) *doormen.DoormanDefinition {
return doormen.QuickNewDoormanDefinition(name, 0.5, 0.5)
}
func TestNewInMemoryDBConnector(t *testing.T) {
c := NewInMemoryDBConnector()
if len(c.memory) != 0 {
t.Error("length at this point should be 0")
}
}
func TestSave(t *testing.T) {
c := NewInMemoryDBConnector()
if err := c.Save(nil); err != nil {
t.Error(err)
}
if len(c.memory) != 1 {
t.Fail()
}
}
func TestDeleteDoorman(t *testing.T) {
c := NewInMemoryDBConnector()
c.Save(createMockDoorman("a"))
c.Save(createMockDoorman("b"))
c.Save(createMockDoorman("c"))
if err := c.DeleteDoorman(c.memory[1].Id); err != nil {
t.Error()
}
if c.memory[0].Name != "a" || c.memory[1].Name != "c" {
t.Error()
}
if err := c.DeleteDoorman(c.memory[1].Id); err != nil {
t.Error()
}
if c.memory[0].Name != "a" {
t.Error()
}
if err := c.DeleteDoorman(c.memory[0].Id); err != nil {
t.Error()
}
if len(c.memory) != 0 {
t.Error()
}
}
func TestGetAllDoormen(t *testing.T) {
c := NewInMemoryDBConnector()
c.Save(createMockDoorman("a"))
c.Save(createMockDoorman("b"))
c.Save(createMockDoorman("c"))
i := 0
expt := []string{"a", "b", "c"}
for w := range c.GetAllDoormen() {
if w.Name != expt[i] {
t.Error(i)
}
i++
}
}
func TestGetDoorman(t *testing.T) {
c := NewInMemoryDBConnector()
wab := createMockDoorman("a")
c.Save(wab)
if w, err := c.GetDoorman(wab.Id); err != nil {
t.Error(err)
} else if w.Name != "a" {
t.Error()
}
if w, err := c.GetDoorman(createMockDoorman("b").Id); err != nil {
t.Error("missing doorman is not an error")
} else if w != nil {
t.Error("should be nil")
}
}
func TestExistsId(t *testing.T) {
c := NewInMemoryDBConnector()
w := createMockDoorman("a")
c.Save(w)
if !c.ExistsId(w.Id) {
t.Error()
}
if c.ExistsId(bson.NewObjectId()) {
t.Error()
}
}
+83
View File
@@ -0,0 +1,83 @@
package inmemoryconnector
import (
"errors"
"github.com/didiercrunch/doormanserver/doormen"
"gopkg.in/mgo.v2/bson"
)
type InMemoryDBConnector struct {
memory []*doormen.DoormanDefinition
}
func NewInMemoryDBConnector() *InMemoryDBConnector {
return &InMemoryDBConnector{make([]*doormen.DoormanDefinition, 0)}
}
func (c *InMemoryDBConnector) Save(dmd *doormen.DoormanDefinition) error {
c.memory = append(c.memory, dmd)
return nil
}
func (c *InMemoryDBConnector) Exists(dmd *doormen.DoormanDefinition) bool {
for _, w := range c.memory {
if w.Name == dmd.Name {
return true
}
}
return false
}
func (c *InMemoryDBConnector) ExistsId(id bson.ObjectId) bool {
for _, w := range c.memory {
if w.Id == id {
return true
}
}
return false
}
func (c *InMemoryDBConnector) GetAllDoormen() <-chan *doormen.DoormanId {
ch := make(chan *doormen.DoormanId)
go func() {
for _, s := range c.memory {
ch <- &doormen.DoormanId{Name: s.Name, Id: s.Id}
}
close(ch)
}()
return ch
}
func (c *InMemoryDBConnector) GetDoorman(id bson.ObjectId) (*doormen.DoormanDefinition, error) {
for _, doorman := range c.memory {
if doorman.Id == id {
return doorman, nil
}
}
return nil, nil
}
func (c *InMemoryDBConnector) DeleteDoorman(id bson.ObjectId) error {
toRemove := -1
for i, doorman := range c.memory {
if doorman.Id == id {
toRemove = i
break
}
}
c.memory = append(c.memory[0:toRemove], c.memory[toRemove+1:len(c.memory)]...)
return nil
}
func (c *InMemoryDBConnector) Update(dmd *doormen.DoormanDefinition) error {
for i, w := range c.memory {
if w.Id == dmd.Id {
c.memory[i] = dmd
return nil
}
}
return errors.New("nothing to update")
}