def update_bike_data(): """ Update the bike point data from the TfL API """ print "requesting more data from TfL" # Get the bike hire data from TfL's API app_id = os.environ.get('APP_ID',"") app_key = os.environ.get('APP_KEY',"") json_data = TfL( auth=(app_id, app_key) ).bikepoints() # Convert the returned data to BikePoint objects bikepoints = [] for j in json_data: if j['commonName'] != "Test Desktop ": bikepoints.append(BikePoint.from_json(j)) # Delete all the old BikePoint objects from the database db.session.query(BikePoint).delete() # Add the all the new BikePoint objects to the database db.session.add_all(bikepoints) # Commit the transaction db.session.commit()
def test_bike_point_from_json(self): test_jsons = [create_bike_point_json(i=i) for i in range(20)] for bp_json in test_jsons: db.session.add(BikePoint.from_json(bp_json)) db.session.commit() # assert that they were added to the DB correctly! for bp in db.session.query(BikePoint).all(): bp_json = test_jsons[int(bp.bp_id.split("_")[1])] assert bp_json['id'] == bp.bp_id assert bp_json['commonName'] == bp.name assert floats_same(bp_json['lat'], bp.lat) assert floats_same(bp_json['lon'], bp.lon)