#!/usr/bin/env python2.7
"""
File       : utils_t.py
Author     : Bjorn Barrefors <bjorn dot peter dot barrefors AT cern dot ch>
Description: Test class for all utils functions
"""

# system modules
import unittest
import os
import json
import datetime
from bson.objectid import ObjectId

# package modules
from cuadrnt.utils.config import get_config
from cuadrnt.utils.db_utils import get_object_id
from cuadrnt.utils.db_utils import datetime_to_object_id
from cuadrnt.utils.io_utils import export_csv
from cuadrnt.utils.io_utils import export_json
from cuadrnt.utils.utils import check_tool
from cuadrnt.utils.utils import weighted_choice
from cuadrnt.utils.utils import daterange
from cuadrnt.utils.utils import bytes_to_gb
from cuadrnt.utils.utils import datetime_to_timestamp
from cuadrnt.utils.utils import timestamp_to_datetime
from cuadrnt.utils.utils import datetime_day
from cuadrnt.utils.utils import datetime_to_string
from cuadrnt.utils.utils import phedex_timestamp_to_datetime
from cuadrnt.utils.utils import pop_db_timestamp_to_datetime
from cuadrnt.utils.utils import datetime_remove_timezone
from cuadrnt.utils.utils import get_json

# get local config file
opt_path = os.path.join(os.path.split(os.path.dirname(os.path.realpath(__file__)))[0], 'etc')

@unittest.skip("Skip Test")
class UtilsTests(unittest.TestCase):
    """
    A test class for util functions
    Test web_utils in services test
    """
    def setUp(self):
        "Set up for test"
        self.config = get_config(path=opt_path, file_name='test.cfg')

    def tearDown(self):
        "Clean up"
        pass

    #@unittest.skip("Skip Test")
    def test_get_config(self):
        "Test get_config function"
        expected = 'bar'
        result = str(self.config['test']['foo'])
        self.assertEqual(result, expected)

    #@unittest.skip("Skip Test")
    def test_get_object_id(self):
        "Test get_object_id function"
        api = 'test'
        params = {'foo':'bar'}
        expected = ObjectId('09f51db4a7ebda7328d11bd4')
        result = get_object_id(str(api)+str(params))
        self.assertEqual(result, expected)

    #@unittest.skip("Skip Test")
    def test_datetime_to_object_id(self):
        "Test get_object_id function"
        datetime_ = datetime.datetime(1987, 10, 27)
        expected = ObjectId('2183e2000000000000000000')
        result = datetime_to_object_id(datetime_)
        self.assertEqual(result, expected)

    #@unittest.skip("Skip Test")
    def test_export_csv(self):
        "Test export_csv function"
        file_name = 'test'
        path = '/var/lib/cuadrnt'
        headers = ('foo', 'bar')
        data = [('Bjorn', 'Barrefors')]
        export_file = '%s/%s.csv' % (path, file_name)
        export_csv(headers=headers, data=data, path=path, file_name=file_name)
        expected = 'foo,bar\nBjorn,Barrefors\n'
        fd = open(export_file, 'r')
        result = fd.read()
        fd.close()
        self.assertEqual(result, expected)
        os.remove(export_file)

    #@unittest.skip("Skip Test")
    def test_export_json(self):
        "Test export_json function"
        file_name = 'test'
        path = '/var/lib/cuadrnt'
        data = [{'foo':'bar'}]
        export_file = '%s/%s.json' % (path, file_name)
        export_json(data=data, path=path, file_name=file_name)
        expected = 'bar'
        fd = open(export_file, 'r')
        result = json.load(fd)
        fd.close()
        self.assertEqual(result['foo'], expected)
        os.remove(export_file)

    #@unittest.skip("Skip Test")
    def test_check_tool(self):
        "Test check_tool function"
        tool = 'rm'
        result = check_tool(tool)