<commit_before>"""
Test Classes for reading, writing and manipulating COMBINE archives
no metadata tests involved
"""
import shutil
import os
import tempfile
import unittest
from test import test_support

from combinearchive import combinearchive, metadata


class BaseReadTest(unittest.TestCase):
    """
    abstract base class for all tests involve only reading from COMBINE archives
    """
    TEST_ARCHIVE = 'test.omex'

    def setUp(self):
        # first copy test archive into a temp location, so validation data does
        # not get harmed
        self.archive_location = tempfile.NamedTemporaryFile(delete=False).name
        if self.TEST_ARCHIVE is not None:
            shutil.copy(self.TEST_ARCHIVE, self.archive_location)

        self._temp_files = list()

    def tearDown(self):
        # close archive and remove temp file
        self.close_archive()
        os.remove(self.archive_location)

        # remove created temp files
        for name in self._temp_files:
            os.remove(name)

    def open_archive(self):
        self.carchive = combinearchive.CombineArchive(self.archive_location)
        return self.carchive

    def close_archive(self):
        if hasattr(self, 'carchive') and self.carchive is not None:
            self.carchive.close()

    def get_random_file(self):
        file = tempfile.NamedTemporaryFile(delete=False)
        file.write("pyCombineArchive Test file")
        name = file.name
        self._temp_files.append(name)
        file.close()

        return name

class ReadTest(BaseReadTest):
    TEST_ARCHIVE = '../../test/all-singing-all-dancing.omex'

    def test_get_entry(self):
        self.open_archive()

        entry = self.carchive.get_entry('documentation/Calzone2007.pdf')
        self.assertIsNotNone(entry, 'archive entry is unexpected None. Should throw KeyError instead')

        with self.assertRaises(KeyError):
            self.carchive.get_entry('does not exist')

        self.close_archive()

    def test_filter_format(self):
        self.open_archive()

        test_format = 'http://purl.org/NET/mediatypes/application/pdf'
        filter_result = self.carchive.filter_format(test_format, regex=False)
        for entry in filter_result:
            self.assertEqual(entry.format, test_format, 'filter_format mismatched {}'.format(entry.format))

        self.close_archive()


class AddDeleteTest(BaseReadTest):
    TEST_ARCHIVE = '../../test/all-singing-all-dancing.omex'

    def test_add(self):
        self.open_archive()

        entry = self.carchive.add_entry(self.get_random_file(), "text/plain", "test/1.txt")
        self.assertIsNotNone(entry, 'created entry is None')

        meta = metadata.OmexMetaDataObject()
        meta.creator.append(metadata.VCard(family_name="Peters", given_name="Martin", organization="University of Rostock"))
        meta.creator.append(metadata.VCard(family_name="Scharm", given_name="Martin", organization="University of Rostock"))
        meta.modified.append(meta.created)
        meta.description = "This is a Test"
        entry.add_description(meta)

        self.carchive.pack()



        self.close_archive()


def do_tests():
    """
    run all tests in this module
    """
    test_support.run_unittest(ReadTest,
                              AddDeleteTest)

if __name__ == '__main__':
    do_tests()<commit_msg>some test for format-string conversion (mime/purl.org)<commit_after>"""
Test Classes for reading, writing and manipulating COMBINE archives
no metadata tests involved
"""
import shutil
import os
import tempfile
import unittest
from test import test_support

from combinearchive import combinearchive, metadata


class BaseReadTest(unittest.TestCase):
    """
    abstract base class for all tests involve only reading from COMBINE archives
    """
    TEST_ARCHIVE = 'test.omex'

    def setUp(self):
        # first copy test archive into a temp location, so validation data does
        # not get harmed
        self.archive_location = tempfile.NamedTemporaryFile(delete=False).name
        if self.TEST_ARCHIVE is not None:
            shutil.copy(self.TEST_ARCHIVE, self.archive_location)

        self._temp_files = list()

    def tearDown(self):
        # close archive and remove temp file
        self.close_archive()
        os.remove(self.archive_location)

        # remove created temp files
        for name in self._temp_files:
            os.remove(name)

    def open_archive(self):
        self.carchive = combinearchive.CombineArchive(self.archive_location)
        return self.carchive

    def close_archive(self):
        if hasattr(self, 'carchive') and self.carchive is not None:
            self.carchive.close()

    def get_random_file(self):
        file = tempfile.NamedTemporaryFile(delete=False)
        file.write("pyCombineArchive Test file")
        name = file.name
        self._temp_files.append(name)
        file.close()

        return name

class ReadTest(BaseReadTest):
    TEST_ARCHIVE = '../../test/all-singing-all-dancing.omex'

    def test_get_entry(self):
        self.open_archive()

        entry = self.carchive.get_entry('documentation/Calzone2007.pdf')
        self.assertIsNotNone(entry, 'archive entry is unexpected None. Should throw KeyError instead')

        with self.assertRaises(KeyError):
            self.carchive.get_entry('does not exist')

        self.close_archive()

    def test_filter_format(self):
        self.open_archive()

        test_format = 'http://purl.org/NET/mediatypes/application/pdf'
        filter_result = self.carchive.filter_format(test_format, regex=False)
        for entry in filter_result:
            self.assertEqual(entry.format, test_format, 'filter_format mismatched {}'.format(entry.format))

        self.close_archive()


class AddDeleteTest(BaseReadTest):
    TEST_ARCHIVE = '../../test/all-singing-all-dancing.omex'

    def test_add(self):
        self.open_archive()

        entry = self.carchive.add_entry(self.get_random_file(), "text/plain", "test/1.txt")
        self.assertIsNotNone(entry, 'created entry is None')

        meta = metadata.OmexMetaDataObject()
        meta.creator.append(metadata.VCard(family_name="Peters", given_name="Martin", organization="University of Rostock"))
        meta.creator.append(metadata.VCard(family_name="Scharm", given_name="Martin", organization="University of Rostock"))
        meta.modified.append(meta.created)
        meta.description = "This is a Test"
        entry.add_description(meta)

        self.carchive.pack()



        self.close_archive()


class FormatConversionTest(unittest.TestCase):

    def test_formatcheck(self):

        with self.assertRaises(combinearchive.CombineArchiveFormatException):
            combinearchive.check_format('foobar', convert=False)                        # no mime type or url
        with self.assertRaises(combinearchive.CombineArchiveFormatException):
            combinearchive.check_format('ftp://purl.org/mediatypes/application/pdf')    # not a valid url schema (only https? allowed)

        # check for identifiers urls
        self.assertEqual(combinearchive.check_format('http://identifiers.org/combine.specifications/cellml'),
                         'http://identifiers.org/combine.specifications/cellml',
                         'check_format alternated identifiers.org link')

        self.assertEqual(combinearchive.check_format('http://identifiers.org/combine.specifications/sbml.level-3.version-1'),
                         'http://identifiers.org/combine.specifications/sbml.level-3.version-1',
                         'check_format alternated identifiers.org link')

    def test_conversion(self):

        # no conversion, but check of mime type
        self.assertEqual(combinearchive.check_format('application/pdf', convert=False),
                         'application/pdf')

        # conversion to purl.org url
        self.assertEqual(combinearchive.check_format('application/pdf', convert=True),
                         'http://purl.org/NET/mediatypes/application/pdf')


def do_tests():
    """
    run all tests in this module
    """
    test_support.run_unittest(ReadTest,
                              AddDeleteTest,
                              FormatConversionTest)

if __name__ == '__main__':
    do_tests()
{'fraction_non_alphanumeric': 0.09888127029953085, 'fraction_numerical': 0.001684109226512691, 'mean_word_length': 2.8225287356321838, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3373788', 'n_tokens_mistral': 2389, 'n_tokens_neox': 2295, 'n_words': 500}