-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add credit allocation api and update functional test (#8)
* test refactor * add creditallocation api * new creditallocation unit test * add ResourceProviderAccount API * remove auth for now * hyperlinked creditallocation * functional test updated
- Loading branch information
Showing
14 changed files
with
554 additions
and
183 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
coral_credits/api/migrations/0006_alter_consumer_unique_together.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Generated by Django 5.0.6 on 2024-07-24 17:44 | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("api", "0005_consumer_consumer_uuid_consumer_user_ref"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterUniqueTogether( | ||
name="consumer", | ||
unique_together={("consumer_uuid", "resource_provider_account")}, | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from django.urls import reverse | ||
import pytest | ||
from rest_framework import status | ||
|
||
import coral_credits.api.models as models | ||
|
||
|
||
@pytest.fixture | ||
def request_data(): | ||
return { | ||
"inventories": {"VCPU": 50, "MEMORY_MB": 2000, "DISK_GB": 1000}, | ||
} | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_credit_allocation_resource_create_success( | ||
credit_allocation, | ||
resource_classes, | ||
api_client, | ||
request_data, | ||
): | ||
|
||
# Prepare data for the API call | ||
url = reverse( | ||
"allocation-resource-list", kwargs={"allocation_pk": credit_allocation.id} | ||
) | ||
|
||
# Make the API call | ||
response = api_client.post(url, request_data, format="json") | ||
|
||
# Check that the request was successful | ||
assert response.status_code == status.HTTP_200_OK, ( | ||
f"Expected {status.HTTP_200_OK}. " | ||
f"Actual status {response.status_code}. " | ||
f"Response text {response.content}" | ||
) | ||
|
||
# Check database entries are as expected | ||
# First check we have the number that we expect | ||
total_cars = models.CreditAllocationResource.objects.filter( | ||
allocation=credit_allocation | ||
) | ||
assert len(total_cars) == len(request_data["inventories"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
from datetime import datetime, timedelta | ||
|
||
from django.utils.timezone import make_aware | ||
import pytest | ||
from rest_framework.test import APIClient | ||
|
||
import coral_credits.api.models as models | ||
|
||
|
||
def pytest_configure(config): | ||
config.PROJECT_ID = "20354d7a-e4fe-47af-8ff6-187bca92f3f9" | ||
config.USER_REF = "caa8b54a-eb5e-4134-8ae2-a3946a428ec7" | ||
config.START_DATE = make_aware(datetime.now()) | ||
config.END_DATE = config.START_DATE + timedelta(days=1) | ||
|
||
|
||
# Fixtures defining all the necessary database entries for testing | ||
@pytest.fixture | ||
def resource_classes(): | ||
vcpu = models.ResourceClass.objects.create(name="VCPU") | ||
memory = models.ResourceClass.objects.create(name="MEMORY_MB") | ||
disk = models.ResourceClass.objects.create(name="DISK_GB") | ||
return vcpu, memory, disk | ||
|
||
|
||
@pytest.fixture | ||
def provider(): | ||
return models.ResourceProvider.objects.create( | ||
name="Test Provider", | ||
email="[email protected]", | ||
info_url="https://testprovider.com", | ||
) | ||
|
||
|
||
@pytest.fixture | ||
def account(): | ||
return models.CreditAccount.objects.create(email="[email protected]", name="test") | ||
|
||
|
||
@pytest.fixture | ||
def resource_provider_account(request, account, provider): | ||
return models.ResourceProviderAccount.objects.create( | ||
account=account, provider=provider, project_id=request.config.PROJECT_ID | ||
) | ||
|
||
|
||
@pytest.fixture | ||
def credit_allocation(account, request): | ||
return models.CreditAllocation.objects.create( | ||
account=account, | ||
name="test", | ||
start=request.config.START_DATE, | ||
end=request.config.END_DATE, | ||
) | ||
|
||
|
||
@pytest.fixture | ||
def api_client(): | ||
return APIClient() | ||
|
||
|
||
@pytest.fixture | ||
def create_credit_allocation_resources(): | ||
# Factory fixture | ||
def _create_credit_allocation_resources( | ||
credit_allocation, resource_classes, allocation_hours | ||
): | ||
vcpu, memory, disk = resource_classes | ||
vcpu_allocation = models.CreditAllocationResource.objects.create( | ||
allocation=credit_allocation, | ||
resource_class=vcpu, | ||
resource_hours=allocation_hours["vcpu"], | ||
) | ||
memory_allocation = models.CreditAllocationResource.objects.create( | ||
allocation=credit_allocation, | ||
resource_class=memory, | ||
resource_hours=allocation_hours["memory"], | ||
) | ||
disk_allocation = models.CreditAllocationResource.objects.create( | ||
allocation=credit_allocation, | ||
resource_class=disk, | ||
resource_hours=allocation_hours["disk"], | ||
) | ||
return (vcpu_allocation, memory_allocation, disk_allocation) | ||
|
||
return _create_credit_allocation_resources |
Oops, something went wrong.