コード例 #1
0
ファイル: test_aircraft.py プロジェクト: mylk/acars-server
    def test_fetch_all_returns_all_aircrafts_when_exist(self):
        seen_datetime = datetime.utcnow()
        self.mapper.insert(
            Aircraft([None, 'foo', None, seen_datetime, seen_datetime]))
        self.mapper.insert(
            Aircraft([None, 'bar', None, seen_datetime, seen_datetime]))

        aircrafts = self.mapper.fetch_all()
        self.assertEqual(2, len(aircrafts))
コード例 #2
0
ファイル: test_aircraft.py プロジェクト: mylk/acars-server
    def test_update_updates_aircraft_when_exists(self):
        seen_datetime = datetime.utcnow()
        aircraft = Aircraft([None, 'foo', None, seen_datetime, seen_datetime])
        aircraft_id = self.mapper.insert(aircraft)

        aircraft = self.mapper.fetch(aircraft_id)
        aircraft.image = 'bar'
        self.mapper.update(aircraft)

        # assert that image column was updated
        aircraft = self.mapper.fetch(aircraft_id)
        self.assertEqual('bar', aircraft.image)
コード例 #3
0
ファイル: test_aircraft.py プロジェクト: mylk/acars-server
    def test_delete_does_nothing_when_aircraft_does_not_exist(self):
        seen_datetime = datetime.utcnow()
        aircraft_existing = Aircraft(
            [None, 'foo', None, seen_datetime, seen_datetime])
        aircraft_id = self.mapper.insert(aircraft_existing)
        aircraft_non_existing = Aircraft(
            [0, 'foo', 'bar', seen_datetime, seen_datetime])

        self.mapper.delete(aircraft_non_existing)

        # assert that existing aircraft was not affected
        aircraft = self.mapper.fetch(aircraft_id)
        self.assertIsNotNone(aircraft)
コード例 #4
0
    def fetch_all(self, order=None, limit=None):
        # default order and limit, if not set
        order = ('id', 'ASC') if order is None else order
        limit = -1 if limit is None else limit

        # the actual query
        self.adapter.execute("""
                SELECT id, registration, image,
                strftime("%Y-%m-%d %H:%M:%S", "first_seen", "localtime") AS first_seen,
                strftime("%Y-%m-%d %H:%M:%S", "last_seen", "localtime") AS last_seen
                FROM aircrafts
                ORDER BY {} {} LIMIT {}
            """.format(order[0], order[1], limit))
        results = self.adapter.fetchall()

        # map to models
        aircrafts = []
        for result in results:
            messages = MessageDbMapper(self.adapter).fetch_by(
                'aircraft_id', result[0], ('created_at', 'DESC'))
            aircraft = Aircraft(result, messages)

            aircrafts.append(aircraft)

        return aircrafts
コード例 #5
0
ファイル: test_image.py プロジェクト: mylk/acars-server
    def test_exists_returns_false_when_image_does_not_exist_locally(self):
        aircraft = Aircraft(
            [None, 'foo', 'foo', self.seen_datetime, self.seen_datetime])

        file_exists = self.image_service.exists(aircraft)

        self.assertFalse(file_exists)
コード例 #6
0
ファイル: test_aircraft.py プロジェクト: mylk/acars-server
    def test_fetch_returns_aircraft_when_exists(self):
        seen_datetime = datetime.utcnow()
        aircraft = Aircraft([None, 'foo', None, seen_datetime, seen_datetime])

        aircraft_id = self.mapper.insert(aircraft)

        aircraft = self.mapper.fetch(aircraft_id)
        self.assertIsNotNone(aircraft)
コード例 #7
0
 def setUp(self):
     timestamp = int(datetime.strftime(datetime.utcnow(), '%s'))
     self.aircraft = Aircraft([1, 'tail', None, timestamp, timestamp])
     self.client = Client(
         [1, '127.0.0.1',
          datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S')])
     self.data = {'flight': 'foo', 'text': 'bar', 'timestamp': timestamp}
     self.mapper = MessageInputMapper()
コード例 #8
0
ファイル: test_aircraft.py プロジェクト: mylk/acars-server
    def test_fetch_identical_returns_aircraft_when_exists(self):
        seen_datetime = datetime.utcnow()
        aircraft = Aircraft([None, 'foo', None, seen_datetime, seen_datetime])

        self.mapper.insert(aircraft)

        aircraft = self.repository.fetch_identical(aircraft)
        self.assertNotEqual(None, aircraft)
コード例 #9
0
ファイル: test_aircraft.py プロジェクト: mylk/acars-server
    def test_init_fills_properties(self):
        aircraft = Aircraft(self.data, self.messages)

        self.assertEqual(self.data[0], aircraft.id)
        self.assertEqual(self.data[1], aircraft.registration)
        self.assertEqual(self.data[2], aircraft.image)
        self.assertEqual(self.data[3], aircraft.first_seen)
        self.assertEqual(self.data[4], aircraft.last_seen)
        self.assertEqual(self.messages, aircraft.messages)
コード例 #10
0
ファイル: test_aircraft.py プロジェクト: mylk/acars-server
    def test_delete_all_deletes_aircrafts_when_exist(self):
        seen_datetime = datetime.utcnow()
        aircraft = Aircraft([None, 'foo', None, seen_datetime, seen_datetime])
        aircraft_id = self.mapper.insert(aircraft)

        self.mapper.delete_all()

        aircraft = self.mapper.fetch(aircraft_id)
        self.assertIsNone(aircraft)
コード例 #11
0
ファイル: test_aircraft.py プロジェクト: mylk/acars-server
    def test_insert_inserts_aircraft_and_returns_row_id(self):
        seen_datetime = datetime.utcnow()
        aircraft = Aircraft([None, 'foo', None, seen_datetime, seen_datetime])

        aircraft_id = self.mapper.insert(aircraft)

        aircrafts = self.mapper.fetch_all()
        self.assertEqual(1, len(aircrafts))
        self.assertIsNotNone(aircraft_id)
コード例 #12
0
ファイル: test_image.py プロジェクト: mylk/acars-server
    def test_exists_returns_true_when_image_exists_locally(self):
        aircraft = Aircraft([
            None, 'foo', 'paper_plane.png', self.seen_datetime,
            self.seen_datetime
        ])

        file_exists = self.image_service.exists(aircraft)

        self.assertTrue(file_exists)
コード例 #13
0
    def test_map_returns_aircraft_when_all_fields_exist(self):
        datetime_expected = datetime.utcfromtimestamp(
            self.data['timestamp']).strftime('%Y-%m-%d %H:%M:%S')

        aircraft_expected = Aircraft([
            None, self.data['tail'], None, datetime_expected, datetime_expected
        ])
        aircraft_actual = self.mapper.map(self.data)

        self.assertEqual(dict(aircraft_expected), dict(aircraft_actual))
コード例 #14
0
ファイル: aircraft.py プロジェクト: mylk/acars-server
    def map(data):
        registration = data['tail']
        first_seen = datetime.utcfromtimestamp(
            data['timestamp']).strftime('%Y-%m-%d %H:%M:%S')
        last_seen = first_seen

        # nones are id, image
        aircraft = Aircraft([None, registration, None, first_seen, last_seen])

        return aircraft
コード例 #15
0
ファイル: test_image.py プロジェクト: mylk/acars-server
    def test_handle_does_nothing_when_aircraft_image_exists_locally(self):
        self.image_service.exists = mock.MagicMock(return_value=True)
        self.image_service.get_url = mock.MagicMock()

        aircraft = Aircraft(
            [None, 'foo', 'foo.png', self.seen_datetime, self.seen_datetime])
        self.image_service.handle(aircraft)

        self.image_service.exists.assert_called_once()
        self.image_service.get_url.assert_not_called()
コード例 #16
0
ファイル: test_aircraft.py プロジェクト: mylk/acars-server
    def test_iter_returns_expected_iterable(self):
        dict_expected = {
            'id': self.data[0],
            'registration': self.data[1],
            'image': self.data[2],
            'first_seen': self.data[3],
            'last_seen': self.data[4]
        }
        aircraft = Aircraft(self.data, self.messages)

        self.assertEqual(dict_expected, dict(aircraft))
コード例 #17
0
ファイル: test_aircraft.py プロジェクト: mylk/acars-server
    def test_str_returns_expected_string(self):
        string_expected = 'ID: {}, Registration: {}, Image: {}, First Seen: {}, Last Seen: {}'.format(
            self.data[0],
            self.data[1],
            self.data[2],
            self.data[3].strftime('%Y-%m-%d %H:%M:%S'),
            self.data[4].strftime('%Y-%m-%d %H:%M:%S'),
        )
        aircraft = Aircraft(self.data, self.messages)

        self.assertEqual(string_expected, str(aircraft))
コード例 #18
0
    def fetch(self, aircraft_id):
        self.adapter.execute(
            'SELECT id, registration, image, first_seen, last_seen FROM aircrafts WHERE id = ?',
            (aircraft_id, ))
        result = self.adapter.fetchone()

        aircraft = None
        if result:
            aircraft = Aircraft(result)

        return aircraft
コード例 #19
0
ファイル: test_image.py プロジェクト: mylk/acars-server
    def test_get_url_returns_none_when_image_provider_response_is_not_the_expected(
            self):
        aircraft = Aircraft(
            [None, 'bar', None, self.seen_datetime, self.seen_datetime])

        response = mock.MagicMock()
        image.json.loads.return_value = json.loads('{"foo": "bar"}')
        image.request.urlopen = mock.MagicMock(return_value=response)
        url = self.image_service.get_url(aircraft)

        self.assertIsNone(url)
        image.json.loads.assert_called_once()
コード例 #20
0
ファイル: test_image.py プロジェクト: mylk/acars-server
    def test_handle_does_nothing_when_aircraft_image_does_not_exist_locally_and_does_not_exist_online_too(
            self):
        self.image_service.exists = mock.MagicMock(return_value=False)
        self.image_service.get_url = mock.MagicMock(return_value=None)
        self.image_service.download_image = mock.MagicMock()

        aircraft = Aircraft(
            [None, 'foo', 'foo.png', self.seen_datetime, self.seen_datetime])
        self.image_service.handle(aircraft)

        self.image_service.exists.assert_called_once()
        self.image_service.get_url.assert_called_once()
        self.image_service.download_image.assert_not_called()
コード例 #21
0
ファイル: test_image.py プロジェクト: mylk/acars-server
    def test_download_image_downloads_the_image(self):
        aircraft = Aircraft(
            [None, 'bar', 'bar.png', self.seen_datetime, self.seen_datetime])

        filename = self.image_service.download_image('http://foo.com/bar.png',
                                                     aircraft)

        image.request.urlretrieve.assert_called_once_with(
            'http://foo.com/bar.png',
            '/acars-server/acarsserver/service/../app/assets/img/aircrafts/large/bar.png'
        )

        self.assertEqual('bar.png', filename)
コード例 #22
0
    def fetch_identical(self, aircraft):
        self.adapter.execute(
            """
                SELECT id, registration, image, first_seen, last_seen
                FROM aircrafts
                WHERE registration = ?
            """, (aircraft.registration, ))
        result = self.adapter.fetchone()

        aircraft = None
        if result:
            aircraft = Aircraft(result)

        return aircraft
コード例 #23
0
ファイル: test_image.py プロジェクト: mylk/acars-server
    def test_get_url_handles_exception_when_image_provider_returns_error(self):
        aircraft = Aircraft(
            [None, 'bar', None, self.seen_datetime, self.seen_datetime])

        http_error = error.HTTPError('http://www.google.com', 404, 'foo',
                                     'hdrs', mock.MagicMock())
        image.request.urlopen = mock.MagicMock(side_effect=http_error)
        url = self.image_service.get_url(aircraft)

        image.request.urlopen.assert_called_once_with(
            'https://commons.wikimedia.org/w/api.php?action=query&generator=categorymembers&'
            +
            'gcmtitle=Category:bar_(aircraft)&gcmtype=file&redirects=1&prop=imageinfo&iiprop=url&format=json'
        )
        self.assertIsNone(url)
        image.json.loads.assert_not_called()
コード例 #24
0
ファイル: test_image.py プロジェクト: mylk/acars-server
    def test_handle_downloads_image_when_aircraft_image_does_not_exist_locally_and_exist_online(
            self):
        self.image_service.exists = mock.MagicMock(return_value=False)
        self.image_service.get_url = mock.MagicMock(
            return_value='http://www.google.com')
        self.image_service.download_image = mock.MagicMock()
        self.image_service.optimize = mock.MagicMock()
        self.image_service.create_thumbnail = mock.MagicMock()

        aircraft = Aircraft(
            [None, 'foo', 'foo.png', self.seen_datetime, self.seen_datetime])
        self.image_service.handle(aircraft)

        self.assertEqual(1, self.image_service.exists.call_count)
        self.assertEqual(1, self.image_service.get_url.call_count)
        self.assertEqual(1, self.image_service.download_image.call_count)
        self.assertEqual(1, self.image_service.optimize.call_count)
        self.assertEqual(1, self.image_service.create_thumbnail.call_count)
        self.assertEqual(1, image.AircraftDbMapper.update.call_count)
コード例 #25
0
ファイル: test_image.py プロジェクト: mylk/acars-server
    def test_get_url_returns_the_image_url_when_image_exists_online(self):
        aircraft = Aircraft(
            [None, 'bar', None, self.seen_datetime, self.seen_datetime])

        path = os.path.dirname(os.path.realpath(__file__))
        with open('{}/../fixtures/aircraft_images.json'.format(
                path)) as fixture_file:
            fixture_data = fixture_file.read()
            fixture_file.close()

        response = mock.MagicMock()
        image.json.loads.return_value = json.loads(fixture_data)
        image.request.urlopen = mock.MagicMock(return_value=response)
        url = self.image_service.get_url(aircraft)

        self.assertEqual(
            'https://upload.wikimedia.org/wikipedia/commons/2/2b/Continental_Airlines_Boeing_767-400%3B_N69059%40ZRH%3B16.07.2010_583bk_%284799578145%29.jpg',
            url)
        image.json.loads.assert_called_once()
コード例 #26
0
ファイル: test_image.py プロジェクト: mylk/acars-server
    def test_download_image_handles_os_and_image_provider_errors(self):
        aircraft = Aircraft(
            [None, 'bar', 'bar.png', self.seen_datetime, self.seen_datetime])

        image.request.urlretrieve = mock.MagicMock(side_effect=OSError)
        filename = self.image_service.download_image('http://foo.com/bar.png',
                                                     aircraft)
        image.request.urlretrieve.assert_called_once_with(
            'http://foo.com/bar.png',
            '/acars-server/acarsserver/service/../app/assets/img/aircrafts/large/bar.png'
        )
        self.assertIsNone(filename)

        image.request.urlretrieve = mock.MagicMock(
            side_effect=error.URLError('Not Found'))
        filename = self.image_service.download_image('http://foo.com/bar.png',
                                                     aircraft)
        image.request.urlretrieve.assert_called_once_with(
            'http://foo.com/bar.png',
            '/acars-server/acarsserver/service/../app/assets/img/aircrafts/large/bar.png'
        )
        self.assertIsNone(filename)
コード例 #27
0
    def callback(self, channel, method, properties, body):
        try:
            body_dict = json.loads(body)
            aircraft = Aircraft([
                body_dict['id'], body_dict['registration'], body_dict['image'],
                body_dict['first_seen'], body_dict['last_seen']
            ])

            ImageService(self.adapter, self.logger).handle(aircraft)

            channel.basic_ack(delivery_tag=method.delivery_tag)

            self.logger.info('RabbitMQ job done!')
        except (AMQPConnectionError, AMQPChannelError, ChannelClosed,
                ConnectionClosed, NoFreeChannels) as ex_ack:
            self.logger.error(str(ex_ack))

            try:
                channel.basic_nack(delivery_tag=method.delivery_tag)
                return None
            except (AMQPConnectionError, AMQPChannelError, ChannelClosed,
                    ConnectionClosed, NoFreeChannels) as ex_nack:
                self.logger.error(str(ex_nack))
                return None
コード例 #28
0
ファイル: test_aircraft.py プロジェクト: mylk/acars-server
    def test_fetch_identical_returns_none_when_aircraft_does_not_exist(self):
        seen_datetime = datetime.utcnow()
        aircraft = Aircraft([None, 'foo', None, seen_datetime, seen_datetime])

        aircraft = self.repository.fetch_identical(aircraft)
        self.assertEqual(None, aircraft)