Пример #1
0
    def test_multiple_trucks(self):
        nicks_tacos = FoodTruck('Nicks Tacos', 45.78985, 34.78998)
        nicks_tacos.fooditems = 'Tacos'

        brians_burgers = FoodTruck('Brians Burgers', 45.78985, 34.78998)
        brians_burgers.fooditems = 'Burgers'

        self.db_session.add(nicks_tacos)
        self.db_session.add(brians_burgers)

        self.db_session.commit()
        res = self.app.get('/food_trucks')
        self.assertEquals(res.status, '200 OK')
        expected_data = {
            u'items': [{
                u'latitude': u'45.78985',
                u'longitude': u'34.78998',
                u'name': u'Nicks Tacos',
                u'fooditems': 'Tacos'
            }, {
                u'fooditems': u'Burgers',
                u'latitude': u'45.78985',
                u'longitude': u'34.78998',
                u'name': u'Brians Burgers'
            }]
        }
        self.assertEquals(json.loads(res.data), expected_data)
Пример #2
0
def main():
    parser = argparse.ArgumentParser(description='Get the data from data.sfgov on food trucks and populate the db with it.')
    parser.add_argument('-u','--url', help='The database url to populate', required=True)
    args = vars(parser.parse_args())

    food_truck_db = args['url']
    
    truck_request = requests.get('http://data.sfgov.org/resource/rqzj-sfat.json')

    if truck_request.status_code != 200:
        raise Exception("Error getting data. Status code: %s, Message: %s" % truck_request.status, truck_request.data)

    truck_list = json.loads(truck_request.content)
    engine = get_engine(food_truck_db)
    session = get_db_session(engine)

    session.query(FoodTruck).delete()

    for t in truck_list:
        print t['applicant']
        try:
            new_truck = FoodTruck(t['applicant'], t['location']['latitude'], t['location']['longitude'])
            new_truck.fooditems = t['fooditems']
            session.add(new_truck)

            session.commit()
        except flask_sqlalchemy.sqlalchemy.exc.IntegrityError:
            session.rollback()
            continue
        except KeyError:
            continue
Пример #3
0
    def test_distance(self):
        nicks_tacos = FoodTruck('Nicks Tacos', 45.78985, 34.78998)
        nicks_tacos.fooditems = 'Tacos'

        brians_burgers = FoodTruck('Brians Burgers', 45.0, 34.0)
        brians_burgers.fooditems = 'Burgers'
        
        self.db_session.add(nicks_tacos)
        self.db_session.add(brians_burgers)
        
        self.db_session.commit()
        res = self.app.get('/food_trucks?current_location=45.78%2C34.78&distance=1')
        self.assertEquals(res.status, '200 OK')
        expected_data = {u'items': [{u'latitude': u'45.78985',
                                    u'longitude': u'34.78998',
                                    u'name': u'Nicks Tacos',
                                    u'fooditems': 'Tacos'}
                                    ]
                        }
        self.assertEquals(json.loads(res.data), expected_data)
Пример #4
0
    def test_basic(self):
        nicks_tacos = FoodTruck('Nicks Tacos', 45.78985, 34.78998)
        nicks_tacos.fooditems = 'Tacos'
        self.db_session.add(nicks_tacos)
        self.db_session.commit()

        results = self.db_session.query(FoodTruck).all()

        self.assertEquals(len(results), 1)
        self.assertEquals(results[0].__repr__(), u'<Truck: Nicks Tacos, Lat: 45.789850, Lng: 34.789980>')

        self.assertEquals(results[0]._serialise(), {'latitude': '45.78985', 'longitude': '34.78998', 'name': u'Nicks Tacos', u'fooditems': 'Tacos'})
Пример #5
0
    def test_single_truck(self):
        nicks_tacos = FoodTruck('Nicks Tacos', 45.78985, 34.78998)
        nicks_tacos.fooditems = 'Tacos'

        self.db_session.add(nicks_tacos)

        self.db_session.commit()
        res = self.app.get('/food_trucks/%d' % nicks_tacos.id)
        self.assertEquals(res.status, '200 OK')
        expected_data = {u'items': [{u'latitude': u'45.78985',
                                    u'longitude': u'34.78998',
                                    u'name': u'Nicks Tacos',
                                    u'fooditems': 'Tacos'}
                                    ]
                        }
        self.assertEquals(json.loads(res.data), expected_data)
Пример #6
0
    def test_single_truck(self):
        nicks_tacos = FoodTruck('Nicks Tacos', 45.78985, 34.78998)
        nicks_tacos.fooditems = 'Tacos'

        self.db_session.add(nicks_tacos)

        self.db_session.commit()
        res = self.app.get('/food_trucks/%d' % nicks_tacos.id)
        self.assertEquals(res.status, '200 OK')
        expected_data = {
            u'items': [{
                u'latitude': u'45.78985',
                u'longitude': u'34.78998',
                u'name': u'Nicks Tacos',
                u'fooditems': 'Tacos'
            }]
        }
        self.assertEquals(json.loads(res.data), expected_data)
Пример #7
0
    def test_basic(self):
        nicks_tacos = FoodTruck('Nicks Tacos', 45.78985, 34.78998)
        nicks_tacos.fooditems = 'Tacos'
        self.db_session.add(nicks_tacos)
        self.db_session.commit()

        results = self.db_session.query(FoodTruck).all()

        self.assertEquals(len(results), 1)
        self.assertEquals(
            results[0].__repr__(),
            u'<Truck: Nicks Tacos, Lat: 45.789850, Lng: 34.789980>')

        self.assertEquals(
            results[0]._serialise(), {
                'latitude': '45.78985',
                'longitude': '34.78998',
                'name': u'Nicks Tacos',
                u'fooditems': 'Tacos'
            })