<commit_before>"""Test that we're meeting delicious API specifications"""
import logging
import os
import StringIO
import transaction
import unittest

from datetime import datetime

from bookie.models import DBSession
from bookie.models import Bmark
from bookie.models.queue import ImportQueue
from bookie.models.queue import ImportQueueMgr
from bookie.lib.urlhash import generate_hash

from bookie.lib.importer import Importer
from bookie.lib.importer import DelImporter
from bookie.lib.importer import DelXMLImporter
from bookie.lib.importer import GBookmarkImporter

from bookie.tests import TestViewBase
from bookie.tests import empty_db


LOG = logging.getLogger(__name__)

API_KEY = None


class TestImports(unittest.TestCase):

    def _delicious_data_test(self):
        """Test that we find the correct set of declicious data after import"""
        # Blatant copy/paste, but I'm on a plane right now so oh well.
        # Now let's do some db sanity checks.
        res = Bmark.query.all()
        self.assertEqual(
            len(res),
            19,
            "We should have 19 results, we got: " + str(len(res)))

        # verify we can find a bookmark by url and check tags, etc
        check_url = u'http://www.ndftz.com/nickelanddime.png'
        check_url_hashed = generate_hash(check_url)
        found = Bmark.query.filter(Bmark.hash_id == check_url_hashed).one()

        self.assertTrue(
            found.hashed.url == check_url, "The url should match our search")
        self.assertEqual(
            len(found.tags),
            7,
            "We should have gotten 7 tags, got: " + str(len(found.tags)))
        self.assertEqual(
            'importer',
            found.inserted_by,
            "The bookmark should have imported: " + found.inserted_by)

        # and check we have a right tag or two
        self.assertTrue(
            'canonical' in found.tag_string(),
            'Canonical should be a valid tag in the bookmark')

        # and check the long description field
        self.assertTrue(
            "description" in found.extended,
            "The extended attrib should have a nice long string in it")

    def _delicious_xml_data_test(self):
        """Test that we find the correct google bmark data after import"""
        res = Bmark.query.all()
        self.assertEqual(
            len(res),
            25,
            "We should have 25 results, we got: " + str(len(res)))

        # verify we can find a bookmark by url and check tags, etc
        check_url = 'http://jekyllrb.com/'
        check_url_hashed = generate_hash(check_url)
        found = Bmark.query.filter(Bmark.hash_id == check_url_hashed).one()

        self.assertTrue(
            found.hashed.url == check_url, "The url should match our search")
        self.assertEqual(
            len(found.tags), 6,
            "We should have gotten 6 tags, got: " + str(len(found.tags)))

        # and check we have a right tag or two
        self.assertTrue(
            'ruby' in found.tag_string(),
            'ruby should be a valid tag in the bookmark')

        # and check the long description field
        self.assertTrue(
            'added for test' in found.extended,
            "'added for test' should be in the extended description")

    def _google_data_test(self):
        """Test that we find the correct google bmark data after import"""
        res = Bmark.query.all()
        self.assertEqual(
            len(res),
            9,
            "We should have 9 results, we got: " + str(len(res)))

        # verify we can find a bookmark by url and check tags, etc
        check_url = 'http://www.alistapart.com/'
        check_url_hashed = generate_hash(check_url)
        found = Bmark.query.filter(Bmark.hash_id == check_url_hashed).one()

        self.assertTrue(
            found.hashed.url == check_url, "The url should match our search")
        self.assertEqual(
            len(found.tags),
            4,
            "We should have gotten 4 tags, got: " + str(len(found.tags)))

        # and check we have a right tag or two
        self.assertTrue(
            'html' in found.tag_string(),
            'html should be a valid tag in the bookmark')

        # and check the long description field
        self.assertTrue(
            "make websites" in found.extended,
            "'make websites' should be in the extended description")

    def _chrome_data_test(self):
        """Test that we find the correct Chrome bmark data after import"""
        res = Bmark.query.all()
        self.assertEqual(
            len(res),
            4,
            "We should have 4 results, we got: " + str(len(res)))

        # Verify we can find a bookmark by url and check tags, etc
        check_url = 'https://addons.mozilla.org/en-US/firefox/bookmarks/'
        check_url_hashed = generate_hash(check_url)
        found = Bmark.query.filter(Bmark.hash_id == check_url_hashed).one()

        self.assertTrue(
            found.hashed.url == check_url, "The url should match our search")
        self.assertEqual(
            len(found.tags),
            2,
            "We should have gotten 2 tags, got: " + str(len(found.tags)))

        # and check we have a right tag or two
        self.assertTrue(
            'imported-from-firefox' in found.tag_string(),
            'imported-from-firefox should be a valid tag in the bookmark')

        # and check the timestamp is correct
        date_should_be = datetime(2012, 10, 15, 22, 8, 54)
        self.assertEqual(date_should_be, found.stored)


class ImporterBaseTest(TestImports):
    """Verify the base import class is working"""

    def test_doesnt_implement_can_handle(self):
        """Verify we get the exception expected when running can_handle"""
        self.assertRaises(NotImplementedError, Importer.can_handle, "")

    def test_doesnt_implement_process(self):
        """Verify we get the exception expected when running process"""
        some_io = StringIO.StringIO()
        imp = Importer(some_io)
        self.assertRaises(NotImplementedError, imp.process)

    def test_factory_gives_delicious(self):
        """"Verify that the base importer will give DelImporter"""
        loc = os.path.dirname(__file__)
        del_file = os.path.join(loc, 'delicious.html')

        with open(del_file) as del_io:
            imp = Importer(del_io, username=u"admin")

            self.assertTrue(
                isinstance(imp, DelImporter),
                "Instance should be a delimporter instance")<commit_msg>Fixes #290<commit_after>"""Test that we're meeting delicious API specifications"""
import logging
import os
import StringIO
import transaction
import unittest

from datetime import datetime

from bookie.models import DBSession
from bookie.models import Bmark
from bookie.models.queue import ImportQueue
from bookie.models.queue import ImportQueueMgr
from bookie.lib.urlhash import generate_hash

from bookie.lib.importer import Importer
from bookie.lib.importer import DelImporter
from bookie.lib.importer import DelXMLImporter
from bookie.lib.importer import GBookmarkImporter

from bookie.tests import TestViewBase
from bookie.tests import empty_db


LOG = logging.getLogger(__name__)

API_KEY = None


class TestImports(unittest.TestCase):

    def _delicious_data_test(self):
        """Test that we find the correct set of declicious data after import"""
        # Blatant copy/paste, but I'm on a plane right now so oh well.
        # Now let's do some db sanity checks.
        res = Bmark.query.all()
        self.assertEqual(
            len(res),
            19,
            "We should have 19 results, we got: " + str(len(res)))

        # verify we can find a bookmark by url and check tags, etc
        check_url = u'http://www.ndftz.com/nickelanddime.png'
        check_url_hashed = generate_hash(check_url)
        found = Bmark.query.filter(Bmark.hash_id == check_url_hashed).one()

        self.assertTrue(
            found.hashed.url == check_url, "The url should match our search")
        self.assertEqual(
            len(found.tags),
            7,
            "We should have gotten 7 tags, got: " + str(len(found.tags)))
        self.assertEqual(
            'importer',
            found.inserted_by,
            "The bookmark should have imported: " + found.inserted_by)

        # and check we have a right tag or two
        self.assertTrue(
            'canonical' in found.tag_string(),
            'Canonical should be a valid tag in the bookmark')

        # and check the long description field
        self.assertTrue(
            "description" in found.extended,
            "The extended attrib should have a nice long string in it")

    def _delicious_xml_data_test(self):
        """Test that we find the correct google bmark data after import"""
        res = Bmark.query.all()
        self.assertEqual(
            len(res),
            25,
            "We should have 25 results, we got: " + str(len(res)))

        # verify we can find a bookmark by url and check tags, etc
        check_url = 'http://jekyllrb.com/'
        check_url_hashed = generate_hash(check_url)
        found = Bmark.query.filter(Bmark.hash_id == check_url_hashed).one()

        self.assertTrue(
            found.hashed.url == check_url, "The url should match our search")
        self.assertEqual(
            len(found.tags), 6,
            "We should have gotten 6 tags, got: " + str(len(found.tags)))

        # and check we have a right tag or two
        self.assertTrue(
            'ruby' in found.tag_string(),
            'ruby should be a valid tag in the bookmark')

        # and check the long description field
        self.assertTrue(
            'added for test' in found.extended,
            "'added for test' should be in the extended description")

    def _google_data_test(self):
        """Test that we find the correct google bmark data after import"""
        res = Bmark.query.all()
        self.assertEqual(
            len(res),
            9,
            "We should have 9 results, we got: " + str(len(res)))

        # verify we can find a bookmark by url and check tags, etc
        check_url = 'http://www.alistapart.com/'
        check_url_hashed = generate_hash(check_url)
        found = Bmark.query.filter(Bmark.hash_id == check_url_hashed).one()

        self.assertTrue(
            found.hashed.url == check_url, "The url should match our search")
        self.assertEqual(
            len(found.tags),
            4,
            "We should have gotten 4 tags, got: " + str(len(found.tags)))

        # and check we have a right tag or two
        self.assertTrue(
            'html' in found.tag_string(),
            'html should be a valid tag in the bookmark')

        # and check the long description field
        self.assertTrue(
            "make websites" in found.extended,
            "'make websites' should be in the extended description")

    def _chrome_data_test(self):
        """Test that we find the correct Chrome bmark data after import"""
        res = Bmark.query.all()
        self.assertEqual(
            len(res),
            4,
            "We should have 4 results, we got: " + str(len(res)))

        # Verify we can find a bookmark by url and check tags, etc
        check_url = 'https://addons.mozilla.org/en-US/firefox/bookmarks/'
        check_url_hashed = generate_hash(check_url)
        found = Bmark.query.filter(Bmark.hash_id == check_url_hashed).one()

        self.assertTrue(
            found.hashed.url == check_url, "The url should match our search")
        self.assertEqual(
            len(found.tags),
            2,
            "We should have gotten 2 tags, got: " + str(len(found.tags)))

        # and check we have a right tag or two
        self.assertTrue(
            'imported-from-firefox' in found.tag_string(),
            'imported-from-firefox should be a valid tag in the bookmark')

        # and check the timestamp is correct
        # relative to user's timezone
        date_should_be = datetime.fromtimestamp(1350353334)
        self.assertEqual(date_should_be, found.stored)


class ImporterBaseTest(TestImports):
    """Verify the base import class is working"""

    def test_doesnt_implement_can_handle(self):
        """Verify we get the exception expected when running can_handle"""
        self.assertRaises(NotImplementedError, Importer.can_handle, "")

    def test_doesnt_implement_process(self):
        """Verify we get the exception expected when running process"""
        some_io = StringIO.StringIO()
        imp = Importer(some_io)
        self.assertRaises(NotImplementedError, imp.process)

    def test_factory_gives_delicious(self):
        """"Verify that the base importer will give DelImporter"""
        loc = os.path.dirname(__file__)
        del_file = os.path.join(loc, 'delicious.html')

        with open(del_file) as del_io:
            imp = Importer(del_io, username=u"admin")

            self.assertTrue(
                isinstance(imp, DelImporter),
                "Instance should be a delimporter instance")
{'fraction_non_alphanumeric': 0.08894817073170731, 'fraction_numerical': 0.005030487804878049, 'mean_word_length': 2.3192512016190237, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 4, 'xml': 2}, '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': '353610', 'n_tokens_mistral': 3736, 'n_tokens_neox': 3645, 'n_words': 1187}