# Licensed under a 3-clause BSD style license - see LICENSE.rst
# -*- coding: utf-8 -*-
"""Test desimodel.inputs
"""
import unittest
from unittest.mock import patch
import os
from requests.auth import HTTPDigestAuth
from ..inputs import ci, docdb, fiberpos, focalplane, focalplane_sync, focalplane_utils, gfa, throughput
from .. import io

desimodel_available = os.path.isdir(io.datadir())
desimodel_message = "The desimodel data set was not detected."


class TestInputs(unittest.TestCase):
    """Test desimodel.inputs
    """
    def setUp(self):
        pass

    def test_docdb__xls_col2int(self):
        self.assertEqual(docdb._xls_col2int('A'), 0)
        self.assertEqual(docdb._xls_col2int('AA'), 26)

    def test_docdb__auth(self):
        with patch('netrc.netrc') as netrc:
            n = netrc.return_value
            n.authenticators.return_value = None
            with self.assertRaises(ValueError) as e:
                h = docdb._auth('www.example.com')
            self.assertEqual(str(e.exception), 'Unable to get user/pass from $HOME/.netrc for www.example.com')
            n.authenticators.return_value = ('username', 'foo', 'password')
            h = docdb._auth('www.example.com')
            try:
                self.assertIsInstance(h, HTTPDigestAuth)
            except AttributeError:
                # Python 2
                pass
            n.authenticators.assert_called_with('www.example.com')

    @unittest.skipUnless(desimodel_available, desimodel_message)
    def test_build_gfa_table(self):
        gfa.build_gfa_table(testdir='.')
        self.assertTrue(os.path.exists('gfa.ecsv'), "Test Failed to create a valid file!")
        os.remove('gfa.ecsv')
