def test_token_user_post_get_thumbnail_phot_id(upload_data_token, public_group, ztf_camera): obj_id = str(uuid.uuid4()) status, data = api('POST', 'sources', data={'id': obj_id, 'ra': 234.22, 'dec': -22.33, 'redshift': 3, 'transient': False, 'ra_dis': 2.3, 'group_ids': [public_group.id]}, token=upload_data_token) assert status == 200 assert data['data']['id'] == obj_id status, data = api('POST', 'photometry', data={'obj_id': obj_id, 'mjd': 58000., 'instrument_id': ztf_camera.id, 'flux': 12.24, 'fluxerr': 0.031, 'zp': 25., 'magsys': 'ab', 'filter': 'ztfg' }, token=upload_data_token) assert status == 200 assert data['status'] == 'success' photometry_id = data['data']['ids'][0] orig_source_thumbnail_count = len(DBSession.query(Obj).filter( Obj.id == obj_id).first().thumbnails) data = base64.b64encode(open(os.path.abspath('skyportal/tests/data/14gqr_new.png'), 'rb').read()) ttype = 'new' status, data = api('POST', 'thumbnail', data={'photometry_id': photometry_id, 'data': data, 'ttype': ttype }, token=upload_data_token) assert status == 200 assert data['status'] == 'success' thumbnail_id = data['data']['id'] assert isinstance(thumbnail_id, int) status, data = api( 'GET', f'thumbnail/{thumbnail_id}', token=upload_data_token) assert status == 200 assert data['status'] == 'success' assert data['data']['type'] == 'new' assert (DBSession.query(Thumbnail).filter(Thumbnail.id == thumbnail_id) .first().obj.id) == obj_id assert len(DBSession.query(Obj).filter(Obj.id == obj_id).first() .thumbnails) == orig_source_thumbnail_count + 1
def test_token_user_post_photometry_thumbnail(upload_data_token, public_source): thumbnails = [ {'data': base64.b64encode(open(os.path.abspath(f'skyportal/tests/data/14gqr_{suffix}.png'), 'rb').read()), 'ttype': suffix} for suffix in ['new', 'ref', 'sub'] ] status, data = api('POST', 'photometry', data={'source_id': str(public_source.id), 'observed_at': str(datetime.datetime.now()), 'time_format': 'iso', 'time_scale': 'utc', 'instrument_id': 1, 'mag': 12.24, 'e_mag': 0.031, 'lim_mag': 14.1, 'filter': 'V', 'thumbnails': thumbnails }, token=upload_data_token) assert status == 200 assert data['status'] == 'success' photometry_id = data['data']['ids'][0] status, data = api( 'GET', f'photometry/{photometry_id}', token=upload_data_token) assert status == 200 assert data['status'] == 'success' assert data['data']['photometry']['mag'] == 12.24 assert len(DBSession.query(Photometry).filter(Photometry.id == photometry_id) .first().thumbnails) == 3
def test_delete_photometry_cascades_to_thumbnail(manage_sources_token, public_source): for phot in public_source.photometry: if len(phot.thumbnails) > 0: photometry_id = phot.id thumbnail_id = phot.thumbnails[0].id break assert DBSession.query(Thumbnail).filter(Thumbnail.id == int(thumbnail_id)).count() > 0 status, data = api( 'DELETE', f'photometry/{photometry_id}', token=manage_sources_token) assert status == 200 assert DBSession.query(Thumbnail).filter(Thumbnail.id == int(thumbnail_id)).count() == 0
def test_delete_obj_deletes_thumbnail_file_on_disk(upload_data_token, super_admin_token, public_group): obj_id = str(uuid.uuid4()) status, data = api( 'POST', 'sources', data={ 'id': obj_id, 'ra': 234.22, 'dec': -22.33, 'redshift': 3, 'transient': False, 'ra_dis': 2.3, 'group_ids': [public_group.id], }, token=upload_data_token, ) assert status == 200 assert data['data']['id'] == obj_id thumbnail_data = base64.b64encode( open(os.path.abspath('skyportal/tests/data/14gqr_new.png'), 'rb').read()) ttype = 'new' status, data = api( 'POST', 'thumbnail', data={ 'obj_id': obj_id, 'data': thumbnail_data, 'ttype': ttype }, token=upload_data_token, ) assert status == 200 assert data['status'] == 'success' thumbnail_id = data['data']['id'] assert isinstance(thumbnail_id, int) status, data = api('GET', f'thumbnail/{thumbnail_id}', token=upload_data_token) assert status == 200 assert data['status'] == 'success' assert data['data']['type'] == ttype thumbnail = DBSession.query(Thumbnail).filter( Thumbnail.id == thumbnail_id).first() assert thumbnail.obj_id == obj_id fpath = thumbnail.file_uri assert os.path.exists(fpath) status, data = api('DELETE', f'objs/{obj_id}', token=super_admin_token) assert status == 200 assert data['status'] == 'success' assert not os.path.exists(fpath)
FollowupRequest, ) import astroplan print("Loading test configuration from _test_config.yaml") basedir = pathlib.Path(os.path.dirname(__file__)) cfg = load_config([(basedir / "../../test_config.yaml").absolute()]) set_server_url(f'http://localhost:{cfg["ports.app"]}') print("Setting test database to:", cfg["database"]) models.init_db(**cfg["database"]) # Add a "test factory" User so that all factory-generated comments have a # proper author, if it doesn't already exist (the user may already be in # there if running the test server and running tests individually) if not DBSession.query(User).filter(User.username == "test factory").scalar(): DBSession.add(User(username="******")) DBSession.commit() def pytest_runtest_setup(item): # Print timestamp when running each test print(datetime.now().strftime('[%H:%M:%S] '), end='') # set up a hook to be able to check if a test has failed @pytest.hookimpl(tryfirst=True, hookwrapper=True) def pytest_runtest_makereport(item, call): # execute all other hooks to obtain the report object outcome = yield rep = outcome.get_result()
#!/usr/bin/env python import datetime from skyportal.models import init_db, Candidate, Source, Obj, DBSession from baselayer.app.env import load_env env, cfg = load_env() init_db(**cfg["database"]) try: n_days = int(cfg["misc.days_to_keep_unsaved_candidates"]) except ValueError: raise ValueError("Invalid (non-integer) value provided for " "days_to_keep_unsaved_candidates in config file.") if not 1 <= n_days <= 30: raise ValueError( "days_to_keep_unsaved_candidates must be an integer between 1 and 30") cutoff_datetime = datetime.datetime.now() - datetime.timedelta(days=n_days) n_deleted = (DBSession().query(Obj).filter( Obj.id.in_(DBSession.query(Candidate.obj_id))).filter( Obj.id.notin_(DBSession.query( Source.obj_id))).filter(Obj.created_at <= cutoff_datetime).delete( synchronize_session='fetch')) DBSession.commit() print(f"Deleted {n_deleted} unsaved candidates.")
def test_token_user_delete_thumbnail_cascade_source(upload_data_token, super_admin_token, public_group, ztf_camera): obj_id = str(uuid.uuid4()) status, data = api( 'POST', 'sources', data={ 'id': obj_id, 'ra': 234.22, 'dec': -22.33, 'redshift': 3, 'transient': False, 'ra_dis': 2.3, 'group_ids': [public_group.id], }, token=upload_data_token, ) assert status == 200 assert data['data']['id'] == obj_id orig_source_thumbnail_count = len( DBSession.query(Obj).filter(Obj.id == obj_id).first().thumbnails) data = base64.b64encode( open(os.path.abspath('skyportal/tests/data/14gqr_new.png'), 'rb').read()) ttype = 'new' status, data = api( 'POST', 'thumbnail', data={ 'obj_id': obj_id, 'data': data, 'ttype': ttype }, token=upload_data_token, ) assert status == 200 assert data['status'] == 'success' thumbnail_id = data['data']['id'] assert isinstance(thumbnail_id, int) status, data = api('GET', f'thumbnail/{thumbnail_id}', token=upload_data_token) assert status == 200 assert data['status'] == 'success' assert data['data']['type'] == 'new' assert (DBSession.query(Thumbnail).filter( Thumbnail.id == thumbnail_id).first().obj.id) == obj_id assert (len( DBSession.query(Obj).filter(Obj.id == obj_id).first().thumbnails) == orig_source_thumbnail_count + 1) status, data = api('DELETE', f'thumbnail/{thumbnail_id}', token=super_admin_token) assert status == 200 assert data['status'] == 'success' assert (len( DBSession.query(Obj).filter(Obj.id == obj_id).first().thumbnails) == orig_source_thumbnail_count)
AnnotationFactory, ClassificationFactory, FollowupRequestFactory, AllocationFactory, InvitationFactory, NotificationFactory, UserNotificationFactory, ThumbnailFactory, ) from skyportal.tests.fixtures import TMP_DIR # noqa: F401 from skyportal.models import Obj # Add a "test factory" User so that all factory-generated comments have a # proper author, if it doesn't already exist (the user may already be in # there if running the test server and running tests individually) if not DBSession.query(User).filter(User.username == "test factory").scalar(): DBSession.add(User(username="******")) DBSession.commit() # Also add the test driver user (testuser-cesium-ml-org) if needed so that the driver # fixture has a user to login as (without needing an invitation token). # With invitations enabled on the test configs, the driver fails to login properly # without this user because the authenticator looks for the user or an # invitation token when neither exists initially on fresh test databases. if not DBSession.query(User).filter( User.username == "testuser-cesium-ml-org").scalar(): DBSession.add( User(username="******", oauth_uid="*****@*****.**")) DBSession.commit()
def test_token_user_post_get_thumbnail(upload_data_token, public_group): source_id = str(uuid.uuid4()) status, data = api('POST', 'sources', data={ 'id': source_id, 'ra': 234.22, 'dec': -22.33, 'redshift': 3, 'transient': False, 'ra_dis': 2.3, 'group_ids': [public_group.id] }, token=upload_data_token) assert status == 200 assert data['data']['id'] == source_id status, data = api('POST', 'photometry', data={ 'source_id': source_id, 'observed_at': str(datetime.datetime.now()), 'time_format': 'iso', 'time_scale': 'utc', 'instrument_id': 1, 'mag': 12.24, 'e_mag': 0.031, 'lim_mag': 14.1, 'filter': 'V' }, token=upload_data_token) assert status == 200 assert data['status'] == 'success' orig_source_thumbnail_count = len( DBSession.query(Source).filter( Source.id == source_id).first().thumbnails) data = base64.b64encode( open(os.path.abspath('skyportal/tests/data/14gqr_new.png'), 'rb').read()) ttype = 'new' status, data = api('POST', 'thumbnail', data={ 'source_id': source_id, 'data': data, 'ttype': ttype }, token=upload_data_token) assert status == 200 assert data['status'] == 'success' thumbnail_id = data['data']['id'] assert isinstance(thumbnail_id, int) status, data = api('GET', f'thumbnail/{thumbnail_id}', token=upload_data_token) assert status == 200 assert data['status'] == 'success' assert data['data']['thumbnail']['type'] == 'new' assert (DBSession.query(Thumbnail).filter( Thumbnail.id == thumbnail_id).first().source.id) == source_id assert len( DBSession.query(Source).filter(Source.id == source_id).first(). thumbnails) == orig_source_thumbnail_count + 1