Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoacierno committed Mar 9, 2024
1 parent 7b68c36 commit 072c9b6
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 11 deletions.
3 changes: 1 addition & 2 deletions backend/google_api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ def get_available_credentials_token(
service: str, min_quota: int
) -> Optional["GoogleCloudToken"]:
credential = (
GoogleCloudOAuthCredential.objects.has_token()
.with_quota_left(service)
GoogleCloudOAuthCredential.objects.with_quota_left(service)
.annotate(
has_token=models.Exists(
GoogleCloudToken.objects.filter(
Expand Down
40 changes: 31 additions & 9 deletions backend/google_api/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,39 @@
import time_machine
from google_api.models import GoogleCloudOAuthCredential, UsedRequestQuota
import pytest


pytestmark = pytest.mark.django_db


# def test_get_available_credentials():
# credential = GoogleCloudOAuthCredential.objects.create(
# quota_limit_for_youtube=10_000,
# )
def test_with_quota_left():
credential = GoogleCloudOAuthCredential.objects.create(
quota_limit_for_youtube=10_000,
)

# found = GoogleCloudOAuthCredential.get_available_credentials(
# service="youtube",
# min_quota=1600,
# )
with time_machine.travel("2023-10-10 00:00:00", tick=False):
result = GoogleCloudOAuthCredential.objects.with_quota_left("youtube").get()
assert result.youtube_quota_left == 10_000

# assert found.id == credential.id
UsedRequestQuota.objects.create(
credentials=credential,
cost=1000,
service="youtube",
)

result = GoogleCloudOAuthCredential.objects.with_quota_left("youtube").get()
assert result.youtube_quota_left == 9_000

with time_machine.travel("2023-10-10 08:00:00", tick=False):
result = GoogleCloudOAuthCredential.objects.with_quota_left("youtube").get()
assert result.youtube_quota_left == 10_000


def test_get_by_client_id():
credential = GoogleCloudOAuthCredential.objects.create(client_id="test123")

assert (
GoogleCloudOAuthCredential.objects.get_by_client_id("test123").id
== credential.id
)
assert GoogleCloudOAuthCredential.objects.get_by_client_id("invalid") is None
53 changes: 53 additions & 0 deletions backend/google_api/tests/test_sdk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import datetime
import time_machine
from google_api.models import GoogleCloudOAuthCredential, GoogleCloudToken
from google_api.exceptions import NoGoogleCloudQuotaLeftError
from google_api.sdk import count_quota, get_available_credentials
import pytest

pytestmark = pytest.mark.django_db


def test_get_available_credentials(admin_user):
stored_credential = GoogleCloudOAuthCredential.objects.create()
GoogleCloudToken.objects.create(
oauth_credential=stored_credential, token="token", admin_user=admin_user
)

available_credentials = get_available_credentials("youtube", 1000)

assert available_credentials.token == "token"


def test_get_available_credentials_fails_when_no_quota_is_left(admin_user):
stored_credential = GoogleCloudOAuthCredential.objects.create(
quota_limit_for_youtube=500
)
GoogleCloudToken.objects.create(
oauth_credential=stored_credential, token="token", admin_user=admin_user
)

with pytest.raises(NoGoogleCloudQuotaLeftError):
get_available_credentials("youtube", 1000)


def test_count_quota(admin_user):
stored_credential = GoogleCloudOAuthCredential.objects.create()
GoogleCloudToken.objects.create(
oauth_credential=stored_credential, token="token", admin_user=admin_user
)

@count_quota("youtube", 1000)
def test_function(*, credentials):
return credentials

with time_machine.travel("2023-10-10 12:00:00", tick=False):
credentials = test_function()

assert credentials.token == "token"
assert stored_credential.usedrequestquota_set.count() == 1

used_quota = stored_credential.usedrequestquota_set.first()
assert used_quota.cost == 1000
assert used_quota.service == "youtube"
assert used_quota.used_at == datetime.datetime.now(tz=datetime.timezone.utc)

0 comments on commit 072c9b6

Please sign in to comment.