import pytest
import os
import numpy as np

import sys
myPath = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(myPath, '..'))

from DBNlogic.sets import exists, full, DataSet, MNIST, SmallerMNIST

def test_existsTrue():
    """The function `exists` returns True if
    given the path of an existing file."""
    file = open('test.test', 'w+')
    file.close()
    assert exists('test.test')
    os.remove('test.test')

def test_existsFalse():
    """The function `exists` returns False if
    given the path of an unexisting file."""
    assert exists('test.test') == False

def test_DataSetConstructor():
    """A DataSet object can be constructed from an array."""
    data = [1, 2, 3, 4, 5, 6, 7]
    dataset = DataSet(data)
    assert all(dataset.data[i] == data[i] for i in range(len(data)))

def test_save():
    """A DataSet object can be saved to a CSV, Pickle, or Matlab file."""
    a = DataSet(np.random.rand(8, 5))
    a.save('test.mat')
    assert exists('test.mat')
    os.remove('test.mat')

def test_fromCSV():
    """A DataSet object can be constructed from a CSV file."""
    dataset = DataSet.fromCSV(full('left_8.csv'))
    assert dataset.shape == (14, 8)

def test_fromPickle():
    """A DataSet object can be constructed from a Pickle file."""
    dataset = DataSet.fromPickle(full('left_8.pkl'))
    assert dataset.shape == (14, 8)

def test_fromMatlab():
    """A DataSet object can be constructed from a Matlab file."""
    SCIPY_AVAILABLE = True
    try:
        import scipy
    except ImportError:
        SCIPY_AVAILABLE = False
    a = np.random.rand(8, 5)
    DataSet(a).save('test.mat')
    b = DataSet.fromMatlab('test.mat')
    if SCIPY_AVAILABLE:
        assert a.shape == b.shape
        os.remove('test.mat')
    else:
        assert len(b) == 0

def test_fromWhatever():
    """A DataSet object can be constructed from a file."""
    dataset = DataSet.fromWhatever('left_8')
    assert dataset.shape == (14, 8)

# def test_allSets():
#     """The available datasets include the MNIST dataset
#     and a downsampling the MNIST dataset."""
#     datasets = DataSet.allSets()
#     assert 'MNIST' in datasets
#     assert 'small_MNIST' in datasets

# def test_SmallerMNIST():
#     """A downsampling of the MNIST dataset
#     can be constructed from a file."""
#     mnist = SmallerMNIST()
#     assert mnist.data.shape == (60000, 49)

@@ -967,24 +967,52 @@
 'test.mat')%0A
+    if SCIPY_AVAILABLE:%0A    
     assert e
@@ -1025,24 +1025,28 @@
 'test.mat')%0A
+    
     os.remov

Update sets.py test for scipy