# -*- coding: utf-8 -*-

"""
dynamic_learn.testsuite
~~~~~~~~~~~~~~~~~~~~~~~

:license: BSD, see LICENSE for more details.
"""

import unittest
import theano.tensor as T
import tempfile
import os

from versus.src.logistic import LogisticRegression
from versus.tools.dataIO import DataIORedis, DataIOHDFS

BATCH_SIZE = 50


class LocalTestCase(unittest.TestCase):
    """
    Setup relevant test context in this class

    """
    def setUp(self):
        unittest.TestCase.setUp(self)


class TestLogistic(LocalTestCase):
    """ Test cases for Logistic model """

    def test_init(self):

        # allocate symbolic variables for the data
        x = T.fmatrix()  # the data is presented as rasterized images (each being a 1-D row vector in x)

        # construct the logistic regression class
        try:
            LogisticRegression(_input=x.reshape((BATCH_SIZE , 28 * 28)), n_in=28 * 28, n_out=10)
        except Exception:
            assert False

        assert True

    def test_negative_log_likelihood(self):

        # allocate symbolic variables for the data
        x = T.fmatrix()  # the data is presented as rasterized images (each being a 1-D row vector in x)
        y = T.lvector()  # the labels are presented as 1D vector of [long int] labels

        # construct the logistic regression class
        try:
            classifier = LogisticRegression(x.reshape((BATCH_SIZE , 28 * 28)), 28 * 28, 10)
            cost = classifier.negative_log_likelihood(y)
        except Exception:
            assert False

        assert cost == 0


class TestRedis(LocalTestCase):
    """ Test cases for Redis read/writes """

    def test_init(self):
        dior = DataIORedis()
        dior.connect()  # connect to local instance

        del dior
        assert True

    def test_simple(self):
        dior = DataIORedis()
        dior.connect()  # connect to local instance

        if not dior.write(key='try', value=1):
            assert False

        if not (dior.read(key='try') == 1):
            assert False

        del dior
        assert True


class TestDataIOHDFS(LocalTestCase):
    """ Test cases for HDFS IO """

    def test_copy_from_local(self):

        tempdir = tempfile.mkdtemp()
        handle, fullpath = tempfile.mkstemp(dir=tempdir)
        hdfs_path = '/user/test'

        io = DataIOHDFS()
        io.copy_from_local(fullpath, hdfs_path)
        filename = os.path.basename(fullpath)

        exists_in_hdfs = False
        for item in io.list(hdfs_path):
            if str(item) == filename:
                exists_in_hdfs = True
        self.assertTrue(exists_in_hdfs)

    def test_copy_to_local(self):
        assert False

    def test_list(self):
        assert False

@@ -2169,32 +2169,91 @@
 m_local(self):%0A%0A
+        io = DataIOHDFS()%0A%0A        # Create a file on HDFS%0A
         tempdir 
@@ -2366,35 +2366,8 @@
 est'
-%0A%0A        io = DataIOHDFS()
 %0A   
@@ -2458,16 +2458,46 @@
 lpath)%0A%0A
+        # Check the hdfs path%0A
         
@@ -2706,32 +2706,33 @@
 ocal(self):%0A
+%0A
         
 assert False
@@ -2715,36 +2715,480 @@
 ):%0A%0A        
-assert False
+io = DataIOHDFS()%0A%0A        # Create a file on HDFS%0A        tempdir = tempfile.mkdtemp()%0A        handle, fullpath = tempfile.mkstemp(dir=tempdir)%0A        hdfs_path = '/user/test'%0A        io.copy_from_local(fullpath, hdfs_path)%0A%0A        # Copy back to the filesystem%0A        tempdir = tempfile.mkdtemp()%0A        handle, fullpath = tempfile.mkstemp(dir=tempdir)%0A        io.copy_to_local(fullpath, hdfs_path)%0A%0A        self.assertTrue(os.path.exists(fullpath))%0A
 %0A%0A    def te
@@ -3202,29 +3202,44 @@
 (self):%0A        
-assert False
+raise NotImplementedError()
 %0A

add - test implementation for test_copy_to_local