Skip to content

Commit

Permalink
GITC-478: Adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nutrina committed Dec 6, 2021
1 parent bbc0d40 commit 91e9485
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 7 deletions.
Empty file.
19 changes: 19 additions & 0 deletions app/git/tests/factories/git_cache_factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import factory
import pytest
from git.models import GitCache


@pytest.mark.django_db
class GitCacheFactory(factory.django.DjangoModelFactory):
class Meta:
model = GitCache

# Unique user handle
handle = factory.Sequence(lambda n: f"user_handle_{n}")

# Cycle through the choices and select one
category = factory.Sequence(lambda n: GitCache.CATEGORY_CHOICES[n % len(GitCache.CATEGORY_CHOICES)][0])

# Generate binary data depending on n
data = factory.Sequence(lambda n: ("{n}" * 100).encode("utf-8"))

Empty file.
40 changes: 40 additions & 0 deletions app/git/tests/models/test_git_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from git.tests.factories.git_cache_factory import GitCacheFactory
import pytest
from git.models import GitCache


@pytest.mark.django_db
class TestGitCache:
"""Test CLRMatch model."""

def test_creation(self):
"""Test GitCache returned by factory is valid."""

git_cache = GitCacheFactory()

assert isinstance(git_cache, GitCache)

def test_get_user(self):
"""Test get_user helper function."""

git_cache = GitCacheFactory()
git_cache.category = GitCache.Category.USER
handle = git_cache.handle
git_cache.save()

saved = GitCache.get_user(handle)
assert git_cache.id == saved.id

def test_update_data(self):
"""Test update_data helper function."""

git_cache = GitCacheFactory()
git_cache.category = GitCache.Category.USER
handle = git_cache.handle
git_cache.save()

new_data = "This is updated data".encode("utf-8")
git_cache.update_data(new_data)

saved = GitCache.get_user(handle)
assert new_data == saved.data.tobytes()
64 changes: 62 additions & 2 deletions app/git/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,21 @@
"""
from datetime import timedelta
from urllib.parse import quote_plus, urlencode
from unittest.mock import MagicMock, patch
from github import NamedUser

from django.conf import settings
from django.test.utils import override_settings
from django.utils import timezone
from git.models import GitCache
from git.tests.factories.git_cache_factory import GitCacheFactory
from faker import Faker

import responses
from git.utils import (
HEADERS, TOKEN_URL, build_auth_dict, delete_issue_comment, get_github_emails, get_github_primary_email,
get_issue_comments, get_issue_timeline_events, is_github_token_valid, org_name, patch_issue_comment,
post_issue_comment, post_issue_comment_reaction, repo_url, reset_token, revoke_token,
get_issue_comments, get_issue_timeline_events, github_connect, is_github_token_valid, org_name, patch_issue_comment,
post_issue_comment, post_issue_comment_reaction, repo_url, reset_token, revoke_token, _get_user
)
from test_plus.test import TestCase

Expand Down Expand Up @@ -230,3 +235,58 @@ def test_is_github_token_valid(self):
# post_issue_comment_reaction(owner, repo, comment_id, 'A comment.')

# assert responses.calls[0].request.url == url

@responses.activate
@patch('git.utils.github_connect')
def test_get_user_caching(self, mock_github_connect):
"""Test the github utility _get_user method."""
fake = Faker()

# Create a dummy handle and some binary data that is supposed to be the serialized user
user_handle = fake.user_name()
user_binary_data = fake.text().encode('utf-8')

def dump_mock(user_obj, file_obj):
file_obj.write(user_binary_data)

gh_user = MagicMock(spec=NamedUser)
gh_user.update = MagicMock()

# Mock the gh client
gh_client = mock_github_connect()
gh_client.load = MagicMock(return_value=gh_user)
gh_client.dump = dump_mock
gh_client.get_user = MagicMock(return_value=gh_user)

# Step 1: Make the call
_get_user(gh_client, user_handle)

# Verify what was called and that the user has been cached:
# - expected: get_user
# - not expected: loaded, update - because user is new
gh_client.get_user.assert_called_once_with(user_handle)
gh_client.load.assert_not_called()
gh_user.update.assert_not_called()

# Verify that user has been cached
saved_user = GitCache.get_user(user_handle)

assert saved_user.handle == user_handle
assert saved_user.data.tobytes() == user_binary_data

# Step 2: Repeat the call, user should be leaded from DB
gh_client.reset_mock()
gh_client.load.reset_mock()
gh_client.get_user.reset_mock()

loaded_user = _get_user(gh_client, user_handle)

# Verify what was called and that the user has been cached:
# - expected: load and update (for the user loaded from DB)
# - not expected: get_user (because user is already in DB)
gh_client.load.assert_called_once()
assert gh_client.load.call_args[0][0].getbuffer() == user_binary_data
gh_client.get_user.assert_not_called()
gh_user.update.assert_called_once()

assert loaded_user == gh_user
5 changes: 0 additions & 5 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ requests_oauthlib
Pillow==8.3.2
premailer
populus
psycopg2-binary==2.7.5
PyJWT==1.5.3
PyPdf2
python-twitter==3.2
sendgrid==5.6.0
Expand All @@ -44,15 +42,13 @@ google-api-python-client==1.7.3
django-environ==0.4.5
eth-utils==1.4.1
jsondiff==1.1.1
social-auth-app-django==5.0.0
django-ipware==2.0.2
geoip2==2.8.0
django-silk==2.0.0
django-extensions==2.2.1
ipdb==0.13.9
django-autotranslate==1.1.1
svgutils==0.3.0
watchdog==0.9.0
Werkzeug[watchdog]==0.15.5
imageio
boto3==1.18.22
Expand Down Expand Up @@ -109,7 +105,6 @@ pyPEG2==2.15.2
base58==2.1.0
libnacl==1.7.2
pyaes==1.6.1
numpy==1.19.5
rjsmin==1.1.0
rcssmin==1.0.6
libsass==0.20.1
Expand Down

0 comments on commit 91e9485

Please sign in to comment.