discount/internal/utils/transfer/audit_test.go
2023-07-04 04:04:31 +00:00

135 lines
4.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package transfer_test
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/types/known/timestamppb"
"penahub.gitlab.yandexcloud.net/pena-services/accruals-service/internal/models"
"penahub.gitlab.yandexcloud.net/pena-services/accruals-service/internal/proto/discount"
"penahub.gitlab.yandexcloud.net/pena-services/accruals-service/internal/utils/transfer"
)
func TestAuditModelToProto(t *testing.T) {
deletedAtModel := time.Unix(1674546287, 0).UTC()
deletedAtProto := timestamppb.Timestamp{Seconds: 1674546287}
t.Run("Перевод аудита с пустой модели в прототип", func(t *testing.T) {
assert.Equal(t, &discount.Audit{
CreatedAt: timestamppb.New(time.Time{}),
UpdatedAt: timestamppb.New(time.Time{}),
}, transfer.AuditModelToProto(&models.Audit{}))
})
t.Run("Перевод аудита с nil модели в прототип", func(t *testing.T) {
assert.Equal(t, &discount.Audit{CreatedAt: nil, UpdatedAt: nil}, transfer.AuditModelToProto(nil))
})
t.Run("Перевод аудита с модели, которая хранит дату удаления, но флаг deleted при этом false в прототип", func(t *testing.T) {
assert.Equal(t,
&discount.Audit{
UpdatedAt: &timestamppb.Timestamp{Seconds: 1674546287},
DeletedAt: &deletedAtProto,
Deleted: true,
CreatedAt: &timestamppb.Timestamp{Seconds: 1674546287},
},
transfer.AuditModelToProto(&models.Audit{
UpdatedAt: time.Unix(1674546287, 0).UTC(),
DeletedAt: &deletedAtModel,
Deleted: false,
CreatedAt: time.Unix(1674546287, 0).UTC(),
}),
)
})
t.Run("Перевод аудита с модели, которая хранит флаг deleted равный true, но при этом не имеет даты удаления в прототип", func(t *testing.T) {
assert.Equal(t,
&discount.Audit{
UpdatedAt: &timestamppb.Timestamp{Seconds: 1674546287},
CreatedAt: &timestamppb.Timestamp{Seconds: 1674546287},
},
transfer.AuditModelToProto(&models.Audit{
UpdatedAt: time.Unix(1674546287, 0).UTC(),
Deleted: true,
CreatedAt: time.Unix(1674546287, 0).UTC(),
}),
)
})
t.Run("Перевод аудита с модели без информации об удалении в прототип", func(t *testing.T) {
assert.Equal(t,
&discount.Audit{
UpdatedAt: &timestamppb.Timestamp{Seconds: 1674546287},
CreatedAt: &timestamppb.Timestamp{Seconds: 1674546287},
},
transfer.AuditModelToProto(&models.Audit{
UpdatedAt: time.Unix(1674546287, 0).UTC(),
CreatedAt: time.Unix(1674546287, 0).UTC(),
}),
)
})
t.Run("Перевод аудита с модели в прототип", func(t *testing.T) {
assert.Equal(t,
&discount.Audit{
UpdatedAt: &timestamppb.Timestamp{Seconds: 1674546287},
DeletedAt: &deletedAtProto,
Deleted: true,
CreatedAt: &timestamppb.Timestamp{Seconds: 1674546287},
},
transfer.AuditModelToProto(&models.Audit{
UpdatedAt: time.Unix(1674546287, 0).UTC(),
DeletedAt: &deletedAtModel,
Deleted: true,
CreatedAt: time.Unix(1674546287, 0).UTC(),
}),
)
})
}
func TestAuditProtoToModel(t *testing.T) {
deletedAtModel := time.Unix(1674546287, 0).UTC()
deletedAtProto := timestamppb.Timestamp{Seconds: 1674546287}
t.Run("Перевод аудита с пустого прототипа в модель", func(t *testing.T) {
assert.Equal(t, &models.Audit{}, transfer.AuditProtoToModel(&discount.Audit{}))
})
t.Run("Перевод аудита с nil прототипа в модель", func(t *testing.T) {
assert.Equal(t, &models.Audit{}, transfer.AuditProtoToModel(nil))
})
t.Run("Перевод аудита с прототипа без информации об удалении в модель", func(t *testing.T) {
assert.Equal(t,
&models.Audit{
UpdatedAt: time.Unix(1674546287, 0).UTC(),
CreatedAt: time.Unix(1674546287, 0).UTC(),
Deleted: true,
},
transfer.AuditProtoToModel(&discount.Audit{
UpdatedAt: &timestamppb.Timestamp{Seconds: 1674546287},
CreatedAt: &timestamppb.Timestamp{Seconds: 1674546287},
Deleted: true,
}),
)
})
t.Run("Перевод аудита с прототипа в модель", func(t *testing.T) {
assert.Equal(t,
&models.Audit{
UpdatedAt: time.Unix(1674546287, 0).UTC(),
DeletedAt: &deletedAtModel,
Deleted: true,
CreatedAt: time.Unix(1674546287, 0).UTC(),
},
transfer.AuditProtoToModel(&discount.Audit{
UpdatedAt: &timestamppb.Timestamp{Seconds: 1674546287},
DeletedAt: &deletedAtProto,
Deleted: true,
CreatedAt: &timestamppb.Timestamp{Seconds: 1674546287},
}),
)
})
}