def test_rov_sol_empty(dbtransaction, global_environ, rover_params, sol): """Test get_rov_sol return value for Rover with no cameras or photos.""" rover = rover_params['name'] DBSession.add(Rover(**rover_params)) DBSession.flush() # expected = {'rover': rover, 'sol': sol, 'photos_by_cam': {}} # assert Photo.get_rov_sol(rover, sol) == expected with pytest.raises(ValueError): Photo.get_rov_sol(rover, sol)
def test_rov_sol_empty(dbtransaction, global_environ, rover_params, sol): """Test get_rov_sol return value for Rover with no cameras or photos.""" rover = rover_params['name'] DBSession.add(Rover(**rover_params)) DBSession.flush() # expected = {'rover': rover, 'sol': sol, 'photos_by_cam': {}} # assert Photo.get_rov_sol(rover, sol) == expected with pytest.raises(ValueError): Photo.get_rov_sol(rover, sol)
def test_get_rov_sol_too_big(dbtransaction, global_environ, photo_params, rover_params, camera_params): """Compare sol with no photos to next sol with photos. Should be equal.""" rover = rover_params['name'] sol = photo_params['sol'] photo = Photo(**photo_params) DBSession.add(Rover(**rover_params)) DBSession.add(Camera(**camera_params)) DBSession.add(photo) DBSession.flush() assert Photo.get_rov_sol(rover, sol + 500) == Photo.get_rov_sol(rover, sol)
def test_get_rov_sol_too_big(dbtransaction, global_environ, photo_params, rover_params, camera_params): """Compare sol with no photos to next sol with photos. Should be equal.""" rover = rover_params['name'] sol = photo_params['sol'] photo = Photo(**photo_params) DBSession.add(Rover(**rover_params)) DBSession.add(Camera(**camera_params)) DBSession.add(photo) DBSession.flush() assert Photo.get_rov_sol(rover, sol + 500) == Photo.get_rov_sol(rover, sol)
def test_rov_sol_one_camera(dbtransaction, global_environ, rover_params, camera_params, sol): """Test get_rov_sol return value for a Rover with one Camera.""" rover = rover_params['name'] # camera = '_'.join((rover, camera_params['name'])) DBSession.add(Rover(**rover_params)) DBSession.add(Camera(**camera_params)) DBSession.flush() # expected = {'rover': rover, 'sol': sol, 'photos_by_cam': {camera: []}} # assert Photo.get_rov_sol(rover, sol) == expected with pytest.raises(ValueError): Photo.get_rov_sol(rover, sol)
def test_rov_sol_one_camera(dbtransaction, global_environ, rover_params, camera_params, sol): """Test get_rov_sol return value for a Rover with one Camera.""" rover = rover_params['name'] # camera = '_'.join((rover, camera_params['name'])) DBSession.add(Rover(**rover_params)) DBSession.add(Camera(**camera_params)) DBSession.flush() # expected = {'rover': rover, 'sol': sol, 'photos_by_cam': {camera: []}} # assert Photo.get_rov_sol(rover, sol) == expected with pytest.raises(ValueError): Photo.get_rov_sol(rover, sol)
def test_rov_sol_lots(pre_pop_transaction, global_environ, rover_name, sol): """Test get_rov_sol returns correct camera names on pre-populated DB.""" result = Photo.get_rov_sol(rover_name, sol) rover = DBSession.query(Rover).filter(Rover.name == rover_name).one() cam_name_list = [camera.name for camera in rover.cameras] assert sorted(cam_name_list) == sorted(list( result['photos_by_cam'].keys()))
def test_rov_sol_photos(pre_pop_transaction, global_environ, rover_name, sol): """Check that all contents of data are Photo objects.""" result = Photo.get_rov_sol(rover_name, sol) photo_list = [photo for photos in result['photos_by_cam'].values() for photo in photos] assert len(photo_list) > 0 and all([isinstance(photo, Photo) for photo in photo_list])
def test_photos_sorted(pre_pop_transaction, rover_name, sol, camera): """Second test that pre-populated photos will come in sorted order.""" from mars_street_view.models import Photo data = Photo.get_rov_sol(rover_name, sol) photos_by_cam = data.get('photos_by_cam', {}) photos = photos_by_cam.get(camera, []) urls_from_method = [photo.img_src for photo in photos] assert urls_from_method == list(sorted(urls_from_method))
def test_left_lens_only(pre_pop_transaction, global_environ, rover_name, sol): """Check that get_sol_rov returns no photos from right lens.""" result = Photo.get_rov_sol(rover_name, sol) photo_list = [photo for photos in result['photos_by_cam'].values() for photo in photos] assert photo_list and all([photo.img_src[-11] != 'R' for photo in photo_list])
def test_rov_sol_lots(pre_pop_transaction, global_environ, rover_name, sol): """Test get_rov_sol returns correct camera names on pre-populated DB.""" result = Photo.get_rov_sol(rover_name, sol) rover = DBSession.query(Rover).filter(Rover.name == rover_name).one() cam_name_list = [camera.name for camera in rover.cameras] assert sorted(cam_name_list) == sorted(list( result['photos_by_cam'].keys()))
def test_rov_sol_photos(pre_pop_transaction, global_environ, rover_name, sol): """Check that all contents of data are Photo objects.""" result = Photo.get_rov_sol(rover_name, sol) photo_list = [ photo for photos in result['photos_by_cam'].values() for photo in photos ] assert len(photo_list) > 0 and all( [isinstance(photo, Photo) for photo in photo_list])
def test_left_lens_only(pre_pop_transaction, global_environ, rover_name, sol): """Check that get_sol_rov returns no photos from right lens.""" result = Photo.get_rov_sol(rover_name, sol) photo_list = [ photo for photos in result['photos_by_cam'].values() for photo in photos ] assert photo_list and all( [photo.img_src[-11] != 'R' for photo in photo_list])
def test_full_params(dbtransaction, full_photo_params): """Test that a full set of params from NASA API will construct models.""" rover = Rover(**full_photo_params['rover']) camera = Camera(**full_photo_params['camera']) # cam_short_name = full_photo_params['camera']['name'] # camera_name = '_'.join((rover_name, cam_short_name)) photo = Photo(**full_photo_params) DBSession.add_all([rover, camera, photo]) DBSession.flush() assert photo.rover_name == rover.name and photo.camera_name == camera.name
def test_photos_seq(pre_pop_transaction, rover_name, sol, camera): """Test that pre-populated photos will be retrieved in url sorted order.""" from mars_street_view.models import Photo data = Photo.get_rov_sol(rover_name, sol) photos_by_cam = data.get('photos_by_cam', {}) photos = photos_by_cam.get(camera, []) urls_from_method = [photo.img_src for photo in photos] prev_url = '' for url in urls_from_method: assert url > prev_url prev_url = url
def test_rov_sol_one_photo(dbtransaction, global_environ, rover_params, camera_params, photo_params): """Test return value for get_rov_sol with one Photo.""" rover = rover_params['name'] sol = photo_params['sol'] camera = '_'.join((rover, camera_params['name'])) photo = Photo(**photo_params) DBSession.add(Rover(**rover_params)) DBSession.add(Camera(**camera_params)) DBSession.add(photo) DBSession.flush() expected = { 'rover': rover, 'sol': sol, 'photos_by_cam': { camera: [photo] }, 'last_day': True, 'first_day': False } assert Photo.get_rov_sol(rover, sol) == expected
def test_photo_rover_relationship(dbtransaction, rover_params, camera_params, photo_params): """Test Rover, Photo have correct relationship after initialization.""" rover = Rover(**rover_params) camera_params['rover_name'] = rover.name camera_params['rover_name'] = rover.name camera = Camera(**camera_params) photo_params['camera_name'] = camera.name photo = Photo(**photo_params) DBSession.add_all([rover, camera, photo]) DBSession.flush() assert photo.rover_name == rover.name
def test_rov_sol_one_photo(dbtransaction, global_environ, rover_params, camera_params, photo_params): """Test return value for get_rov_sol with one Photo.""" rover = rover_params['name'] sol = photo_params['sol'] camera = '_'.join((rover, camera_params['name'])) photo = Photo(**photo_params) DBSession.add(Rover(**rover_params)) DBSession.add(Camera(**camera_params)) DBSession.add(photo) DBSession.flush() expected = {'rover': rover, 'sol': sol, 'photos_by_cam': {camera: [photo]}, 'last_day': True, 'first_day': False} assert Photo.get_rov_sol(rover, sol) == expected
def test_photo_camera_relationship(dbtransaction, rover_params, camera_params, photo_params): """Test that Photo can access Camera's attributes through relationship.""" rover = Rover(**rover_params) camera_params['rover_name'] = rover.name camera_params['rover_name'] = rover.name camera = Camera(**camera_params) photo_params['camera_name'] = camera.name photo = Photo(**photo_params) DBSession.add_all([rover, camera, photo]) DBSession.flush() assert all([ getattr(camera, attr) == getattr(photo.camera, attr) for attr in camera_params ])
def pre_pop_transaction(request, sqlengine): """Create database transaction connection.""" from mars_street_view.models import init_rovers_and_cameras from mars_street_view.api_call import load_full_sample_data connection = sqlengine.connect() transaction = connection.begin() DBSession.configure(bind=connection) init_rovers_and_cameras() rov_cam_data = init_rovers_and_cameras() DBSession.add_all(rov_cam_data) sample_data = load_full_sample_data() DBSession.add_all([Photo(**result) for result in sample_data]) DBSession.flush() def teardown(): transaction.rollback() connection.close() DBSession.remove() request.addfinalizer(teardown) return connection
def test_photo_init_error_no_camera(full_photo_params): """Ensure that Photo throws an error when initializing without a camera.""" del full_photo_params['camera'] with pytest.raises(KeyError): Photo(**full_photo_params)
def test_photo_init_error_no_camera_name(full_photo_params): """Ensure that Photo throws an error when camera has no name.""" del full_photo_params['camera']['name'] with pytest.raises(KeyError): Photo(**full_photo_params)
def populate_sample_data(): """Put all photos from sample json data into database.""" results = load_full_sample_data() photo_list = [Photo(**result) for result in results] populate_from_data(photo_list)
def populate_one_sol(rover, sol, fetch): """Initialize database, query API and write models to database.""" results = get_one_sol(rover, sol, fetch) photo_list = [Photo(**result) for result in results] populate_from_data(photo_list)