# Path: wtframework/wtf/utils/file_utils.py
# def temp_path(file_name=None):
# def create_temp_file(file_name=None, string_or_another_file=""):
# def download_to_tempfile(url, file_name=None, extension=None):


from wtframework.wtf.utils import file_utils
import tempfile
import unittest2
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with WTFramework.  If not, see <http://www.gnu.org/licenses/>.
##########################################################################



class TestFileUtils(unittest2.TestCase):

    def test_generate_temp_path(self):
        "Tests creating a file in temp directory."
        temp_name = file_utils.temp_path("blah")
        self.assertTrue(tempfile.gettempdir() in temp_name)

    def test_create_temp_file_creates_file_with_desired_content(self):
        "Tests tempfile is created with desired contents."
        temp_file = file_utils.create_temp_file(
            string_or_another_file="hello world")
        temp_file_contents = open(temp_file).read()
        self.assertEqual("hello world", temp_file_contents)

    def test_download_to_tempfile_downloads_content(self):
        "Test file is downloaded to temp file correctly"
        # This is a data URI that has a URL encoded text "hello world"
        hello_world_data_uri = "data:text/plain;charset=utf-8;base64,aGVsbG8gd29ybGQ="

        temp_file = file_utils.download_to_tempfile(hello_world_data_uri)
        temp_file_contents = open(temp_file).read()


# Path: wtframework/wtf/utils/file_utils.py
# def temp_path(file_name=None):
# def create_temp_file(file_name=None, string_or_another_file=""):
# def download_to_tempfile(url, file_name=None, extension=None):


# Path: wtframework/wtf/utils/tests/test_file_utils.py
from wtframework.wtf.utils import file_utils
import tempfile
import unittest2

#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with WTFramework.  If not, see <http://www.gnu.org/licenses/>.
##########################################################################



class TestFileUtils(unittest2.TestCase):

    def test_generate_temp_path(self):
        "Tests creating a file in temp directory."
        temp_name = file_utils.temp_path("blah")
        self.assertTrue(tempfile.gettempdir() in temp_name)

    def test_create_temp_file_creates_file_with_desired_content(self):
        "Tests tempfile is created with desired contents."
        temp_file = file_utils.create_temp_file(
            string_or_another_file="hello world")
        temp_file_contents = open(temp_file).read()
        self.assertEqual("hello world", temp_file_contents)

    def test_download_to_tempfile_downloads_content(self):
        "Test file is downloaded to temp file correctly"
        # This is a data URI that has a URL encoded text "hello world"
        hello_world_data_uri = "data:text/plain;charset=utf-8;base64,aGVsbG8gd29ybGQ="

        temp_file = file_utils.download_to_tempfile(hello_world_data_uri)
        temp_file_contents = open(temp_file).read()


        self.assertEqual("hello world", temp_file_contents)