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

# Copyright (c) 2010-2011, GEM Foundation.
#
# OpenQuake is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License version 3
# only, as published by the Free Software Foundation.
#
# OpenQuake is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License version 3 for more details
# (a copy is included in the LICENSE file that accompanied this code).
#
# You should have received a copy of the GNU Lesser General Public License
# version 3 along with OpenQuake.  If not, see
# <http://www.gnu.org/licenses/lgpl-3.0.txt> for a copy of the LGPLv3 License.

"""
Tests related to the various Writer base classes.
"""

import mock
import unittest

from openquake import writer


class FileWriterTestCase(unittest.TestCase):
    """Tests related to the `FileWriter` abstract base class."""

    def test__init_file_with_mode_not_set(self):
        """
        _init_file() will open and truncate the file if no mode was specified.
        """
        path = "/tmp/a"
        # Only mock the open() built-in in the writer module.
        with mock.patch("openquake.writer.open", create=True) as mock_open:
            fw = writer.FileWriter(path)
            fw.open()
            self.assertEqual((path, "w"), mock_open.call_args[0])

    def test__init_file_with_mode_start(self):
        """
        _init_file() will not open the file in mode `MODE_START`.
        """
        path = "/tmp/b"
        # Only mock the open() built-in in the writer module.
        with mock.patch("openquake.writer.open", create=True) as mock_open:
            fw = writer.FileWriter(path)
            fw.set_params(path, writer.MODE_START)
            fw.open()
            self.assertEqual(0, mock_open.call_count)

    def test__init_file_with_mode_start_and_end(self):
        """
        _init_file() will open and truncate the file if the mode passed was
        `MODE_START_AND_END`.
        """
        path = "/tmp/c"
        # Only mock the open() built-in in the writer module.
        with mock.patch("openquake.writer.open", create=True) as mock_open:
            fw = writer.FileWriter(path)
            fw.set_params(path, writer.MODE_START_AND_END)
            fw.open()
            self.assertEqual((path, "w"), mock_open.call_args[0])

    def test__init_file_with_mode_in_the_middle(self):
        """
        _init_file() will not open the file in mode `MODE_IN_THE_MIDDLE`.
        """
        path = "/tmp/d"
        # Only mock the open() built-in in the writer module.
        with mock.patch("openquake.writer.open", create=True) as mock_open:
            fw = writer.FileWriter(path)
            fw.set_params(path, writer.MODE_IN_THE_MIDDLE)
            fw.open()
            self.assertEqual(0, mock_open.call_count)

    def test__init_file_with_mode_end(self):
        """
        _init_file() will open and truncate the file if the mode passed was
        `MODE_END`.
        """
        path = "/tmp/e"
        # Only mock the open() built-in in the writer module.
        with mock.patch("openquake.writer.open", create=True) as mock_open:
            fw = writer.FileWriter(path)
            fw.set_params(path, writer.MODE_END)
            fw.open()
            self.assertEqual((path, "w"), mock_open.call_args[0])

@@ -1028,34 +1028,28 @@
    def test_
-_init_file
+open
 _with_mode_n
@@ -1078,34 +1078,28 @@
 %22%22%22%0A        
-_init_file
+open
 () will open
@@ -1460,34 +1460,28 @@
    def test_
-_init_file
+open
 _with_mode_s
@@ -1508,34 +1508,28 @@
 %22%22%22%0A        
-_init_file
+open
 () will not 
@@ -1916,34 +1916,28 @@
    def test_
-_init_file
+open
 _with_mode_s
@@ -1972,34 +1972,28 @@
 %22%22%22%0A        
-_init_file
+open
 () will open
@@ -2440,34 +2440,28 @@
    def test_
-_init_file
+open
 _with_mode_i
@@ -2496,34 +2496,28 @@
 %22%22%22%0A        
-_init_file
+open
 () will not 
@@ -2924,26 +2924,20 @@
 ef test_
-_init_file
+open
 _with_mo
@@ -2974,18 +2974,12 @@
     
-_init_file
+open
 () w

fix test names/doc strings