def test_start_requests_when_fleet_id_is_none(self): spider = MarineTrafficSpider(poskey=self.api_key, fleet_name='MT_API', fleet_id=None) idx = None for idx, item in enumerate(spider.start_requests()): self.assertFalse(True) # Unreachable self.assertIsNone(idx)
def test_start_requests_update_fleet(self): spider = MarineTrafficSpider(fleet='true', getkey=self.api_key, fleet_name='MT_API', fleet_id='xyz') idx = -1 for idx, item in enumerate(spider.start_requests()): self.assertIn('/api/getfleet/{}/fleet_id:xyz'.format(self.api_key), item.url) self.assertEqual(item.callback, spider.update_fleet) self.assertEqual(idx, 0)
def test_start_requests_positions(self): spider = MarineTrafficSpider(poskey=self.api_key, fleet_name='MT_API', fleet_id='xyz') idx = -1 for idx, item in enumerate(spider.start_requests()): self.assertIn( '/exportvessels/{}/v:8/timespan:{}/msgtype:{}'.format( self.api_key, spider.TIMESPAN, spider.MSGTYPE), item.url, ) self.assertEqual(item.callback, spider.parse) self.assertEqual(idx, 0)
def test_start_requests_no_fleet_name(self): spider = MarineTrafficSpider(getkey=self.api_key, fleet_id='xyz') with patch( 'kp_scrapers.spiders.ais.marinetraffic.MarineTrafficSpider.logger' ): idx = -1 for idx, item in enumerate(spider.start_requests()): self.assertIn( '/api/getfleet/{}/fleet_id:xyz'.format(self.api_key), item.url) self.assertIs(item.callback, spider.show_fleet) spider.logger.error.assert_called_once_with( "invalid fleet name specified") self.assertEqual(idx, -1)
def test_spider_init_mutually_exclusive_actions(self): with patch( 'kp_scrapers.spiders.ais.marinetraffic.MarineTrafficSpider.logger' ): spider = MarineTrafficSpider( getkey=self.api_key, fleet_name='MT_API', fleet='true', showfleet='true', fleet_id='xyz', ) idx = -1 for idx, item in enumerate(spider.start_requests()): self.assertIn( '/api/getfleet/{}/fleet_id:xyz'.format(self.api_key), item.url) self.assertEqual(item.callback, spider.show_fleet) self.assertEqual(idx, 0) spider.logger.warning.assert_called_once_with( 'Contradictory arguments: cannot update and ' 'show fleet at once. Will assume command is ' 'show fleet.')
def test_spider_init_wrong_msg_type(self): msgtype = 'WRONG' with patch( 'kp_scrapers.spiders.ais.marinetraffic.MarineTrafficSpider.logger' ): spider = MarineTrafficSpider(poskey=self.api_key, fleet_name='MT_API', msgtype=msgtype, fleet_id='xyz') idx = -1 for idx, item in enumerate(spider.start_requests()): self.assertIn( '/exportvessels/{}/v:8/timespan:{}/msgtype:{}'.format( self.api_key, spider.TIMESPAN, spider.MSGTYPE), item.url, ) self.assertEqual(item.callback, spider.parse) self.assertEqual(idx, 0) spider.logger.warning.assert_called_once_with( 'Unknown message type "{}", will use default instead: "{}"'. format(msgtype.lower(), spider.MSGTYPE))