Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tompollard committed Aug 22, 2017
2 parents 54857ae + aa179cb commit 9ce86a3
Show file tree
Hide file tree
Showing 136 changed files with 480 additions and 625 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ target/
# Jupyter Notebook checkpoints
.ipynb_checkpoints/


# data files
travisdata/


43 changes: 15 additions & 28 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,42 +1,29 @@
# Link repository with Travis CI
# https://travis-ci.org/

# Master branch only
branches:
only:
- travis
- travis

# Start the databases
# See https://docs.travis-ci.com/user/database-setup/
addons:
postgresql: "9.4"
postgresql: '9.4'

services:
- postgresql
- mysql
- postgresql
- mysql

# Set the language
language: python
python:
- "2.7"
env:
- BUILD_DIR=$TRAVIS_BUILD_DIR/buildmimic/postgres
- DATA_DIR=$TRAVIS_BUILD_DIR/tests/testdata/v1_4
- '2.7'

before_script:
- sudo apt-get install python-dev
# - psql -c 'create database mimic_test;' -U postgres
# - psql -c 'create schema mimiciii;' -d mimic_test -U postgres
# - psql target-db -U <admin user> -p <port> -h <DB instance name> -c "\copy source-table from 'source-table.csv' with DELIMITER ','"
# - psql -f './buildmimic/postgres/postgres_create_tables.sql' -U postgres
- sudo apt-get install python-dev

# command to install dependencies
install:
- pip install psycopg2 --quiet
- pip install pandas --quiet
- pip install MySQL-python --quiet
# - pip install .
# - pip install -r requirements.txt
- pip install psycopg2 --quiet
- pip install pandas --quiet
- pip install MySQL-python --quiet

# command to run tests
script: nosetests
script: nosetests --verbose --nocapture

env:
global:
- secure: tUQpMvHoLtRvo7hDfZfSWrSYlDpGZn3OZXQ1ogVMcIQYZ+3yseJNQ71Ov6/Z5JVQgtKmQrMidlmQiWVJnzDtKEIt41bDPOn29yMcTOc/iMwsP48vncIW1ge8JPZXOZeFfRIkhUUMMW/AqBe84nwlAWRz1m4cC2qjYuQ6+FtCqQtPFl7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iQc8VDjTVmNgxs1WRwtmkDe33n2EWFT4sltlHF9mTaRLFBKFsYtcWtNPF5jEvE2AP5uD+jWqHR4/8tbjD12XLIv/wYB/zkxH912ix3+X0fixATRvTO6RZlPw=
9 changes: 8 additions & 1 deletion buildmimic/postgres/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Config
PHYSIONETURL=https://physionet.org/works/MIMICIIIClinicalDatabase/files/
PHYSIONETDEMOURL=https://physionet.org/works/MIMICIIIClinicalDatabaseDemo/

# Set the following parameters to defaults
# These will be overwritten by settings passed to the makefile
Expand Down Expand Up @@ -148,9 +149,15 @@ mimic-download:
@echo ''
wget --user ${physionetuser} --ask-password -P ${datadir} -A csv.gz -m -p -E -k -K -np -nd "$(PHYSIONETURL)"

mimic-demo-download:
@echo '------------------------------------------'
@echo '-- Downloading MIMIC-III from PhysioNet --'
@echo '------------------------------------------'
@echo ''
wget --user ${physionetuser} --ask-password -P ${datadir} -A csv.gz -m -p -E -k -K -np -nd "$(PHYSIONETDEMOURL)"

#This is fairly inelegant and could be tidied with a for loop and an if to check for gzip,
#but need to maintain compatability with Windows, which baffling lacks these things
#but need to maintain compatibility with Windows, which baffling lacks these things
mimic-check-gz:
@echo ''
@echo '-----------------------'
Expand Down
300 changes: 150 additions & 150 deletions tests/test_mysql_build.py
Original file line number Diff line number Diff line change
@@ -1,150 +1,150 @@
import unittest
import pandas as pd
import os
from subprocess import call
import MySQLdb

# Config
sqluser = 'root'
testdbname = 'mimic_test_db'
hostname = 'localhost'
datadir = 'testdata/v1_4/'
schema = 'mimiciii'

# Set paths for scripts to be tested
curpath = os.path.join(os.path.dirname(__file__)) + '/'

# Display environment variables
print(os.environ)

# Create dictionary with table details for use in testing
row_dict = {
"ADMISSIONS": 58976,
"CALLOUT": 34499,
"CAREGIVERS": 7567,
"CHARTEVENTS": 330712483,
"CPTEVENTS": 573146,
"D_CPT": 134,
"D_ICD_DIAGNOSES": 14567,
"D_ICD_PROCEDURES": 3882,
"D_ITEMS": 12478,
"D_LABITEMS": 753,
"DATETIMEEVENTS": 4485937,
"DIAGNOSES_ICD": 651047,
"DRGCODES": 125557,
"ICUSTAYS": 61532,
"INPUTEVENTS_CV": 17527935,
"INPUTEVENTS_MV": 3618991,
"LABEVENTS": 27854055,
"MICROBIOLOGYEVENTS": 631726,
"NOTEEVENTS": 2083180,
"OUTPUTEVENTS": 4349218,
"PATIENTS": 46520,
"PRESCRIPTIONS": 4156848,
"PROCEDUREEVENTS_MV": 258066,
"PROCEDURES_ICD": 240095,
"SERVICES": 73343,
"TRANSFERS": 261897 }

def run_mysql_build_scripts(cur):
# Create tables and loads data
fn = curpath + '../buildmimic/mysql/1-define.sql'
cur.execute(open(fn, "r").read())
if os.environ.has_key('USER') and os.environ['USER'] == 'jenkins':
# use full dataset
mimic_data_dir = '/home/mimicadmin/data/mimiciii_1_3/'
else:
mimic_data_dir = curpath+datadir
call(['mysql','-f',fn,'-d',testdbname,'-U',sqluser,'-v','mimic_data_dir='+mimic_data_dir])
# # Add constraints
# fn = curpath + '../buildmimic/mysql/3-constraints.sql'
# cur.execute(open(fn, "r").read())
# # Add indexes
# fn = curpath + '../buildmimic/mysql/2-indexes.sql'
# cur.execute(open(fn, "r").read())
pass


# Class to run unit tests
class test_mysql(unittest.TestCase):
# setUpClass runs once for the class
@classmethod
def setUpClass(cls):
# Connect to default mysql database
cls.con = MySQLdb.connect(host=hostname, user=sqluser)
cls.cur = cls.con.cursor()
# Create test database
try:
cls.cur.execute('DROP DATABASE ' + testdbname)
except MySQLdb.OperationalError:
pass
cls.cur.execute('CREATE DATABASE ' + testdbname)
cls.cur.close()
cls.con.close()
# Connect to the test database
cls.con = MySQLdb.connect(db=testdbname, user=sqluser)
cls.cur = cls.con.cursor()
# Build the test database
# run_mysql_build_scripts(cls.cur)
cls.cur.close()
cls.con.close()

# tearDownClass runs once for the class
@classmethod
def tearDownClass(cls):
# Connect to default mysql database
cls.con = MySQLdb.connect(host=hostname, user=sqluser)
cls.cur = cls.con.cursor()
# Drop test database
cls.cur.execute('DROP DATABASE ' + testdbname)
cls.cur.close()
cls.con.close()

# setUp runs once for each test method
def setUp(self):
# Connect to the test database
self.con = MySQLdb.connect(db=testdbname, user=sqluser)
self.cur = self.con.cursor()

# tearDown runs once for each test method
def tearDown(self):
self.cur.close()
self.con.close()

# The MIMIC test db has been created by this point
# Add unit tests below
def test_run_sample_query(self):
test_query = """
SELECT 'hello world';
"""
hello_world = pd.read_sql_query(test_query,self.con)
self.assertEqual(hello_world.values[0][0],'hello world')

# def test_testddl(self):
# # Creates and drops an example schema and table
# fn = curpath + 'testddl.sql'
# self.cur.execute(open(fn, "r").read())
# # self.assertEqual(1,1)

# --------------------------------------------------
# Run a series of checks to ensure ITEMIDs are valid
# All checks should return 0.
# --------------------------------------------------


# ----------------------------------------------------
# RUN THE FOLLOWING TESTS ON THE FULL DATASET ONLY ---
# ----------------------------------------------------

# if os.environ.has_key('USER') and os.environ['USER'] == 'jenkins':
# def test_row_counts_are_as_expected(self):
# for tablename,expectedrows in row_dict.iteritems():
# query = "SELECT COUNT(*) FROM " + schema + "." + tablename + ";"
# queryresult = pd.read_sql_query(query,self.con)
# self.assertEqual(queryresult.values[0][0],expectedrows)

def main():
unittest.main()

if __name__ == '__main__':
main()
# import unittest
# import pandas as pd
# import os
# from subprocess import call
# import MySQLdb

# # Config
# sqluser = 'root'
# testdbname = 'mimic_test_db'
# hostname = 'localhost'
# datadir = 'testdata/v1_4/'
# schema = 'mimiciii'

# # Set paths for scripts to be tested
# curpath = os.path.join(os.path.dirname(__file__)) + '/'

# # Display environment variables
# print(os.environ)

# # Create dictionary with table details for use in testing
# row_dict = {
# "ADMISSIONS": 58976,
# "CALLOUT": 34499,
# "CAREGIVERS": 7567,
# "CHARTEVENTS": 330712483,
# "CPTEVENTS": 573146,
# "D_CPT": 134,
# "D_ICD_DIAGNOSES": 14567,
# "D_ICD_PROCEDURES": 3882,
# "D_ITEMS": 12478,
# "D_LABITEMS": 753,
# "DATETIMEEVENTS": 4485937,
# "DIAGNOSES_ICD": 651047,
# "DRGCODES": 125557,
# "ICUSTAYS": 61532,
# "INPUTEVENTS_CV": 17527935,
# "INPUTEVENTS_MV": 3618991,
# "LABEVENTS": 27854055,
# "MICROBIOLOGYEVENTS": 631726,
# "NOTEEVENTS": 2083180,
# "OUTPUTEVENTS": 4349218,
# "PATIENTS": 46520,
# "PRESCRIPTIONS": 4156848,
# "PROCEDUREEVENTS_MV": 258066,
# "PROCEDURES_ICD": 240095,
# "SERVICES": 73343,
# "TRANSFERS": 261897 }

# def run_mysql_build_scripts(cur):
# # Create tables and loads data
# fn = curpath + '../buildmimic/mysql/1-define.sql'
# cur.execute(open(fn, "r").read())
# if os.environ.has_key('USER') and os.environ['USER'] == 'jenkins':
# # use full dataset
# mimic_data_dir = '/home/mimicadmin/data/mimiciii_1_3/'
# else:
# mimic_data_dir = curpath+datadir
# call(['mysql','-f',fn,'-d',testdbname,'-U',sqluser,'-v','mimic_data_dir='+mimic_data_dir])
# # # Add constraints
# # fn = curpath + '../buildmimic/mysql/3-constraints.sql'
# # cur.execute(open(fn, "r").read())
# # # Add indexes
# # fn = curpath + '../buildmimic/mysql/2-indexes.sql'
# # cur.execute(open(fn, "r").read())
# pass


# # Class to run unit tests
# class test_mysql(unittest.TestCase):
# # setUpClass runs once for the class
# @classmethod
# def setUpClass(cls):
# # Connect to default mysql database
# cls.con = MySQLdb.connect(host=hostname, user=sqluser)
# cls.cur = cls.con.cursor()
# # Create test database
# try:
# cls.cur.execute('DROP DATABASE ' + testdbname)
# except MySQLdb.OperationalError:
# pass
# cls.cur.execute('CREATE DATABASE ' + testdbname)
# cls.cur.close()
# cls.con.close()
# # Connect to the test database
# cls.con = MySQLdb.connect(db=testdbname, user=sqluser)
# cls.cur = cls.con.cursor()
# # Build the test database
# # run_mysql_build_scripts(cls.cur)
# cls.cur.close()
# cls.con.close()

# # tearDownClass runs once for the class
# @classmethod
# def tearDownClass(cls):
# # Connect to default mysql database
# cls.con = MySQLdb.connect(host=hostname, user=sqluser)
# cls.cur = cls.con.cursor()
# # Drop test database
# cls.cur.execute('DROP DATABASE ' + testdbname)
# cls.cur.close()
# cls.con.close()

# # setUp runs once for each test method
# def setUp(self):
# # Connect to the test database
# self.con = MySQLdb.connect(db=testdbname, user=sqluser)
# self.cur = self.con.cursor()

# # tearDown runs once for each test method
# def tearDown(self):
# self.cur.close()
# self.con.close()

# # The MIMIC test db has been created by this point
# # Add unit tests below
# def test_run_sample_query(self):
# test_query = """
# SELECT 'hello world';
# """
# hello_world = pd.read_sql_query(test_query,self.con)
# self.assertEqual(hello_world.values[0][0],'hello world')

# # def test_testddl(self):
# # # Creates and drops an example schema and table
# # fn = curpath + 'testddl.sql'
# # self.cur.execute(open(fn, "r").read())
# # # self.assertEqual(1,1)

# # --------------------------------------------------
# # Run a series of checks to ensure ITEMIDs are valid
# # All checks should return 0.
# # --------------------------------------------------


# # ----------------------------------------------------
# # RUN THE FOLLOWING TESTS ON THE FULL DATASET ONLY ---
# # ----------------------------------------------------

# # if os.environ.has_key('USER') and os.environ['USER'] == 'jenkins':
# # def test_row_counts_are_as_expected(self):
# # for tablename,expectedrows in row_dict.iteritems():
# # query = "SELECT COUNT(*) FROM " + schema + "." + tablename + ";"
# # queryresult = pd.read_sql_query(query,self.con)
# # self.assertEqual(queryresult.values[0][0],expectedrows)

# def main():
# unittest.main()

# if __name__ == '__main__':
# main()
Loading

0 comments on commit 9ce86a3

Please sign in to comment.