github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/model_test.go (about)

     1  package internal
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func TestFinishStage(t *testing.T) {
    10  	operation, error := NewProvisioningOperation("1", ProvisioningParameters{})
    11  	assert.NoError(t, error)
    12  	assert.NotEmpty(t, operation)
    13  
    14  	t.Run("should not add empty stage", func(t *testing.T) {
    15  		stage := ""
    16  		operation.FinishStage(stage)
    17  		foundStages := countStageOccurrences(operation, stage)
    18  		assert.Equal(t, 0, foundStages)
    19  	})
    20  
    21  	t.Run("should add one unique stage", func(t *testing.T) {
    22  		stage := "start"
    23  		operation.FinishStage(stage)
    24  		foundStages := countStageOccurrences(operation, stage)
    25  		assert.Equal(t, 1, foundStages)
    26  	})
    27  
    28  	t.Run("should not add duplicated stages", func(t *testing.T) {
    29  		stage := "create_runtime"
    30  		operation.FinishStage(stage)
    31  		operation.FinishStage(stage)
    32  		foundStages := countStageOccurrences(operation, stage)
    33  		assert.Equal(t, 1, foundStages)
    34  	})
    35  
    36  	t.Run("should add two distinct stages", func(t *testing.T) {
    37  		stage1 := "start"
    38  		stage2 := "create_runtime"
    39  		operation.FinishStage(stage1)
    40  		operation.FinishStage(stage2)
    41  		foundStages1 := countStageOccurrences(operation, stage1)
    42  		foundStages2 := countStageOccurrences(operation, stage2)
    43  		assert.Equal(t, 1, foundStages1)
    44  		assert.Equal(t, 1, foundStages2)
    45  	})
    46  }
    47  
    48  func countStageOccurrences(operation ProvisioningOperation, stage string) int {
    49  	foundStages := 0
    50  	for _, v := range operation.FinishedStages {
    51  		if v == stage {
    52  			foundStages++
    53  		}
    54  	}
    55  	return foundStages
    56  }