def test_unique_items_in_mailing(self): feed = fixtures.create_feed('http://example.com/example-1.rss', self.access_key) feed.sending_interval = 60 * 60 * 24 feed.access_key = self.access_key # Создаем два элемента с разными `pub_date` и одинаковыми `guid` item1 = fixtures.create_feed_item(1) item2 = fixtures.create_feed_item(1) item3 = fixtures.create_feed_item(3) item2.pub_date = dt.datetime(2013, 11, 21, 12, 00, 00) - dt.timedelta(days=2) feed.items.extend([item2, item1, item3]) db.session.add(feed) db.session.commit() # Проверяем, что в рассылке не будет дублирующихся элементов фида with mock.patch('mailtank.Mailtank.create_mailing', autospec=True) as create_mailing_mock: send_feeds.send_feed(feed) call, args = create_mailing_mock.call_args context = args['context'] assert len(context['items']) == 2 assert context['items'][1]['pub_date'] == \ item1.pub_date.strftime('%Y-%m-%d %H:%M:%S')
def test_context_contains_channel_data(self): feed = fixtures.create_feed('http://example.com/example-1.rss', self.access_key) item = fixtures.create_feed_item(1) feed.items.append(item) db.session.add(feed) db.session.commit() with mock.patch('mailtank.Mailtank.create_mailing', autospec=True) as create_mailing_mock: send_feeds.send_feed(feed) call, args = create_mailing_mock.call_args context = args['context'] assert context['channel']['link'] == feed.channel_link assert context['channel']['description'] == feed.channel_description assert context['channel']['title'] == feed.channel_title assert context['channel']['image_url'] == feed.channel_image_url
def test_send_feed_boundary_cases(self): feed = fixtures.create_feed('http://example.com/example-1.rss', self.access_key) feed.access_key = self.access_key db.session.add(feed) db.session.commit() # Притворяемся глупыми и зовёт send_feed с фидом, у которого нет # новых элементов. Ожидаем споткнуться о проверку: with pytest.raises(AssertionError): send_feeds.send_feed(feed) # Добавляем в него элементов for i in range(1, 3): feed_item = fixtures.create_feed_item(i) feed_item.created_at = dt.datetime.utcnow() + dt.timedelta(days=i) feed.items.append(feed_item) db.session.commit() class MailtankErrorStub(mailtank.MailtankError): def __init__(self, code, message): self.code = code self.message = message # Делаем вид, что Mailtank API вернул 503 mailtank_error_stub = MailtankErrorStub(503, 'Whoops') with mock.patch('mailtank.Mailtank.create_mailing', autospec=True, side_effect=mailtank_error_stub): with testfixtures.LogCapture() as l: send_feeds.send_feed(feed) # Проверяем, что происшествие отражено в логах log_record = l.records[-1] assert log_record.levelname == 'WARNING' log_message = log_record.getMessage() assert repr(mailtank_error_stub) in log_message assert repr(feed) in log_message