Skip to content

Commit

Permalink
polish of the test scripts.
Browse files Browse the repository at this point in the history
  • Loading branch information
kelle committed Oct 13, 2023
1 parent 442328b commit ef1b484
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
39 changes: 24 additions & 15 deletions tests/scheduled_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
import pytest
import sys
import requests
from astrodbkit2.astrodb import create_database, Database
from scripts.ingests.utils import check_internet_connection

sys.path.append('.')
sys.path.append(".")
from simple.schema import *
from astrodbkit2.astrodb import create_database, Database
from sqlalchemy import except_, select, and_
from . import REFERENCE_TABLES
from scripts.ingests.utils import check_internet_connection

DB_NAME = 'temp.db'
DB_PATH = 'data'

DB_NAME = "temp.db"
DB_PATH = "data"


# Load the database for use in individual tests
Expand All @@ -22,20 +22,27 @@ def db():

if os.path.exists(DB_NAME):
os.remove(DB_NAME)
connection_string = 'sqlite:///' + DB_NAME
connection_string = "sqlite:///" + DB_NAME
create_database(connection_string)
assert os.path.exists(DB_NAME)

# Connect to the new database and confirm it has the Sources table
db = Database(connection_string, reference_tables=REFERENCE_TABLES)
assert db
assert 'source' in [c.name for c in db.Sources.columns]
assert "source" in [c.name for c in db.Sources.columns]

# Load data into an in-memory sqlite database first, for performance
temp_db = Database('sqlite://', reference_tables=REFERENCE_TABLES) # creates and connects to a temporary in-memory database
temp_db.load_database(DB_PATH, verbose=False) # loads the data from the data files into the database
temp_db.dump_sqlite(DB_NAME) # dump in-memory database to file
db = Database('sqlite:///' + DB_NAME, reference_tables=REFERENCE_TABLES) # replace database object with new file version

# create and connects to a temporary in-memory database
temp_db = Database("sqlite://", reference_tables=REFERENCE_TABLES)

# load the data from the data files into the database
temp_db.load_database(DB_PATH, verbose=False)

# dump in-memory database to file
temp_db.dump_sqlite(DB_NAME)
# replace database object with new file version
db = Database("sqlite:///" + DB_NAME, reference_tables=REFERENCE_TABLES)

return db

Expand All @@ -46,12 +53,14 @@ def test_spectra_urls(db):
codes = []
internet = check_internet_connection()
if internet:
for spectrum_url in spectra_urls['spectrum']:
for spectrum_url in spectra_urls["spectrum"]:
request_response = requests.head(spectrum_url)
status_code = request_response.status_code
status_code = request_response.status_code
# The website is up if the status code is 200
# cuny academic commons links give 301 status code
if status_code != 200 and status_code != 301:
broken_urls.append(spectrum_url)
codes.append(status_code)
assert len(broken_urls) == 149, f'found {len(broken_urls)} broken spectra urls: {broken_urls}, {codes}'
assert (
len(broken_urls) == 149
), f"found {len(broken_urls)} broken spectra urls: {broken_urls}, {codes}"
12 changes: 6 additions & 6 deletions tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
import os
import pytest
import sys
import requests
from astrodbkit2.astrodb import create_database, Database
from sqlalchemy import except_, select, and_

sys.path.append('.')
from simple.schema import *
from astrodbkit2.astrodb import create_database, Database
from sqlalchemy import except_, select, and_
from . import REFERENCE_TABLES
from scripts.ingests.utils import check_internet_connection


DB_NAME = 'temp.db'
DB_PATH = 'data'
Expand Down Expand Up @@ -508,7 +507,7 @@ def test_Kirk19_ingest(db):

# Test spectral types added

# Test parallaxes
# Test parallaxes
ref = 'Kirk19'
t = db.query(db.Parallaxes).filter(db.Parallaxes.c.reference == ref).astropy()
assert len(t) == 23, f'found {len(t)} parallax entries for {ref}'
Expand Down Expand Up @@ -572,8 +571,9 @@ def test_suar22_ingest(db):
t = db.query(db.Spectra).filter(db.Spectra.c.reference == ref).astropy()
assert len(t) == 112, f'found {len(t)} spectra entries for {ref}'


def test_modeledparameters(db):
# Test to verify existing counts of modeled parameters
ref = 'Fili15'
t = db.query(db.ModeledParameters).filter(db.ModeledParameters.c.reference == ref).astropy()
assert len(t) == 696, f'found {len(t)} modeled parameters with {ref} reference'
assert len(t) == 696, f'found {len(t)} modeled parameters with {ref} reference'

0 comments on commit ef1b484

Please sign in to comment.