示例#1
0
    def test_external(self):
        with self.app.test_request_context():
            l = Self(external=True)

            expected = {'self': {'href': 'http://localhost/'}}

            assert l.to_dict() == expected
示例#2
0
    def test_to_json(self):
        with self.app.test_request_context():
            l = Self(foo='foo', name='foo')

            expected = json.dumps({'self': {
                'href': '/',
                'name': 'foo',
            }})

            assert l.to_json() == expected
示例#3
0
    def test_external(self):
        with self.app.test_request_context():
            l = Self(external=True)

            expected = {
                'self': {
                    'href': 'http://localhost/'
                }
            }

            assert l.to_dict() == expected
示例#4
0
    def test_to_json(self):
        with self.app.test_request_context():
            l = Self(foo='foo', name='foo')

            expected = json.dumps({
                'self': {
                    'href': '/',
                    'name': 'foo',
                }
            })

            assert l.to_json() == expected
示例#5
0
    def test_to_dict(self):
        with self.app.test_request_context():
            l = Self(foo='foo', name='foo')

            expected = {
                'self': {
                    'href': '/',
                    'name': 'foo',
                }
            }

            assert l.to_dict() == expected
示例#6
0
    def test_to_dict(self):
        with self.app.test_request_context():
            l = Self(foo='foo', name='foo')

            expected = {
                'self': {
                    'href': '/',
                    'name': 'foo',
                }
            }

            assert l.to_dict() == expected
示例#7
0
    def test_with_server_name(self):
        self.app.config['SERVER_NAME'] = 'foo.com'
        with self.app.test_request_context():
            l = Self(foo='foo', name='foo')

            expected = {
                'self': {
                    'href': 'http://foo.com/',
                    'name': 'foo',
                }
            }

            assert l.to_dict() == expected
示例#8
0
    def test_with_server_name(self):
        self.app.config['SERVER_NAME'] = 'foo.com'
        with self.app.test_request_context():
            l = Self(foo='foo', name='foo')

            expected = {
                'self': {
                    'href': 'http://foo.com/',
                    'name': 'foo',
                }
            }

            assert l.to_dict() == expected
示例#9
0
    def get(self):
        '''
        Get all devices available
        '''

        devices_db = g.con.get_devices()

        if not devices_db:
            return create_error_response(404, "No devices found",
                                     'There is no devices in DB', 'Devices')

        # create collection of links
        links = Collection(
            Self(),
            Link('device', '/wind/api/device/{id}')
        )

        # links to dict
        l = links.to_dict()

        # combine links and speed to one dict and add items where the actual values are
        dump = l
        dump.update({'items': devices_db})

        # return Response
        return Response(json.dumps(dump), 200, mimetype=JSONHAL)
示例#10
0
    def get(self, id):
        '''
        Get device with given id from db
        :param id
        '''

        device_db = g.con.get_device(id)

        if not device_db:
            return create_error_response(404, "No device found",
                                     'There is no device info on given device id %s' % id, 'Device')

        #create collection of links
        links = Collection(
            Self(),
            Link('list', '/wind/api/devices/'),
            Link('data:speeds-all', request.path + '/speeds/')
        )

        #links to dict
        l = links.to_dict()

        #combine links and speed to one dict to be returned in response
        #dump = dict(list(device_db.items()) + list(l.items()))
        dump = dict(list(l.items()) + list(device_db.items()))

        #return Response
        return Response(json.dumps(dump), 200, mimetype=JSONHAL)
示例#11
0
    def get(self, id):
        '''
        get all speeds
        params: id

        '''
        #extract speeds from db
        speeds_db = g.con.get_speeds(id)
        if not speeds_db:
            return create_error_response(404, "No speeds found",
                                     'There is no speeds data on given device id %s' % id, 'Speeds')

        # create collection of links
        links = Collection(
            Self(),
            Link('device', '/wind/api/device/' + id + '/'),
            Link('speed', '/wind/api/device/' + id + '/speed/{timestamp}')
        )

        # links to dict
        l = links.to_dict()

        # combine links and speed to one dict
        dump = l
        dump.update({'items': speeds_db})

        # return Response
        return Response(json.dumps(dump), 200, mimetype=JSONHAL)
示例#12
0
    def get(self, id, timestamp):
        '''
        :param id, timestamp:
        '''

        direction_db = g.con.get_direction(id, timestamp)

        if not direction_db:
            return create_error_response(404, "No direction data found",
                                         'There is no direction data on device id %s with timestamp %s' % (id, timestamp),
                                         'Direction')

        #create collection of links
        links = Collection(
            Self(),
            Link('device', '/wind/api/device/' + id + '/'),
            Link('list', '/wind/api/device/' + id + '/directions/'),
            Link('all-batteries', '/wind/api/device/' + id + '/batteries/')
        )

        #links to dict
        l = links.to_dict()

        #combine links and speed to one dict
        dump = dict(list(l.items()) + list(direction_db.items()))

        #return Response
        return Response(json.dumps(dump), 200, mimetype=JSONHAL)
示例#13
0
    def get(self, id, timestamp):
        '''
        :param id, timestamp:
        '''

        battery_db = g.con.get_battery(id, timestamp)

        if not battery_db:
            return create_error_response(404, "No battery data found",
                                         'There is no battery data on device id %s with timestamp %s' % (id, timestamp),
                                         'Battery')

        #create collection of links
        links = Collection(
            Self(),
            Link('device', '/wind/api/device/' + id + '/'),
            Link('list', '/wind/api/device/' + id + '/batteries/')
            # last one, no links to other resources except device and collection
        )

        #links to dict
        l = links.to_dict()

        #combine links and speed to one dict
        dump = dict(list(l.items()) + list(battery_db.items()))

        #return Response
        return Response(json.dumps(dump), 200, mimetype=JSONHAL)
示例#14
0
    def get(self, id, timestamp):
        '''
        :param id, timestamp:
        '''

        speed_db = g.con.get_speed(id, timestamp)

        if not speed_db:
             return create_error_response(404, "No speed found",
                                'There is no speed data on device id %s with timestamp %s' % (id, timestamp), 'Speed')

        #create collection of links
        links = Collection(
            Self(),
            Link('device', '/wind/api/device/' + id + '/'),
            Link('list', '/wind/api/device/' + id + '/speeds/'),
            Link('temperatures-all', '/wind/api/device/' + id + '/temperatures/')
        )

        #links to dict
        l = links.to_dict()

        #combine links and speed to one dict
        dump = dict(list(l.items()) + list(speed_db.items()))

        #return Response
        return Response(json.dumps(dump), 200, mimetype=JSONHAL)
示例#15
0
    def get(self, id):
        '''
        get all battery values on device
        :param id:
        '''

        batteries_db = g.con.get_batteries(id)

        if not batteries_db:
            return create_error_response(404, "No batteries found",
                                     'There is no batteries data on given device id %s' % id, 'Batteries')

        # create collection of links
        links = Collection(
            Self(),
            Link('device', '/wind/api/device/' + id + '/'),
            Link('battery', '/wind/api/device/' + id + '/battery/{timestamp}')
        )

        # links to dict
        l = links.to_dict()

        # combine links and speed to one dict
        dump = l
        dump.update({'items': batteries_db})

        # return Response
        return Response(json.dumps(dump), 200, mimetype=JSONHAL)
示例#16
0
    def get(self, id):
        humidities_db = g.con.get_humidities(id)

        if not humidities_db:
            return create_error_response(404, "No humidities found",
                                     'There is no humidities data on given device id %s' % id, 'Humidities')

        # create collection of links
        links = Collection(
            Self(),
            Link('device', '/wind/api/device/' + id + '/'),
            Link('humidity', '/wind/api/device/' + id + '/humidity/{timestamp}')
        )

        # links to dict
        l = links.to_dict()

        # combine links and speed to one dict
        dump = l
        dump.update({'items': humidities_db})

        # return Response
        return Response(json.dumps(dump), 200, mimetype=JSONHAL)
示例#17
0
    def get(self, id, timestamp):
        humidity_db = g.con.get_humidity(id, timestamp)
        if not humidity_db:
            return create_error_response(404, "No humidity found",
                                         'There is no humidity data on device id %s with timestamp %s' % (id, timestamp),
                                         'Humidity')

        #create collection of links
        links = Collection(
            Self(),
            Link('device', '/wind/api/device/' + id + '/'),
            Link('list', '/wind/api/humidities/'),
            Link('directions-all', '/wind/api/device/' + id + '/directions/')
        )

        #links to dict
        l = links.to_dict()

        #combine links and speed to one dict
        dump = dict(list(l.items()) + list(humidity_db.items()))

        #return Response
        return Response(json.dumps(dump), 200, mimetype=JSONHAL)
示例#18
0
    def test_only_valid_link_attrs_set(self):
        with self.app.test_request_context():
            l = Self(foo='foo', name='foo')

            assert not hasattr(l, 'foo')
            assert l.name == 'foo'