# Licensed under a 3-clause BSD style license - see LICENSE.rst
import tempfile
import os
import pytest
from pathlib import Path

from .. import update_acq_stats as acq_stats
from .. import acq_stats as read_acq_stats

HAS_OBSPAR_ARCHIVE = os.path.exists(
        acq_stats.mica.archive.obspar.CONFIG['data_root'])
HAS_ACQ_TABLE = Path(read_acq_stats.TABLE_FILE).exists()


@pytest.mark.skipif('not HAS_ACQ_TABLE', reason='Test requires acq stats table')
def read_single_star_stats():
    # Fetch the stats for the slot 5 BOT of obsid 5438 by id
    single = read_acq_stats.get_star_stats(839386400)
    assert single[single['obsid'] == 5438]['acqid'][0] is True


@pytest.mark.skipif('not HAS_OBSPAR_ARCHIVE', reason='Test requires mica obspars')
def test_calc_stats():
    acq_stats.calc_stats(17210)
    acq_stats.calc_stats(15175)
    acq_stats.calc_stats(4911)
    acq_stats.calc_stats(19386)


@pytest.mark.skipif('not HAS_OBSPAR_ARCHIVE', reason='Test requires mica obspars')
def test_make_acq_stats():
    """
    Save the acq stats for one obsid into a newly-created table
    """
    # Get a temporary file, but then delete it, because _save_acq_stats will only
    # make a new table if the supplied file doesn't exist
    fh, fn = tempfile.mkstemp(suffix='.h5')
    os.unlink(fn)
    acq_stats.table_file = fn
    obsid = 20001
    obsid_info, acq, star_info, catalog, temp = acq_stats.calc_stats(obsid)
    t = acq_stats.table_acq_stats(obsid_info, acq, star_info, catalog, temp)
    acq_stats._save_acq_stats(t)
    os.unlink(fn)

@@ -379,33 +379,32 @@
 est.mark.skipif(
-'
 not HAS_ACQ_TABL
@@ -404,17 +404,16 @@
 CQ_TABLE
-'
 , reason
@@ -450,20 +450,20 @@
 e')%0Adef 
-read
+test
 _single_
@@ -649,16 +649,8 @@
 %5D%5B0%5D
- is True
 %0A%0A%0A@
@@ -660,33 +660,32 @@
 est.mark.skipif(
-'
 not HAS_OBSPAR_A
@@ -682,33 +682,32 @@
 S_OBSPAR_ARCHIVE
-'
 , reason='Test r
@@ -901,17 +901,16 @@
 .skipif(
-'
 not HAS_
@@ -923,17 +923,16 @@
 _ARCHIVE
-'
 , reason

Fix skip idioms, rename new test, fix assert