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
Beispiel #2
0
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
Beispiel #3
0
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_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)
Beispiel #5
0
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 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_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
Beispiel #8
0
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_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)
Beispiel #10
0
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_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_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
Beispiel #13
0
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)
Beispiel #15
0
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
Beispiel #17
0
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
    ])
Beispiel #18
0
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
Beispiel #19
0
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