import unittest
import os

from lokit import Office, LoKitInitializeError, LoKitImportError, LoKitExportError


class LokitTest(unittest.TestCase):
    def setUp(self):
        self.lo_path = os.getenv('LO_PATH', '/usr/lib/libreoffice/program/')

    def test_init_no_lo_path(self):
        self.assertRaises(LoKitInitializeError, Office, "wronglopath")

    def test_init(self):
        lo = Office(self.lo_path)
        self.assertIsNotNone(lo)

    def test_no_input_file(self):
        lo = Office(self.lo_path)
        self.assertRaises(LoKitImportError, lo.documentLoad, "foo")

    def test_no_output_file(self):
        lo = Office(self.lo_path)
        with lo.documentLoad("tests/foo.doc") as doc:
            self.assertRaises(LoKitExportError, doc.saveAs, "")

    def test_wrong_filter(self):
        with Office(self.lo_path) as lo:
            with lo.documentLoad("tests/foo.doc") as doc:
                self.assertRaises(LoKitExportError, doc.saveAs, "tests/out.rtf", fmt="foobar")

    def test_wrong_options(self):
        with Office(self.lo_path) as lo:
            with lo.documentLoad("tests/foo.doc") as doc:
                self.assertRaises(LoKitExportError, doc.saveAs, "tests/out.rtf", options="foobar")

    def test_filter_and_options(self):
        with Office(self.lo_path) as lo:
            with lo.documentLoad("tests/foo.doc") as doc:
                doc.saveAs("tests/out.docx", fmt="docx", options="SkipImages")
                os.unlink("tests/out.docx")

    def test_multiple_calls(self):
        with Office(self.lo_path) as lo:
            with lo.documentLoad("tests/foo.doc") as doc:
                doc.saveAs("tests/out.docx", fmt="docx", options="SkipImages")
                doc.saveAs("tests/out.pdf")
                os.unlink("tests/out.docx")
                os.unlink("tests/out.pdf")
            with lo.documentLoad("tests/foo.doc") as doc:
                doc.saveAs("tests/out.pdf")
                os.unlink("tests/out.pdf")

    def tearDown(self):
        assert os.path.exists("tests/.~lock.foo.doc#") == False


if __name__ == '__main__':
    unittest.main()

@@ -104,16 +104,53 @@
 tError%0A%0A
+TEST_DIR = os.path.dirname(__file__)%0A
 %0Aclass L
@@ -275,16 +275,422 @@
 ogram/')
+%0A        self.test_doc = self._to_abspath(%22tests/foo.doc%22)%0A        self.test_out_rtf = self._to_abspath(%22tests/out.rtf%22)%0A        self.test_out_docx = self._to_abspath(%22tests/out.docx%22)%0A        self.test_out_pdf = self._to_abspath(%22tests/out.pdf%22)%0A        self.test_lockfile = self._to_abspath(%22.~lock.foo.doc#%22)%0A%0A    def _to_abspath(self, path):%0A        return os.path.abspath(os.path.join(TEST_DIR, path))
 %0A%0A    de
@@ -1115,39 +1115,37 @@
 ocumentLoad(
-%22tests/foo.
+self.test_
 doc
-%22
 ) as doc:%0A  
@@ -1310,39 +1310,37 @@
 ocumentLoad(
-%22tests/foo.
+self.test_
 doc
-%22
 ) as doc:%0A  
@@ -1401,31 +1401,33 @@
 saveAs, 
-%22
+self.
 test
-s/out.
+_out_
 rtf
-%22
 , fmt=%22f
@@ -1539,39 +1539,37 @@
 ocumentLoad(
-%22tests/foo.
+self.test_
 doc
-%22
 ) as doc:%0A  
@@ -1630,31 +1630,33 @@
 saveAs, 
-%22
+self.
 test
-s/out.
+_out_
 rtf
-%22
 , option
@@ -1777,39 +1777,37 @@
 ocumentLoad(
-%22tests/foo.
+self.test_
 doc
-%22
 ) as doc:%0A  
@@ -1831,32 +1831,34 @@
 .saveAs(
-%22
+self.
 test
-s/out.
+_out_
 docx
-%22
 , fmt=%22d
@@ -1911,32 +1911,34 @@
 .unlink(
-%22
+self.
 test
-s/out.
+_out_
 docx
-%22
 )%0A%0A    d
@@ -2037,39 +2037,37 @@
 ocumentLoad(
-%22tests/foo.
+self.test_
 doc
-%22
 ) as doc:%0A  
@@ -2091,32 +2091,34 @@
 .saveAs(
-%22
+self.
 test
-s/out.
+_out_
 docx
-%22
 , fmt=%22d
@@ -2168,39 +2168,41 @@
  doc.saveAs(
-%22
+self.
 test
-s/out.
+_out_
 pdf
-%22
 )%0A          
@@ -2217,32 +2217,34 @@
 .unlink(
-%22
+self.
 test
-s/out.
+_out_
 docx
-%22
 )%0A      
@@ -2263,31 +2263,33 @@
 .unlink(
-%22
+self.
 test
-s/out.
+_out_
 pdf
-%22
 )%0A      
@@ -2315,31 +2315,29 @@
 entLoad(
-%22tests/foo.
+self.test_
 doc
-%22
 ) as doc
@@ -2365,31 +2365,33 @@
 .saveAs(
-%22
+self.
 test
-s/out.
+_out_
 pdf
-%22
 )%0A      
@@ -2410,31 +2410,33 @@
 .unlink(
-%22
+self.
 test
-s/out.
+_out_
 pdf
-%22
 )%0A%0A    d
@@ -2488,31 +2488,26 @@
 sts(
-%22tests/.~lock.foo.doc#%22
+self.test_lockfile
 ) ==

use absolute paths in tests