def test_gets_existing_agent(self, agent_mock, get_object_mock, agent_xml):
        presentation.premisAgentXMLToObject(agent_xml.obj_xml)

        assert get_object_mock.called
        # If agent_mock was not called, we know that an existing Agent was
        # retrieved.
        assert not agent_mock.called
    def test_raises_exception_when_agent_name_is_missing(self, agent_xml):
        xml_obj = objectify.fromstring(agent_xml.obj_xml)
        del xml_obj.agentName

        with pytest.raises(Exception) as e:
            presentation.premisAgentXMLToObject(etree.tostring(xml_obj))

        expected_message = "Unable to set 'agent_name'"
        assert expected_message in str(e), 'The exception message matches'
    def test_creates_new_agent(self, agent_mock, get_object_mock, agent_xml):
        """Check that a new Agent object is created if an existing Agent
        cannot be retrieved.
        """
        # get_object_mock is the mock that is patched over premisAgentXMLgetObject.
        # The name change is for brevity.
        get_object_mock.side_effect = Exception
        presentation.premisAgentXMLToObject(agent_xml.obj_xml)

        # Agent will be called only if the call to premisAgentXMLgetObject
        # raises an exception. We will verify that both mocks have been
        # called to assert that premisAgentXMLgetObject raised and exception
        # and a new Agent was created.
        assert get_object_mock.called
        assert agent_mock.called
 def test_exception_not_raised_when_agent_note_is_missing(self, agent_xml):
     xml_obj = objectify.fromstring(agent_xml.obj_xml)
     del xml_obj.agentNote
     presentation.premisAgentXMLToObject(etree.tostring(xml_obj))
 def test_sets_agent_note(self, agent_xml):
     agent = presentation.premisAgentXMLToObject(agent_xml.obj_xml)
     xml_obj = objectify.fromstring(agent_xml.obj_xml)
     assert agent.agent_note == xml_obj.agentNote
 def test_sets_agent_identifier(self, agent_xml):
     agent = presentation.premisAgentXMLToObject(agent_xml.obj_xml)
     xml_obj = objectify.fromstring(agent_xml.obj_xml)
     assert agent.agent_identifier == xml_obj.agentIdentifier.agentIdentifierValue
 def test_returns_agent(self, agent_xml):
     agent = presentation.premisAgentXMLToObject(agent_xml.obj_xml)
     assert isinstance(agent, models.Agent)