def test_get_nonexistent_list(self): with responses.RequestsMock() as rsps: rsps.add(responses.GET, 'https://us1.api.mailchimp.com/3.0/lists/57afe96172', json={'error': 'non existent list'}, status=404, content_type='application/json') with self.assertRaises(MCListNotFound): MCList.get('57afe96172')
def test_get_list(self): with responses.RequestsMock() as rsps: rsps.add(responses.GET, 'https://us1.api.mailchimp.com/3.0/lists/57afe96172', json=self.get_list_1(), status=200, content_type='application/json') mc_list = MCList.get('57afe96172') self.assertEqual(1, len(rsps.calls)) self.assertEqual( "https://us1.api.mailchimp.com/3.0/lists/57afe96172", rsps.calls[0].request.url) self.assertEqual('GET', rsps.calls[0].request.method) self.assertEqual("application/json", rsps.calls[0].request.headers['Accept']) self.assertEqual("application/json", rsps.calls[0].request.headers['Content-Type']) self.assertEqual( "Basic dXNlcm5hbWU6YTY1YTY1YTY1YTY1YTY1YTU2YTVhNi11czE=", rsps.calls[0].request.headers['Authorization']) # Check list item self.assertEqual('57afe96172', mc_list.id) self.assertEqual("Freddie's Jokes", mc_list.name) self.assertEqual(12, len(mc_list.links)) self.assertTrue(isinstance(mc_list.links[0], MCLink)) self.assertTrue(isinstance(mc_list.contact, MCContact)) self.assertEqual('MailChimp', mc_list.contact.company) self.assertTrue(isinstance(mc_list.stats, MCListStats)) self.assertEqual(203, mc_list.stats.member_count) self.assertTrue( isinstance(mc_list.campaign_defaults, MCCampaignDefaults))
def test_init(self): mc_list = MCList(self.get_list_1()) self.assertEqual('57afe96172', mc_list.id) self.assertEqual("Freddie's Jokes", mc_list.name) self.assertEqual(12, len(mc_list.links)) self.assertTrue(isinstance(mc_list.links[0], MCLink)) self.assertTrue(isinstance(mc_list.contact, MCContact)) self.assertEqual('MailChimp', mc_list.contact.company) self.assertTrue(isinstance(mc_list.stats, MCListStats)) self.assertEqual(203, mc_list.stats.member_count) self.assertTrue( isinstance(mc_list.campaign_defaults, MCCampaignDefaults))
def test_empty_init(self): mc_list = MCList() for prop in [ 'id', 'name', 'permission_reminder', 'user_archive_bar', 'notify_on_subscribe', 'notify_on_unsubscribe', 'date_created', 'list_rating', 'email_type_option', 'subscribe_url_short', 'subscribe_url_long', 'beamer_address', 'visibility', 'modules' ]: self.assertIsNone(getattr(mc_list, prop)) self.assertEqual(mc_list.links, []) self.assertTrue(isinstance(mc_list.contact, MCContact)) self.assertTrue(isinstance(mc_list.stats, MCListStats)) self.assertTrue( isinstance(mc_list.campaign_defaults, MCCampaignDefaults))
def test_item_url(self): mc_list = MCList() self.assertEqual("/lists", mc_list.item_url)
def test_valid_search_params(self): mc_list = MCList() self.assertEqual([ 'offset', 'count', 'fields', 'exclude_fields', 'before_date_created', 'since_date_created' ], mc_list.valid_search_params)
def test_repr(self): mc_list = MCList(self.get_list_1()) self.assertEqual('<MCList: 57afe96172>', str(repr(mc_list)))
def test_to_str(self): mc_list = MCList(self.get_list_1()) self.assertEqual('57afe96172', str(mc_list))
def test_list(self): with responses.RequestsMock() as rsps: rsps.add( responses.GET, 'https://us1.api.mailchimp.com/3.0/lists', json={ "lists": [self.get_list_1(), self.get_list_2()], "_links": [{ "rel": "self", "href": "https://us1.api.mailchimp.com/3.0/lists", "method": "GET", "targetSchema": "https://api.mailchimp.com/schema/3.0/Lists/Collection.json", "schema": "https://api.mailchimp.com/schema/3.0/CollectionLinks/Lists.json" }, { "rel": "parent", "href": "https://us1.api.mailchimp.com/3.0/", "method": "GET", "targetSchema": "https://api.mailchimp.com/schema/3.0/Root.json" }, { "rel": "create", "href": "https://us1.api.mailchimp.com/3.0/lists", "method": "POST", "schema": "https://api.mailchimp.com/schema/3.0/Lists/Instance.json" }], "total_items": 2 }, status=200, content_type='application/json') lists = MCList.list() self.assertEqual(2, len(lists)) self.assertEqual(1, len(rsps.calls)) self.assertEqual("https://us1.api.mailchimp.com/3.0/lists", rsps.calls[0].request.url) self.assertEqual('GET', rsps.calls[0].request.method) self.assertEqual("application/json", rsps.calls[0].request.headers['Accept']) self.assertEqual("application/json", rsps.calls[0].request.headers['Content-Type']) self.assertEqual( "Basic dXNlcm5hbWU6YTY1YTY1YTY1YTY1YTY1YTU2YTVhNi11czE=", rsps.calls[0].request.headers['Authorization']) # Check item 1 item1 = lists[0] self.assertEqual('57afe96172', item1.id) self.assertEqual("Freddie's Jokes", item1.name) self.assertEqual(12, len(item1.links)) self.assertTrue(isinstance(item1.links[0], MCLink)) self.assertTrue(isinstance(item1.contact, MCContact)) self.assertEqual('MailChimp', item1.contact.company) self.assertTrue(isinstance(item1.stats, MCListStats)) self.assertEqual(203, item1.stats.member_count) self.assertTrue( isinstance(item1.campaign_defaults, MCCampaignDefaults)) # Check item 2 item2 = lists[1] self.assertEqual('9617257afe', item2.id) self.assertEqual("Freddie's New Jokes", item2.name) self.assertEqual(12, len(item2.links)) self.assertTrue(isinstance(item2.links[0], MCLink)) self.assertTrue(isinstance(item2.contact, MCContact)) self.assertEqual('MailChimp 2', item2.contact.company) self.assertTrue(isinstance(item2.stats, MCListStats)) self.assertEqual(205, item2.stats.member_count) self.assertTrue( isinstance(item2.campaign_defaults, MCCampaignDefaults))