This repository has been archived by the owner on Aug 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
Mock network requests for faster unit testing #13 #84
Open
hydrosquall
wants to merge
4
commits into
sendgrid:master
Choose a base branch
from
hydrosquall:feature/ISSUE-13_mock-network-requests
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
vcrpy==1.11.1 |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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,32 @@ | ||
interactions: | ||
- request: | ||
body: '{"from": {"email": "DX Team <[email protected]>"}, "content": [{"value": | ||
"Data logged to DB on Heroku.", "type": "text/plain"}], "personalizations": | ||
[{"to": [{"email": "[email protected]"}]}], "subject": "Package | ||
Manager Open Source Downloads and GitHub Data Updated [Automated]"}' | ||
headers: | ||
Accept: [application/json] | ||
Authorization: [Bearer REDACTED] | ||
Connection: [close] | ||
Content-Length: ['291'] | ||
Content-Type: [application/json] | ||
Host: [api.sendgrid.com] | ||
User-Agent: [sendgrid/3.6.3;python] | ||
method: POST | ||
uri: https://api.sendgrid.com/v3/mail/send | ||
response: | ||
body: {string: ''} | ||
headers: | ||
Access-Control-Allow-Headers: ['Authorization, Content-Type, On-behalf-of, x-sg-elas-acl'] | ||
Access-Control-Allow-Methods: [POST] | ||
Access-Control-Allow-Origin: ['https://sendgrid.api-docs.io'] | ||
Access-Control-Max-Age: ['600'] | ||
Connection: [close] | ||
Content-Length: ['0'] | ||
Content-Type: [text/plain; charset=utf-8] | ||
Date: ['Mon, 23 Oct 2017 01:52:29 GMT'] | ||
Server: [nginx] | ||
X-Message-Id: [olIHMcpGScuj3gasX_OKuQ] | ||
X-No-CORS-Reason: ['https://sendgrid.com/docs/Classroom/Basics/API/cors.html'] | ||
status: {code: 202, message: Accepted} | ||
version: 1 |
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 |
---|---|---|
@@ -1,9 +1,18 @@ | ||
import os | ||
import datetime | ||
import vcr | ||
|
||
try: | ||
import unittest2 as unittest | ||
except ImportError: | ||
import unittest | ||
|
||
try: | ||
from mock import patch | ||
except ImportError: | ||
from unittest.mock import patch | ||
|
||
|
||
if os.environ.get('TRAVIS') is None: | ||
from db_connector import DBConnector, GitHubData, PackageManagerData | ||
from config import Config | ||
|
@@ -45,40 +54,68 @@ def test_initialization(self): | |
self.assertTrue(isinstance(self.config.email_subject, basestring)) | ||
self.assertTrue(isinstance(self.config.email_body, basestring)) | ||
|
||
# This dummy config prevents changes to the user-defined config | ||
# from changing their results when doing testing. | ||
MOCK_DATA = { | ||
'package_urls': [ | ||
"https://www.nuget.org/packages/SendGrid", | ||
"https://www.nuget.org/packages/SendGrid.CSharp.HTTP.Client", | ||
"https://www.npmjs.com/package/sendgrid", | ||
"https://www.npmjs.com/package/sendgrid-rest", | ||
"https://packagist.org/packages/sendgrid/sendgrid", | ||
"https://packagist.org/packages/sendgrid/php-http-client", | ||
"https://pypi.python.org/pypi/sendgrid", | ||
"https://pypi.python.org/pypi/python_http_client", | ||
"https://pypi.python.org/pypi/open_source_library_data_collector", | ||
"https://rubygems.org/gems/sendgrid-ruby", | ||
"https://rubygems.org/gems/ruby_http_client", | ||
], | ||
'github_config': { | ||
'user': 'sendgrid', | ||
'repositories': [ | ||
"sendgrid-csharp", | ||
# "smtpapi-csharp" | ||
] | ||
} | ||
} | ||
|
||
|
||
class TestDBConnector(unittest.TestCase): | ||
|
||
def setUp(self): | ||
if os.environ.get('TRAVIS') == None: | ||
self.db = DBConnector() | ||
|
||
def test_add_and_delete_data(self): | ||
def test_add_and_delete_github_data(self): | ||
if os.environ.get('TRAVIS') == None: | ||
github_data_import = GitHubData( | ||
date_updated=datetime.datetime.now(), | ||
language='repo_name', | ||
pull_requests=0, | ||
open_issues=0, | ||
number_of_commits=0, | ||
number_of_branches=0, | ||
number_of_releases=0, | ||
number_of_contributors=0, | ||
number_of_watchers=0, | ||
number_of_stargazers=0, | ||
number_of_forks=0 | ||
) | ||
date_updated=datetime.datetime.now(), | ||
language='repo_name', | ||
pull_requests=0, | ||
open_issues=0, | ||
number_of_commits=0, | ||
number_of_branches=0, | ||
number_of_releases=0, | ||
number_of_contributors=0, | ||
number_of_watchers=0, | ||
number_of_stargazers=0, | ||
number_of_forks=0 | ||
) | ||
res = self.db.add_data(github_data_import) | ||
self.assertTrue(isinstance(res, GitHubData)) | ||
res = self.db.delete_data(res.id, 'github_data') | ||
self.assertTrue(res) | ||
|
||
def test_add_and_delete_package_data(self): | ||
if os.environ.get('TRAVIS') == None: | ||
packagedata = PackageManagerData( | ||
date_updated=datetime.datetime.now(), | ||
csharp_downloads=0, | ||
nodejs_downloads=0, | ||
php_downloads=0, | ||
python_downloads=0, | ||
ruby_downloads=0 | ||
) | ||
date_updated=datetime.datetime.now(), | ||
csharp_downloads=0, | ||
nodejs_downloads=0, | ||
php_downloads=0, | ||
python_downloads=0, | ||
ruby_downloads=0 | ||
) | ||
res = self.db.add_data(packagedata) | ||
self.assertTrue(isinstance(res, PackageManagerData)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
res = self.db.delete_data(res.id, 'package_manager_data') | ||
|
@@ -92,52 +129,64 @@ def test_get_data(self): | |
|
||
|
||
class TestGitHub(unittest.TestCase): | ||
|
||
@patch.dict('os.environ', {'GITHUB_TOKEN': 'REDACTED'}) | ||
def setUp(self): | ||
if os.environ.get('TRAVIS') == None: | ||
self.github = GitHub() | ||
self.db = DBConnector() | ||
self.config = Config() | ||
|
||
@vcr.use_cassette('test/fixtures/get_github_data') | ||
def test_update_library_data(self): | ||
if os.environ.get('TRAVIS') == None: | ||
res = self.github.update_library_data(self.config.github_user, | ||
self.config.github_repos[0]) | ||
config = MOCK_DATA['github_config'] | ||
res = self.github.update_library_data(config['user'], | ||
config['repositories'][0]) | ||
self.assertTrue(isinstance(res, GitHubData)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
res = self.db.delete_data(res.id, 'github_data') | ||
self.assertTrue(res) | ||
|
||
|
||
# This is very tied to the languages that Sendgrid supports, but is | ||
# not generalizable to all PackageManager classes. | ||
class TestPackageManagers(unittest.TestCase): | ||
def setUp(self): | ||
if os.environ.get('TRAVIS') == None: | ||
self.pm = PackageManagers() | ||
self.db = DBConnector() | ||
self.config = Config() | ||
|
||
@vcr.use_cassette('test/fixtures/get_package_manager_data') | ||
def test_update_package_manager_data(self): | ||
if os.environ.get('TRAVIS') == None: | ||
res = self.pm.update_package_manager_data( | ||
self.config.package_manager_urls) | ||
MOCK_DATA['package_urls']) | ||
self.assertTrue(isinstance(res, PackageManagerData)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
res = self.db.delete_data(res.id, 'package_manager_data') | ||
self.assertTrue(res) | ||
|
||
|
||
|
||
class TestSendGridEmail(unittest.TestCase): | ||
|
||
@patch.dict('os.environ', {'SENDGRID_API_KEY': 'REDACTED'}) | ||
def setUp(self): | ||
if os.environ.get('TRAVIS') == None: | ||
self.sg = SendGrid() | ||
self.config = Config() | ||
|
||
@vcr.use_cassette('test/fixtures/send_sendgrid_email') | ||
def test_send_email(self): | ||
if os.environ.get('TRAVIS') == None: | ||
res = self.sg.send_email( | ||
'[email protected]', | ||
self.config.from_email, | ||
self.config.email_subject, | ||
self.config.email_body | ||
) | ||
) | ||
self.assertEqual(202, res[0]) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self.assertIsInstance
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@42B thanks for catching these items - with that said, these lines fall outside the scope of the topics for this active PR, if you're interested, maybe those changes could go into a separate PR