def test_delete_tracklets(tracklet_dict_list): '''Test deleting multiple tracklets.''' # set up db & table if os.path.isfile(sql.fetch_db_filepath()): os.remove(sql.fetch_db_filepath()) conn = sql.create_connection(sql.fetch_db_filepath()) cur = conn.cursor() sql.create_specific_table(conn) # upload data ... JD = [tracklet_dic['JD'] for tracklet_dic in tracklet_dict_list] HP = [tracklet_dic['HP'] for tracklet_dic in tracklet_dict_list] tracklet_name = [tracklet_dic['tracklet_name'] for tracklet_dic in tracklet_dict_list] sql.upsert_tracklets(conn, JD, HP, tracklet_name, tracklet_dict_list) # query the data & check that required # of dictionaries are returned list_of_tuples = sql.query_tracklets_jdhp(conn, JD[0], HP[0]) assert isinstance(list_of_tuples, list) and len(list_of_tuples) == 1 list_of_tuples = sql.query_tracklets_jdhp(conn, JD[1], HP[1]) assert isinstance(list_of_tuples, list) and len(list_of_tuples) == 1 # now delete two tracklets & check that two fewer dictionaries remain sql.delete_tracklets(conn, [tracklet_dict_list[0]['tracklet_name'], tracklet_dict_list[1]['tracklet_name']]) list_of_tuples = sql.query_tracklets_jdhp(conn, JD[0], HP[0]) assert isinstance(list_of_tuples, list) and len(list_of_tuples) == 0 list_of_tuples = sql.query_tracklets_jdhp(conn, JD[1], HP[1]) assert isinstance(list_of_tuples, list) and len(list_of_tuples) == 0 # Delete the db to facilitate future testing os.remove(sql.fetch_db_filepath())
def test_tracklets_upsert(tracklet_dict_list): """ Here we are updating/inserting **lists** of tracklet data """ # set up db & table if os.path.isfile(sql.fetch_db_filepath()): os.remove(sql.fetch_db_filepath()) conn = sql.create_connection(sql.fetch_db_filepath()) cur = conn.cursor() sql.create_specific_table(conn) # upload data ... JD = [tracklet_dic['JD'] for tracklet_dic in tracklet_dict_list] HP = [tracklet_dic['HP'] for tracklet_dic in tracklet_dict_list] tracklet_name = [tracklet_dic['tracklet_name'] for tracklet_dic in tracklet_dict_list] sql.upsert_tracklets(conn, JD, HP, tracklet_name, tracklet_dict_list) # test that the data was actually uploaded cur.execute('SELECT * from tracklets') f = cur.fetchall() assert(len(f) == len(tracklet_dict_list)), 'data not uploaded' for ii, fi in enumerate(f): assert fi[3] == tracklet_name[ii], 'data not uploaded' # Delete the db to facilitate future testing os.remove(sql.fetch_db_filepath())
def test_db_creation(): '''Test that an empty database can be created.''' # Where do we want the db to live assert 'sifter' in sql.fetch_db_filepath() if os.path.isfile(sql.fetch_db_filepath()): os.remove(sql.fetch_db_filepath()) # Does a db get created conn = sql.create_connection(sql.fetch_db_filepath()) assert os.path.isfile(os.path.join(sql.fetch_db_filepath()))
def test_table_creation(): '''Test table creation.''' expected_table_name = 'tracklets' # set up db & table if os.path.isfile(sql.fetch_db_filepath()): os.remove(sql.fetch_db_filepath()) conn = sql.create_connection(sql.fetch_db_filepath()) cur = conn.cursor() # Create the table sql.create_specific_table(conn) # - get the count of tables with the name cur.execute('SELECT name from sqlite_master WHERE type = "table" AND name = "tracklets"') # - if the count is 1, then table exists assert len(cur.fetchone()) == 1, 'table does not exist' # Delete the db to facilitate future testing os.remove(sql.fetch_db_filepath())
def convenience_func_create_db_and_tables(): ''' In order to save data, we require sql-db to exist, so let's set that up... Force deletion then creation of db... ''' if os.path.isfile(sql.fetch_db_filepath()): os.remove(sql.fetch_db_filepath()) conn = sql.create_connection(sql.fetch_db_filepath()) cur = conn.cursor() # Create required table(s) sql.create_specific_table(conn) # Double-check that this worked by getting the count of tables with the name # - if the count is 1, then table exists cur.execute('SELECT name from sqlite_master WHERE ' 'type = "table" AND name = "tracklets"') res = cur.fetchone() assert len(res) == 1, 'table does not exist' conn.close()
def test_tracklet_upsert(tracklet_dict_list): '''Test tracklet upsertion into the database.''' # set up db & table if os.path.isfile(sql.fetch_db_filepath()): os.remove(sql.fetch_db_filepath()) conn = sql.create_connection(sql.fetch_db_filepath()) cur = conn.cursor() sql.create_specific_table(conn) # create some data and then upload it ... tracklet_dict = tracklet_dict_list[0] sql.upsert_tracklet(conn, tracklet_dict['JD'], tracklet_dict['HP'], tracklet_dict['tracklet_name'], tracklet_dict) # test that the data was actually uploaded cur.execute('SELECT * from tracklets') f = cur.fetchone() assert (len(f) > 3 and f[3] == tracklet_dict['tracklet_name']),\ 'data not uploaded' # Delete the db to facilitate future testing os.remove(sql.fetch_db_filepath())
def test_tracklet_query_mutiple_HP(tracklet_dict_list): '''Test querying multiple Heal Pix.''' # set up db & table if os.path.isfile(sql.fetch_db_filepath()): os.remove(sql.fetch_db_filepath()) conn = sql.create_connection(sql.fetch_db_filepath()) cur = conn.cursor() sql.create_specific_table(conn) # upload data ... JD = [tracklet_dic['JD'] for tracklet_dic in tracklet_dict_list] HP = [tracklet_dic['HP'] for tracklet_dic in tracklet_dict_list] tracklet_name = [tracklet_dic['tracklet_name'] for tracklet_dic in tracklet_dict_list] sql.upsert_tracklets(conn, JD, HP, tracklet_name, tracklet_dict_list) # query the data & check that requisite dictionaries are returned list_of_tuples = sql.query_tracklets_jd_hplist(conn, JD[0], HP) assert isinstance(list_of_tuples, list) and len(list_of_tuples) == 1 # Delete the db to facilitate future testing os.remove(sql.fetch_db_filepath())
def test_instantiation_with_observations(observation_pairs): '''Test instantiation of the Tracklets class with some observations.''' # Create db from scratch convenience_func_create_db_and_tables() # instantiate with observation_pair T = precalc.Tracklets(observation_pairs) # test that the above caused the tracklet to be uploaded to db cur = T.conn.cursor() cur.execute('SELECT * from tracklets') f = cur.fetchone() assert (len(f) > 3), 'data not uploaded' # Completely delete db to facilitate future testing os.remove(sql.fetch_db_filepath())
def test_save_tracklets(observation_pair_list): '''Test that creating a db works and saving stuff to it works.''' # Create db from scratch convenience_func_create_db_and_tables() # Set up a Tracklet & use parse_tracklet_observations to get JD, HP, ... T = precalc.Tracklets() tracklet_dictionary_list = [ T.parse_tracklet_observations(obs_pair) for obs_pair in observation_pair_list ] # Now save the data in the db T.save_tracklets(tracklet_dictionary_list) # Test the data was uploaded and can be downloaded cur = T.conn.cursor() cur.execute('SELECT * from tracklets') f = cur.fetchall() assert (len(f) == 2 and np.all([len(_) > 3 for _ in f])), 'data not uploaded' # Completely delete db to facilitate future testing os.remove(sql.fetch_db_filepath())