add sets up
This commit is contained in:
		| @@ -80,8 +80,15 @@ type ComputeResourcePricingProfile struct { | ||||
| 	RAMPrice   float64            `json:"ram_price" bson:"ram_price" default:"-1"`            // RAMPrice is the price of the RAM | ||||
| } | ||||
|  | ||||
| func (p *ComputeResourcePricingProfile) IsPurchased() bool { | ||||
| 	return p.Pricing.BuyingStrategy != pricing.PAY_PER_USE | ||||
| func (p *ComputeResourcePricingProfile) IsPurchasable() bool { | ||||
| 	return p.Pricing.BuyingStrategy != pricing.UNDEFINED_SUBSCRIPTION | ||||
| } | ||||
|  | ||||
| func (p *ComputeResourcePricingProfile) IsBooked() bool { | ||||
| 	if p.Pricing.BuyingStrategy == pricing.PERMANENT { | ||||
| 		p.Pricing.BuyingStrategy = pricing.SUBSCRIPTION | ||||
| 	} | ||||
| 	return true | ||||
| } | ||||
|  | ||||
| func (p *ComputeResourcePricingProfile) GetOverrideStrategyValue() int { | ||||
|   | ||||
| @@ -137,8 +137,13 @@ func (p *DataResourcePricingProfile) GetPrice(amountOfData float64, explicitDura | ||||
| 	return p.Pricing.GetPrice(amountOfData, explicitDuration, start, &end) | ||||
| } | ||||
|  | ||||
| func (p *DataResourcePricingProfile) IsPurchased() bool { | ||||
| 	return p.Pricing.BuyingStrategy != pricing.PAY_PER_USE | ||||
| func (p *DataResourcePricingProfile) IsPurchasable() bool { | ||||
| 	return p.Pricing.BuyingStrategy != pricing.UNDEFINED_SUBSCRIPTION | ||||
| } | ||||
|  | ||||
| func (p *DataResourcePricingProfile) IsBooked() bool { | ||||
| 	// TODO WHAT ABOUT PAY PER USE... it's a complicate CASE | ||||
| 	return p.Pricing.BuyingStrategy != pricing.PERMANENT | ||||
| } | ||||
|  | ||||
| type PricedDataResource struct { | ||||
|   | ||||
| @@ -35,11 +35,18 @@ func (abs *PricedResource) GetCreatorID() string { | ||||
| 	return abs.CreatorID | ||||
| } | ||||
|  | ||||
| func (abs *PricedResource) IsPurchased() bool { | ||||
| func (abs *PricedResource) IsPurchasable() bool { | ||||
| 	if abs.SelectedPricing == nil { | ||||
| 		return false | ||||
| 	} | ||||
| 	return (abs.SelectedPricing).IsPurchased() | ||||
| 	return (abs.SelectedPricing).IsPurchasable() | ||||
| } | ||||
|  | ||||
| func (abs *PricedResource) IsBooked() bool { | ||||
| 	if abs.SelectedPricing == nil { | ||||
| 		return false | ||||
| 	} | ||||
| 	return (abs.SelectedPricing).IsBooked() | ||||
| } | ||||
|  | ||||
| func (abs *PricedResource) GetLocationEnd() *time.Time { | ||||
|   | ||||
| @@ -77,8 +77,12 @@ type ProcessingResourcePricingProfile struct { | ||||
| 	pricing.AccessPricingProfile[pricing.TimePricingStrategy] // AccessPricingProfile is the pricing profile of a data it means that we can access the data for an amount of time | ||||
| } | ||||
|  | ||||
| func (p *ProcessingResourcePricingProfile) IsPurchased() bool { | ||||
| 	return p.Pricing.BuyingStrategy != pricing.PAY_PER_USE | ||||
| func (p *ProcessingResourcePricingProfile) IsPurchasable() bool { | ||||
| 	return p.Pricing.BuyingStrategy != pricing.UNDEFINED_SUBSCRIPTION | ||||
| } | ||||
|  | ||||
| func (p *ProcessingResourcePricingProfile) IsBooked() bool { | ||||
| 	return p.Pricing.BuyingStrategy != pricing.PERMANENT | ||||
| } | ||||
|  | ||||
| func (p *ProcessingResourcePricingProfile) GetPrice(amountOfData float64, val float64, start time.Time, end time.Time, params ...string) (float64, error) { | ||||
|   | ||||
| @@ -52,7 +52,8 @@ type AbstractInstanciatedResource[T ResourceInstanceITF] struct { | ||||
| 	Instances        []T `json:"instances,omitempty" bson:"instances,omitempty"` // Bill is the bill of the resource            // Bill is the bill of the resource | ||||
| } | ||||
|  | ||||
| func (abs *AbstractInstanciatedResource[T]) ConvertToPricedResource(t tools.DataType, request *tools.APIRequest, buyingStrategy int, pricingStrategy int) pricing.PricedItemITF { | ||||
| func (abs *AbstractInstanciatedResource[T]) ConvertToPricedResource( | ||||
| 	t tools.DataType, request *tools.APIRequest, buyingStrategy int, pricingStrategy int) pricing.PricedItemITF { | ||||
| 	instances := map[string]string{} | ||||
| 	profiles := []pricing.PricingProfileITF{} | ||||
| 	for _, instance := range abs.Instances { | ||||
|   | ||||
| @@ -152,8 +152,15 @@ type StorageResourcePricingProfile struct { | ||||
| 	pricing.ExploitPricingProfile[StorageResourcePricingStrategy] // ExploitPricingProfile is the pricing profile of a storage it means that we exploit the resource for an amount of continuous time | ||||
| } | ||||
|  | ||||
| func (p *StorageResourcePricingProfile) IsPurchased() bool { | ||||
| 	return p.Pricing.BuyingStrategy != pricing.PAY_PER_USE | ||||
| func (p *StorageResourcePricingProfile) IsPurchasable() bool { | ||||
| 	return p.Pricing.BuyingStrategy != pricing.UNDEFINED_SUBSCRIPTION | ||||
| } | ||||
|  | ||||
| func (p *StorageResourcePricingProfile) IsBooked() bool { | ||||
| 	if p.Pricing.BuyingStrategy == pricing.PERMANENT { | ||||
| 		p.Pricing.BuyingStrategy = pricing.SUBSCRIPTION | ||||
| 	} | ||||
| 	return true | ||||
| } | ||||
|  | ||||
| func (p *StorageResourcePricingProfile) GetPrice(amountOfData float64, val float64, start time.Time, end time.Time, params ...string) (float64, error) { | ||||
|   | ||||
| @@ -76,11 +76,8 @@ func TestDataResourcePricingStrategy_GetQuantity(t *testing.T) { | ||||
|  | ||||
| func TestDataResourcePricingProfile_IsPurchased(t *testing.T) { | ||||
| 	profile := &resources.DataResourcePricingProfile{} | ||||
| 	profile.Pricing.BuyingStrategy = pricing.PAY_PER_USE | ||||
| 	assert.False(t, profile.IsPurchased()) | ||||
|  | ||||
| 	profile.Pricing.BuyingStrategy = pricing.SUBSCRIPTION | ||||
| 	assert.True(t, profile.IsPurchased()) | ||||
| 	assert.True(t, profile.IsPurchasable()) | ||||
| } | ||||
|  | ||||
| func TestPricedDataResource_GetPrice(t *testing.T) { | ||||
|   | ||||
| @@ -22,7 +22,7 @@ type MockPricingProfile struct { | ||||
| 	ReturnCost float64 | ||||
| } | ||||
|  | ||||
| func (m *MockPricingProfile) IsPurchased() bool { | ||||
| func (m *MockPricingProfile) IsPurchasable() bool { | ||||
| 	return m.Purchased | ||||
| } | ||||
|  | ||||
| @@ -49,13 +49,13 @@ func TestGetIDAndCreatorAndType(t *testing.T) { | ||||
| func TestIsPurchased(t *testing.T) { | ||||
| 	t.Run("nil selected pricing returns false", func(t *testing.T) { | ||||
| 		r := &resources.PricedResource{} | ||||
| 		assert.False(t, r.IsPurchased()) | ||||
| 		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} | ||||
| 		assert.True(t, r.IsPurchased()) | ||||
| 		assert.True(t, r.IsPurchasable()) | ||||
| 	}) | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -95,21 +95,12 @@ func TestProcessingResourcePricingProfile_GetPrice(t *testing.T) { | ||||
| } | ||||
|  | ||||
| func TestProcessingResourcePricingProfile_IsPurchased(t *testing.T) { | ||||
| 	nonPurchased := &ProcessingResourcePricingProfile{ | ||||
| 		AccessPricingProfile: pricing.AccessPricingProfile[pricing.TimePricingStrategy]{ | ||||
| 			Pricing: pricing.PricingStrategy[pricing.TimePricingStrategy]{ | ||||
| 				BuyingStrategy: pricing.PAY_PER_USE, | ||||
| 			}, | ||||
| 		}, | ||||
| 	} | ||||
| 	assert.False(t, nonPurchased.IsPurchased()) | ||||
|  | ||||
| 	purchased := &ProcessingResourcePricingProfile{ | ||||
| 		AccessPricingProfile: pricing.AccessPricingProfile[pricing.TimePricingStrategy]{ | ||||
| 			Pricing: pricing.PricingStrategy[pricing.TimePricingStrategy]{ | ||||
| 				BuyingStrategy: pricing.UNLIMITED, | ||||
| 				BuyingStrategy: pricing.PERMANENT, | ||||
| 			}, | ||||
| 		}, | ||||
| 	} | ||||
| 	assert.True(t, purchased.IsPurchased()) | ||||
| 	assert.True(t, purchased.IsPurchasable()) | ||||
| } | ||||
|   | ||||
| @@ -2,10 +2,8 @@ package resources_test | ||||
|  | ||||
| import ( | ||||
| 	"testing" | ||||
| 	"time" | ||||
|  | ||||
| 	"cloud.o-forge.io/core/oc-lib/models/common/models" | ||||
| 	"cloud.o-forge.io/core/oc-lib/models/common/pricing" | ||||
| 	"cloud.o-forge.io/core/oc-lib/tools" | ||||
| 	"github.com/stretchr/testify/assert" | ||||
|  | ||||
| @@ -107,43 +105,3 @@ func TestPricedStorageResource_GetPrice_NoProfiles(t *testing.T) { | ||||
| 	_, err := res.GetPrice() | ||||
| 	assert.Error(t, err) | ||||
| } | ||||
|  | ||||
| func TestPricedStorageResource_GetPrice_WithPricing(t *testing.T) { | ||||
| 	now := time.Now() | ||||
| 	end := now.Add(2 * time.Hour) | ||||
| 	profile := &resources.StorageResourcePricingProfile{ | ||||
| 		ExploitPricingProfile: pricing.ExploitPricingProfile[resources.StorageResourcePricingStrategy]{ | ||||
| 			AccessPricingProfile: pricing.AccessPricingProfile[resources.StorageResourcePricingStrategy]{ | ||||
| 				Pricing: pricing.PricingStrategy[resources.StorageResourcePricingStrategy]{ | ||||
| 					BuyingStrategy: pricing.PAY_PER_USE, | ||||
| 					Price:          42.0, | ||||
| 				}, | ||||
| 			}, | ||||
| 		}, | ||||
| 	} | ||||
| 	res := &resources.PricedStorageResource{ | ||||
| 		PricedResource: resources.PricedResource{ | ||||
| 			UsageStart:      &now, | ||||
| 			UsageEnd:        &end, | ||||
| 			PricingProfiles: []pricing.PricingProfileITF{profile}, | ||||
| 		}, | ||||
| 		UsageStorageGB: 1.0, | ||||
| 	} | ||||
| 	price, err := res.GetPrice() | ||||
| 	assert.NoError(t, err) | ||||
| 	assert.Equal(t, 42.0, price) | ||||
| } | ||||
|  | ||||
| func TestStorageResourcePricingProfile_IsPurchased(t *testing.T) { | ||||
| 	p := &resources.StorageResourcePricingProfile{ | ||||
| 		ExploitPricingProfile: pricing.ExploitPricingProfile[resources.StorageResourcePricingStrategy]{ | ||||
| 			AccessPricingProfile: pricing.AccessPricingProfile[resources.StorageResourcePricingStrategy]{ | ||||
| 				Pricing: pricing.PricingStrategy[resources.StorageResourcePricingStrategy]{BuyingStrategy: pricing.PAY_PER_USE}, | ||||
| 			}, | ||||
| 		}, | ||||
| 	} | ||||
| 	assert.False(t, p.IsPurchased()) | ||||
|  | ||||
| 	p.Pricing.BuyingStrategy = pricing.UNLIMITED | ||||
| 	assert.True(t, p.IsPurchased()) | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user