<commit_before>import sys
import unittest
try: #python2
    # Must try this first as io also exists in python2
    # but in the wrong one!
    import StringIO as io
except ImportError: #python3
    import io

from resync.resource import Resource
from resync.list_base import ListBase
from resync.sitemap import Sitemap, SitemapIndexError

# etree gives ParseError in 2.7, ExpatError in 2.6
etree_error_class = None
if (sys.version_info < (2,7)):
    from xml.parsers.expat import ExpatError
    etree_error_class = ExpatError
else:
    from xml.etree.ElementTree import ParseError
    etree_error_class = ParseError

class TestListBase(unittest.TestCase):

    def test_05_len_count(self):
        # count sets explicit number of resources, len(resources) not used
        lb = ListBase()
        self.assertEqual( len(lb), 0 )
        lb.add(Resource('a'))
        self.assertEqual( len(lb), 1 )
        lb = ListBase( count=100 )
        self.assertEqual( len(lb), 100 )

    def test_08_print(self):
        lb = ListBase()
        lb.add( Resource(uri='a',lastmod='2001-01-01',length=1234) )
        lb.add( Resource(uri='b',lastmod='2002-02-02',length=56789) )
        lb.add( Resource(uri='c',lastmod='2003-03-03',length=0) )
        lb.md['from']=None #avoid now being added
        print(lb)
        x = lb.as_xml()
        self.assertEqual( x, '<?xml version=\'1.0\' encoding=\'UTF-8\'?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:rs="http://www.openarchives.org/rs/terms/"><rs:md capability="unknown" /><url><loc>a</loc><lastmod>2001-01-01T00:00:00Z</lastmod><rs:md length="1234" /></url><url><loc>b</loc><lastmod>2002-02-02T00:00:00Z</lastmod><rs:md length="56789" /></url><url><loc>c</loc><lastmod>2003-03-03T00:00:00Z</lastmod><rs:md length="0" /></url></urlset>' )

    def test_11_parse_2(self):
        xml = '<?xml version=\'1.0\' encoding=\'UTF-8\'?>\n\
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:rs="http://www.openarchives.org/rs/terms/">\
<rs:md capability="unknown" from="2013-02-12T14:09:00Z" />\
<url><loc>/tmp/rs_test/src/file_a</loc><lastmod>2012-03-14T18:37:36Z</lastmod><rs:md length="12" /></url>\
<url><loc>/tmp/rs_test/src/file_b</loc><lastmod>2012-03-14T18:37:36Z</lastmod><rs:md length="32" /></url>\
</urlset>'
        lb=ListBase()
        lb.parse(fh=io.StringIO(xml))<commit_msg>Remove debug print<commit_after>import sys
import unittest
try: #python2
    # Must try this first as io also exists in python2
    # but in the wrong one!
    import StringIO as io
except ImportError: #python3
    import io

from resync.resource import Resource
from resync.list_base import ListBase
from resync.sitemap import Sitemap, SitemapIndexError

# etree gives ParseError in 2.7, ExpatError in 2.6
etree_error_class = None
if (sys.version_info < (2,7)):
    from xml.parsers.expat import ExpatError
    etree_error_class = ExpatError
else:
    from xml.etree.ElementTree import ParseError
    etree_error_class = ParseError

class TestListBase(unittest.TestCase):

    def test_01_len_count(self):
        # count sets explicit number of resources, len(resources) not used
        lb = ListBase()
        self.assertEqual( len(lb), 0 )
        lb.add(Resource('a'))
        self.assertEqual( len(lb), 1 )
        lb = ListBase( count=100 )
        self.assertEqual( len(lb), 100 )

    def test_02_print(self):
        lb = ListBase()
        lb.add( Resource(uri='a',lastmod='2001-01-01',length=1234) )
        lb.add( Resource(uri='b',lastmod='2002-02-02',length=56789) )
        lb.add( Resource(uri='c',lastmod='2003-03-03',length=0) )
        lb.md['from']=None #avoid now being added
        x = lb.as_xml()
        self.assertEqual( x, '<?xml version=\'1.0\' encoding=\'UTF-8\'?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:rs="http://www.openarchives.org/rs/terms/"><rs:md capability="unknown" /><url><loc>a</loc><lastmod>2001-01-01T00:00:00Z</lastmod><rs:md length="1234" /></url><url><loc>b</loc><lastmod>2002-02-02T00:00:00Z</lastmod><rs:md length="56789" /></url><url><loc>c</loc><lastmod>2003-03-03T00:00:00Z</lastmod><rs:md length="0" /></url></urlset>' )

    def test_03_parse(self):
        xml = '<?xml version=\'1.0\' encoding=\'UTF-8\'?>\n\
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:rs="http://www.openarchives.org/rs/terms/">\
<rs:md capability="unknown" from="2013-02-12T14:09:00Z" />\
<url><loc>/tmp/rs_test/src/file_a</loc><lastmod>2012-03-14T18:37:36Z</lastmod><rs:md length="12" /></url>\
<url><loc>/tmp/rs_test/src/file_b</loc><lastmod>2012-03-14T18:37:36Z</lastmod><rs:md length="32" /></url>\
</urlset>'
        lb=ListBase()
        lb.parse(fh=io.StringIO(xml))
{'fraction_non_alphanumeric': 0.17873063043939716, 'fraction_numerical': 0.07026109106346848, 'mean_word_length': 3.8577319587628867, 'pattern_counts': {'":': 0, '<': 91, '<?xml version=': 4, '>': 89, 'https://': 0, 'lorem ipsum': 0, 'www.': 8, 'xml': 22}, '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': '6852600', 'n_tokens_mistral': 1931, 'n_tokens_neox': 1712, 'n_words': 302}