Example #1
0
    def test_infer_computed_relation(self):
        class Person(EntityType):
            name = String()

        class Company(EntityType):
            name = String()

        class Service(EntityType):
            name = String()

        class works_for(RelationDefinition):
            subject = 'Person'
            object = 'Company'

        class produce(RelationDefinition):
            subject = ('Person', 'Company')
            object = 'Service'

        class achete(RelationDefinition):
            subject = 'Person'
            object = 'Service'

        class produces_and_buys(ComputedRelation):
            rule = 'S produce O, S achete O'

        class produces_and_buys2(ComputedRelation):
            rule = 'S works_for SO, SO produce O'

        class reproduce(ComputedRelation):
            rule = 'S produce O'

        schema = build_schema_from_namespace(vars().items())

        # check object/subject type
        self.assertEqual([('Person', 'Service')],
                         list(schema['produces_and_buys'].rdefs.keys()))
        self.assertEqual([('Person', 'Service')],
                         list(schema['produces_and_buys2'].rdefs.keys()))
        self.assertCountEqual([('Company', 'Service'), ('Person', 'Service')],
                              list(schema['reproduce'].rdefs.keys()))
        # check relation definitions are marked infered
        rdef = schema['produces_and_buys'].rdefs[('Person', 'Service')]
        self.assertTrue(rdef.infered)
        # and have no add/delete permissions
        self.assertEqual(rdef.permissions, {
            'add': (),
            'delete': (),
            'read': ('managers', 'users', 'guests')
        })

        class autoname(ComputedRelation):
            rule = 'S produce X, X name O'

        with self.assertRaises(BadSchemaDefinition) as cm:
            build_schema_from_namespace(vars().items())
        self.assertEqual(str(cm.exception),
                         'computed relations cannot be final')
Example #2
0
    def test_computed_attribute(self):
        """Check schema finalization for computed attributes."""
        class Person(EntityType):
            salary = Int()

        class works_for(RelationDefinition):
            subject = 'Person'
            object = 'Company'
            cardinality = '?*'

        class Company(EntityType):
            total_salary = Int(
                formula='Any SUM(SA) GROUPBY X WHERE P works_for X, P salary SA'
            )

        good_schema = build_schema_from_namespace(vars().items())
        rdef = good_schema['Company'].rdef('total_salary')
        # ensure 'X is Company' is added to the rqlst to avoid ambiguities, see #4901163
        self.assertEqual(
            str(rdef.formula_select),
            'Any SUM(SA) GROUPBY X WHERE P works_for X, P salary SA, X is Company'
        )
        # check relation definition permissions
        self.assertEqual(rdef.permissions, {
            'add': (),
            'update': (),
            'read': ('managers', 'users', 'guests')
        })

        class Company(EntityType):  # noqa
            total_salary = String(formula='Any SUM(SA) GROUPBY X WHERE '
                                  'P works_for X, P salary SA')

        with self.assertRaises(BadSchemaDefinition) as exc:
            build_schema_from_namespace(vars().items())

        self.assertEqual(
            str(exc.exception), 'computed attribute total_salary on Company: '
            'computed attribute type (Int) mismatch with '
            'specified type (String)')
Example #3
0
    def simple_schema(self):
        THISYEAR = 2014

        class Person(EntityType):
            name = String()
            salary = Int()
            birth_year = Int(required=True)
            works_for = SubjectRelation('Company')
            age = Int(formula='Any %d - D WHERE X birth_year D' % THISYEAR)

        class Company(EntityType):
            name = String()
            total_salary = Int(formula='Any SUM(SA) GROUPBY X WHERE P works_for X, P salary SA')

        schema = build_schema_from_namespace(vars().items())
        return schema
Example #4
0
def _schema():

    class Personne(EntityType):
        pass

    class Societe(EntityType):
        pass

    class Tag(EntityType):
        pass

    class travaille(RelationDefinition):
        subject = 'Personne'
        object = 'Societe'

    class tags(RelationDefinition):
        subject = 'Tag'
        object = ('Personne', 'Societe', 'Tag')

    return build_schema_from_namespace(locals().items())