def test_walker_create_vso_instrument(vso_session): entries = walker.create(vso.attrs.Instrument("RHESSI"), vso_session) assert entries == [ tables.DatabaseEntry( id=1, source=u"RHESSI", provider=u"LSSP", physobs=u"intensity", fileid=u"/hessidata/2011/09/20/hsi_20110920_010920", observation_time_start=datetime(2011, 9, 20, 1, 9, 20), observation_time_end=datetime(2011, 9, 20, 2, 27, 40), instrument=u"RHESSI", size=-1.0, wavemin=0.4132806430668068, wavemax=7.293187818826002e-05, ), tables.DatabaseEntry( id=2, source=u"RHESSI", provider=u"LSSP", physobs=u"intensity", fileid=u"/hessidata/2011/09/19/hsi_20110919_233340", observation_time_start=datetime(2011, 9, 19, 23, 33, 40), observation_time_end=datetime(2011, 9, 20, 1, 9, 20), instrument=u"RHESSI", size=-1.0, wavemin=0.4132806430668068, wavemax=7.293187818826002e-05, ), ]
def test_walker_create_vso_instrument(vso_session): entries = walker.create(vso.attrs.Instrument('RHESSI'), vso_session) # TODO: Flaky expected = [ tables.DatabaseEntry(id=1, source=u'RHESSI', provider=u'LSSP', physobs=u'intensity', fileid=u'/hessidata/2011/09/20/hsi_20110920_010920', observation_time_start=datetime(2011, 9, 20, 1, 9, 20), observation_time_end=datetime(2011, 9, 20, 2, 27, 40), instrument=u'RHESSI', wavemin=0.4132806579880238, wavemax=7.293188082141598e-05), tables.DatabaseEntry(id=2, source=u'RHESSI', provider=u'LSSP', physobs=u'intensity', fileid=u'/hessidata/2011/09/19/hsi_20110919_233340', observation_time_start=datetime(2011, 9, 19, 23, 33, 40), observation_time_end=datetime(2011, 9, 20, 1, 9, 20), instrument=u'RHESSI', wavemin=0.4132806579880238, wavemax=7.293188082141598e-05)] expected_2 = [ tables.DatabaseEntry(id=1, source=u'RHESSI', provider=u'LSSP', physobs=u'intensity', fileid=u'/hessidata/2011/09/19/hsi_20110919_233340', observation_time_start=datetime(2011, 9, 19, 23, 33, 40), observation_time_end=datetime(2011, 9, 20, 1, 9, 20), instrument=u'RHESSI', wavemin=0.4132806579880238, wavemax=7.293188082141598e-05), tables.DatabaseEntry(id=2, source=u'RHESSI', provider=u'LSSP', physobs=u'intensity', fileid=u'/hessidata/2011/09/20/hsi_20110920_010920', observation_time_start=datetime(2011, 9, 20, 1, 9, 20), observation_time_end=datetime(2011, 9, 20, 2, 27, 40), instrument=u'RHESSI', wavemin=0.4132806579880238, wavemax=7.293188082141598e-05)] assert len(entries) == len(expected) assert entries in [expected, expected_2]
def test_walker_create_vso_instrument(vso_session): entries = walker.create(vso.attrs.Instrument('AIA'), vso_session) assert entries == [ tables.DatabaseEntry(id=1, source='SDO', provider='JSOC', physobs='intensity', fileid='aia__lev1:193:1086652831', observation_time_start=datetime(2011, 6, 8, 23, 59, 55), observation_time_end=datetime(2011, 6, 8, 23, 59, 56), instrument='AIA', size=66200.0, wavemin=19.3, wavemax=19.3), tables.DatabaseEntry(id=2, source='SDO', provider='JSOC', physobs='intensity', fileid='aia__lev1:94:1086652832', observation_time_start=datetime(2011, 6, 8, 23, 59, 56), observation_time_end=datetime(2011, 6, 8, 23, 59, 57), instrument='AIA', size=66200.0, wavemin=9.4, wavemax=9.4), tables.DatabaseEntry(id=3, source='SDO', provider='JSOC', physobs='intensity', fileid='aia__lev1:131:1086652833', observation_time_start=datetime(2011, 6, 8, 23, 59, 57), observation_time_end=datetime(2011, 6, 8, 23, 59, 58), instrument='AIA', size=66200.0, wavemin=13.1, wavemax=13.1), tables.DatabaseEntry(id=4, source='SDO', provider='JSOC', physobs='intensity', fileid='aia__lev1:171:1086652835', observation_time_start=datetime(2011, 6, 9, 0, 0), observation_time_end=datetime(2011, 6, 9, 0, 0, 1), instrument='AIA', size=66200.0, wavemin=17.1, wavemax=17.1), tables.DatabaseEntry(id=5, source='SDO', provider='JSOC', physobs='intensity', fileid='aia__lev1:211:1086652836', observation_time_start=datetime(2011, 6, 9, 0, 0), observation_time_end=datetime(2011, 6, 9, 0, 0, 1), instrument='AIA', size=66200.0, wavemin=21.1, wavemax=21.1)]
def test_walker_create_downloadtime_inverted(session): tag = tables.Tag('foo') tag.id = 1 fits_header_entry = tables.FitsHeaderEntry('INSTRUME', 'EIT') fits_header_entry.id = 1 download_time = ~DownloadTime( datetime(2005, 6, 15, 7), datetime(2005, 6, 15, 9)) entries = walker.create(download_time, session) assert len(entries) == 7 assert entries == [ tables.DatabaseEntry( id=1, download_time=datetime(2005, 6, 15, 1)), tables.DatabaseEntry( id=2, starred=True, download_time=datetime(2005, 6, 15, 2)), tables.DatabaseEntry( id=3, path='/tmp', download_time=datetime(2005, 6, 15, 3)), tables.DatabaseEntry( id=4, starred=True, download_time=datetime(2005, 6, 15, 4)), tables.DatabaseEntry( id=5, tags=[tag], download_time=datetime(2005, 6, 15, 5)), tables.DatabaseEntry( id=6, starred=True, path='/tmp', download_time=datetime(2005, 6, 15, 6)), tables.DatabaseEntry( id=10, starred=True, tags=[tag], download_time=datetime(2005, 6, 15, 10), fits_header_entries=[fits_header_entry])]
def test_walker_create_downloadtime_exists(session): download_time = DownloadTime(datetime(2005, 6, 15, 7), datetime(2005, 6, 15, 9)) entries = walker.create(download_time, session) assert entries == [ tables.DatabaseEntry(id=7, download_time=datetime(2005, 6, 15, 7)), tables.DatabaseEntry(id=8, starred=True, download_time=datetime(2005, 6, 15, 8)), tables.DatabaseEntry(id=9, path="/tmp", download_time=datetime(2005, 6, 15, 9)), ]
def query(self, *query, **kwargs): """ query(*query[, sortby]) Send the given query to the database and return a list of database entries that satisfy all of the given attributes. Apart from the attributes supported by the VSO interface, the following attributes are supported: - :class:`sunpy.database.attrs.Starred` - :class:`sunpy.database.attrs.Tag` - :class:`sunpy.database.attrs.Path` - :class:`sunpy.database.attrs.DownloadTime` - :class:`sunpy.database.attrs.FitsHeaderEntry` An important difference to the VSO attributes is that these attributes may also be used in negated form using the tilde ~ operator. Parameters ---------- query : list A variable number of attributes that are chained together via the boolean AND operator. The | operator may be used between attributes to express the boolean OR operator. sortby : str, optional The column by which to sort the returned entries. The default is to sort by the start of the observation. See the attributes of :class:`sunpy.database.tables.DatabaseEntry` for a list of all possible values. Raises ------ TypeError if no attribute is given or if some keyword argument other than 'sortby' is given. Examples -------- The query in the following example searches for all non-starred entries with the tag 'foo' or 'bar' (or both). >>> database.query(~attrs.Starred(), attrs.Tag('foo') | attrs.Tag('bar')) """ if not query: raise TypeError('at least one attribute required') sortby = kwargs.pop('sortby', 'observation_time_start') if kwargs: k, v = kwargs.popitem() raise TypeError('unexpected keyword argument {0!r}'.format(k)) return sorted( walker.create(and_(*query), self.session), key=operator.attrgetter(sortby))
def test_walker_create_path_attr_exists(session): entries = walker.create(Path('/tmp'), session) assert len(entries) == 3 assert tables.DatabaseEntry( id=3, path='/tmp', download_time=datetime(2005, 6, 15, 3)) in entries assert tables.DatabaseEntry( id=6, path='/tmp', starred=True, download_time=datetime(2005, 6, 15, 6)) in entries assert tables.DatabaseEntry( id=9, path='/tmp', download_time=datetime(2005, 6, 15, 9)) in entries
def test_walker_create_anded_query(session): entries = walker.create(Tag('foo') & Starred(), session) assert len(entries) == 1 tag = tables.Tag('foo') tag.id = 1 fits_header_entry = tables.FitsHeaderEntry('INSTRUME', 'EIT') fits_header_entry.id = 1 assert tables.DatabaseEntry( id=10, starred=True, tags=[tag], download_time=datetime(2005, 6, 15, 10), fits_header_entries=[fits_header_entry]) in entries
def test_walker_create_fitsheader(session): tag = tables.Tag('foo') tag.id = 1 entries = walker.create(FitsHeaderEntry('INSTRUME', 'EIT'), session) fits_header_entry = tables.FitsHeaderEntry('INSTRUME', 'EIT') fits_header_entry.id = 1 assert len(entries) == 1 assert entries == [tables.DatabaseEntry( id=10, starred=True, tags=[tag], download_time=datetime(2005, 6, 15, 10), fits_header_entries=[fits_header_entry])]
def test_walker_create_starred_false(session): entries = walker.create(~Starred(), session) tag = tables.Tag("foo") tag.id = 1 assert len(entries) == 5 assert entries == [ tables.DatabaseEntry(id=1, download_time=datetime(2005, 6, 15, 1)), tables.DatabaseEntry(id=3, path="/tmp", download_time=datetime(2005, 6, 15, 3)), tables.DatabaseEntry(id=5, tags=[tag], download_time=datetime(2005, 6, 15, 5)), tables.DatabaseEntry(id=7, download_time=datetime(2005, 6, 15, 7)), tables.DatabaseEntry(id=9, path="/tmp", download_time=datetime(2005, 6, 15, 9)), ]
def test_walker_create_downloadtime_exists(session): download_time = DownloadTime(datetime(2005, 6, 15, 7), datetime(2005, 6, 15, 9)) entries = walker.create(download_time, session) assert entries == [ tables.DatabaseEntry(id=7, download_time=datetime(2005, 6, 15, 7)), tables.DatabaseEntry(id=8, starred=True, download_time=datetime(2005, 6, 15, 8)), tables.DatabaseEntry(id=9, path='/tmp', download_time=datetime(2005, 6, 15, 9)) ]
def test_walker_create_anded_query(session): entries = walker.create(Tag('foo') & Starred(), session) assert len(entries) == 1 tag = tables.Tag('foo') tag.id = 1 fits_header_entry = tables.FitsHeaderEntry('INSTRUME', 'EIT') fits_header_entry.id = 1 assert tables.DatabaseEntry(id=10, starred=True, tags=[tag], download_time=datetime(2005, 6, 15, 10), fits_header_entries=[fits_header_entry ]) in entries
def test_walker_create_tag_negative(session): entries = walker.create(~Tag("foo"), session) assert len(entries) == 8 assert entries == [ tables.DatabaseEntry(id=1, download_time=datetime(2005, 6, 15, 1)), tables.DatabaseEntry(id=2, starred=True, download_time=datetime(2005, 6, 15, 2)), tables.DatabaseEntry(id=3, path="/tmp", download_time=datetime(2005, 6, 15, 3)), tables.DatabaseEntry(id=4, starred=True, download_time=datetime(2005, 6, 15, 4)), tables.DatabaseEntry(id=6, path="/tmp", starred=True, download_time=datetime(2005, 6, 15, 6)), tables.DatabaseEntry(id=7, download_time=datetime(2005, 6, 15, 7)), tables.DatabaseEntry(id=8, starred=True, download_time=datetime(2005, 6, 15, 8)), tables.DatabaseEntry(id=9, path="/tmp", download_time=datetime(2005, 6, 15, 9)), ]
def test_walker_create_time(vso_session): time = vso.attrs.Time( datetime(2011, 9, 17, 0, 0, 0), datetime(2011, 9, 20, 0, 0, 0)) entries = walker.create(time, vso_session) assert len(entries) == 1 assert entries == [ tables.DatabaseEntry(id=2, source=u'RHESSI', provider=u'LSSP', physobs=u'intensity', fileid=u'/hessidata/2011/09/19/hsi_20110919_233340', observation_time_start=datetime(2011, 9, 19, 23, 33, 40), observation_time_end=datetime(2011, 9, 20, 1, 9, 20), instrument=u'RHESSI', size=-1.0, wavemin=0.4132806430668068, wavemax=7.293187818826002e-05)]
def test_walker_create_time(vso_session): time = vso.attrs.Time( datetime(2011, 9, 17, 0, 0, 0), datetime(2011, 9, 20, 0, 0, 0)) entries = walker.create(time, vso_session) assert len(entries) == 1 assert entries == [ tables.DatabaseEntry(id=2, source=u'RHESSI', provider=u'LSSP', physobs=u'intensity', fileid=u'/hessidata/2011/09/19/hsi_20110919_233340', observation_time_start=datetime(2011, 9, 19, 23, 33, 40), observation_time_end=datetime(2011, 9, 20, 1, 9, 20), instrument=u'RHESSI', size=-1.0, wavemin=0.4132806579880238, wavemax=7.293188082141598e-05)]
def test_walker_create_fitsheader(session): tag = tables.Tag('foo') tag.id = 1 entries = walker.create(FitsHeaderEntry('INSTRUME', 'EIT'), session) fits_header_entry = tables.FitsHeaderEntry('INSTRUME', 'EIT') fits_header_entry.id = 1 assert len(entries) == 1 assert entries == [ tables.DatabaseEntry(id=10, starred=True, tags=[tag], download_time=datetime(2005, 6, 15, 10), fits_header_entries=[fits_header_entry]) ]
def test_walker_create_wave(vso_session): entries = walker.create(vso.attrs.Wave(100, 180), vso_session) assert len(entries) == 2 assert entries == [ tables.DatabaseEntry(id=3, source='SDO', provider='JSOC', physobs='intensity', fileid='aia__lev1:131:1086652833', observation_time_start=datetime(2011, 6, 8, 23, 59, 57), observation_time_end=datetime(2011, 6, 8, 23, 59, 58), instrument='AIA', size=66200.0, wavemin=13.1, wavemax=13.1), tables.DatabaseEntry(id=4, source='SDO', provider='JSOC', physobs='intensity', fileid='aia__lev1:171:1086652835', observation_time_start=datetime(2011, 6, 9, 0, 0), observation_time_end=datetime(2011, 6, 9, 0, 0, 1), instrument='AIA', size=66200.0, wavemin=17.1, wavemax=17.1)]
def test_walker_create_starred_false(session): entries = walker.create(~Starred(), session) tag = tables.Tag('foo') tag.id = 1 assert len(entries) == 5 assert entries == [ tables.DatabaseEntry(id=1, download_time=datetime(2005, 6, 15, 1)), tables.DatabaseEntry( id=3, path='/tmp', download_time=datetime(2005, 6, 15, 3)), tables.DatabaseEntry( id=5, tags=[tag], download_time=datetime(2005, 6, 15, 5)), tables.DatabaseEntry( id=7, download_time=datetime(2005, 6, 15, 7)), tables.DatabaseEntry( id=9, path='/tmp', download_time=datetime(2005, 6, 15, 9))]
def test_walker_create_fitsheader_inverted(session): tag = tables.Tag("foo") tag.id = 1 entries = walker.create(~FitsHeaderEntry("INSTRUME", "EIT"), session) assert len(entries) == 9 assert entries == [ tables.DatabaseEntry(id=1, download_time=datetime(2005, 6, 15, 1)), tables.DatabaseEntry(id=2, starred=True, download_time=datetime(2005, 6, 15, 2)), tables.DatabaseEntry(id=3, path="/tmp", download_time=datetime(2005, 6, 15, 3)), tables.DatabaseEntry(id=4, starred=True, download_time=datetime(2005, 6, 15, 4)), tables.DatabaseEntry(id=5, tags=[tag], download_time=datetime(2005, 6, 15, 5)), tables.DatabaseEntry(id=6, starred=True, path="/tmp", download_time=datetime(2005, 6, 15, 6)), tables.DatabaseEntry(id=7, download_time=datetime(2005, 6, 15, 7)), tables.DatabaseEntry(id=8, starred=True, download_time=datetime(2005, 6, 15, 8)), tables.DatabaseEntry(id=9, path="/tmp", download_time=datetime(2005, 6, 15, 9)), ]
def test_walker_create_path_attr_exists(session): entries = walker.create(Path('/tmp'), session) assert len(entries) == 3 assert tables.DatabaseEntry(id=3, path='/tmp', download_time=datetime(2005, 6, 15, 3)) in entries assert tables.DatabaseEntry(id=6, path='/tmp', starred=True, download_time=datetime(2005, 6, 15, 6)) in entries assert tables.DatabaseEntry(id=9, path='/tmp', download_time=datetime(2005, 6, 15, 9)) in entries
def test_walker_create_tag_positive(session): entries = walker.create(Tag("foo"), session) tag = tables.Tag("foo") tag.id = 1 fits_header_entry = tables.FitsHeaderEntry("INSTRUME", "EIT") fits_header_entry.id = 1 assert len(entries) == 2 assert entries == [ tables.DatabaseEntry(id=5, tags=[tag], download_time=datetime(2005, 6, 15, 5)), tables.DatabaseEntry( id=10, starred=True, tags=[tag], download_time=datetime(2005, 6, 15, 10), fits_header_entries=[fits_header_entry], ), ]
def test_walker_create_vso_instrument(vso_session): entries = walker.create(vso.attrs.Instrument('RHESSI'), vso_session) expected = [ tables.DatabaseEntry(id=1, source=u'RHESSI', provider=u'LSSP', physobs=u'intensity', fileid=u'/hessidata/2011/09/19/hsi_20110919_233340_002.fits', observation_time_start=datetime(2011, 9, 19, 23, 33, 40), observation_time_end=datetime(2011, 9, 20, 1, 9, 20), instrument='RHESSI', wavemin=0.4132806579880238, wavemax=7.293188082141598e-05), tables.DatabaseEntry(id=2, source=u'RHESSI', provider=u'LSSP', physobs=u'intensity', fileid=u'/hessidata/2011/09/20/hsi_20110920_010920_001.fits', observation_time_start=datetime(2011, 9, 20, 1, 9, 20), observation_time_end=datetime(2011, 9, 20, 2, 27, 40), instrument='RHESSI', wavemin=0.4132806579880238, wavemax=7.293188082141598e-05)] assert len(entries) == len(expected) assert entries in [expected]
def test_walker_create_complex_query(session): query = Tag('foo') & Starred() | ~Tag('foo') & ~Starred() entries = walker.create(query, session) assert len(entries) == 5 tag = tables.Tag('foo') tag.id = 1 fits_header_entry = tables.FitsHeaderEntry('INSTRUME', 'EIT') fits_header_entry.id = 1 assert tables.DatabaseEntry( id=1, download_time=datetime(2005, 6, 15, 1)) in entries assert tables.DatabaseEntry( id=3, path='/tmp', download_time=datetime(2005, 6, 15, 3)) in entries assert tables.DatabaseEntry( id=7, download_time=datetime(2005, 6, 15, 7)) in entries assert tables.DatabaseEntry( id=9, path='/tmp', download_time=datetime(2005, 6, 15, 9)) in entries assert tables.DatabaseEntry( id=10, starred=True, tags=[tag], download_time=datetime(2005, 6, 15, 10), fits_header_entries=[fits_header_entry]) in entries
def test_walker_create_starred_true(session): entries = walker.create(Starred(), session) tag = tables.Tag("foo") tag.id = 1 fits_header_entry = tables.FitsHeaderEntry("INSTRUME", "EIT") fits_header_entry.id = 1 assert len(entries) == 5 assert entries == [ tables.DatabaseEntry(id=2, starred=True, download_time=datetime(2005, 6, 15, 2)), tables.DatabaseEntry(id=4, starred=True, download_time=datetime(2005, 6, 15, 4)), tables.DatabaseEntry(id=6, path="/tmp", starred=True, download_time=datetime(2005, 6, 15, 6)), tables.DatabaseEntry(id=8, starred=True, download_time=datetime(2005, 6, 15, 8)), tables.DatabaseEntry( id=10, starred=True, tags=[tag], download_time=datetime(2005, 6, 15, 10), fits_header_entries=[fits_header_entry], ), ]
def test_walker_create_tag_negative(session): entries = walker.create(~Tag('foo'), session) assert len(entries) == 8 assert entries == [ tables.DatabaseEntry(id=1, download_time=datetime(2005, 6, 15, 1)), tables.DatabaseEntry( id=2, starred=True, download_time=datetime(2005, 6, 15, 2)), tables.DatabaseEntry( id=3, path='/tmp', download_time=datetime(2005, 6, 15, 3)), tables.DatabaseEntry( id=4, starred=True, download_time=datetime(2005, 6, 15, 4)), tables.DatabaseEntry( id=6, path='/tmp', starred=True, download_time=datetime(2005, 6, 15, 6)), tables.DatabaseEntry( id=7, download_time=datetime(2005, 6, 15, 7)), tables.DatabaseEntry( id=8, starred=True, download_time=datetime(2005, 6, 15, 8)), tables.DatabaseEntry( id=9, path='/tmp', download_time=datetime(2005, 6, 15, 9))]
def test_walker_create_starred_true(session): entries = walker.create(Starred(), session) tag = tables.Tag('foo') tag.id = 1 fits_header_entry = tables.FitsHeaderEntry('INSTRUME', 'EIT') fits_header_entry.id = 1 assert len(entries) == 5 assert entries == [ tables.DatabaseEntry( id=2, starred=True, download_time=datetime(2005, 6, 15, 2)), tables.DatabaseEntry( id=4, starred=True, download_time=datetime(2005, 6, 15, 4)), tables.DatabaseEntry( id=6, path='/tmp', starred=True, download_time=datetime(2005, 6, 15, 6)), tables.DatabaseEntry( id=8, starred=True, download_time=datetime(2005, 6, 15, 8)), tables.DatabaseEntry( id=10, starred=True, tags=[tag], download_time=datetime(2005, 6, 15, 10), fits_header_entries=[fits_header_entry])]
def test_walker_create_time(vso_session): time = vso.attrs.Time( datetime(2011, 9, 17, 0, 0, 0), datetime(2011, 9, 20, 0, 0, 0)) entries = walker.create(time, vso_session) assert len(entries) == 1 # TODO: Flaky and the only change the id number. assert entries in [ [tables.DatabaseEntry(id=1, source=u'RHESSI', provider=u'LSSP', physobs=u'intensity', fileid=u'/hessidata/2011/09/19/hsi_20110919_233340', observation_time_start=datetime(2011, 9, 19, 23, 33, 40), observation_time_end=datetime(2011, 9, 20, 1, 9, 20), instrument=u'RHESSI', wavemin=0.4132806579880238, wavemax=7.293188082141598e-05)], [tables.DatabaseEntry(id=2, source=u'RHESSI', provider=u'LSSP', physobs=u'intensity', fileid=u'/hessidata/2011/09/19/hsi_20110919_233340', observation_time_start=datetime(2011, 9, 19, 23, 33, 40), observation_time_end=datetime(2011, 9, 20, 1, 9, 20), instrument=u'RHESSI', wavemin=0.4132806579880238, wavemax=7.293188082141598e-05)]]
def test_walker_create_path_inverted(session): tag = tables.Tag("foo") tag.id = 1 fits_header_entry = tables.FitsHeaderEntry("INSTRUME", "EIT") fits_header_entry.id = 1 entries = walker.create(~Path("/tmp"), session) assert len(entries) == 7 assert entries == [ tables.DatabaseEntry(id=1, download_time=datetime(2005, 6, 15, 1)), tables.DatabaseEntry(id=2, starred=True, download_time=datetime(2005, 6, 15, 2)), tables.DatabaseEntry(id=4, starred=True, download_time=datetime(2005, 6, 15, 4)), tables.DatabaseEntry(id=5, tags=[tag], download_time=datetime(2005, 6, 15, 5)), tables.DatabaseEntry(id=7, download_time=datetime(2005, 6, 15, 7)), tables.DatabaseEntry(id=8, starred=True, download_time=datetime(2005, 6, 15, 8)), tables.DatabaseEntry( id=10, starred=True, tags=[tag], download_time=datetime(2005, 6, 15, 10), fits_header_entries=[fits_header_entry], ), ]
def test_walker_create_ored_query(session): entries = walker.create(Tag('foo') | Starred(), session) assert len(entries) == 6 tag = tables.Tag('foo') tag.id = 1 fits_header_entry = tables.FitsHeaderEntry('INSTRUME', 'EIT') fits_header_entry.id = 1 assert tables.DatabaseEntry( id=2, starred=True, download_time=datetime(2005, 6, 15, 2)) in entries assert tables.DatabaseEntry( id=4, starred=True, download_time=datetime(2005, 6, 15, 4)) in entries assert tables.DatabaseEntry( id=5, tags=[tag], download_time=datetime(2005, 6, 15, 5)) in entries assert tables.DatabaseEntry( id=6, path='/tmp', starred=True, download_time=datetime(2005, 6, 15, 6)) in entries assert tables.DatabaseEntry( id=8, starred=True, download_time=datetime(2005, 6, 15, 8)) in entries assert tables.DatabaseEntry( id=10, starred=True, tags=[tag], download_time=datetime(2005, 6, 15, 10), fits_header_entries=[fits_header_entry]) in entries
def test_walker_create_path_inverted(session): tag = tables.Tag('foo') tag.id = 1 fits_header_entry = tables.FitsHeaderEntry('INSTRUME', 'EIT') fits_header_entry.id = 1 entries = walker.create(~Path('/tmp'), session) assert len(entries) == 7 assert entries == [ tables.DatabaseEntry( id=1, download_time=datetime(2005, 6, 15, 1)), tables.DatabaseEntry( id=2, starred=True, download_time=datetime(2005, 6, 15, 2)), tables.DatabaseEntry( id=4, starred=True, download_time=datetime(2005, 6, 15, 4)), tables.DatabaseEntry( id=5, tags=[tag], download_time=datetime(2005, 6, 15, 5)), tables.DatabaseEntry(id=7, download_time=datetime(2005, 6, 15, 7)), tables.DatabaseEntry( id=8, starred=True, download_time=datetime(2005, 6, 15, 8)), tables.DatabaseEntry( id=10, starred=True, tags=[tag], download_time=datetime(2005, 6, 15, 10), fits_header_entries=[fits_header_entry])]
def test_walker_create_fitsheader_inverted(session): tag = tables.Tag('foo') tag.id = 1 entries = walker.create(~FitsHeaderEntry('INSTRUME', 'EIT'), session) assert len(entries) == 9 assert entries == [ tables.DatabaseEntry( id=1, download_time=datetime(2005, 6, 15, 1)), tables.DatabaseEntry( id=2, starred=True, download_time=datetime(2005, 6, 15, 2)), tables.DatabaseEntry( id=3, path='/tmp', download_time=datetime(2005, 6, 15, 3)), tables.DatabaseEntry( id=4, starred=True, download_time=datetime(2005, 6, 15, 4)), tables.DatabaseEntry( id=5, tags=[tag], download_time=datetime(2005, 6, 15, 5)), tables.DatabaseEntry( id=6, starred=True, path='/tmp', download_time=datetime(2005, 6, 15, 6)), tables.DatabaseEntry(id=7, download_time=datetime(2005, 6, 15, 7)), tables.DatabaseEntry( id=8, starred=True, download_time=datetime(2005, 6, 15, 8)), tables.DatabaseEntry( id=9, path='/tmp', download_time=datetime(2005, 6, 15, 9))]
def test_walker_create_downloadtime_notfound(session): download_time = DownloadTime( datetime(2005, 6, 15, 11), datetime(2005, 6, 15, 11)) entries = walker.create(download_time, session) assert entries == []
def test_walker_create_path_attr_notfound(session): assert walker.create(Path('doesnotexist'), session) == []
def test_walker_create_dummy(session): with pytest.raises(TypeError): walker.create(DummyAttr(), session)
def query(self, *query, **kwargs): """ query(*query[, sortby]) Send the given query to the database and return a list of database entries that satisfy all of the given attributes. Apart from the attributes supported by the VSO interface, the following attributes are supported: - :class:`sunpy.database.attrs.Starred` - :class:`sunpy.database.attrs.Tag` - :class:`sunpy.database.attrs.Path` - :class:`sunpy.database.attrs.DownloadTime` - :class:`sunpy.database.attrs.FitsHeaderEntry` An important difference to the VSO attributes is that these attributes may also be used in negated form using the tilde ~ operator. Parameters ---------- query : list A variable number of attributes that are chained together via the boolean AND operator. The | operator may be used between attributes to express the boolean OR operator. sortby : str, optional The column by which to sort the returned entries. The default is to sort by the start of the observation. See the attributes of :class:`sunpy.database.tables.DatabaseEntry` for a list of all possible values. Raises ------ TypeError if no attribute is given or if some keyword argument other than 'sortby' is given. Examples -------- The query in the following example searches for all non-starred entries with the tag 'foo' or 'bar' (or both). >>> database.query(~attrs.Starred(), attrs.Tag('foo') | attrs.Tag('bar')) # doctest: +SKIP """ if not query: raise TypeError('at least one attribute required') sortby = kwargs.pop('sortby', 'observation_time_start') if kwargs: k, v = kwargs.popitem() raise TypeError('unexpected keyword argument {0!r}'.format(k)) db_entries = walker.create(and_(*query), self.session) # If any of the DatabaseEntry-s lack the sorting attribute, the # sorting key should fall back to 'id', orherwise it fails with # TypeError on py3 if any([getattr(entry, sortby) is None for entry in db_entries]): sortby = 'id' return sorted(db_entries, key=operator.attrgetter(sortby))
def test_walker_create_wave(vso_session): entries = walker.create(vso.attrs.Wave(0 * u.AA, 10 * u.AA), vso_session) assert len(entries) == 2 entries = walker.create(vso.attrs.Wave(5 * u.AA, 10 * u.AA), vso_session) assert len(entries) == 0
def test_walker_create_wave(vso_session): entries = walker.create(vso.attrs.Wave(0, 10), vso_session) assert len(entries) == 2 entries = walker.create(vso.attrs.Wave(5, 10), vso_session) assert len(entries) == 0
def test_walker_create_downloadtime_notfound(session): download_time = DownloadTime(datetime(2005, 6, 15, 11), datetime(2005, 6, 15, 11)) entries = walker.create(download_time, session) assert entries == []
def test_walker_create_wave(vso_session): entries = walker.create(a.Wavelength(0 * u.AA, 10 * u.AA), vso_session) assert len(entries) == 2 entries = walker.create(a.Wavelength(5 * u.AA, 10 * u.AA), vso_session) assert len(entries) == 0