Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add tests for the current resources #9

Merged
merged 1 commit into from
Aug 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions resources/pool_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package resources_test

import (
"github.com/RackHD/ipam/models"
. "github.com/RackHD/ipam/resources"
"gopkg.in/mgo.v2/bson"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("PoolCreator", func() {
It("should return a PoolV1 resource by default", func() {
resource, err := PoolCreator("")
Expect(err).ToNot(HaveOccurred())
Expect(resource).To(BeAssignableToTypeOf(&PoolV1{}))
})
})

var _ = Describe("PoolV1", func() {
var (
resource = PoolV1{
ID: bson.NewObjectId().Hex(),
Name: "PoolV1 Name",
Tags: []string{"PoolV1"},
Metadata: "PoolV1 Metadata",
}
model = models.Pool{
ID: bson.NewObjectId(),
Name: "Pool Name",
Tags: []string{"Pool"},
Metadata: "Pool Metadata",
}
)

Describe("Type", func() {
It("should return the correct resource type", func() {
Expect(resource.Type()).To(Equal(PoolResourceType))
})
})

Describe("Version", func() {
It("should return the correct resource version", func() {
Expect(resource.Version()).To(Equal(PoolResourceVersionV1))
})
})

Describe("Marshal", func() {
It("should copy the models.Pool to itself", func() {
err := resource.Marshal(model)
Expect(err).ToNot(HaveOccurred())

Expect(resource.ID).To(Equal(model.ID.Hex()))
Expect(resource.Name).To(Equal(model.Name))
Expect(resource.Tags).To(Equal(model.Tags))
Expect(resource.Metadata).To(Equal(model.Metadata))
})

It("should return an error if a model.Pool is not provided", func() {
err := resource.Marshal("invalid")
Expect(err).To(HaveOccurred())
})
})

Describe("Unmarshal", func() {
It("should copy itself to a models.Pool", func() {
m, err := resource.Unmarshal()
Expect(err).ToNot(HaveOccurred())
Expect(m).To(BeAssignableToTypeOf(models.Pool{}))

if result, ok := m.(models.Pool); ok {
Expect(result.ID.Hex()).To(Equal(resource.ID))
Expect(result.Name).To(Equal(resource.Name))
Expect(result.Tags).To(Equal(resource.Tags))
Expect(result.Metadata).To(Equal(resource.Metadata))
}
})

It("should generate a new object ID if one is not present", func() {
resource.ID = ""

m, err := resource.Unmarshal()
Expect(err).ToNot(HaveOccurred())
Expect(m).To(BeAssignableToTypeOf(models.Pool{}))

if result, ok := m.(models.Pool); ok {
Expect(result.ID.Hex()).To(Equal(resource.ID))
Expect(result.Name).To(Equal(resource.Name))
Expect(result.Tags).To(Equal(resource.Tags))
Expect(result.Metadata).To(Equal(resource.Metadata))
}
})
})
})
70 changes: 70 additions & 0 deletions resources/pools_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package resources_test

import (
"github.com/RackHD/ipam/models"
. "github.com/RackHD/ipam/resources"
"gopkg.in/mgo.v2/bson"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("PoolsCreator", func() {
It("should return a PoolsV1 resource by default", func() {
resource, err := PoolsCreator("")
Expect(err).ToNot(HaveOccurred())
Expect(resource).To(BeAssignableToTypeOf(&PoolsV1{}))
})
})

var _ = Describe("PoolsV1", func() {
var (
resource = PoolsV1{}
pools = []models.Pool{
{
ID: bson.NewObjectId(),
Name: "PoolV1 Name",
Tags: []string{"PoolV1"},
Metadata: "PoolV1 Metadata",
},
}
)

Describe("Type", func() {
It("should return the correct resource type", func() {
Expect(resource.Type()).To(Equal(PoolsResourceType))
})
})

Describe("Version", func() {
It("should return the correct resource version", func() {
Expect(resource.Version()).To(Equal(PoolsResourceVersionV1))
})
})

Describe("Marshal", func() {
It("should copy the []models.Pool to itself", func() {
err := resource.Marshal(pools)
Expect(err).ToNot(HaveOccurred())

Expect(len(resource.Pools)).To(Equal(1))

Expect(resource.Pools[0].ID).To(Equal(pools[0].ID.Hex()))
Expect(resource.Pools[0].Name).To(Equal(pools[0].Name))
Expect(resource.Pools[0].Tags).To(Equal(pools[0].Tags))
Expect(resource.Pools[0].Metadata).To(Equal(pools[0].Metadata))
})

It("should return an error if a []model.Pool is not provided", func() {
err := resource.Marshal("invalid")
Expect(err).To(HaveOccurred())
})
})

Describe("Unmarshal", func() {
It("should return an error because the operation is not supported", func() {
_, err := resource.Unmarshal()
Expect(err).To(HaveOccurred())
})
})
})
94 changes: 94 additions & 0 deletions resources/reservation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package resources_test

import (
"github.com/RackHD/ipam/models"
. "github.com/RackHD/ipam/resources"
"gopkg.in/mgo.v2/bson"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("ReservationCreator", func() {
It("should return a ReservationV1 resource by default", func() {
resource, err := ReservationCreator("")
Expect(err).ToNot(HaveOccurred())
Expect(resource).To(BeAssignableToTypeOf(&ReservationV1{}))
})
})

var _ = Describe("ReservationV1", func() {
var (
resource = ReservationV1{
ID: bson.NewObjectId().Hex(),
Name: "ReservationV1 Name",
Tags: []string{"ReservationV1"},
Metadata: "ReservationV1 Metadata",
}
model = models.Reservation{
ID: bson.NewObjectId(),
Name: "Reservation Name",
Tags: []string{"Reservation"},
Metadata: "Reservation Metadata",
}
)

Describe("Type", func() {
It("should return the correct resource type", func() {
Expect(resource.Type()).To(Equal(ReservationResourceType))
})
})

Describe("Version", func() {
It("should return the correct resource version", func() {
Expect(resource.Version()).To(Equal(ReservationResourceVersionV1))
})
})

Describe("Marshal", func() {
It("should copy the models.Reservation to itself", func() {
err := resource.Marshal(model)
Expect(err).ToNot(HaveOccurred())

Expect(resource.ID).To(Equal(model.ID.Hex()))
Expect(resource.Name).To(Equal(model.Name))
Expect(resource.Tags).To(Equal(model.Tags))
Expect(resource.Metadata).To(Equal(model.Metadata))
})

It("should return an error if a model.Reservation is not provided", func() {
err := resource.Marshal("invalid")
Expect(err).To(HaveOccurred())
})
})

Describe("Unmarshal", func() {
It("should copy itself to a models.Reservation", func() {
m, err := resource.Unmarshal()
Expect(err).ToNot(HaveOccurred())
Expect(m).To(BeAssignableToTypeOf(models.Reservation{}))

if result, ok := m.(models.Reservation); ok {
Expect(result.ID.Hex()).To(Equal(resource.ID))
Expect(result.Name).To(Equal(resource.Name))
Expect(result.Tags).To(Equal(resource.Tags))
Expect(result.Metadata).To(Equal(resource.Metadata))
}
})

It("should generate a new object ID if one is not present", func() {
resource.ID = ""

m, err := resource.Unmarshal()
Expect(err).ToNot(HaveOccurred())
Expect(m).To(BeAssignableToTypeOf(models.Reservation{}))

if result, ok := m.(models.Reservation); ok {
Expect(result.ID.Hex()).To(Equal(resource.ID))
Expect(result.Name).To(Equal(resource.Name))
Expect(result.Tags).To(Equal(resource.Tags))
Expect(result.Metadata).To(Equal(resource.Metadata))
}
})
})
})
70 changes: 70 additions & 0 deletions resources/reservations_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package resources_test

import (
"github.com/RackHD/ipam/models"
. "github.com/RackHD/ipam/resources"
"gopkg.in/mgo.v2/bson"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("ReservationsCreator", func() {
It("should return a ReservationsV1 resource by default", func() {
resource, err := ReservationsCreator("")
Expect(err).ToNot(HaveOccurred())
Expect(resource).To(BeAssignableToTypeOf(&ReservationsV1{}))
})
})

var _ = Describe("ReservationsV1", func() {
var (
resource = ReservationsV1{}
reservations = []models.Reservation{
{
ID: bson.NewObjectId(),
Name: "ReservationV1 Name",
Tags: []string{"ReservationV1"},
Metadata: "ReservationV1 Metadata",
},
}
)

Describe("Type", func() {
It("should return the correct resource type", func() {
Expect(resource.Type()).To(Equal(ReservationsResourceType))
})
})

Describe("Version", func() {
It("should return the correct resource version", func() {
Expect(resource.Version()).To(Equal(ReservationsResourceVersionV1))
})
})

Describe("Marshal", func() {
It("should copy the []models.Reservation to itself", func() {
err := resource.Marshal(reservations)
Expect(err).ToNot(HaveOccurred())

Expect(len(resource.Reservations)).To(Equal(1))

Expect(resource.Reservations[0].ID).To(Equal(reservations[0].ID.Hex()))
Expect(resource.Reservations[0].Name).To(Equal(reservations[0].Name))
Expect(resource.Reservations[0].Tags).To(Equal(reservations[0].Tags))
Expect(resource.Reservations[0].Metadata).To(Equal(reservations[0].Metadata))
})

It("should return an error if a []model.Reservation is not provided", func() {
err := resource.Marshal("invalid")
Expect(err).To(HaveOccurred())
})
})

Describe("Unmarshal", func() {
It("should return an error because the operation is not supported", func() {
_, err := resource.Unmarshal()
Expect(err).To(HaveOccurred())
})
})
})
13 changes: 13 additions & 0 deletions resources/resources_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package resources_test

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"testing"
)

func TestResources(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Resources Suite")
}
Loading