예제 #1
0
    def test_update_with_hidden_attributes(self):
        """
        When the auditing method makes a call to ``self.hide_attributes()``, the values should change but the attributes
        should not be returned
        """
        from fhirbug.Fhir.resources import HumanName

        inst = models.MixinModelWithSettersAndAuditingProtected()
        inst.audit_update = inst.audit_update2
        resource = SimpleNamespace(active=False,
                                   name=HumanName(family="Family name",
                                                  given="Given name"))
        inst.update_from_resource(resource)

        self.assertEqual(
            inst.to_fhir().as_json(),
            {
                "name": [{
                    "family": "Family name",
                    "given": ["Given name"]
                }],
                "resourceType": "Patient",
            },
        )

        # Name should have changed
        self.assertEqual(inst.Fhir.name.as_json(), {
            "family": "Family name",
            "given": ["Given name"]
        })

        # Active should not have changed
        self.assertEqual(inst.Fhir.active, False)
예제 #2
0
    def test_create_with_auditing(self):
        """
        When calling create_from_resource, if the class has a method called `audit_create`, the method should be called
        """
        from fhirbug.Fhir.resources import HumanName

        _after_create_mock = models.Auditing_after_create
        _audit_create_mock = models.Auditing_audit_create_success
        cls = models.MixinModelWithSettersAndAuditing

        resource = SimpleNamespace(active=False,
                                   name=HumanName(family="family_name",
                                                  given="given_name"))
        query = unittest.mock.Mock()

        # If auditing is successful an instance should be created
        inst = cls.create_from_resource(resource, query)
        # After creat should be properly called
        _after_create_mock.assert_called_with(inst)
        self.assertEqual(inst._name.as_json(), {
            "family": "family_name",
            "given": ["given_name"]
        })
        # The audit_method should have been called with the query as a parameter
        _audit_create_mock.assert_called_with(query)

        _after_create_mock.reset_mock()
        cls.audit_create = models.Auditing_audit_create_failure
        with self.assertRaises(AuthorizationError):
            inst = cls.create_from_resource(resource, query)
        _after_create_mock.assert_not_called()
예제 #3
0
class BetterBaseMixinModel(FhirAbstractBaseMixin, FhirBaseModelMixin):
    from fhirbug.Fhir.resources import HumanName

    _name = HumanName(family="sponge", given="bob")
    _age = 12
    __Resource__ = "Patient"

    class FhirMap:
        active = Attribute(const(True))
        name = Attribute("_name", "_name")
        age = Attribute("_age", "_age")
예제 #4
0
class MixinModelWithSetters(FhirAbstractBaseMixin, FhirBaseModelMixin):
    from fhirbug.Fhir.resources import HumanName

    __Resource__ = "Patient"
    _name = HumanName(family="sponge", given="bob")
    _active = True
    _after_update = MixinModelWithSetters_after_update
    _after_create = MixinModelWithSetters_after_create

    class FhirMap:
        name = Attribute("_name", "_name")
        active = Attribute("_active", "_active")
예제 #5
0
    def test_getter_setter(self):
        from fhirbug.Fhir.resources import HumanName
        inst = models.NameAttibuteModel()
        self.assertTrue(isinstance(inst.withSetters, HumanName))
        self.assertEqual(inst.withSetters.as_json(), {
            "family": "squarepants",
            "given": ["sponge"]
        })

        inst.withSetters = [
            HumanName({
                "family": "Squarepants",
                "given": ["SpongeBob"]
            })
        ]
        self.assertEqual(inst._model._family, "Squarepants")
        self.assertEqual(inst._model._given1, "SpongeBob")

        inst.withJoin = [
            HumanName({
                "family": "Squarepants",
                "given": ["Sponge", "bob"]
            })
        ]
        self.assertEqual(inst._model._family, "Squarepants")
        self.assertEqual(inst._model._given1, "Sponge bob")

        inst.withPass = [
            HumanName({
                "family": "Someone",
                "given": ["Some", "One"]
            })
        ]
        self.assertEqual(inst._model._family, "Someone")
        inst.givenSetterMock.assert_called_with(inst, ["Some", "One"])
        self.assertEqual(inst._model._given1, "Sponge bob")
예제 #6
0
    def test_create_with_protected_attributes(self):
        """
        When the auditing method makes a call to ``self.protect_attributes()``, the protected values should not be set
        """
        from fhirbug.Fhir.resources import HumanName

        cls = models.MixinModelWithSettersAndAuditingProtected
        resource = SimpleNamespace(active=False,
                                   name=HumanName(family="fam", given="giv"))

        inst = cls.create_from_resource(resource)
        self.assertEqual(inst._active, None)
        self.assertEqual(inst.Fhir.name.as_json(), {
            "family": "fam",
            "given": ["giv"]
        })
예제 #7
0
    def test_update_with_protected_attributes(self):
        """
        When the auditing method makes a call to ``self.protect_attributes()``, the protected values should not change
        """
        from fhirbug.Fhir.resources import HumanName

        inst = models.MixinModelWithSettersAndAuditingProtected()
        resource = SimpleNamespace(active=False,
                                   name=HumanName(family="Family name",
                                                  given="Given name"))
        inst.update_from_resource(resource)

        # Name should have changed
        self.assertEquals(inst.Fhir.name.as_json(), {
            "family": "Family name",
            "given": ["Given name"]
        })

        # Active should not have changed
        self.assertEquals(inst.Fhir.active, None)
예제 #8
0
    def test_create_from_resource(self):
        """
        When _create_from_resource(resource) is called on an instance,
        its fields should be updated and inst._after_update should be called with the instance as a parameter
        """
        from fhirbug.Fhir.resources import HumanName

        _after_create_mock = models.MixinModelWithSetters_after_create
        cls = models.MixinModelWithSetters
        resource = SimpleNamespace(active=False,
                                   name=HumanName(family="family name",
                                                  given="given name"))
        inst = cls.create_from_resource(resource)

        self.assertEqual(inst._active, False)
        self.assertEqual(inst.Fhir.active, False)
        self.assertEqual(inst._name.as_json(), {
            "family": "family name",
            "given": ["given name"]
        })
        _after_create_mock.assert_called_with(inst)