Skip to content
This repository has been archived by the owner on Aug 28, 2020. It is now read-only.

Mock network requests for faster unit testing #13 #84

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ python:
- '3.4'
- '3.5'
install:
- pip install mock==2.0 # http://railslide.io/travis-mock-afternoon-of-frustration.html
- pip install -r requirements.txt
script:
- pip install -r requirements_dev.txt
- python -m unittest discover
notifications:
hipchat:
Expand Down
1 change: 1 addition & 0 deletions requirements_dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
vcrpy==1.11.1
7,080 changes: 7,080 additions & 0 deletions test/fixtures/get_github_data

Large diffs are not rendered by default.

2,500 changes: 2,500 additions & 0 deletions test/fixtures/get_package_manager_data

Large diffs are not rendered by default.

32 changes: 32 additions & 0 deletions test/fixtures/send_sendgrid_email
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
97 changes: 73 additions & 24 deletions test/test.py
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
Expand Down Expand Up @@ -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))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

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

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))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

res = self.db.delete_data(res.id, 'package_manager_data')
Expand All @@ -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))
Copy link

Choose a reason for hiding this comment

The 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))
Copy link

Choose a reason for hiding this comment

The 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()