Example #1
0
def test_on_user_contact_attribute_change_update_subitem(
        user_manager, user, method_name, attr_name, dynamo_lib_name):
    # test adding for the first time
    new_item = {**user.item, attr_name: 'the-value'}
    with patch.object(user_manager, dynamo_lib_name) as dynamo_lib_mock:
        getattr(user_manager, method_name)(user.id, new_item=new_item)
    assert dynamo_lib_mock.mock_calls == [call.add('the-value', user.id)]

    # test changing to a different value
    old_item = new_item.copy()
    new_item = {**old_item, attr_name: 'new-value'}
    with patch.object(user_manager, dynamo_lib_name) as dynamo_lib_mock:
        getattr(user_manager, method_name)(user.id,
                                           new_item=new_item,
                                           old_item=old_item)
    assert dynamo_lib_mock.mock_calls == [
        call.add('new-value', user.id),
        call.delete('the-value', user.id)
    ]

    # test deleting the value
    old_item = new_item.copy()
    with patch.object(user_manager, dynamo_lib_name) as dynamo_lib_mock:
        getattr(user_manager, method_name)(user.id, old_item=old_item)
    assert dynamo_lib_mock.mock_calls == [call.delete('new-value', user.id)]
 def test_do(self):
     char = Character(alias='test char 1')
     skill = Skill(9, level=5)
     # Action
     econt_calls_before = len(char._eos_fit.skills.mock_calls)
     char.skills.add(skill)
     econt_calls_after = len(char._eos_fit.skills.mock_calls)
     # Pyfa model
     self.assertEqual(len(char.skills), 1)
     self.assertIn(skill, char.skills)
     self.assertEqual(skill.eve_id, 9)
     self.assertEqual(skill.level, 5)
     self.assertEqual(skill.eve_name, 'Item 9 (TQ)')
     # Eos model
     self.assertEqual(econt_calls_after - econt_calls_before, 1)
     self.assertEqual(char._eos_fit.skills.mock_calls[-1], call.add(skill._eos_item))
     # Reload model via persistence (DB check)
     char.persist()
     econt_calls_before = len(char._eos_fit.skills.mock_calls)
     self.pyfadb_force_reload()
     chars = self.query_chars()
     self.assertEqual(len(chars), 1)
     char = chars[0]
     econt_calls_after = len(char._eos_fit.skills.mock_calls)
     # Pyfa model
     self.assertEqual(len(char.skills), 1)
     skill = next(iter(char.skills))
     self.assertEqual(skill.eve_id, 9)
     self.assertEqual(skill.level, 5)
     self.assertEqual(skill.eve_name, 'Item 9 (TQ)')
     # Eos model
     self.assertEqual(econt_calls_after - econt_calls_before, 1)
     self.assertEqual(char._eos_fit.skills.mock_calls[-1], call.add(skill._eos_item))
 def test_do(self):
     fit = Fit(name='test fit 1', ship=Ship(1))
     subsystem = Subsystem(2)
     # Action
     econt_calls_before = len(fit._eos_fit.subsystems.mock_calls)
     fit.ship.subsystems.add(subsystem)
     econt_calls_after = len(fit._eos_fit.subsystems.mock_calls)
     # Pyfa model
     self.assertEqual(len(fit.ship.subsystems), 1)
     self.assertIn(subsystem, fit.ship.subsystems)
     self.assertEqual(subsystem.eve_id, 2)
     self.assertEqual(subsystem.eve_name, 'Item 2 (TQ)')
     # Eos model
     self.assertEqual(econt_calls_after - econt_calls_before, 1)
     self.assertEqual(fit._eos_fit.subsystems.mock_calls[-1], call.add(subsystem._eos_item))
     # Command queue
     self.assertIs(fit.has_undo, True)
     self.assertIs(fit.has_redo, False)
     # Reload model via persistence (DB check)
     fit.persist()
     econt_calls_before = len(fit._eos_fit.subsystems.mock_calls)
     self.pyfadb_force_reload()
     fits = self.query_fits()
     self.assertEqual(len(fits), 1)
     fit = fits[0]
     econt_calls_after = len(fit._eos_fit.subsystems.mock_calls)
     # Pyfa model
     self.assertEqual(len(fit.ship.subsystems), 1)
     subsystem = next(iter(fit.ship.subsystems))
     self.assertEqual(subsystem.eve_id, 2)
     self.assertEqual(subsystem.eve_name, 'Item 2 (TQ)')
     # Eos model
     self.assertEqual(econt_calls_after - econt_calls_before, 1)
     self.assertEqual(fit._eos_fit.subsystems.mock_calls[-1], call.add(subsystem._eos_item))
Example #4
0
    def test_save_all(self):
        objs = ['a', 'b']
        session = MagicMock()
        k = KvkUpdateMessageRepository(session)
        self.assertEqual(objs, k.save_all(objs))

        session.assert_has_calls([
            call.add('a'),
            call.add('b'),
            call.flush(),
        ])
 def test_persistence(self):
     eos_skillcore = self.eos_skillcore
     eos_skillcore.return_value = sentinel.eskill
     char = Character(alias='test char 1')
     skill = Skill(6, level=3)
     char.skills.add(skill)
     # Reload model via persistence (DB check)
     char.persist()
     eskill_calls_before = len(eos_skillcore.mock_calls)
     econt_calls_before = len(char._eos_fit.skills.mock_calls)
     self.pyfadb_force_reload()
     chars = self.query_chars()
     self.assertEqual(len(chars), 1)
     char = chars[0]
     eskill_calls_after = len(eos_skillcore.mock_calls)
     econt_calls_after = len(char._eos_fit.skills.mock_calls)
     # Pyfa model
     self.assertEqual(len(char.skills), 1)
     skill = next(iter(char.skills))
     self.assertEqual(skill.eve_id, 6)
     self.assertEqual(skill.level, 3)
     self.assertEqual(skill.eve_name, 'Item 6 (TQ)')
     # Eos model
     self.assertEqual(eskill_calls_after - eskill_calls_before, 1)
     self.assertEqual(eos_skillcore.mock_calls[-1], call(6, level=3))
     self.assertEqual(econt_calls_after - econt_calls_before, 1)
     self.assertEqual(char._eos_fit.skills.mock_calls[-1], call.add(sentinel.eskill))
Example #6
0
    def test_create(self, Document, Paragraph):
        """Should be able to create a document."""
        runs = [Mock() for _ in range(3)]
        runs[0].text = "one"
        runs[0].italic = False
        runs[1].text = "two"
        runs[1].italic = True
        runs[2].text = "three"
        runs[2].italic = False
        doc = Mock()
        doc.paragraphs = [Mock(), Mock()]
        doc.paragraphs[0].runs = runs[0:2]
        doc.paragraphs[1].runs = runs[2:3]
        Document.return_value = doc
        para_objs = [Mock(), Mock()]
        para_objs[0].word_count = 5
        para_objs[1].word_count = 10
        Paragraph.side_effect = list(para_objs)
        book = Book(Settings().latest_config)
        book = book.init()

        orig = OriginalDocx("path/to/docx", book)
        config = orig.book.config
        assert orig.book.word_count == 15
        questionable_ticks = orig.questionable_ticks

        Document.assert_called_once_with("path/to/docx")
        para_objs[0].assert_has_calls([
            call.add('one', False),
            call.add('two', True),
            call.get_word_count(),
            call.fix_spaces(config),
            call.fix_italic_boundaries(config),
            call.fix_quotes_and_dashes(config),
            call.fix_ticks(config, questionable_ticks)
        ])
        para_objs[1].assert_has_calls([
            call.add('three', False),
            call.get_word_count(),
            call.fix_spaces(config),
            call.fix_italic_boundaries(config),
            call.fix_quotes_and_dashes(config),
            call.fix_ticks(config, questionable_ticks)
        ])
        assert orig.paragraphs == para_objs
Example #7
0
    def test_save(self):
        session = MagicMock()
        k = KvkUpdateMessageRepository(session)
        message = KvkUpdateMessage()

        self.assertEqual(message, k.save(message))
        session.assert_has_calls([
            call.add(message),
            call.flush(),
        ])
def test_release_force(clone_from_github_mock,
                       check_output_mock,
                       open_mock,
                       date_mock,
                       expanduser_mock):
    repo_mock = Mock()
    repo_mock.latest_tag.return_value = '20.1.0'
    repo_mock.authors_since.return_value = ['*****@*****.**', '*****@*****.**']
    repo_mock.diff_name_status.return_value = [
        ('apis/foo/v1.ts', _git.Status.ADDED),
        ('apis/baz/v1.ts', _git.Status.UPDATED),
    ]
    side_effect = common.clone_from_github_mock_side_effect(repo_mock)
    clone_from_github_mock.side_effect = side_effect
    check_output_mock.return_value = '20.1.0'
    open_package_json_mock = mock_open(read_data=('{"version": "20.1.0"}'))
    open_changelog_md_mock = mock_open(read_data='...\n')
    open_npmrc_mock = mock_open()
    open_index_md_mock = mock_open(
        read_data=('...\n\n'
                   '### ...\n\n'
                   '* [v20.1.0 (latest)](...)\n'
                   '...\n'))
    open_mock.side_effect = [
        open_package_json_mock.return_value,
        open_package_json_mock.return_value,
        open_changelog_md_mock.return_value,
        open_changelog_md_mock.return_value,
        open_npmrc_mock.return_value,
        open_index_md_mock.return_value,
        open_index_md_mock.return_value
    ]
    date_mock.today.return_value.strftime.return_value = '1 September 2017'
    expanduser_mock.side_effect = lambda x: '/home/test' + x[1:]

    google_api_nodejs_client.release(
        '/tmp', common.GITHUB_ACCOUNT, _NPM_ACCOUNT, force=True)
    # We don't bother verifying all calls in this case, since we only want to
    # verify that the different authors check was passed.
    assert repo_mock.mock_calls == [
        call.latest_tag(),
        call.authors_since('20.1.0'),
        call.diff_name_status(rev='20.1.0'),
        call.commit('20.2.0', 'Alice', '*****@*****.**'),
        call.tag('20.2.0'),
        call.push(),
        call.push(tags=True),
        call.checkout('gh-pages'),
        call.add(['latest', '20.2.0']),
        call.commit('20.2.0', 'Alice', '*****@*****.**'),
        call.push(branch='gh-pages'),
        call.checkout('master')
    ]
Example #9
0
async def test_text_creates_gallery_before_uploading(mock_io, mock_gallery,
                                                     mocker):
    mocker.patch('imgbox._output._all_files_ok', return_value=True)
    calls = AsyncMock(add=Mock(return_value=AsyncIterator(())))
    mock_gallery.create = calls.create
    mock_gallery.add = calls.add
    with mock_io():
        exit_code = await _output.text(mock_gallery, ['path/to/foo.jpg'])
    assert exit_code == 0
    assert calls.mock_calls == [
        call.create(),
        call.add(['path/to/foo.jpg']),
    ]
    def test_module_wraps_messages_add(self):
        msgs = Mock(Messages)
        m = Module(Config(), msgs)
        m.add_message(Level.INFO, 'category', 'subcat', 'a message')

        self.assert_equal([
            call.add(Level.INFO,
                     'category',
                     'subcat',
                     'a message',
                     details=None,
                     hint=None)
        ], msgs.mock_calls)
 def test_do_via_core_switch(self):
     char1 = Character(alias='test char 1')
     char2 = Character(alias='test char 2')
     skill_core = Skill(6, level=3)
     char2.skills.add(skill_core)
     fit = Fit(name='test fit 1')
     fit.character_core = char1
     # Action
     econt_calls_before = len(fit._eos_fit.skills.mock_calls)
     fit.character_core = char2
     econt_calls_after = len(fit._eos_fit.skills.mock_calls)
     # Pyfa model
     self.assertEqual(len(fit.character_proxy.skills), 1)
     skill_proxy = next(iter(fit.character_proxy.skills))
     self.assertEqual(skill_proxy.eve_id, 6)
     self.assertEqual(skill_proxy.eve_name, 'Item 6 (TQ)')
     # Eos model
     self.assertEqual(econt_calls_after - econt_calls_before, 1)
     self.assertEqual(fit._eos_fit.skills.mock_calls[-1], call.add(skill_proxy._eos_item))
     # Command queue
     self.assertIs(fit.has_undo, False)
     self.assertIs(fit.has_redo, False)
     # Reload model via persistence (DB check)
     fit.persist()
     econt_calls_before = len(fit._eos_fit.skills.mock_calls)
     self.pyfadb_force_reload()
     fits = self.query_fits()
     self.assertEqual(len(fits), 1)
     fit = fits[0]
     econt_calls_after = len(fit._eos_fit.skills.mock_calls)
     # Pyfa model
     self.assertEqual(len(fit.character_proxy.skills), 1)
     skill_proxy = next(iter(fit.character_proxy.skills))
     self.assertEqual(skill_proxy.eve_id, 6)
     self.assertEqual(skill_proxy.eve_name, 'Item 6 (TQ)')
     # Eos model
     self.assertEqual(econt_calls_after - econt_calls_before, 1)
     self.assertEqual(fit._eos_fit.skills.mock_calls[-1], call.add(skill_proxy._eos_item))
Example #12
0
def test_apprise_error(srv, caplog):

    with caplog.at_level(logging.DEBUG):

        mock_connection = mock.MagicMock()

        # Make the call to `notify` raise an exception.
        def error(*args, **kwargs):
            raise Exception("something failed")

        mock_connection.notify = error

        with mock.patch("apprise.Apprise",
                        side_effect=[mock_connection],
                        create=True) as mock_client:
            with mock.patch("apprise.AppriseAsset", create=True) as mock_asset:
                module = load_module_from_file(
                    "mqttwarn/services/apprise_single.py")

                item = Item(
                    config={
                        "baseuri":
                        "mailtos://*****:*****@mail.example.org"
                    },
                    target="test",
                    addrs=["*****@*****.**", "*****@*****.**"],
                    title="⚽ Message title ⚽",
                    message="⚽ Notification message ⚽",
                )

                outcome = module.plugin(srv, item)

                assert mock_client.mock_calls == [
                    mock.call(asset=mock.ANY),
                ]
                assert mock_connection.mock_calls == [
                    call.add(
                        "mailtos://*****:*****@mail.example.org?to=foo%40example.org%2Cbar%40example.org"
                    ),
                ]

                assert outcome is False
                assert (
                    "Sending notification to Apprise. target=test, addresses=['*****@*****.**', '*****@*****.**']"
                    in caplog.messages)
                assert (
                    "Sending message using Apprise failed. target=test, error=something failed"
                    in caplog.messages)
    def test_save_must_save_the_model(self):
        #Arrange
        db_mock = Mock()
        repository = Repository()
        repository.set_db(db_mock)

        #Action
        repository.save('model')

        #Asserts
        db_mock_calls = db_mock.mock_calls
        self.assertEqual(4, len(db_mock_calls))
        db_mock.assert_has_calls([
            call.add('model'),
            call.commit(),
            call.refresh('model'),
            call.close(),
        ])
Example #14
0
def test_apprise_multi_error(srv, caplog):

    with caplog.at_level(logging.DEBUG):

        mock_connection = mock.MagicMock()

        # Make the call to `notify` raise an exception.
        def error(*args, **kwargs):
            raise Exception("something failed")

        mock_connection.notify = error

        with mock.patch("apprise.Apprise",
                        side_effect=[mock_connection],
                        create=True) as mock_client:
            with mock.patch("apprise.AppriseAsset", create=True) as mock_asset:
                module = load_module_by_name("mqttwarn.services.apprise_multi")

                item = Item(
                    addrs=[{
                        "baseuri": "json://localhost:1234/mqtthook"
                    }],
                    title="⚽ Message title ⚽",
                    message="⚽ Notification message ⚽",
                )

                outcome = module.plugin(srv, item)

                assert mock_client.mock_calls == [
                    mock.call(asset=mock.ANY),
                ]
                assert mock_connection.mock_calls == [
                    call.add("json://localhost:1234/mqtthook"),
                ]

                assert outcome is False
                assert (
                    "Sending notification to Apprise. target=None, addresses=[{'baseuri': 'json://localhost:1234/mqtthook'}]"
                    in caplog.messages)
                assert (
                    "Sending message using Apprise failed. target=None, error=something failed"
                    in caplog.messages)