Example #1
0
File: tests.py Project: msherry/wac
    def test_iter(self, Page):
        page1 = Mock()
        page1.items = [1, 2, 3]
        page2 = Mock()
        page2.items = [4, 5, 6]
        page3 = Mock()
        page3.items = [7, 8]
        page4 = Mock()
        page4.items = [9]
        page1.next = page2
        page2.next = page3
        page3.next = page4
        page4.next = None

        Page.return_value = page1
        uri = "/a/uri"
        pagination = wac.Pagination(None, uri, 25)
        pages = [p for p in pagination]
        self.assertEqual([page1, page2, page3, page4], pages)

        Page.return_value = page2
        uri = "/a/uri"
        pagination = wac.Pagination(None, uri, 25)
        pages = [p for p in pagination]
        self.assertEqual([page2, page3, page4], pages)
Example #2
0
    def test_links(self, Page):
        page1 = Mock()
        page1.items = [1, 2, 3]
        page2 = Mock()
        page2.items = [4, 5, 6]
        page3 = Mock()
        page3.items = [7, 8]
        page1.previous = None
        page1.next = page2
        page2.previous = page1
        page2.next = page3
        page3.previous = page2
        page3.next = None

        Page.return_value = page1
        uri = "/a/uri"
        pagination = wac.Pagination(Resource1, uri, 25, page1)
        self.assertEqual(pagination.current, page1)

        for expected_page in [page2, page3]:
            page = pagination.next()
            self.assertEqual(page, expected_page)
            self.assertEqual(pagination.current, expected_page)
        page = pagination.next()
        self.assertEqual(page, None)
        self.assertEqual(pagination.current, expected_page)

        for expected_page in [page2, page1]:
            page = pagination.previous()
            self.assertEqual(page, expected_page)
            self.assertEqual(pagination.current, expected_page)
        page = pagination.previous()
        self.assertEqual(page, None)
        self.assertEqual(pagination.current, expected_page)
Example #3
0
    def test_iter(self):
        page1 = Mock()
        page1.items = [1, 2, 3]
        page2 = Mock()
        page2.items = [4, 5, 6]
        page3 = Mock()
        page3.items = [7, 8]
        page4 = Mock()
        page4.items = [9]
        page1.next = page2
        page2.next = page3
        page3.next = page4
        page4.next = None

        with patch.object(Resource1.client, "_op") as _op:
            with patch.object(Resource1, "page_cls") as page_cls:
                page_cls.return_value = page1
                resp = _op.return_value
                resp.data = {}

                page_cls.return_value = page1
                uri = "/a/uri"
                pagination = wac.Pagination(Resource1, uri, 25, None)
                pages = [p for p in pagination]
                self.assertEqual([page1, page2, page3, page4], pages)

                page_cls.return_value = page2
                uri = "/a/uri"
                pagination = wac.Pagination(Resource1, uri, 25, None)
                pages = [p for p in pagination]
                self.assertEqual([page2, page3, page4], pages)
    def test_save_payment_arcs_command_mocked(self):
        command_with_item_forms = Mock()
        items = [
            PagSegItem(description="Python Birds", price=Decimal("18.99"), quantity=3),
            PagSegItem(
                description="Objetos Pythônicos", price=Decimal("24.99"), quantity=2, reference=ndb.Key(PagSegItem, 10)
            ),
        ]
        command_with_item_forms.items = items

        ndb.put_multi(items)
        payment = mommy.save_one(PagSegPayment)
        command_with_item_forms.result = payment

        payment_owner = mommy.save_one(Node)
        save_payment_arcs_cmd = SavePaymentArcsCmd(payment_owner)
        CommandSequential(command_with_item_forms, save_payment_arcs_cmd).execute()

        self.assertEqual(payment, pagseguro_facade.search_payments(payment_owner)()[0])

        logs = SearchLogs(payment)()
        self.assertEqual(1, len(logs))
        self.assertEqual(STATUS_CREATED, logs[0].status)

        self.assertListEqual(items, SearchItems(payment)())
    def test_save_pagseguro_data(self):
        access_data = CreateOrUpdateAccessData("renzo@gmail.com", "abc1234")()
        command_with_item_forms = Mock()
        command_with_item_forms.items = [
            PagSegItem(description="Python Birds", price=Decimal("18.99"), quantity=3),
            PagSegItem(
                description="Objetos Pythônicos", price=Decimal("24.99"), quantity=2, reference=ndb.Key(PagSegItem, 10)
            ),
        ]
        address_form_mock = Mock()
        client_form_mock = Mock()
        command_with_item_forms.address_form = address_form_mock
        command_with_item_forms.client_form = client_form_mock
        command_with_item_forms.access_data = access_data

        save_pagseguro_data_cmd = SavePagseguroDataCmd()
        CommandSequential(command_with_item_forms, save_pagseguro_data_cmd).execute()

        items = PagSegItem.query().fetch()
        self.assertEqual(2, len(items))
        self.assertEqual(set(i.key for i in items), set(i.key for i in save_pagseguro_data_cmd.items))
        payment = PagSegPayment.query().get()
        self.assertIsNotNone(payment)
        self.assertEqual(payment, save_pagseguro_data_cmd.result)
        self.assertEqual(
            Decimal("106.95"), save_pagseguro_data_cmd.result.total, "Total should be the some of the total of items"
        )
        self.assertEqual("renzo@gmail.com", save_pagseguro_data_cmd.access_data.email)
        self.assertEqual("abc1234", save_pagseguro_data_cmd.access_data.token)

        # Data used for sequential commands
        self.assertEqual(save_pagseguro_data_cmd.address_form, address_form_mock)
        self.assertEqual(save_pagseguro_data_cmd.client_form, client_form_mock)
Example #6
0
 def create_response(self):
     response = Mock()
     response.items = Mock(return_value=[("Content-Type", "text/html; charset=utf-8")])
     response.status_code = 200
     response.reason_phrase = "OK"
     response.content = "Hello, world. You're at the polls index."
     return response
    def test_save_item_arcs_command(self):
        command_with_item_forms = Mock()
        access_data = CreateOrUpdateAccessData("renzo@gmail.com", "abc1234")()

        items = [
            PagSegItem(description="Python Birds", price=Decimal("18.99"), quantity=3),
            PagSegItem(
                description="Objetos Pythônicos", price=Decimal("24.99"), quantity=2, reference=ndb.Key(PagSegItem, 10)
            ),
        ]
        command_with_item_forms.items = items

        address_form_mock = Mock()
        client_form_mock = Mock()

        command_with_item_forms.address_form = address_form_mock
        command_with_item_forms.client_form = client_form_mock
        command_with_item_forms.access_data = access_data

        ndb.put_multi(items)
        payment = mommy.save_one(PagSegPayment)
        command_with_item_forms.result = payment

        save_payment_to_items_arcs_cmd = SavePaymentToItemsArcs()
        CommandSequential(command_with_item_forms, save_payment_to_items_arcs_cmd).execute()

        self.assertListEqual(items, SearchItems(payment)())
        self.assertListEqual(items, save_payment_to_items_arcs_cmd.items)

        # Data used for sequential commands
        self.assertEqual("renzo@gmail.com", save_payment_to_items_arcs_cmd.access_data.email)
        self.assertEqual("abc1234", save_payment_to_items_arcs_cmd.access_data.token)
        self.assertEqual(save_payment_to_items_arcs_cmd.address_form, address_form_mock)
        self.assertEqual(save_payment_to_items_arcs_cmd.client_form, client_form_mock)
    def setup(self):
        self.config_module = Mock()
        self.config_module.listen_host = "0.0.0.0"
        self.config_module.listen_port = 10052
        self.config_module.item_files = list()

        self._patcher = patch("logging.config")
        self.mock_logging_conf = self._patcher.start()

        self.config_module.logging_conf = os.path.join(CONFIG_DIR, "logging.conf")

        self.modules = {CONFIG_PATH: self.config_module}

        for i in range(ITEM_MODULES_COUNT):
            item_module = Mock()
            item_module.items = {str(i): lambda: i, OVERRIDDEN_KEY: lambda: i}

            item_module_path = os.path.join(ITEMS_DIR, "{0}.py".format(i))
            self.config_module.item_files.append(item_module_path)
            self.modules[item_module_path] = item_module

        def return_module(module_path):
            return self.modules[module_path]

        self.config_loader = Mock()
        self.config_loader.load.side_effect = return_module
        self.config_manager = ConfigManager(CONFIG_PATH, self.config_loader)
Example #9
0
    def test_suggest_when_method_set_but_invalid_and_data_field(self):
        args = Mock()
        args.method = "http://example.com/"
        args.url = "data=field"
        args.items = []

        self.httpie_argument_parser.suggest_method(args)

        self.assertEquals(args.method, "POST")
        self.assertEquals(args.url, "http://example.com/")
        self.assertEquals(args.items, [KeyValue(key="data", value="field", sep="=", orig="data=field")])
Example #10
0
    def test_suggest_when_method_set_but_invalid_and_header_field(self):
        args = Mock()
        args.method = "http://example.com/"
        args.url = "test:header"
        args.items = []

        self.httpie_argument_parser.suggest_method(args)

        self.assertEquals(args.method, "GET")
        self.assertEquals(args.url, "http://example.com/")
        self.assertEquals(args.items, [KeyValue(key="test", value="header", sep=":", orig="test:header")])
Example #11
0
    def test_suggest_when_method_not_set(self):
        args = Mock()
        args.method = None
        args.url = "http://example.com/"
        args.items = []

        self.httpie_argument_parser.suggest_method(args)

        self.assertEquals(args.method, "GET")
        self.assertEquals(args.url, "http://example.com/")
        self.assertEquals(args.items, [])
Example #12
0
    def test_iter(self):
        page1 = Mock()
        page1.items = [0, 1, 2, 3]
        page1.total = 10
        page2 = Mock()
        page2.items = [4, 5, 6, 7]
        page2.total = 10
        page3 = Mock()
        page3.items = [8, 9]
        page3.total = 10

        page1.next = page2
        page2.next = page3
        page3.next = None

        with patch("wac.Page") as Page:
            Page.side_effect = [page1, page1, page2, page3]
            uri = "/a/uri"
            q = wac.Query(None, uri, 4)
            vs = [v for v in q]
            self.assertEqual(range(10), vs)
 def test_save_items(self):
     command_with_item_forms = Mock()
     command_with_item_forms.items = [
         PagSegItem(description="Python Birds", price=Decimal("18.99"), quantity=3),
         PagSegItem(
             description="Objetos Pythônicos", price=Decimal("24.99"), quantity=2, reference=ndb.Key(PagSegItem, 10)
         ),
     ]
     save_items_cmd = SaveItemCmd()
     CommandSequential(command_with_item_forms, save_items_cmd).execute()
     items = PagSegItem.query().fetch()
     self.assertEqual(2, len(items))
     self.assertEqual(items, save_items_cmd.result)
Example #14
0
    def test_suggest_when_method_set_but_invalid_and_item_exists(self):
        args = Mock()
        args.method = "http://example.com/"
        args.url = "new_item=a"
        args.items = [KeyValue(key="old_item", value="b", sep="=", orig="old_item=b")]

        self.httpie_argument_parser.suggest_method(args)

        self.assertEquals(
            args.items,
            [
                KeyValue(key="new_item", value="a", sep="=", orig="new_item=a"),
                KeyValue(key="old_item", value="b", sep="=", orig="old_item=b"),
            ],
        )
Example #15
0
    def test_iter(self):
        page1 = Mock()
        page1.items = [0, 1, 2, 3]
        page1.total = 10
        page2 = Mock()
        page2.items = [4, 5, 6, 7]
        page2.total = 10
        page3 = Mock()
        page3.items = [8, 9]
        page3.total = 10

        page1.next = page2
        page2.next = page3
        page3.next = None

        with patch.object(Resource1, "page_cls") as page_cls:
            with patch.object(Resource1.client, "_op") as _op:
                page_cls.side_effect = [page1, page1, page2, page3]
                resp = _op.return_value
                resp.data = {"_type": "page"}
                uri = "/a/uri"
                q = wac.Query(Resource1, uri, 4)
                vs = [v for v in q]
                self.assertEqual(range(10), vs)
Example #16
0
def test_metric_initialization():
    fake_items = [("foo", "bar"), ("foobar", "baz")]

    fake_xpath = Mock(attrib={"name": "xpath_values"}, text="xpath_text")

    metric_mock = Mock()
    metric_mock.items = MagicMock(return_value=fake_items)
    metric_mock.findall = MagicMock(return_value=[fake_xpath])

    # When I create a metric with xpath
    m = Metric(metric_mock)

    # Then it should assign object attributes
    m.xpath_values.should.be("xpath_text")
    m.foo.should.be("bar")
    m.foobar.should.be("baz")
Example #17
0
 def setUp(self):
     self.pcmd = taskqueue.daemonlib.parse_cmdline
     opts = Mock()
     opts.foreground = True
     taskqueue.daemonlib.parse_cmdline = Mock(return_value=opts)
     self.config = ConfigParser()
     taskqueue.daemonlib.pika = Mock()
     fake_ctx = Mock()
     fake_ctx.__exit__ = Mock()
     fake_ctx.__enter__ = Mock()
     taskqueue.daemonlib.daemon.DaemonContext = Mock(return_value=fake_ctx)
     taskqueue.daemonlib.logging.config = Mock()
     config = Mock()
     config.items = Mock(return_value=[])
     taskqueue.daemonlib.ConfigParser = Mock(return_value=config)
     self.daemon = taskqueue.daemonlib.Daemon(self.config)
Example #18
0
    def test_stateHandling(self):
        self.assertEqual(self.scope.topDown(), [self.actor])
        self.assertEqual(self.controlled.getScope(), self.scope)
        self.assertEqual(self.controlled.getScopedItems(), [self.actor])

        def f(a):
            raise OutputClosed()

        output = Mock()
        output.write = Mock(side_effect=f)

        self.assertRaises(OutputClosed, lambda: self.actor._handle(output, output))
        output.write.assert_called_once_with("hello")

        for x in self.controlled.getScopedItems():
            self.assertRaises(OutputClosed, lambda: x._handle(output, output))

        input = Mock()
        input.nouns = []
        input.items = []
        output = self.controlled.handle(input)

        self.assertEqual(output.text, "hello")
Example #19
0
    def test_success(self, UrlFetchClassMock):
        # Mocking pagseguro Fetch
        UrlFetchClassMock.return_value = _build_mock()

        # Mocking previous command
        data_cmd_mock = Mock()
        data_cmd_mock.access_data.email = "foo@bar.com"
        data_cmd_mock.access_data.token = "4567890oiuytfgh"
        reference0 = ItemReferenceMock()
        reference1 = ItemReferenceMock()
        items = [
            PagSegItem(description="Python Course", price=Decimal("120"), quantity=1, reference=reference0.key),
            PagSegItem(description="Another Python Course", price=Decimal("240"), quantity=2, reference=reference1.key),
        ]
        ndb.put_multi(items)
        data_cmd_mock.items = items
        data_cmd_mock.client_form = ValidateClientCmd(email="jhon@bar.com", name="Jhon Doe").form
        data_cmd_mock.address_form = pagseguro_facade.validate_address_cmd(
            "Rua 1", "2", "meu bairro", "12345678", "São Paulo", "SP", "apto 4"
        ).form

        payment = PagSegPayment()
        payment.put()
        data_cmd_mock.result = payment

        contact_pagseguro_cmd = ContactPagseguro("https://store.com/pagseguro")
        CommandSequential(data_cmd_mock, contact_pagseguro_cmd).execute()

        self.assertEqual(payment, contact_pagseguro_cmd.result)
        self.assertEqual(payment.status, STATUS_SENT_TO_PAGSEGURO)
        self.assertEqual(
            contact_pagseguro_cmd.checkout_code,
            _SUCCESS_PAGSEGURO_CODE,
            "Should have the code extracted from xml _PAGSEGURO_DETAIL_XML",
        )
        self.assertIsNone(payment.code)