def update_live_locations(): # Delete existing data from vehicles Vehicle.query.delete() # Get live vehicles as json data json_data = requests.get(API_VEHICLES, headers=API_HEADER) vehicle_dict = json.loads(json_data.text) # Array of service objects vehicles = vehicle_dict["vehicles"] # Update the vehicle table's last_updated field vehicles_date = datetime.fromtimestamp(int(vehicle_dict["last_updated"])) update_table_date("vehicle", vehicles_date) # Create a vehicle instance of each vehicle. for vehicle in vehicles: new_vehicle = Vehicle() new_vehicle.vehicle_id = vehicle["vehicle_id"] new_vehicle.destination = vehicle["destination"] new_vehicle.speed = vehicle["speed"] new_vehicle.heading = vehicle["heading"] new_vehicle.latitude = vehicle["latitude"] new_vehicle.longitude = vehicle["longitude"] new_vehicle.service_name = vehicle["service_name"] db_session.add(new_vehicle) db_session.commit() db_session.flush()
def insert_invalid_null(self): new_vehicle = Vehicle() new_vehicle.vehicle_id = None # Should not be null, so we expect and exception new_vehicle.service_name = "test" new_vehicle.destination = "test" new_vehicle.heading = 50 new_vehicle.latitude = 4.00 new_vehicle.longitude = 5.00 new_vehicle.speed = 40 db_session.add(new_vehicle) db_session.commit() db_session.flush()
def test_successful_insert(self): # Create sample vehicle and insert it into our test database new_vehicle = Vehicle() new_vehicle.vehicle_id = 5 new_vehicle.service_name = "test" new_vehicle.destination = "test" new_vehicle.heading = 50 new_vehicle.latitude = 4.00 new_vehicle.longitude = 5.00 new_vehicle.speed = 40 db_session.add(new_vehicle) db_session.commit() db_session.flush() vehicles = Vehicle.query.all() self.assertEqual(len(vehicles), 1) test_vehicle = vehicles[0] self.assertEqual(test_vehicle.service_name, "test")