import datetime
import os

import isodate
import tables

import pyhis


TEST_FILE_PATH = '/tmp/pyhis_test.h5'


def test_init():
    if os.path.exists(TEST_FILE_PATH):
        os.remove(TEST_FILE_PATH)
    assert not os.path.exists(TEST_FILE_PATH)
    pyhis.usgs.pytables.init_h5(TEST_FILE_PATH)
    assert os.path.exists(TEST_FILE_PATH)


def test_parse_get_sites():
    site_files = ['RI_daily.xml', 'RI_instantaneous.xml']
    sites = {}
    for site_file in site_files:
        with open(site_file, 'r') as f:
            sites.update(pyhis.usgs.core._parse_sites(f))

    assert len(sites) == 63
    return sites


def test_update_site_list():
    assert _count_rows('/usgs/sites') == 0
    sites = test_parse_get_sites()
    pyhis.usgs.pytables._update_site_table(sites, TEST_FILE_PATH)
    assert _count_rows('/usgs/sites') == 63


def test_pytables_get_sites():
    sites = pyhis.usgs.pytables.get_sites(TEST_FILE_PATH)
    assert len(sites) == 63


def test_update_or_append():
    h5file = tables.openFile(TEST_FILE_PATH, mode="r+")
    test_table = _create_test_table(h5file, 'update_or_append', pyhis.usgs.pytables.USGSValue)
    where_filter = '(datetime == "%(datetime)s")'

    initial_values = [
            {'datetime': isodate.datetime_isoformat(datetime.datetime(2000, 1, 1) + \
                datetime.timedelta(days=i)),
             'value': 'initial',
             'qualifiers': ''}
            for i in range(1000)]


    update_values = [
            {'datetime': isodate.datetime_isoformat(datetime.datetime(2000, 1, 1) + \
                datetime.timedelta(days=i)),
             'value': 'updated',
             'qualifiers': ''}
            for i in [20, 30, 10, 999, 1000, 2000, 399]]

    pyhis.usgs.pytables._update_or_append(test_table, initial_values, where_filter)
    h5file.close()

    assert _count_rows('/test/update_or_append') == 1000

    h5file = tables.openFile(TEST_FILE_PATH, mode="r+")
    test_table = h5file.getNode('/test/update_or_append')
    pyhis.usgs.pytables._update_or_append(test_table, update_values, where_filter)
    h5file.close()
    assert _count_rows('/test/update_or_append') == 1002


def _count_rows(path):
    h5file = tables.openFile(TEST_FILE_PATH, mode="r")
    table = h5file.getNode(path)
    number_of_rows = len([1 for i in table.iterrows()])
    h5file.close()
    return number_of_rows


def _create_test_table(h5file, table_name, description):
    test_table = h5file.createTable('/test', table_name, description,
                                    createparents=True)
    return test_table

@@ -130,75 +130,26 @@
     
-if os.path.exists(TEST_FILE_PATH):%0A        os.remove(TEST_FILE_PATH
+_remove_test_file(
 )%0A  
@@ -2099,16 +2099,407 @@
  1002%0A%0A%0A
+def test_core_get_sites_by_state_code():%0A    sites = pyhis.usgs.core.get_sites(state_code='RI')%0A    assert len(sites) == 63%0A%0A%0Adef test_core_get_sites_single_site():%0A    sites = pyhis.usgs.core.get_sites(sites='08068500')%0A    assert len(sites) == 1%0A%0A%0Adef test_core_get_sites_multiple_sites():%0A    sites = pyhis.usgs.core.get_sites(sites=%5B'08068500', '08041500'%5D)%0A    assert len(sites) == 2%0A%0A%0A
 def _cou
@@ -2909,8 +2909,109 @@
 t_table%0A
+%0A%0Adef _remove_test_file():%0A    if os.path.exists(TEST_FILE_PATH):%0A        os.remove(TEST_FILE_PATH)%0A%0A

add a few tests for usgs.core