<commit_before>from tests.testcase_with_tmpdir import TestCase
from tests.capture_stdout import capture_stdout
from tests.webserver_context import webserver

import unittest
import re
import logging
from testfixtures import LogCapture
import sys
import os.path

from resync.client import Client, ClientError, ClientFatalError
from resync.resource import Resource
from resync.resource_list import ResourceList
from resync.change_list import ChangeList

logging.basicConfig(level=logging.INFO)


class TestClient(TestCase):

    def test01_make_resource_list_empty(self):
        c = Client()
        # No mapping is an error
        self.assertRaises(ClientFatalError, c.build_resource_list)

    def test02_bad_source_uri(self):
        c = Client()
        self.assertRaises(ClientFatalError, c.baseline_or_audit)
        c.set_mappings(['http://example.org/bbb', '/tmp/this_does_not_exist'])
        self.assertRaises(ClientFatalError, c.baseline_or_audit)

    def test03_sitemap_uri(self):
        c = Client()
        c.set_mappings(['http://example.org/c', '/tmp/not_there_at_all'])
        self.assertEqual(c.sitemap_uri('abcd1'), 'http://example.org/c/abcd1')
        self.assertEqual(c.sitemap_uri('/abcd2'), '/abcd2')
        self.assertEqual(c.sitemap_uri('scheme:/abcd3'), 'scheme:/abcd3')

    def test04_read_resource_list(self):
        c = Client()
        rl = c.read_resource_list('tests/testdata/client/dir1/resourcelist.xml')
        self.assertEqual(len(rl), 3)
        rl = c.read_resource_list('file:tests/testdata/client/dir1/resourcelist.xml')
        self.assertEqual(len(rl), 3)
        self.assertEqual(len(rl), 3)
        self.assertRaises(ClientError, c.read_resource_list, 'file://tests/testdata/client/dir1/resourcelist.xml')
        self.assertRaises(ClientError, c.read_resource_list, 'file:///tests/testdata/client/dir1/resourcelist.xml')
        self.assertRaises(ClientError, c.read_resource_list, 'DOES_NOT_EXIST')

    def test05_find_resource_list(self):
        c = Client()
        # Filesystem tests
        c.set_mappings(['tests/testdata/find/find1', 'xxx'])
        self.assertEqual(c.find_resource_list().up, 'find1')
        c.set_mappings(['tests/testdata/find/find2', 'xxx'])
        self.assertEqual(c.find_resource_list().up, 'find2')
        c.set_mappings(['tests/testdata/find/find3', 'xxx'])
        self.assertEqual(c.find_resource_list().up, 'find3')
        # Tests requiring a server
        with webserver('tests/testdata/find', 'localhost', 9999):
            c.set_mappings(['http://localhost:9999/find1', 'xxx'])
            self.assertEqual(c.find_resource_list().up, 'find1')
            c.set_mappings(['http://localhost:9999/find2', 'xxx'])
            self.assertEqual(c.find_resource_list().up, 'find2')
            c.set_mappings(['http://localhost:9999/find3', 'xxx'])
            self.assertEqual(c.find_resource_list().up, 'find3')
        with webserver('tests/testdata/find/find1', 'localhost', 9999):
            c.set_mappings(['http://localhost:9999/data', 'xxx'])
            self.assertEqual(c.find_resource_list().up, 'find1')
        with webserver('tests/testdata/find/find3', 'localhost', 9999):
            c.set_mappings(['http://localhost:9999/data/data1', 'xxx'])
            self.assertEqual(c.find_resource_list().up, 'find3')

    def test10_baseline_or_audit(self):
        # FIXME - this is the guts of the client, tough to test, need to work
        # through more cases...
        c = Client()
        dst = os.path.join(self.tmpdir, 'dst_dir1')
        with webserver('tests/testdata/client', 'localhost', 9999):
            c.set_mappings(['http://localhost:9999/dir1', dst])
            # audit with empty dst, should say 3 to create
            with LogCapture() as lc:
                c.baseline_or_audit(audit_only=True)
                self.assertTrue(
                    re.match(r'Status:\s+NOT IN SYNC.*to create=3', lc.records[-2].msg))
                self.assertEqual(lc.records[-1].msg, 'Completed audit')
            # now do the sync
            with LogCapture() as lc:
                c.baseline_or_audit()
                self.assertTrue(
                    re.match(r'Status:\s+SYNCED.*created=3', lc.records[-2].msg))
                self.assertEqual(lc.records[-1].msg, 'Completed baseline sync')

    def test18_update_resource(self):
        c = Client()
        resource = Resource(uri='http://example.org/dir/2')
        filename = os.path.join(self.tmpdir, 'dir/resource2')
        # dryrun
        with LogCapture() as lc:
            c.dryrun = True
            c.logger = logging.getLogger('resync.client')
            n = c.update_resource(resource, filename)
            self.assertEqual(n, 0)
            self.assertTrue(
                lc.records[-1].msg.startswith('dryrun: would GET http://example.org/dir/2 '))
        c.dryrun = False
        with webserver('tests/testdata', 'localhost', 9999):
            # get from file uri that does not exist
            resource = Resource(uri='http://localhost:9999/i_do_not_exist')
            self.assertRaises(ClientFatalError,
                            c.update_resource, resource, filename)
            # get from file uri that does not exist but with c.ignore_failures to
            # log
            resource = Resource(uri='http://localhost:9999/i_do_not_exist')
            with LogCapture() as lc:
                c.logger = logging.getLogger('resync.client')
                c.ignore_failures = True
                n = c.update_resource(resource, filename)
                self.assertEqual(n, 0)
                self.assertTrue(
                    lc.records[-1].msg.startswith('Failed to GET http://localhost:9999/i_do_not_exist '))
            # get from file uri
            resource = Resource(uri='http://localhost:9999/examples_from_spec/resourcesync_ex_1.xml',<commit_msg>make pycodestyle happy<commit_after>from tests.testcase_with_tmpdir import TestCase
from tests.capture_stdout import capture_stdout
from tests.webserver_context import webserver

import unittest
import re
import logging
from testfixtures import LogCapture
import sys
import os.path

from resync.client import Client, ClientError, ClientFatalError
from resync.resource import Resource
from resync.resource_list import ResourceList
from resync.change_list import ChangeList

logging.basicConfig(level=logging.INFO)


class TestClient(TestCase):

    def test01_make_resource_list_empty(self):
        c = Client()
        # No mapping is an error
        self.assertRaises(ClientFatalError, c.build_resource_list)

    def test02_bad_source_uri(self):
        c = Client()
        self.assertRaises(ClientFatalError, c.baseline_or_audit)
        c.set_mappings(['http://example.org/bbb', '/tmp/this_does_not_exist'])
        self.assertRaises(ClientFatalError, c.baseline_or_audit)

    def test03_sitemap_uri(self):
        c = Client()
        c.set_mappings(['http://example.org/c', '/tmp/not_there_at_all'])
        self.assertEqual(c.sitemap_uri('abcd1'), 'http://example.org/c/abcd1')
        self.assertEqual(c.sitemap_uri('/abcd2'), '/abcd2')
        self.assertEqual(c.sitemap_uri('scheme:/abcd3'), 'scheme:/abcd3')

    def test04_read_resource_list(self):
        c = Client()
        rl = c.read_resource_list('tests/testdata/client/dir1/resourcelist.xml')
        self.assertEqual(len(rl), 3)
        rl = c.read_resource_list('file:tests/testdata/client/dir1/resourcelist.xml')
        self.assertEqual(len(rl), 3)
        self.assertEqual(len(rl), 3)
        self.assertRaises(ClientError, c.read_resource_list, 'file://tests/testdata/client/dir1/resourcelist.xml')
        self.assertRaises(ClientError, c.read_resource_list, 'file:///tests/testdata/client/dir1/resourcelist.xml')
        self.assertRaises(ClientError, c.read_resource_list, 'DOES_NOT_EXIST')

    def test05_find_resource_list(self):
        c = Client()
        # Filesystem tests
        c.set_mappings(['tests/testdata/find/find1', 'xxx'])
        self.assertEqual(c.find_resource_list().up, 'find1')
        c.set_mappings(['tests/testdata/find/find2', 'xxx'])
        self.assertEqual(c.find_resource_list().up, 'find2')
        c.set_mappings(['tests/testdata/find/find3', 'xxx'])
        self.assertEqual(c.find_resource_list().up, 'find3')
        # Tests requiring a server
        with webserver('tests/testdata/find', 'localhost', 9999):
            c.set_mappings(['http://localhost:9999/find1', 'xxx'])
            self.assertEqual(c.find_resource_list().up, 'find1')
            c.set_mappings(['http://localhost:9999/find2', 'xxx'])
            self.assertEqual(c.find_resource_list().up, 'find2')
            c.set_mappings(['http://localhost:9999/find3', 'xxx'])
            self.assertEqual(c.find_resource_list().up, 'find3')
        with webserver('tests/testdata/find/find1', 'localhost', 9999):
            c.set_mappings(['http://localhost:9999/data', 'xxx'])
            self.assertEqual(c.find_resource_list().up, 'find1')
        with webserver('tests/testdata/find/find3', 'localhost', 9999):
            c.set_mappings(['http://localhost:9999/data/data1', 'xxx'])
            self.assertEqual(c.find_resource_list().up, 'find3')

    def test10_baseline_or_audit(self):
        # FIXME - this is the guts of the client, tough to test, need to work
        # through more cases...
        c = Client()
        dst = os.path.join(self.tmpdir, 'dst_dir1')
        with webserver('tests/testdata/client', 'localhost', 9999):
            c.set_mappings(['http://localhost:9999/dir1', dst])
            # audit with empty dst, should say 3 to create
            with LogCapture() as lc:
                c.baseline_or_audit(audit_only=True)
                self.assertTrue(
                    re.match(r'Status:\s+NOT IN SYNC.*to create=3', lc.records[-2].msg))
                self.assertEqual(lc.records[-1].msg, 'Completed audit')
            # now do the sync
            with LogCapture() as lc:
                c.baseline_or_audit()
                self.assertTrue(
                    re.match(r'Status:\s+SYNCED.*created=3', lc.records[-2].msg))
                self.assertEqual(lc.records[-1].msg, 'Completed baseline sync')

    def test18_update_resource(self):
        c = Client()
        resource = Resource(uri='http://example.org/dir/2')
        filename = os.path.join(self.tmpdir, 'dir/resource2')
        # dryrun
        with LogCapture() as lc:
            c.dryrun = True
            c.logger = logging.getLogger('resync.client')
            n = c.update_resource(resource, filename)
            self.assertEqual(n, 0)
            self.assertTrue(
                lc.records[-1].msg.startswith('dryrun: would GET http://example.org/dir/2 '))
        c.dryrun = False
        with webserver('tests/testdata', 'localhost', 9999):
            # get from file uri that does not exist
            resource = Resource(uri='http://localhost:9999/i_do_not_exist')
            self.assertRaises(ClientFatalError,
                              c.update_resource, resource, filename)
            # get from file uri that does not exist but with c.ignore_failures to
            # log
            resource = Resource(uri='http://localhost:9999/i_do_not_exist')
            with LogCapture() as lc:
                c.logger = logging.getLogger('resync.client')
                c.ignore_failures = True
                n = c.update_resource(resource, filename)
                self.assertEqual(n, 0)
                self.assertTrue(
                    lc.records[-1].msg.startswith('Failed to GET http://localhost:9999/i_do_not_exist '))
            # get from file uri
            resource = Resource(uri='http://localhost:9999/examples_from_spec/resourcesync_ex_1.xml',
{'fraction_non_alphanumeric': 0.1284341243514502, 'fraction_numerical': 0.020583482180828443, 'mean_word_length': 2.6346213292117464, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, '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': '5408502', 'n_tokens_mistral': 3733, 'n_tokens_neox': 3540, 'n_words': 627}