PricedItem evolved
This commit is contained in:
@@ -63,9 +63,16 @@ func TestPricedComputeResource_GetPriceHT(t *testing.T) {
|
||||
start := time.Now()
|
||||
end := start.Add(1 * time.Hour)
|
||||
r := resources.PricedComputeResource{
|
||||
PricedResource: resources.PricedResource{
|
||||
ResourceID: "comp456",
|
||||
SelectedPricing: &MockPricingProfile{ReturnCost: 1.0},
|
||||
PricedResource: resources.PricedResource[*resources.ComputeResourcePricingProfile]{
|
||||
ResourceID: "comp456",
|
||||
SelectedPricing: &resources.ComputeResourcePricingProfile{
|
||||
CPUsPrices: map[string]float64{"Xeon": 2.0},
|
||||
ExploitPricingProfile: pricing.ExploitPricingProfile[pricing.TimePricingStrategy]{
|
||||
AccessPricingProfile: pricing.AccessPricingProfile[pricing.TimePricingStrategy]{
|
||||
Pricing: pricing.PricingStrategy[pricing.TimePricingStrategy]{Price: 1.0},
|
||||
},
|
||||
},
|
||||
},
|
||||
BookingConfiguration: &resources.BookingConfiguration{
|
||||
UsageStart: &start,
|
||||
UsageEnd: &end,
|
||||
@@ -73,8 +80,8 @@ func TestPricedComputeResource_GetPriceHT(t *testing.T) {
|
||||
},
|
||||
},
|
||||
CPUsLocated: map[string]float64{"Xeon": 2},
|
||||
GPUsLocated: map[string]float64{"Tesla": 1},
|
||||
RAMLocated: 4,
|
||||
GPUsLocated: map[string]float64{},
|
||||
RAMLocated: 0,
|
||||
}
|
||||
|
||||
price, err := r.GetPriceHT()
|
||||
@@ -84,7 +91,7 @@ func TestPricedComputeResource_GetPriceHT(t *testing.T) {
|
||||
|
||||
func TestPricedComputeResource_GetPriceHT_MissingProfile(t *testing.T) {
|
||||
r := resources.PricedComputeResource{
|
||||
PricedResource: resources.PricedResource{
|
||||
PricedResource: resources.PricedResource[*resources.ComputeResourcePricingProfile]{
|
||||
ResourceID: "comp789",
|
||||
},
|
||||
}
|
||||
|
||||
@@ -76,7 +76,7 @@ func TestDataResourcePricingStrategy_GetQuantity(t *testing.T) {
|
||||
|
||||
func TestDataResourcePricingProfile_IsPurchased(t *testing.T) {
|
||||
profile := &resources.DataResourcePricingProfile{}
|
||||
profile.Pricing.BuyingStrategy = pricing.SUBSCRIPTION
|
||||
profile.Pricing.BuyingStrategy = pricing.PERMANENT
|
||||
assert.True(t, profile.IsPurchasable())
|
||||
}
|
||||
|
||||
@@ -91,7 +91,7 @@ func TestPricedDataResource_GetPriceHT(t *testing.T) {
|
||||
pricingProfile.Pricing.OverrideStrategy = resources.PER_GB_DOWNLOADED
|
||||
|
||||
r := &resources.PricedDataResource{
|
||||
PricedResource: resources.PricedResource{
|
||||
PricedResource: resources.PricedResource[*resources.DataResourcePricingProfile]{
|
||||
SelectedPricing: pricingProfile,
|
||||
BookingConfiguration: &resources.BookingConfiguration{
|
||||
UsageStart: &now,
|
||||
@@ -107,7 +107,7 @@ func TestPricedDataResource_GetPriceHT(t *testing.T) {
|
||||
|
||||
func TestPricedDataResource_GetPriceHT_NoProfiles(t *testing.T) {
|
||||
r := &resources.PricedDataResource{
|
||||
PricedResource: resources.PricedResource{
|
||||
PricedResource: resources.PricedResource[*resources.DataResourcePricingProfile]{
|
||||
ResourceID: "test-resource",
|
||||
},
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ func (m *MockPricingProfile) GetPriceHT(amount float64, explicitDuration float64
|
||||
// ---- Tests ----
|
||||
|
||||
func TestGetIDAndCreatorAndType(t *testing.T) {
|
||||
r := resources.PricedResource{
|
||||
r := resources.PricedResource[pricing.PricingProfileITF]{
|
||||
ResourceID: "res-123",
|
||||
CreatorID: "user-abc",
|
||||
ResourceType: tools.DATA_RESOURCE,
|
||||
@@ -48,19 +48,19 @@ func TestGetIDAndCreatorAndType(t *testing.T) {
|
||||
|
||||
func TestIsPurchased(t *testing.T) {
|
||||
t.Run("nil selected pricing returns false", func(t *testing.T) {
|
||||
r := &resources.PricedResource{}
|
||||
r := &resources.PricedResource[pricing.PricingProfileITF]{}
|
||||
assert.False(t, r.IsPurchasable())
|
||||
})
|
||||
|
||||
t.Run("returns true if pricing profile is purchased", func(t *testing.T) {
|
||||
mock := &MockPricingProfile{Purchased: true}
|
||||
r := &resources.PricedResource{SelectedPricing: mock}
|
||||
r := &resources.PricedResource[pricing.PricingProfileITF]{SelectedPricing: mock}
|
||||
assert.True(t, r.IsPurchasable())
|
||||
})
|
||||
}
|
||||
|
||||
func TestGetAndSetLocationStartEnd(t *testing.T) {
|
||||
r := &resources.PricedResource{}
|
||||
r := &resources.PricedResource[pricing.PricingProfileITF]{}
|
||||
|
||||
now := time.Now()
|
||||
r.SetLocationStart(now)
|
||||
@@ -72,7 +72,7 @@ func TestGetAndSetLocationStartEnd(t *testing.T) {
|
||||
|
||||
func TestGetExplicitDurationInS(t *testing.T) {
|
||||
t.Run("uses explicit duration if set", func(t *testing.T) {
|
||||
r := &resources.PricedResource{BookingConfiguration: &resources.BookingConfiguration{
|
||||
r := &resources.PricedResource[pricing.PricingProfileITF]{BookingConfiguration: &resources.BookingConfiguration{
|
||||
ExplicitBookingDurationS: 3600,
|
||||
},
|
||||
}
|
||||
@@ -82,7 +82,7 @@ func TestGetExplicitDurationInS(t *testing.T) {
|
||||
t.Run("computes duration from start and end", func(t *testing.T) {
|
||||
start := time.Now()
|
||||
end := start.Add(2 * time.Hour)
|
||||
r := &resources.PricedResource{
|
||||
r := &resources.PricedResource[pricing.PricingProfileITF]{
|
||||
BookingConfiguration: &resources.BookingConfiguration{
|
||||
UsageStart: &start, UsageEnd: &end,
|
||||
},
|
||||
@@ -91,14 +91,14 @@ func TestGetExplicitDurationInS(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("defaults to 1 hour when times not set", func(t *testing.T) {
|
||||
r := &resources.PricedResource{}
|
||||
r := &resources.PricedResource[pricing.PricingProfileITF]{}
|
||||
assert.InDelta(t, 3600.0, r.GetExplicitDurationInS(), 0.1)
|
||||
})
|
||||
}
|
||||
|
||||
func TestGetPriceHT(t *testing.T) {
|
||||
t.Run("returns error if no pricing profile", func(t *testing.T) {
|
||||
r := &resources.PricedResource{ResourceID: "no-profile"}
|
||||
r := &resources.PricedResource[pricing.PricingProfileITF]{ResourceID: "no-profile"}
|
||||
price, err := r.GetPriceHT()
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "pricing profile must be set")
|
||||
@@ -107,7 +107,7 @@ func TestGetPriceHT(t *testing.T) {
|
||||
|
||||
t.Run("defaults BookingConfiguration when nil", func(t *testing.T) {
|
||||
mock := &MockPricingProfile{ReturnCost: 42.0}
|
||||
r := &resources.PricedResource{
|
||||
r := &resources.PricedResource[pricing.PricingProfileITF]{
|
||||
SelectedPricing: mock,
|
||||
}
|
||||
price, err := r.GetPriceHT()
|
||||
@@ -119,7 +119,7 @@ func TestGetPriceHT(t *testing.T) {
|
||||
start := time.Now()
|
||||
end := start.Add(1 * time.Hour)
|
||||
mock := &MockPricingProfile{ReturnErr: true}
|
||||
r := &resources.PricedResource{
|
||||
r := &resources.PricedResource[pricing.PricingProfileITF]{
|
||||
SelectedPricing: mock,
|
||||
BookingConfiguration: &resources.BookingConfiguration{
|
||||
UsageStart: &start,
|
||||
@@ -135,7 +135,7 @@ func TestGetPriceHT(t *testing.T) {
|
||||
start := time.Now()
|
||||
end := start.Add(1 * time.Hour)
|
||||
mock := &MockPricingProfile{ReturnCost: 10.0}
|
||||
r := &resources.PricedResource{
|
||||
r := &resources.PricedResource[pricing.PricingProfileITF]{
|
||||
SelectedPricing: mock,
|
||||
BookingConfiguration: &resources.BookingConfiguration{
|
||||
UsageStart: &start,
|
||||
|
||||
@@ -40,7 +40,7 @@ func TestPricedProcessingResource_GetExplicitDurationInS(t *testing.T) {
|
||||
{
|
||||
name: "Nil start time, non-service",
|
||||
input: PricedProcessingResource{
|
||||
PricedResource: PricedResource{
|
||||
PricedResource: PricedResource[*ProcessingResourcePricingProfile]{
|
||||
BookingConfiguration: &resources.BookingConfiguration{
|
||||
UsageStart: nil,
|
||||
},
|
||||
@@ -51,7 +51,7 @@ func TestPricedProcessingResource_GetExplicitDurationInS(t *testing.T) {
|
||||
{
|
||||
name: "Duration computed from start and end",
|
||||
input: PricedProcessingResource{
|
||||
PricedResource: PricedResource{
|
||||
PricedResource: PricedResource[*ProcessingResourcePricingProfile]{
|
||||
BookingConfiguration: &resources.BookingConfiguration{
|
||||
UsageStart: &now,
|
||||
UsageEnd: &after,
|
||||
@@ -63,7 +63,7 @@ func TestPricedProcessingResource_GetExplicitDurationInS(t *testing.T) {
|
||||
{
|
||||
name: "Explicit duration takes precedence",
|
||||
input: PricedProcessingResource{
|
||||
PricedResource: PricedResource{
|
||||
PricedResource: PricedResource[*ProcessingResourcePricingProfile]{
|
||||
BookingConfiguration: &resources.BookingConfiguration{
|
||||
ExplicitBookingDurationS: 1337,
|
||||
},
|
||||
@@ -96,7 +96,7 @@ func TestProcessingResourcePricingProfile_GetPriceHT(t *testing.T) {
|
||||
},
|
||||
}
|
||||
profile := &ProcessingResourcePricingProfile{AccessPricingProfile: mockPricing}
|
||||
price, err := profile.GetPriceHT(0, 0, start, end, []*pricing.PricingVariation{})
|
||||
price, err := profile.GetPriceHT(1, 0, start, end, []*pricing.PricingVariation{})
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 100.0, price)
|
||||
}
|
||||
|
||||
@@ -81,8 +81,8 @@ func TestGetSelectedInstance_NoIndex(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCanUpdate_WhenOnlyStateDiffers(t *testing.T) {
|
||||
resource := &resources.AbstractResource{AbstractObject: utils.AbstractObject{IsDraft: false}}
|
||||
set := &MockDBObject{isDraft: true}
|
||||
resource := &resources.AbstractResource{AbstractObject: utils.AbstractObject{IsDraft: true}}
|
||||
set := &MockDBObject{isDraft: false}
|
||||
canUpdate, updated := resource.CanUpdate(set)
|
||||
assert.True(t, canUpdate)
|
||||
assert.Equal(t, set, updated)
|
||||
@@ -105,8 +105,13 @@ type FakeResource struct {
|
||||
resources.AbstractInstanciatedResource[*MockInstance]
|
||||
}
|
||||
|
||||
func (f *FakeResource) SetAllowedInstances(*tools.APIRequest, ...string) {}
|
||||
func (f *FakeResource) VerifyAuth(string, *tools.APIRequest) bool { return true }
|
||||
func (f *FakeResource) SetAllowedInstances(req *tools.APIRequest, instance_id ...string) []resources.ResourceInstanceITF {
|
||||
return nil
|
||||
}
|
||||
func (f *FakeResource) ConvertToPricedResource(t tools.DataType, a *int, b *int, c *int, d *int, e *int, req *tools.APIRequest) (pricing.PricedItemITF, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (f *FakeResource) VerifyAuth(string, *tools.APIRequest) bool { return true }
|
||||
|
||||
func TestNewAccessor_ReturnsValid(t *testing.T) {
|
||||
acc := resources.NewAccessor[*FakeResource](tools.COMPUTE_RESOURCE, &tools.APIRequest{}, func() utils.DBObject {
|
||||
|
||||
@@ -96,7 +96,7 @@ func TestStorageResourcePricingStrategy_GetQuantity_Invalid(t *testing.T) {
|
||||
|
||||
func TestPricedStorageResource_GetPriceHT_NoProfiles(t *testing.T) {
|
||||
res := &resources.PricedStorageResource{
|
||||
PricedResource: resources.PricedResource{
|
||||
PricedResource: resources.PricedResource[*resources.StorageResourcePricingProfile]{
|
||||
ResourceID: "res-id",
|
||||
},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user