github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/domain/operation/converter_test.go (about)

     1  package operation_test
     2  
     3  import (
     4  	"database/sql"
     5  	"testing"
     6  
     7  	"github.com/kyma-incubator/compass/components/director/internal/domain/operation"
     8  	"github.com/kyma-incubator/compass/components/director/internal/model"
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestEntityConverter_ToEntity(t *testing.T) {
    14  	t.Run("success all nullable properties filled", func(t *testing.T) {
    15  		// GIVEN
    16  		opModel := fixOperationModel(ordOpType, model.OperationStatusScheduled)
    17  		require.NotNil(t, opModel)
    18  
    19  		conv := operation.NewConverter()
    20  
    21  		// WHEN
    22  		entity := conv.ToEntity(opModel)
    23  
    24  		// THEN
    25  		expectedOperation := fixEntityOperation(operationID, ordOpType, model.OperationStatusScheduled)
    26  
    27  		assert.Equal(t, expectedOperation, entity)
    28  	})
    29  	t.Run("success all nullable properties empty", func(t *testing.T) {
    30  		// GIVEN
    31  		opModel := &model.Operation{
    32  			ID:         operationID,
    33  			OpType:     ordOpType,
    34  			Status:     model.OperationStatusScheduled,
    35  			Data:       nil,
    36  			Error:      nil,
    37  			Priority:   1,
    38  			CreatedAt:  nil,
    39  			FinishedAt: nil,
    40  		}
    41  
    42  		expectedEntity := &operation.Entity{
    43  			ID:         operationID,
    44  			Type:       ordOpType,
    45  			Status:     string(model.OperationStatusScheduled),
    46  			Data:       sql.NullString{},
    47  			Error:      sql.NullString{},
    48  			Priority:   1,
    49  			CreatedAt:  nil,
    50  			FinishedAt: nil,
    51  		}
    52  		conv := operation.NewConverter()
    53  
    54  		// WHEN
    55  		entity := conv.ToEntity(opModel)
    56  
    57  		// THEN
    58  		assert.Equal(t, expectedEntity, entity)
    59  	})
    60  }
    61  
    62  func TestEntityConverter_FromEntity(t *testing.T) {
    63  	t.Run("success all nullable properties filled", func(t *testing.T) {
    64  		// GIVEN
    65  		entity := fixEntityOperation(operationID, ordOpType, model.OperationStatusScheduled)
    66  		conv := operation.NewConverter()
    67  
    68  		// WHEN
    69  		opModel := conv.FromEntity(entity)
    70  
    71  		// THEN
    72  		expectedOperation := fixOperationModel(ordOpType, model.OperationStatusScheduled)
    73  		assert.Equal(t, expectedOperation, opModel)
    74  	})
    75  
    76  	t.Run("success all nullable properties empty", func(t *testing.T) {
    77  		// GIVEN
    78  		entity := &operation.Entity{
    79  			ID:         operationID,
    80  			Type:       ordOpType,
    81  			Status:     string(model.OperationStatusScheduled),
    82  			Data:       sql.NullString{},
    83  			Error:      sql.NullString{},
    84  			Priority:   1,
    85  			CreatedAt:  nil,
    86  			FinishedAt: nil,
    87  		}
    88  		expectedModel := &model.Operation{
    89  			ID:         operationID,
    90  			OpType:     ordOpType,
    91  			Status:     model.OperationStatusScheduled,
    92  			Data:       nil,
    93  			Error:      nil,
    94  			Priority:   1,
    95  			CreatedAt:  nil,
    96  			FinishedAt: nil,
    97  		}
    98  		conv := operation.NewConverter()
    99  
   100  		// WHEN
   101  		opModel := conv.FromEntity(entity)
   102  
   103  		// THEN
   104  		assert.Equal(t, expectedModel, opModel)
   105  	})
   106  }