Skip to content

Commit

Permalink
chore: unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
johnabass committed Dec 26, 2024
1 parent 3aa464e commit 2ad3e7a
Showing 1 changed file with 111 additions and 0 deletions.
111 changes: 111 additions & 0 deletions service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,117 @@ func (suite *ServiceSuite) TestHashBasicServiceTo() {
suite.NotZero(b.Len())
}

func (suite *ServiceSuite) testMapUpdateNil() {
var m Map[string, int] // nil Map
suite.Zero(m.Len())

expected := []string{"service1", "service2"}
visited := make([]string, 0, len(expected))
for u := range m.Update(expected...) {
suite.False(u.Exists)
suite.Zero(u.Value)
visited = append(visited, u.Service)
}

suite.Equal(expected, visited)
}

func (suite *ServiceSuite) testMapUpdateAllServicesExist() {
m := Map[string, int]{
"service1": 123,
"service2": 456,
"service3": 789,
}

suite.Equal(3, m.Len())

expected := []string{"service2", "service3"}
visited := make([]string, 0, len(expected))
for u := range m.Update(expected...) {
suite.True(u.Exists)
suite.Equal(m[u.Service], u.Value)
visited = append(visited, u.Service)
}

suite.Equal(expected, visited)
}

func (suite *ServiceSuite) testMapUpdateNoServicesExist() {
m := Map[string, int]{
"service1": 123,
"service2": 456,
"service3": 789,
}

suite.Equal(3, m.Len())

expected := []string{"service9", "service6", "service8"}
visited := make([]string, 0, len(expected))
for u := range m.Update(expected...) {
suite.False(u.Exists)
suite.Zero(u.Value)
visited = append(visited, u.Service)
}

suite.Equal(expected, visited)
}

func (suite *ServiceSuite) testMapUpdateSomeServicesExist() {
m := Map[string, int]{
"service1": 123,
"service2": 456,
"service3": 789,
}

suite.Equal(3, m.Len())

exists := make([]string, 0, 4)
notExists := make([]string, 0, 4)
for u := range m.Update("service4", "service3", "service8", "service1") {
if u.Exists {
suite.Equal(m[u.Service], u.Value)
exists = append(exists, u.Service)
} else {
suite.Zero(u.Value)
notExists = append(notExists, u.Service)
}
}

suite.Equal([]string{"service3", "service1"}, exists)
suite.Equal([]string{"service4", "service8"}, notExists)
}

func (suite *ServiceSuite) testMapUpdateBreak() {
m := Map[string, int]{
"service1": 123,
"service2": 456,
"service3": 789,
}

suite.Equal(3, m.Len())

visited := make([]string, 0, 1)
for u := range m.Update("first", "second") {
if u.Service != "first" {
break
}

visited = append(visited, u.Service)
}

suite.Equal([]string{"first"}, visited)
}

func (suite *ServiceSuite) TestMap() {
suite.Run("Update", func() {
suite.Run("Nil", suite.testMapUpdateNil)
suite.Run("AllServicesExist", suite.testMapUpdateAllServicesExist)
suite.Run("NoServicesExist", suite.testMapUpdateNoServicesExist)
suite.Run("SomeServicesExist", suite.testMapUpdateSomeServicesExist)
suite.Run("Break", suite.testMapUpdateBreak)
})
}

func TestService(t *testing.T) {
suite.Run(t, new(ServiceSuite))
}

0 comments on commit 2ad3e7a

Please sign in to comment.