def test_db_add(dbtransaction, model_test_params): """Test that one of each type of model can be added to the DB.""" for model_name, params in model_test_params: model = globals()[model_name] instance = model(**params) DBSession.add(instance) DBSession.flush() assert DBSession.query(model).count() == 1
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_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 sqlengine(request): """Return sql engine.""" engine = create_engine(TEST_DATABASE_URL) DBSession.configure(bind=engine) Base.metadata.create_all(engine) def teardown(): Base.metadata.drop_all(engine) request.addfinalizer(teardown) return engine
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_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 dbtransaction(request, sqlengine): """Create database transaction connection.""" connection = sqlengine.connect() transaction = connection.begin() DBSession.configure(bind=connection) def teardown(): transaction.rollback() connection.close() DBSession.remove() request.addfinalizer(teardown) return connection
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 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_photo_model_json(pre_pop_transaction, global_environ, photo_params, dummy_request): """Assert Photo __json__ value can be converted to and from json.""" import json photo = DBSession.query(Photo).first() json_string = json.dumps(photo.__json__(dummy_request)) assert isinstance(json_string, str) assert isinstance(json.loads(json_string), dict)
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_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 populate_from_data(objects): """Push the given list of photo dictionaries into the database.""" database_url = os.environ.get("MARS_DATABASE_URL", None) engine = create_engine(database_url) DBSession.configure(bind=engine) Base.metadata.create_all(engine) with transaction.manager: DBSession.add_all(objects) DBSession.flush()
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_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 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_populated_rel_photo_rover(pre_pop_transaction, global_environ): """Test that Rover-Photo relationships of prepopulated DB are correct.""" for photo in DBSession.query(Photo).all(): assert photo.rover_name for rover in DBSession.query(Rover).all(): assert rover.photos.count() > 1
def test_populated_rel_rover_camera(pre_pop_transaction, global_environ): """Test that Rover-Camera relationships of prepopulated DB are correct.""" for camera in DBSession.query(Camera).all(): assert camera.rover_name for rover in DBSession.query(Rover).all(): assert rover.cameras.count() > 1
def test_db_is_empty(dbtransaction, model_name): """Test that the database is empty of each model at test session start.""" model = globals()[model_name] query = DBSession.query(model) assert query.count() == 0
def test_pre_populate_sample_data_photos(pre_pop_transaction, global_environ): """Test that sample data can be loaded into DB.""" from mars_street_view.api_call import load_full_sample_data sample_data = load_full_sample_data() assert DBSession.query(Photo).count() == len(sample_data)
def teardown(): transaction.rollback() connection.close() DBSession.remove()
def test_populated_rel_photo_camera(pre_pop_transaction, global_environ): """Test that Camera-Photo relationships of prepopulated DB are correct.""" for photo in DBSession.query(Photo).all(): assert photo.camera_name