Пример #1
0
    def test_retrieve_positions(self):
        response = FakeXmlResponse(
            os.path.join(FIXTURE_PATH, 'position-and-eta.xml'))
        # result = etree.fromstring(response.body)
        spider = MarineTrafficSpider(fleet_name='MT_API', msgtype='extended')
        responses = [{
            'aisType': 'T-AIS',
            'master_imo': '7357452',
            'master_mmsi': '205194000',
            'master_callsign': 'ONAE',
            'master_name': 'METHANIA',
            # 'master_shipType': 'tankships',
            'nextDestination_aisType': 'T-AIS',
            'nextDestination_destination': 'MARSEILLE',
            'nextDestination_eta': '2015-12-16T10:00:00',
            'position_aisType': 'T-AIS',
            'position_course': '245',
            'position_draught': '9.5',
            'position_lat': '43.332330',
            'position_lon': '5.342667',
            'position_navState': None,
            'position_speed': 0.1,
            'position_timeReceived': '2015-06-12T10:46:46',
            'provider_id': 'MT_API',
        }]

        idx = None
        for idx, item in enumerate(spider.parse(response)):
            relevant_fields = list(strip_meta_fields(item).keys())
            for key in relevant_fields:
                self.assertEqual(item.get(key), responses[idx].get(key))

            # Ensure the item and our test dict have the same number of keys.
            self.assertEqual(len(relevant_fields), len(responses[idx]))
        self.assertIsNotNone(idx)  # Ensures the loop iterated at least once
Пример #2
0
    def test_error_response(self):
        response = FakeResponse(
            os.path.join(FIXTURE_PATH, 'error-response.xml'))
        spider = MarineTrafficSpider()
        with patch(
                'kp_scrapers.spiders.ais.marinetraffic.MarineTrafficSpider.logger'
        ):
            for item in spider.parse(response):
                pass

            spider.logger.error.assert_called_once_with(
                "Data retrieval error: {message}".format(
                    message="ABOVE SERVICE CALL LIMIT"))
Пример #3
0
    def test_retrieve_sattelite_position(self):
        response = FakeXmlResponse(
            os.path.join(FIXTURE_PATH, 'position-v5-sattelite.xml'))
        # result = etree.fromstring(response.body)
        spider = MarineTrafficSpider(fleet_name='MT_API', msgtype='simple')
        responses = [{
            'aisType': 'S-AIS',
            'master_mmsi': '205194000',
            # 'master_shipType': 'tankships',
            'position_aisType': 'S-AIS',
            'position_course': '11',
            'position_heading': 329,
            'position_lat': '43.332330',
            'position_lon': '5.342667',
            'position_navState': '5',
            'position_speed': 0.0,
            'position_timeReceived': '2015-09-17T06:49:55',
            'provider_id': 'MT_API',
        }]

        idx = None
        for idx, item in enumerate(spider.parse(response)):
            relevant_fields = list(strip_meta_fields(item).keys())
            for key in relevant_fields:
                self.assertEqual(item.get(key), responses[idx].get(key))

            # Ensure the item and our test dict have the same number of keys.
            ikeys = set(item.keys())
            dkeys = set(responses[0])
            self.assertEqual(
                len(relevant_fields),
                len(responses[idx]),
                'Missing keys: {}, Extraneous keys: {}'.format(
                    ', '.join(list(dkeys - ikeys)),
                    ', '.join(list(ikeys - dkeys))),
            )
        self.assertIsNotNone(idx)  # Ensures the loop iterated at least once