from datetime import date
class Ticket(object):
def __init__(self, buyer, price, schedule_date):
self.buyer = buyer
self.price = price
self.schedule_date = schedule_date
def has_expired(self):
return date.today() > self.schedule_date
#############################
ticket = Ticket("Bernardo", 10, date(2015, 10, 10))
if not ticket.has_expired():
#processa ticket válido
class Ticket(TestCase):
def test_3_returns_fizz(self):
self.assertEqual("fizz", fizzbuzz(3))
def fizzbuzz(number):
return "fizz"
class Ticket(TestCase):
def test_3_returns_fizz(self):
self.assertEqual("fizz", fizzbuzz(3))
class TestTicketManager(TestCase):
def test_expire_ticket(self):
ticket = Ticket.objects.create(id=30)
self.assertFalse(ticket.expired)
manager = TicketManager()
manager.expire_ticket(ticket_id=30)
ticket = Ticket.objects.get(id=30)
self.assertTrue(ticket.expired)
class TestTicketManager(TestCase):
def test_expire_ticket(self):
manager = TicketManager()
manager.expire_ticket(ticket_id=30)
from mock import Mock
class TestTicketManager(TestCase):
def test_expire_ticket(self):
tickets_repository = Mock(TicketsRepository)
tickets_repository.get_by_id.return_value = Ticket()
manager = TicketManager(tickets_repository)
manager.expire_ticket(ticket_id=30)
tickets_repository.get_by_id.assert_called_once_with(30)
class TicketManager(object):
def __init__(self, tickets_repository):
self.repository = tickets_repository
def expire_ticket(self, ticket_id):
self.repository.get_by_id(ticket_id)
class Ticket(object):
pass
class TicketsRepository(object):
def get_by_id(self, id):
"Must return a Ticket object"
pass
from mock import Mock
class TestTicketManager(TestCase):
def test_expire_ticket(self):
ticket = Mock(Ticket)
tickets_repository = Mock(TicketsRepository)
tickets_repository.get_by_id.return_value = ticket
manager = TicketManager(tickets_repository)
manager.expire_ticket(ticket_id=30)
tickets_repository.get_by_id.assert_called_once_with(30)
ticket.expire.assert_called_once_with()
class TicketManager(object):
def __init__(self, tickets_repository):
self.repository = tickets_repository
def expire_ticket(self, ticket_id):
ticket = self.repository.get_by_id(ticket_id)
ticket.expire()
class Ticket(object):
def expire(self):
pass
class TicketsRepository(object):
def get_by_id(self, id):
"Must return a Ticket object"
pass
from mock import Mock
class TestTicketManager(TestCase):
def test_expire_ticket(self):
ticket = Mock(Ticket)
tickets_repository = Mock(TicketsRepository)
tickets_repository.get_by_id.return_value = ticket
notificator = Mock(TicketNotificator)
manager = TicketManager(tickets_repository, notificator)
manager.expire_ticket(ticket_id=30)
tickets_repository.get_by_id.assert_called_once_with(30)
ticket.expire.assert_called_once_with()
notificator.notify_expired.assert_called_once_with(ticket)
class TicketManager(object):
def __init__(self, tickets_repository, notificator):
self.repository = tickets_repository
self.notificator = notificator
def expire_ticket(self, ticket_id):
ticket = self.repository.get_by_id(ticket_id)
ticket.expire()
self.notificator.notify_expired(ticket)
class Ticket(object):
def expire(self):
pass
class TicketsRepository(object):
def get_by_id(self, id):
"Must return a Ticket object"
pass
class TicketNotificator(object):
def notify_expired(self, ticket):
pass
Bernardo Fontes