import unittest
from scrapyz.util import JsonSelector
from util import fake_response
from spiders import *


class TestGenericSpiders(unittest.TestCase):
    """
        Tests the basic functionality of GenericSpider.
    """
    expected_items = [
        {
             'disclaimer': u'Disclaimer One',
             'discount': u'Discount One',
             'image_url': u'Image One',
             'offer_url': 'http://www.test.com/offer_1',
             'title': u'Title One'
        },
        {
             'disclaimer': u'Disclaimer Two',
             'discount': u'Discount Two',
             'image_url': u'Image Two',
             'offer_url': 'http://www.test.com/offer_2',
             'title': u'Title Two'
        },
        {
             'disclaimer': u'Disclaimer Three',
             'discount': u'Discount Three',
             'image_url': u'Image Three',
             'offer_url': 'http://www.test.com/offer_3',
             'title': u'Title Three'
        }
    ]

    def test_basic_parse(self):
        spider = BasicParseTestSpider()
        response = fake_response("basic_parse.html")
        results = [item for item in spider.parse(response)]
        self.assertEqual(len(results), 3)
        for result, expected in zip(results, self.expected_items):
            for key in result.keys():
                self.assertEqual(result[key], expected[key])

    def test_start_requests(self):
        spider = BasicParseTestSpider()
        spider.start_urls = ["http://abc.com", "http://123.com", "http://abc.com"]
        for i, request in enumerate(spider.start_requests()):
            self.assertEqual(request.url, spider.start_urls[i])

    def test_json_spider(self):
        expected = [
            {
                "greeting" : "hello",
                "word" : "neck!!"
            },
            {
                "greeting" : "hello",
                "word" : "neck!!"
            },
        ]
        response = fake_response("test.json")
        spider = JsonTestSpider()
        results = [item for item in spider.parse(response)]
        self.assertEqual(len(results), 2)
        for result, expected in zip(results, expected):
            for key in result.keys():
                self.assertEqual(result[key], expected[key])

class TestSpiderExceptions(unittest.TestCase):
    """
        Tests that the proper exceptions are raised in the right situations.
    """
    def test_bad_spider(self):
        classes = [NoMetaSpider, NoStartSpider]
        for cls in classes:
            with self.assertRaises(AttributeError):
                spider = cls()
        try:
            spider = GoodSpider()
        except AttributeError:
            self.fail("GoodSpider raised SWOSpiderValidationError when it shouldn't.")

    def test_good_spider(self):
        try:
            spider = GoodSpider()
        except Exception:
            self.fail()


class TestUtil(unittest.TestCase):

    def test_json_selecot(self):
        data = {"a" : "a", "b": "b", "c": { "d" : ["1", "2", "3"]}}
        js = JsonSelector(data=data)
        self.assertEqual(js["a"], data["a"])
        self.assertEqual(js["c.d"], ["1", "2", "3"])
        self.assertEqual(js["c.d[2]"], "3")

6f83282e381c41290e82673896921d3a7dd7ebc7
<mask token>


class TestGenericSpiders(unittest.TestCase):
    <mask token>
    <mask token>
    <mask token>
    <mask token>
    <mask token>


class TestSpiderExceptions(unittest.TestCase):
    """
        Tests that the proper exceptions are raised in the right situations.
    """

    def test_bad_spider(self):
        classes = [NoMetaSpider, NoStartSpider]
        for cls in classes:
            with self.assertRaises(AttributeError):
                spider = cls()
        try:
            spider = GoodSpider()
        except AttributeError:
            self.fail(
                "GoodSpider raised SWOSpiderValidationError when it shouldn't."
                )

    def test_good_spider(self):
        try:
            spider = GoodSpider()
        except Exception:
            self.fail()


class TestUtil(unittest.TestCase):

    def test_json_selecot(self):
        data = {'a': 'a', 'b': 'b', 'c': {'d': ['1', '2', '3']}}
        js = JsonSelector(data=data)
        self.assertEqual(js['a'], data['a'])
        self.assertEqual(js['c.d'], ['1', '2', '3'])
        self.assertEqual(js['c.d[2]'], '3')

<mask token>


class TestGenericSpiders(unittest.TestCase):
    <mask token>
    <mask token>
    <mask token>
    <mask token>

    def test_json_spider(self):
        expected = [{'greeting': 'hello', 'word': 'neck!!'}, {'greeting':
            'hello', 'word': 'neck!!'}]
        response = fake_response('test.json')
        spider = JsonTestSpider()
        results = [item for item in spider.parse(response)]
        self.assertEqual(len(results), 2)
        for result, expected in zip(results, expected):
            for key in result.keys():
                self.assertEqual(result[key], expected[key])


class TestSpiderExceptions(unittest.TestCase):
    """
        Tests that the proper exceptions are raised in the right situations.
    """

    def test_bad_spider(self):
        classes = [NoMetaSpider, NoStartSpider]
        for cls in classes:
            with self.assertRaises(AttributeError):
                spider = cls()
        try:
            spider = GoodSpider()
        except AttributeError:
            self.fail(
                "GoodSpider raised SWOSpiderValidationError when it shouldn't."
                )

    def test_good_spider(self):
        try:
            spider = GoodSpider()
        except Exception:
            self.fail()


class TestUtil(unittest.TestCase):

    def test_json_selecot(self):
        data = {'a': 'a', 'b': 'b', 'c': {'d': ['1', '2', '3']}}
        js = JsonSelector(data=data)
        self.assertEqual(js['a'], data['a'])
        self.assertEqual(js['c.d'], ['1', '2', '3'])
        self.assertEqual(js['c.d[2]'], '3')

<mask token>


class TestGenericSpiders(unittest.TestCase):
    <mask token>
    expected_items = [{'disclaimer': u'Disclaimer One', 'discount':
        u'Discount One', 'image_url': u'Image One', 'offer_url':
        'http://www.test.com/offer_1', 'title': u'Title One'}, {
        'disclaimer': u'Disclaimer Two', 'discount': u'Discount Two',
        'image_url': u'Image Two', 'offer_url':
        'http://www.test.com/offer_2', 'title': u'Title Two'}, {
        'disclaimer': u'Disclaimer Three', 'discount': u'Discount Three',
        'image_url': u'Image Three', 'offer_url':
        'http://www.test.com/offer_3', 'title': u'Title Three'}]

    def test_basic_parse(self):
        spider = BasicParseTestSpider()
        response = fake_response('basic_parse.html')
        results = [item for item in spider.parse(response)]
        self.assertEqual(len(results), 3)
        for result, expected in zip(results, self.expected_items):
            for key in result.keys():
                self.assertEqual(result[key], expected[key])

    def test_start_requests(self):
        spider = BasicParseTestSpider()
        spider.start_urls = ['http://abc.com', 'http://123.com',
            'http://abc.com']
        for i, request in enumerate(spider.start_requests()):
            self.assertEqual(request.url, spider.start_urls[i])

    def test_json_spider(self):
        expected = [{'greeting': 'hello', 'word': 'neck!!'}, {'greeting':
            'hello', 'word': 'neck!!'}]
        response = fake_response('test.json')
        spider = JsonTestSpider()
        results = [item for item in spider.parse(response)]
        self.assertEqual(len(results), 2)
        for result, expected in zip(results, expected):
            for key in result.keys():
                self.assertEqual(result[key], expected[key])


class TestSpiderExceptions(unittest.TestCase):
    """
        Tests that the proper exceptions are raised in the right situations.
    """

    def test_bad_spider(self):
        classes = [NoMetaSpider, NoStartSpider]
        for cls in classes:
            with self.assertRaises(AttributeError):
                spider = cls()
        try:
            spider = GoodSpider()
        except AttributeError:
            self.fail(
                "GoodSpider raised SWOSpiderValidationError when it shouldn't."
                )

    def test_good_spider(self):
        try:
            spider = GoodSpider()
        except Exception:
            self.fail()


class TestUtil(unittest.TestCase):

    def test_json_selecot(self):
        data = {'a': 'a', 'b': 'b', 'c': {'d': ['1', '2', '3']}}
        js = JsonSelector(data=data)
        self.assertEqual(js['a'], data['a'])
        self.assertEqual(js['c.d'], ['1', '2', '3'])
        self.assertEqual(js['c.d[2]'], '3')

import unittest
from scrapyz.util import JsonSelector
from util import fake_response
from spiders import *


class TestGenericSpiders(unittest.TestCase):
    """
        Tests the basic functionality of GenericSpider.
    """
    expected_items = [{'disclaimer': u'Disclaimer One', 'discount':
        u'Discount One', 'image_url': u'Image One', 'offer_url':
        'http://www.test.com/offer_1', 'title': u'Title One'}, {
        'disclaimer': u'Disclaimer Two', 'discount': u'Discount Two',
        'image_url': u'Image Two', 'offer_url':
        'http://www.test.com/offer_2', 'title': u'Title Two'}, {
        'disclaimer': u'Disclaimer Three', 'discount': u'Discount Three',
        'image_url': u'Image Three', 'offer_url':
        'http://www.test.com/offer_3', 'title': u'Title Three'}]

    def test_basic_parse(self):
        spider = BasicParseTestSpider()
        response = fake_response('basic_parse.html')
        results = [item for item in spider.parse(response)]
        self.assertEqual(len(results), 3)
        for result, expected in zip(results, self.expected_items):
            for key in result.keys():
                self.assertEqual(result[key], expected[key])

    def test_start_requests(self):
        spider = BasicParseTestSpider()
        spider.start_urls = ['http://abc.com', 'http://123.com',
            'http://abc.com']
        for i, request in enumerate(spider.start_requests()):
            self.assertEqual(request.url, spider.start_urls[i])

    def test_json_spider(self):
        expected = [{'greeting': 'hello', 'word': 'neck!!'}, {'greeting':
            'hello', 'word': 'neck!!'}]
        response = fake_response('test.json')
        spider = JsonTestSpider()
        results = [item for item in spider.parse(response)]
        self.assertEqual(len(results), 2)
        for result, expected in zip(results, expected):
            for key in result.keys():
                self.assertEqual(result[key], expected[key])


class TestSpiderExceptions(unittest.TestCase):
    """
        Tests that the proper exceptions are raised in the right situations.
    """

    def test_bad_spider(self):
        classes = [NoMetaSpider, NoStartSpider]
        for cls in classes:
            with self.assertRaises(AttributeError):
                spider = cls()
        try:
            spider = GoodSpider()
        except AttributeError:
            self.fail(
                "GoodSpider raised SWOSpiderValidationError when it shouldn't."
                )

    def test_good_spider(self):
        try:
            spider = GoodSpider()
        except Exception:
            self.fail()


class TestUtil(unittest.TestCase):

    def test_json_selecot(self):
        data = {'a': 'a', 'b': 'b', 'c': {'d': ['1', '2', '3']}}
        js = JsonSelector(data=data)
        self.assertEqual(js['a'], data['a'])
        self.assertEqual(js['c.d'], ['1', '2', '3'])
        self.assertEqual(js['c.d[2]'], '3')

import unittest
from scrapyz.util import JsonSelector
from util import fake_response
from spiders import *


class TestGenericSpiders(unittest.TestCase):
    """
        Tests the basic functionality of GenericSpider.
    """
    expected_items = [
        {
             'disclaimer': u'Disclaimer One',
             'discount': u'Discount One',
             'image_url': u'Image One',
             'offer_url': 'http://www.test.com/offer_1',
             'title': u'Title One'
        },
        {
             'disclaimer': u'Disclaimer Two',
             'discount': u'Discount Two',
             'image_url': u'Image Two',
             'offer_url': 'http://www.test.com/offer_2',
             'title': u'Title Two'
        },
        {
             'disclaimer': u'Disclaimer Three',
             'discount': u'Discount Three',
             'image_url': u'Image Three',
             'offer_url': 'http://www.test.com/offer_3',
             'title': u'Title Three'
        }
    ]

    def test_basic_parse(self):
        spider = BasicParseTestSpider()
        response = fake_response("basic_parse.html")
        results = [item for item in spider.parse(response)]
        self.assertEqual(len(results), 3)
        for result, expected in zip(results, self.expected_items):
            for key in result.keys():
                self.assertEqual(result[key], expected[key])

    def test_start_requests(self):
        spider = BasicParseTestSpider()
        spider.start_urls = ["http://abc.com", "http://123.com", "http://abc.com"]
        for i, request in enumerate(spider.start_requests()):
            self.assertEqual(request.url, spider.start_urls[i])

    def test_json_spider(self):
        expected = [
            {
                "greeting" : "hello",
                "word" : "neck!!"
            },
            {
                "greeting" : "hello",
                "word" : "neck!!"
            },
        ]
        response = fake_response("test.json")
        spider = JsonTestSpider()
        results = [item for item in spider.parse(response)]
        self.assertEqual(len(results), 2)
        for result, expected in zip(results, expected):
            for key in result.keys():
                self.assertEqual(result[key], expected[key])

class TestSpiderExceptions(unittest.TestCase):
    """
        Tests that the proper exceptions are raised in the right situations.
    """
    def test_bad_spider(self):
        classes = [NoMetaSpider, NoStartSpider]
        for cls in classes:
            with self.assertRaises(AttributeError):
                spider = cls()
        try:
            spider = GoodSpider()
        except AttributeError:
            self.fail("GoodSpider raised SWOSpiderValidationError when it shouldn't.")

    def test_good_spider(self):
        try:
            spider = GoodSpider()
        except Exception:
            self.fail()


class TestUtil(unittest.TestCase):

    def test_json_selecot(self):
        data = {"a" : "a", "b": "b", "c": { "d" : ["1", "2", "3"]}}
        js = JsonSelector(data=data)
        self.assertEqual(js["a"], data["a"])
        self.assertEqual(js["c.d"], ["1", "2", "3"])
        self.assertEqual(js["c.d[2]"], "3")
