Skip to content

Commit

Permalink
Add a test for _store_data
Browse files Browse the repository at this point in the history
  • Loading branch information
153957 committed May 7, 2016
1 parent 605046d commit 92680e0
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 3 deletions.
40 changes: 40 additions & 0 deletions sapphire/tests/test_data/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
How to create the test data
===========================


Notes on recreating publicdb.h5 and publicdb_src.h5
---------------------------------------------------

This test data is simply 5 minutes of real data from station 501 downloaded
from the raw data via the Public Database. Data from 2016-4-21 is used because
it contains many types of data: comparator, config, events, singles, and
weather. Because the download script downloads all blobs for the day, those
need to be removed after downloading, and the file repacked otherwise the file
remains large. This can be done using the following script:

>>> import tables
>>> from datetime import datetime
>>> from sapphire.publicdb import download_data
>>> with tables.open_file('publicdb_temp.h5', 'w') as data:
... download_data(data, '/station_501', 501, datetime(2016, 4, 21),
... datetime(2016, 4, 21, 0, 1, 30), get_blobs=True)
... max_blobs_id = data.root.s501.events.col('traces').max()
... data.root.s501.blobs.truncate(max_blobs_id + 1)

$ ptrepack --complevel 9 --complib blosc publicdb_temp.h5 publicdb_src.h5
$ rm publicdb_temp.h5
$ cp publicdb_src.h5 publicdb_src_tmp.h5

>>> import tables
>>> from datetime import datetime
>>> from sapphire.publicdb import _store_data
>>> f = tables.Filters(complevel=1)
>>> with tables.open_file('publicdb.h5', 'w', filters=f) as data:
... _store_data(data, '/s501', 'publicdb_src_tmp.h5',
... datetime(2016, 4, 21), datetime(2016, 4, 21, 0, 1))


Other data
----------

The other data is created by the `esd_load_data.py` script one level up.
Binary file added sapphire/tests/test_data/publicdb.h5
Binary file not shown.
Binary file added sapphire/tests/test_data/publicdb_src.h5
Binary file not shown.
25 changes: 22 additions & 3 deletions sapphire/tests/test_publicdb.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import unittest
from datetime import datetime
import logging
import shutil
import os

from mock import sentinel, Mock, patch
import tables

from sapphire import publicdb
from sapphire.tests.validate_results import validate_results

from esd_load_data import create_tempfile_path


self_path = os.path.dirname(__file__)
test_data_src_path = os.path.join(self_path, 'test_data/publicdb_src.h5')
test_data_path = os.path.join(self_path, 'test_data/publicdb.h5')


class DownloadDataTest(unittest.TestCase):
Expand Down Expand Up @@ -40,10 +50,19 @@ def test_download_data(self, mock_server, mock_retrieve, mock_store):
sentinel.group, sentinel.station_id, start,
end, get_blobs=sentinel.blobs)

@unittest.skip('WIP')
def test__store_data(self):
pass
# publicdb._store_data(dst_file, dst_group, src_filename, t0, t1)
# store data removes the source data when completed, so use a temp
tmp_src_path = create_tempfile_path()
shutil.copy(test_data_src_path, tmp_src_path)

output_path = create_tempfile_path()
start = datetime(2016, 4, 21)
end = datetime(2016, 4, 21, 0, 1)
filters = tables.Filters(complevel=1)
with tables.open_file(output_path, 'w', filters=filters) as datafile:
publicdb._store_data(datafile, '/s501', tmp_src_path, start, end)
validate_results(self, test_data_path, output_path)
os.remove(output_path)

def test_datetimerange(self):
combinations = [
Expand Down

0 comments on commit 92680e0

Please sign in to comment.