Skip to content

Commit

Permalink
#4 move tests to their designated files
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-dammeier committed Nov 28, 2023
1 parent 24232ab commit 53ea0ce
Show file tree
Hide file tree
Showing 10 changed files with 169 additions and 162 deletions.
6 changes: 3 additions & 3 deletions pkg/domain/blueprintMaskV1.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ type BlueprintMaskV1 struct {
// Validate checks the structure and data of a blueprint mask and returns an error if there are any problems
func (blueprintMask *BlueprintMaskV1) Validate() error {
if blueprintMask.API == "" {
return errors.Errorf("could not validate mask API, mask API must not be empty")
return errors.Errorf("could not Validate mask API, mask API must not be empty")
}
if blueprintMask.ID == "" {
return errors.Errorf("could not validate mask ID, mask ID must not be empty")
return errors.Errorf("could not Validate mask ID, mask ID must not be empty")
}

err := blueprintMask.validateDogus()
Expand Down Expand Up @@ -60,7 +60,7 @@ func (blueprintMask *BlueprintMaskV1) validateDoguUniqueness() error {
for _, dogu := range blueprintMask.Dogus {
_, seen := seenDogu[dogu.Name]
if seen {
return errors.Errorf("could not validate blueprint mask, there is at least one duplicate for this dogu: %s", dogu.Name)
return errors.Errorf("could not Validate blueprint mask, there is at least one duplicate for this dogu: %s", dogu.Name)
}
seenDogu[dogu.Name] = true
}
Expand Down
22 changes: 11 additions & 11 deletions pkg/domain/blueprintV2.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,27 @@ type RegistryConfig map[string]map[string]interface{}
func (blueprint *BlueprintV2) Validate() error {
err := blueprint.validateDogus()
if err != nil {
return err
return errors.Wrap(err, "could not Validate blueprint")
}

err = blueprint.validateDoguUniqueness()
if err != nil {
return err
return errors.Wrap(err, "could not Validate blueprint")
}

err = blueprint.validateComponents()
if err != nil {
return err
return errors.Wrap(err, "could not Validate blueprint")
}

err = blueprint.validateComponentUniqueness()
if err != nil {
return err
return errors.Wrap(err, "could not Validate blueprint")
}

err = blueprint.validateRegistryConfig()
if err != nil {
return err
return errors.Wrap(err, "could not Validate blueprint")
}

return nil
Expand All @@ -65,7 +65,7 @@ func (blueprint *BlueprintV2) Validate() error {
func (blueprint *BlueprintV2) validateDogus() error {
for _, dogu := range blueprint.Dogus {

err := dogu.validate()
err := dogu.Validate()
if err != nil {
return err
}
Expand All @@ -78,15 +78,15 @@ func (blueprint *BlueprintV2) validateDoguUniqueness() error {
doguNames := Map(blueprint.Dogus, func(dogu TargetDogu) string { return dogu.Name })
duplicates := getDuplicates(doguNames)
if len(duplicates) != 0 {
return errors.Errorf("could not validate blueprint, there are duplicate dogus: %v", duplicates)
return errors.Errorf("could not Validate blueprint, there are duplicate dogus: %v", duplicates)
}
return nil
}

func (blueprint *BlueprintV2) validateComponents() error {
for _, component := range blueprint.Components {

err := component.validate()
err := component.Validate()
if err != nil {
return err
}
Expand All @@ -100,15 +100,15 @@ func (blueprint *BlueprintV2) validateComponentUniqueness() error {
componentNames := Map(blueprint.Components, func(component Component) string { return component.Name })
duplicates := getDuplicates(componentNames)
if len(duplicates) != 0 {
return errors.Errorf("could not validate blueprint, there are duplicate components: %v", duplicates)
return errors.Errorf("could not Validate blueprint, there are duplicate components: %v", duplicates)
}
return nil
}

func (blueprint *BlueprintV2) validateRegistryConfig() error {
for key, value := range blueprint.RegistryConfig {
if len(key) == 0 {
return errors.Errorf("could not validate blueprint, a config key is empty")
return errors.Errorf("could not Validate blueprint, a config key is empty")
}

err := validateKeysNotEmpty(value)
Expand All @@ -123,7 +123,7 @@ func (blueprint *BlueprintV2) validateRegistryConfig() error {
func validateKeysNotEmpty(config map[string]interface{}) error {
for key, value := range config {
if len(key) == 0 {
return errors.Errorf("could not validate blueprint, a config key is empty")
return errors.Errorf("could not Validate blueprint, a config key is empty")
}

switch vTyped := value.(type) {
Expand Down
130 changes: 5 additions & 125 deletions pkg/domain/blueprintV2_test.go
Original file line number Diff line number Diff line change
@@ -1,109 +1,12 @@
package domain

import (
"encoding/json"
"github.com/stretchr/testify/require"
"reflect"
"testing"

"github.com/stretchr/testify/assert"
)

func TestTargetState_String(t *testing.T) {
tests := []struct {
name string
state TargetState
want string
}{
{
"String() map enum to string",
TargetStatePresent,
"present",
},
{
"String() map enum to string",
TargetStateAbsent,
"absent",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.state.String(); got != tt.want {
t.Errorf("TargetState.String() = %v, want %v", got, tt.want)
}
})
}
}

func TestTargetState_MarshalJSON(t *testing.T) {
tests := []struct {
name string
state TargetState
want []byte
wantErr bool
}{
{
"MarshalJSON to bytes",
TargetStatePresent,
[]byte("\"present\""),
false,
},
{
"MarshalJSON to bytes",
TargetStateAbsent,
[]byte("\"absent\""),
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.state.MarshalJSON()
if (err != nil) != tt.wantErr {
t.Errorf("TargetState.MarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("TargetState.MarshalJSON() = %v, want %v", got, tt.want)
}
})
}
}

func TestTargetState_UnmarshalJSON_secondValue(t *testing.T) {
jsonBlob := []byte("\"absent\"")
var sut TargetState
err := json.Unmarshal(jsonBlob, &sut)

assert.Nil(t, err)
assert.Equal(t, TargetState(TargetStateAbsent), sut)
}

func TestTargetState_UnmarshalJSON_firstValue(t *testing.T) {
jsonBlob := []byte("\"present\"")
var sut TargetState
err := json.Unmarshal(jsonBlob, &sut)

assert.Nil(t, err)
assert.Equal(t, TargetState(TargetStatePresent), sut)
}

func TestTargetState_UnmarshalJSON_unknownValueParsesToFirstState(t *testing.T) {
jsonBlob := []byte("\"test\"")
var sut TargetState
err := json.Unmarshal(jsonBlob, &sut)

assert.Nil(t, err)
assert.Equal(t, TargetState(TargetStatePresent), sut)
}

func TestTargetState_UnmarshalJSON_error(t *testing.T) {
jsonBlob := []byte("test")
var sut TargetState
err := json.Unmarshal(jsonBlob, &sut)

assert.NotNil(t, err)
}

func Test_validate_ok(t *testing.T) {
dogus := []TargetDogu{
{Name: "absent/dogu1", Version: "3.2.1-0", TargetState: TargetStateAbsent},
Expand Down Expand Up @@ -135,30 +38,7 @@ func Test_validate_errorOnDoguProblem(t *testing.T) {
err := blueprint.Validate()

require.NotNil(t, err)
assert.Contains(t, err.Error(), "could not validate blueprint, dogu field Name must not be empty")
}
func Test_validateDogu_errorOnMissingDoguName(t *testing.T) {
dogus := []TargetDogu{
{Version: "3.2.1-2", TargetState: TargetStatePresent},
}
blueprint := BlueprintV2{Dogus: dogus}

err := blueprint.Validate()

require.NotNil(t, err)
assert.Contains(t, err.Error(), "could not validate blueprint, dogu field Name must not be empty")
}

func Test_validateDogu_errorOnEmptyDoguName(t *testing.T) {
dogus := []TargetDogu{
{Name: "", Version: "3.2.1-2", TargetState: TargetStatePresent},
}
blueprint := BlueprintV2{Dogus: dogus}

err := blueprint.Validate()

require.NotNil(t, err)
assert.Contains(t, err.Error(), "could not validate blueprint, dogu field Name must not be empty")
assert.Contains(t, err.Error(), "could not Validate blueprint: dogu field Name must not be empty")
}

func Test_validateDogus_ok(t *testing.T) {
Expand Down Expand Up @@ -200,7 +80,7 @@ func Test_validateComponents_ok(t *testing.T) {
require.Nil(t, err)
}

func Test_validateComponents_errorOnMissingDoguName(t *testing.T) {
func Test_validateComponents_errorOnMissingName(t *testing.T) {
components := []Component{
{Name: "absent-component", TargetState: TargetStateAbsent},
{Name: "present-component", Version: "3.2.1-2", TargetState: TargetStatePresent},
Expand All @@ -222,7 +102,7 @@ func Test_validateDoguUniqueness(t *testing.T) {
err := blueprint.validateDoguUniqueness()

require.NotNil(t, err)
assert.Contains(t, err.Error(), "could not validate blueprint, there are duplicate dogus: [present/dogu1]")
assert.Contains(t, err.Error(), "could not Validate blueprint, there are duplicate dogus: [present/dogu1]")
}

func Test_validateComponentUniqueness(t *testing.T) {
Expand All @@ -236,7 +116,7 @@ func Test_validateComponentUniqueness(t *testing.T) {
err := blueprint.validateComponentUniqueness()

require.NotNil(t, err)
assert.Contains(t, err.Error(), "could not validate blueprint, there are duplicate components: [present/component1]")
assert.Contains(t, err.Error(), "could not Validate blueprint, there are duplicate components: [present/component1]")
}

func TestValidationGlobalValid(t *testing.T) {
Expand Down Expand Up @@ -266,7 +146,7 @@ func TestValidationGlobalEmptyKeyError(t *testing.T) {
err := blueprint.Validate()
assert.NotNil(t, err)

assert.Contains(t, err.Error(), "could not validate blueprint, a config key is empty")
assert.Contains(t, err.Error(), "could not Validate blueprint, a config key is empty")
}

func TestSetDoguRegistryKeysSuccessful(t *testing.T) {
Expand Down
7 changes: 4 additions & 3 deletions pkg/domain/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ type Component struct {
TargetState TargetState `json:"targetState"`
}

func (component *Component) validate() error {
// Validate checks if the component is semantically correct.
func (component *Component) Validate() error {
if component.Name == "" {
return errors.Errorf("could not validate blueprint, component name must not be empty: %s", component)
return errors.Errorf("component name must not be empty: %s", component)
}
if component.TargetState != TargetStateAbsent && component.Version == "" {
return errors.Errorf("could not validate blueprint, component version must not be empty: %s", component)
return errors.Errorf("component version must not be empty: %s", component)
}

return nil
Expand Down
20 changes: 10 additions & 10 deletions pkg/domain/component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,51 +9,51 @@ import (
func Test_validateComponents_errorOnMissingComponentVersion(t *testing.T) {
component := Component{Name: "present-component", TargetState: TargetStatePresent}

err := component.validate()
err := component.Validate()

require.NotNil(t, err)
assert.Contains(t, err.Error(), "could not validate blueprint, component version must not be empty")
assert.Contains(t, err.Error(), "component version must not be empty")
}

func Test_validateComponents_errorOnEmptyComponentVersion(t *testing.T) {
component := Component{Name: "present/component", Version: "", TargetState: TargetStatePresent}

err := component.validate()
err := component.Validate()

require.NotNil(t, err)
assert.Contains(t, err.Error(), "could not validate blueprint, component version must not be empty")
assert.Contains(t, err.Error(), "component version must not be empty")
}

func Test_validateComponents_errorOnMissingComponentName(t *testing.T) {
component := Component{Version: "1.2.3", TargetState: TargetStatePresent}

err := component.validate()
err := component.Validate()

require.NotNil(t, err)
assert.Contains(t, err.Error(), "could not validate blueprint, component name must not be empty")
assert.Contains(t, err.Error(), "component name must not be empty")
}

func Test_validateComponents_errorOnEmptyComponentName(t *testing.T) {
component := Component{Name: "", Version: "1.2.3", TargetState: TargetStatePresent}

err := component.validate()
err := component.Validate()

require.NotNil(t, err)
assert.Contains(t, err.Error(), "could not validate blueprint, component name must not be empty")
assert.Contains(t, err.Error(), "component name must not be empty")
}

func Test_validateComponents_emptyComponentStateDefaultsToPresent(t *testing.T) {
component := Component{Name: "present-component", Version: "1.2.3"}

err := component.validate()
err := component.Validate()

require.Nil(t, err)
}

func Test_validateComponents_missingComponentVersionOkayForAbsent(t *testing.T) {
component := Component{Name: "present-component", TargetState: TargetStateAbsent}

err := component.validate()
err := component.Validate()

require.Nil(t, err)
}
4 changes: 2 additions & 2 deletions pkg/domain/maskTargetDogu.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ type MaskTargetDogu struct {

func (dogu MaskTargetDogu) validate() error {
if dogu.Name == "" {
return errors.Errorf("could not validate blueprint mask, dogu field Name must not be empty: %s", dogu)
return errors.Errorf("could not Validate blueprint mask, dogu field Name must not be empty: %s", dogu)
}
if dogu.TargetState.String() == "" {
return errors.Errorf("could not validate dogu, dogu target state must not be empty: %s", dogu)
return errors.Errorf("could not Validate dogu, dogu target state must not be empty: %s", dogu)
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/domain/maskTargetDogu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ func Test_validateMaskTargetDogu_errorOnMissingNameForDogu(t *testing.T) {
err := dogu.validate()

require.Error(t, err)
require.ErrorContains(t, err, "could not validate blueprint mask, dogu field Name must not be empty")
require.ErrorContains(t, err, "could not Validate blueprint mask, dogu field Name must not be empty")
}
Loading

0 comments on commit 53ea0ce

Please sign in to comment.