Skip to content

Commit

Permalink
Merge conflict.
Browse files Browse the repository at this point in the history
  • Loading branch information
hover2pi committed Apr 20, 2017
2 parents 4d20f3d + 75ae329 commit d32dffc
Show file tree
Hide file tree
Showing 9 changed files with 1,215 additions and 443 deletions.
12 changes: 8 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ language: python

python:
- 2.7
- 3.3
- 3.4
- 3.5
- 3.6

before_script:
- "export DISPLAY=:99.0" # xvfb is used to ensure matplotlib plots don't fail
- "sh -e /etc/init.d/xvfb start"
- sleep 3 # give xvfb some time to start

env:
global:
Expand All @@ -16,7 +21,7 @@ env:
# For this package-template, we include examples of Cython modules,
# so Cython is required for testing. If your package does not include
# Cython code, you can set CONDA_DEPENDENCIES=''
- CONDA_DEPENDENCIES='matplotlib'
- CONDA_DEPENDENCIES='matplotlib pandas'
matrix:
# Make sure that egg_info works without dependencies
# - SETUP_CMD='egg_info'
Expand All @@ -25,13 +30,12 @@ env:

matrix:
allow_failures:
- python: 3.3
- python: 3.4
- python: 3.5

install:
- git clone git://github.com/astropy/ci-helpers.git
- source ci-helpers/travis/setup_conda_$TRAVIS_OS_NAME.sh
- pip install requests

script:
- $SETUP_CMD
2 changes: 1 addition & 1 deletion astrodbkit/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from pkg_resources import get_distribution
__version__ = '0.3.0.post7'
__version__ = '0.6.4'
1,247 changes: 887 additions & 360 deletions astrodbkit/astrodb.py

Large diffs are not rendered by default.

162 changes: 141 additions & 21 deletions astrodbkit/tests/test_astrodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,172 @@
import os
from astropy.utils.data import download_file
from .. import astrodb
from sqlite3 import IntegrityError

filename = os.path.join(tempfile.mkdtemp(), 'empty_db.db')


def setup_module(module):
db_path = download_file("http://github.com/BDNYC/BDNYCdb/raw/master/BDNYCv1.0.db")
try:
db_path = download_file("http://github.com/BDNYC/BDNYCdb/raw/master/bdnyc_database.db")
except:
db_path = download_file("http://github.com/BDNYC/BDNYCdb/raw/master/BDNYCv1.0.db")
module.bdnyc_db = astrodb.Database(db_path)
filename = os.path.join(tempfile.mkdtemp(), 'empty_db.db')
module.empty_db = astrodb.create_database(filename)
astrodb.create_database(filename)
module.empty_db = astrodb.Database(filename)


def test_loaddb():
def test_load_bdnyc():
print(bdnyc_db)
assert not isinstance(bdnyc_db, type(None))


def test_load_empty():
print(empty_db)
assert not isinstance(empty_db, type(None))


def test_search():
bdnyc_db.search('1234','sources')
bdnyc_db.search('young', 'sources')
bdnyc_db.search((222.106, 10.533), 'sources')
bdnyc_db.search((338.673, 40.694), 'sources', radius=5)
bdnyc_db.search((338.673, 40.694), 'sources', radius=5, sql_search=True)


def test_inventory():
bdnyc_db.inventory(825)
bdnyc_db.inventory(1)
bdnyc_db.inventory(11)


def test_sqlquery():
bdnyc_db.query("SELECT s.id, s.ra, s.dec, s.shortname, p.source_id, p.band, p.magnitude "
"FROM sources as s JOIN photometry as p ON s.id=p.source_id "
"WHERE s.dec<=-10 AND p.band=='W1'")


def test_schema():
bdnyc_db.schema('sources')

@pytest.mark.xfail
def test_add_data():
assert False

@pytest.mark.xfail
def test_clean_up():
assert False
empty_db.schema('sources')


def test_table():
columns = ['id', 'ra', 'dec', 'shortname', 'source_id']
types = ['INTEGER', 'REAL', 'REAL', 'TEXT', 'INTEGER']
constraints = ['NOT NULL UNIQUE', '', '', '', '']
empty_db.table('new_sources', columns, types, constraints, new_table=True)
t = empty_db.query("PRAGMA table_info(new_sources)", fmt='table')
print(t)
assert sorted(columns) == sorted(t['name'])


def test_add_data_empty():
t1 = empty_db.query('SELECT * FROM sources', fmt='array')

if isinstance(t1, type(None)):
len_t1 = 0
else:
len_t1 = len(t1)

data = list()
data.append(['ra', 'dec', 'shortname', 'comments'])
data.append([12, -12, 'fakesource', ''])
empty_db.add_data(data, 'sources', verbose=True)

t2 = empty_db.query('SELECT * FROM sources', fmt='array')
assert len(t2) == len_t1 + 1


def test_new_ids():
available = bdnyc_db._lowest_rowids('sources', 10)
t = bdnyc_db.query('SELECT id FROM sources')
assert available not in t


def test_add_foreign_key():
empty_db.add_foreign_key('new_sources', ['sources'], ['source_id'], ['id'])


def test_foreign_key_support():
data = list()
data.append(['ra', 'dec', 'shortname', 'source_id'])
data.append([12, -12, 'fakesource', 9999])
with pytest.raises(IntegrityError):
empty_db.add_data(data, 'new_sources') # Foreign key error expected


def test_lookup():
bdnyc_db.lookup([1, '2MASS'], 'sources')


def test_clean_up(monkeypatch):
data = list()
data.append(['ra', 'dec', 'shortname', 'source_id'])
data.append([12, -12, 'fakesource', 1])
data.append([12, -12, 'fakesource2', 1])

# Fake user input
inputs = ['help', 'r', 'y']
input_generator = (i for i in inputs)
monkeypatch.setattr('astrodbkit.astrodb.get_input', lambda prompt: next(input_generator))

empty_db.add_data(data, 'new_sources', verbose=True) # This internally calls clean_up

t = empty_db.query('SELECT * FROM new_sources', fmt='table')
assert len(t) == 1 # Only one record should have been kept


@pytest.mark.xfail
def test_merge():
empty_db.modify('DROP TABLE new_sources')
bdnyc_db.merge(filename, 'sources')
assert False

@pytest.mark.xfail

def test_output_spectrum():
assert False
t = bdnyc_db.query('SELECT spectrum FROM spectra LIMIT 1', fmt='array', fetch='one')
try:
w, f = t[0].data
except ValueError:
w, f, e = t[0].data
assert any(w) # extracted wavelength
assert any(f) # extracted flux


@pytest.mark.xfail
def test_plot_spectrum():
assert False
t = bdnyc_db.query('SELECT id FROM spectra LIMIT 1', fmt='array', fetch='one')
bdnyc_db.plot_spectrum(t[0])

@pytest.mark.xfail
def test_table():
assert False

def test_cuny():
file = 'http://academicworks.cuny.edu/context/hc_pubs/article/1093/type/native/viewcontent' # txt file
data = astrodb.convert_spectrum(file)
assert not isinstance(data, type(''))

file = 'http://academicworks.cuny.edu/context/hc_pubs/article/1118/type/native/viewcontent' # FITS file
data = astrodb.convert_spectrum(file)
assert not isinstance(data, type(''))


def test_references():
t = bdnyc_db.query('SELECT id FROM publications', fmt='table')
id = t['id'][0]
bdnyc_db.references(id, column_name='publication_id')


def test_get_bibtex():
bdnyc_db.get_bibtex(52)
bdnyc_db.get_bibtex('Cruz03')


def test_save():
empty_db.save(directory='tempempty')
bdnyc_db.save(directory='tempdata')


def test_close(monkeypatch):
# Fake user input
inputs = ['n', 'n']
input_generator = (i for i in inputs)
monkeypatch.setattr('astrodbkit.astrodb.get_input', lambda prompt: next(input_generator))

bdnyc_db.close()
79 changes: 37 additions & 42 deletions astrodbkit/votools.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,6 @@
from astropy.io.votable import from_table


def table_add(tab, data, col):
"""
Function to parse dictionary list **data** and add the data to table **tab** for column **col**
Parameters
----------
tab: Table class
Table to store values
data: list
Dictionary list from the SQL query
col: str
Column name (ie, dictionary key) for the column to add
Returns
-------
None
"""

x = []
for i in range(len(data)):

# If the particular key is not present, use a place-holder value (used for photometry tables)
if col not in data[i]:
temp = ''
else:
temp = data[i][col]

# Fix up None elements
if temp is None: temp = ''

x.append(temp)

print('Adding column {}'.format(col))
tab.add_column(Column(x, name=col))

def dict_tovot(tabdata, tabname='votable.xml', phot=False, binary=True):
"""
Converts dictionary table **tabdata** to a VOTable with name **tabname**
Expand All @@ -60,10 +24,6 @@ def dict_tovot(tabdata, tabname='votable.xml', phot=False, binary=True):
Parameter specifying if the VOTable should be saved as a binary.
This is necessary for tables with lots of text columns.
Returns
-------
None
"""

# Check if input is a dictionary
Expand Down Expand Up @@ -123,7 +83,9 @@ def photaddline(tab, sourceid):
Returns
-------
Dictionary with all the data for the specified source
tmpdict: dict
Dictionary with all the data for the specified source
"""

colnames = tab[0].keys()
Expand Down Expand Up @@ -182,4 +144,37 @@ def photparse(tab):
tmpdict = photaddline(tab, sourceid)
newtab.append(tmpdict)

return newtab
return newtab


def table_add(tab, data, col):
"""
Function to parse dictionary list **data** and add the data to table **tab** for column **col**
Parameters
----------
tab: Table class
Table to store values
data: list
Dictionary list from the SQL query
col: str
Column name (ie, dictionary key) for the column to add
"""

x = []
for i in range(len(data)):

# If the particular key is not present, use a place-holder value (used for photometry tables)
if col not in data[i]:
temp = ''
else:
temp = data[i][col]

# Fix up None elements
if temp is None: temp = ''

x.append(temp)

print('Adding column {}'.format(col))
tab.add_column(Column(x, name=col))
4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@
# built documents.
#
# The short X.Y version.
version = '0.3'
version = '0.5'
# The full version, including alpha/beta/rc tags.
release = '0.3.0'
release = '0.5.2'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down

0 comments on commit d32dffc

Please sign in to comment.