Skip to content

Commit 4b1f484

Browse files
committed
Fixed unit tests and linting
1 parent f7b5d68 commit 4b1f484

File tree

8 files changed

+117
-110
lines changed

8 files changed

+117
-110
lines changed

coral_credits/api/db_utils.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,9 @@ def get_all_active_reservations(resource_provider_account):
7070

7171

7272
def get_credit_allocation(id):
73-
now = timezone.now()
7473

7574
credit_allocation = models.CreditAllocation.objects.filter(id=id).first()
76-
if credit_allocation == None:
75+
if credit_allocation is None:
7776
raise db_exceptions.NoCreditAllocation("Invalid allocation_id")
7877

7978
return credit_allocation
@@ -342,14 +341,21 @@ def create_credit_resource_allocations(credit_allocation, resource_allocations):
342341
car, created = models.CreditAllocationResource.objects.get_or_create(
343342
allocation=credit_allocation,
344343
resource_class=resource_class,
345-
defaults={"resource_hours": resource_hours, "allocated_resource_hours": resource_hours},
344+
defaults={
345+
"resource_hours": resource_hours,
346+
"allocated_resource_hours": resource_hours,
347+
},
346348
)
347349
# If exists, update:
348350
if not created:
349351
newly_allocated_resource_hours = resource_hours
350-
car.resource_hours += newly_allocated_resource_hours - car.allocated_resource_hours
352+
car.resource_hours += (
353+
newly_allocated_resource_hours - car.allocated_resource_hours
354+
)
351355
if car.resource_hours < 0:
352-
raise db_exceptions.InsufficientCredits("Cannot set credits to fewer than currently consumed")
356+
raise db_exceptions.InsufficientCredits(
357+
"Cannot set credits to fewer than currently consumed"
358+
)
353359
car.allocated_resource_hours = newly_allocated_resource_hours
354360
car.save()
355361

coral_credits/api/serializers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class CreditAllocationResourceSerializer(serializers.ModelSerializer):
4646

4747
class Meta:
4848
model = models.CreditAllocationResource
49-
fields = ["id", "resource_class", "resource_hours","allocated_resource_hours"]
49+
fields = ["id", "resource_class", "resource_hours", "allocated_resource_hours"]
5050

5151
def to_representation(self, instance):
5252
"""Pass the context to the ResourceClassSerializer"""

coral_credits/api/tests/allocation_tests.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ def test_credit_allocation_resource_create_success(
3131
response = api_client.post(url, request_data, format="json", secure=True)
3232

3333
# Check that the request was successful
34-
assert response.status_code == status.HTTP_200_OK, (
35-
f"Expected {status.HTTP_200_OK}. "
34+
assert response.status_code == status.HTTP_201_CREATED, (
35+
f"Expected {status.HTTP_201_CREATED}. "
3636
f"Actual status {response.status_code}. "
3737
f"Response text {response.content}"
3838
)

coral_credits/api/tests/conftest.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,19 @@ def _create_credit_allocation_resources(
9898
allocation=credit_allocation,
9999
resource_class=vcpu,
100100
resource_hours=allocation_hours["VCPU"],
101+
allocated_resource_hours=allocation_hours["VCPU"],
101102
)
102103
memory_allocation = models.CreditAllocationResource.objects.create(
103104
allocation=credit_allocation,
104105
resource_class=memory,
105106
resource_hours=allocation_hours["MEMORY_MB"],
107+
allocated_resource_hours=allocation_hours["VCPU"],
106108
)
107109
disk_allocation = models.CreditAllocationResource.objects.create(
108110
allocation=credit_allocation,
109111
resource_class=disk,
110112
resource_hours=allocation_hours["DISK_GB"],
113+
allocated_resource_hours=allocation_hours["VCPU"],
111114
)
112115
return (vcpu_allocation, memory_allocation, disk_allocation)
113116

coral_credits/api/views.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ def _create_update_credit_allocations(self, request, allocation_pk):
7979
serializer = serializers.CreditAllocationResourceSerializer(
8080
updated_allocations, many=True, context={"request": request}
8181
)
82-
# To allow for the allocation resource endpoint to operate as a single REST object
83-
# with GET,POST,PATCH etc after creation of single item
82+
# To allow for the allocation resource endpoint to operate as a single
83+
# REST object with GET,POST,PATCH etc after creation of single item
8484
# When creating with multiple resources a list of multiple entries is returned,
8585
# each with their own unique ID
8686
if len(serializer.data) == 1:
@@ -161,7 +161,8 @@ def retrieve(self, request, pk=None):
161161

162162
# TODO(johngarbutt) look for any during the above allocations
163163

164-
# ISSUE (stuartc) this creates errors if objects are actually found when the account entry
164+
# ISSUE (stuartc) this creates errors if objects
165+
# are actually found when the account entry
165166
# is retrieved. Unable to serialise the list.
166167
all_allocations_query = models.CreditAllocation.objects.filter(account__pk=pk)
167168
allocations = serializers.CreditAllocationSerializer(
@@ -223,12 +224,14 @@ class ConsumerViewSet(viewsets.ModelViewSet):
223224
permission_classes = [permissions.IsAuthenticated]
224225
serializer_class = serializers.ConsumerRequestSerializer
225226

226-
# TODO: need to split the Consumer and ConsumerRequest logic really
227+
# TODO(wtripp180901): need to split the Consumer and ConsumerRequest logic really
227228
def retrieve(self, request, pk=None):
228229
consumer = get_object_or_404(self.queryset, pk=pk)
229230
serializer = serializers.Consumer(consumer, context={"request": request})
230231
return Response(serializer.data)
231232

233+
# TODO(wtripp180901): this doesn't seem consistent with what's
234+
# actually in the database
232235
def list(self, request):
233236
serializer = serializers.Consumer(
234237
self.queryset, many=True, context={"request": request}

tofu/tests/tofu_test_data.py

Lines changed: 54 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,80 @@
1-
from datetime import datetime
2-
from dateutil.relativedelta import relativedelta
3-
import uuid
4-
import json
51
import copy
2+
import json
3+
import uuid
64

7-
def get_standard_test_vars(q1_start,q1_end,q2_start,q2_end,q1_0_resources):
5+
6+
def get_standard_test_vars(q1_start, q1_end, q2_start, q2_end, q1_0_resources):
87
return {
98
"resource_provider_name": "Test Provider",
109
"resource_provider_email": "[email protected]",
1110
"resource_provider_info_url": "https://www.google.com",
12-
"accounts": json.dumps([
13-
{
14-
"name": "TestAccount1",
15-
"email": "[email protected]",
16-
"openstack_project_id": "c2eced313b324cdb8e670e6e30bf387d"
17-
},
18-
{
19-
"name": "TestAccount2",
20-
"email": "[email protected]",
21-
"openstack_project_id": "2fbf511968aa443e883a82283b0f0160"
22-
}
23-
]),
24-
"allocations": json.dumps({
25-
"Q1": {
26-
"start_date": str(q1_start),
27-
"end_date": str(q1_end),
28-
"projects": [
11+
"accounts": json.dumps(
12+
[
2913
{
30-
"account_email": "[email protected]",
31-
"resources": q1_0_resources
14+
"name": "TestAccount1",
15+
"email": "[email protected]",
16+
"openstack_project_id": "c2eced313b324cdb8e670e6e30bf387d",
3217
},
3318
{
34-
"account_email": "[email protected]",
35-
"resources": {
36-
"VCPU": "20000",
37-
"MEMORY_MB": "2000000",
38-
"DISK_GB": "200000"
39-
}
40-
}
41-
]
42-
},
43-
"Q2": {
44-
"start_date": str(q2_start),
45-
"end_date": str(q2_end),
46-
"projects": [
47-
{
48-
"account_email": "[email protected]",
49-
"resources": {
50-
"VCPU": "80000",
51-
"MEMORY_MB": "8000000",
52-
"DISK_GB": "300000"
53-
}
54-
}
55-
]
19+
"name": "TestAccount2",
20+
"email": "[email protected]",
21+
"openstack_project_id": "2fbf511968aa443e883a82283b0f0160",
22+
},
23+
]
24+
),
25+
"allocations": json.dumps(
26+
{
27+
"Q1": {
28+
"start_date": str(q1_start),
29+
"end_date": str(q1_end),
30+
"projects": [
31+
{
32+
"account_email": "[email protected]",
33+
"resources": q1_0_resources,
34+
},
35+
{
36+
"account_email": "[email protected]",
37+
"resources": {
38+
"VCPU": "20000",
39+
"MEMORY_MB": "2000000",
40+
"DISK_GB": "200000",
41+
},
42+
},
43+
],
44+
},
45+
"Q2": {
46+
"start_date": str(q2_start),
47+
"end_date": str(q2_end),
48+
"projects": [
49+
{
50+
"account_email": "[email protected]",
51+
"resources": {
52+
"VCPU": "80000",
53+
"MEMORY_MB": "8000000",
54+
"DISK_GB": "300000",
55+
},
56+
}
57+
],
58+
},
5659
}
57-
})
60+
),
5861
}
5962

63+
6064
def get_empty_test_data_copy(data):
6165
tmp = copy.deepcopy(data)
6266
tmp["allocations"] = "{}"
6367
tmp["accounts"] = "[]"
6468
return tmp
6569

70+
6671
def get_no_q1_copy(data):
6772
tmp = copy.deepcopy(data)
6873
tmp["allocations"] = json.dumps({"Q2": json.loads(tmp["allocations"])["Q2"]})
6974
return tmp
7075

71-
def get_lease_request_json(start,end):
76+
77+
def get_lease_request_json(start, end):
7278
start_time = start
7379
end_time = end
7480
return {

0 commit comments

Comments
 (0)