-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Paul Baecher
committed
Mar 2, 2018
1 parent
24a9f8d
commit b1ca976
Showing
5 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package models | ||
|
||
import ( | ||
"errors" | ||
"time" | ||
|
||
"github.com/MEDIGO/laika/notifier" | ||
) | ||
|
||
type EnvironmentDeleted struct { | ||
Name string `json:"name"` | ||
} | ||
|
||
func (e *EnvironmentDeleted) Validate(s *State) (error, error) { | ||
if s.getEnvByName(e.Name) == nil { | ||
return errors.New("Bad environment"), nil | ||
} | ||
|
||
return nil, nil | ||
} | ||
|
||
func (e *EnvironmentDeleted) Update(s *State, t time.Time) *State { | ||
state := *s | ||
state.Environments = []Environment{} | ||
|
||
for _, env := range s.Environments { | ||
if env.Name != e.Name { | ||
state.Environments = append(state.Environments, env) | ||
} | ||
} | ||
|
||
for _, feature := range s.Features { | ||
delete(state.Enabled, EnvFeature{e.Name, feature.Name}) | ||
} | ||
|
||
return &state | ||
} | ||
|
||
func (e *EnvironmentDeleted) PrePersist(*State) (Event, error) { | ||
return e, nil | ||
} | ||
|
||
func (*EnvironmentDeleted) Notify(*State, notifier.Notifier) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package models | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestEnvironmentDeleted(t *testing.T) { | ||
var time time.Time | ||
s := NewState() | ||
|
||
s = requireValid(t, s, &EnvironmentCreated{Name: "e1"}).Update(s, time) | ||
s = requireValid(t, s, &EnvironmentCreated{Name: "e2"}).Update(s, time) | ||
s = requireValid(t, s, &FeatureCreated{Name: "f1"}).Update(s, time) | ||
s = requireValid(t, s, &FeatureCreated{Name: "f2"}).Update(s, time) | ||
s = requireValid(t, s, &FeatureCreated{Name: "f3"}).Update(s, time) | ||
s = requireValid(t, s, &FeatureToggled{Environment: "e1", Feature: "f2", Status: true}).Update(s, time) | ||
|
||
// successful deletion | ||
s = requireValid(t, s, &EnvironmentDeleted{Name: "e1"}).Update(s, time) | ||
|
||
require.Len(t, s.Environments, 1) | ||
require.Equal(t, "e2", s.Environments[0].Name) | ||
|
||
_, ok := s.Enabled[EnvFeature{Env: "e1", Feature: "f2"}] | ||
require.False(t, ok) | ||
|
||
// non-existing feature is invalid | ||
fd := &EnvironmentDeleted{Name: "e1"} | ||
valErr, err := fd.Validate(s) | ||
require.NoError(t, err) | ||
require.Error(t, valErr) | ||
|
||
// non-existing environment should not cause harm | ||
before := *s | ||
s = fd.Update(s, time) | ||
require.Equal(t, before, *s) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters