Skip to content

Instantly share code, notes, and snippets.

@rsyring
Last active March 30, 2016 11:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rsyring/fe31b35830ec889b8c2569401b3e6eca to your computer and use it in GitHub Desktop.
Save rsyring/fe31b35830ec889b8c2569401b3e6eca to your computer and use it in GitHub Desktop.
File Promotion Example
from flask_mail import Message
from keg import current_app
from pypicalc.app import PyPICalc
class IMailService(object):
def send(self):
raise NotImplemented
class MockMailService(IMailService):
sent_messages = []
def send(self, msg):
if current_app.testing:
self.sent_messages.append(msg)
else:
PyPICalc.mail.send(msg)
class MailService(IMailService):
def send(self, msg):
PyPICalc.mail.send(msg)
class ServiceManager(object):
def __init__(self):
if current_app.testing:
self.mail = MockMailService()
else:
self.mail = MailService()
services = ServiceManager()
def send_contact_email(user_email, subject, body):
msg = Message(subject, reply_to=user_email)
msg.add_recipient('somebodyelse@example.com')
msg.body = body
services.mail.send(msg)
class TestSendContactEmail(object):
def test_message_creation(self):
send_contact_email('me@me.com', 'hi there', 'you rock!')
assert len(services.mail.sent_messages) == 1
msg = services.mail.sent_messages[0]
assert msg.subject == 'hi there'
assert msg.reply_to == 'me@me.com'
assert msg.sender == 'fakesender@example.com'
assert msg.recipients == ['somebodyelse@example.com']
import csv
import arrow
def calculate_age_sum(csv_fpath):
with open(csv_fpath) as csvfo:
reader = csv.reader(csvfo)
return calculate_age_sum_helper(reader)
def calculate_age_sum_helper(iterable):
age_sum = 0
for row in iterable:
date_delta = arrow.now() - arrow.get(row[1])
age_sum += date_delta.days // 365
return age_sum
class TestAgeSum(object):
def test_temporary_file(self, tmpdir):
fake_csv_tmpfile = tmpdir.join('fake.csv')
with fake_csv_tmpfile.open('w') as csv:
csv.write('amy,2000-01-01\n')
csv.write('judy,1980-02-01\n')
csv_fpath = fake_csv_tmpfile.strpath
assert calculate_age_sum(csv_fpath) == 52
def test_iterable_data(self):
data = [
('amy', '2000-01-01'),
('judy', '1980-02-01'),
]
assert calculate_age_sum_helper(data) == 52
import requests
import requests_mock
def calc_project_bandwidth(project_data):
total_bytes = 0
if 'urls' not in project_data:
return 0
for url in project_data['urls']:
total_bytes += url['size'] * url['downloads']
return total_bytes
def get_project_bandwidth(project_name, _requests=requests, _cpb=calc_project_bandwidth):
url = 'https://pypi.python.org/pypi/{0}/json'.format(project_name)
resp = _requests.get(url)
return _cpb(resp.json())
class TestGetProjectBandwidth(object):
def test_all_with_requests_mock(self):
with requests_mock.mock() as m:
m.get('https://pypi.python.org/pypi/foo/json',
json={'urls': [{'downloads': 5, 'size': 10}]})
assert get_project_bandwidth('foo') == 50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment