Ejemplo n.º 1
0
    def test_relationById(self):
        """ test retrieving relation by id"""
        manager = QgsRelationManager()

        rel = manager.relation('does not exist')
        self.assertFalse(rel.isValid())

        # add two relations
        rel = self.createRelation()
        rel.setId('rel1')
        rel.setName('Relation Number One')
        assert rel.isValid()
        manager.addRelation(rel)
        rel = self.createRelation()
        rel.setId('rel2')
        rel.setName('Relation Number Two')
        assert rel.isValid()
        manager.addRelation(rel)

        rel = manager.relation('does not exist')
        self.assertFalse(rel.isValid())

        rel = manager.relation('rel1')
        self.assertEqual(rel.id(), 'rel1')

        rel = manager.relation('rel2')
        self.assertEqual(rel.id(), 'rel2')
Ejemplo n.º 2
0
    def test_addRelation(self):
        """ test adding relations to a manager """
        manager = QgsRelationManager()

        relations = manager.relations()
        self.assertEqual(len(relations), 0)

        rel = self.createRelation()
        rel.setRelationId('rel1')
        rel.setRelationName('Relation Number One')
        assert rel.isValid()

        manager.addRelation(rel)

        relations = manager.relations()
        self.assertEqual(len(relations), 1)
        self.assertEqual(relations['rel1'].id(), 'rel1')

        rel = self.createRelation()
        rel.setRelationId('rel2')
        rel.setRelationName('Relation Number Two')
        assert rel.isValid()

        manager.addRelation(rel)

        relations = manager.relations()
        self.assertEqual(len(relations), 2)
        ids = [r.id() for r in list(relations.values())]
        self.assertEqual(set(ids), set(['rel1', 'rel2']))
Ejemplo n.º 3
0
    def test_relationById(self):
        """ test retrieving relation by id"""
        manager = QgsRelationManager()

        rel = manager.relation('does not exist')
        self.assertFalse(rel.isValid())

        # add two relations
        rel = self.createRelation()
        rel.setId('rel1')
        rel.setName('Relation Number One')
        assert rel.isValid()
        manager.addRelation(rel)
        rel = self.createRelation()
        rel.setId('rel2')
        rel.setName('Relation Number Two')
        assert rel.isValid()
        manager.addRelation(rel)

        rel = manager.relation('does not exist')
        self.assertFalse(rel.isValid())

        rel = manager.relation('rel1')
        self.assertEqual(rel.id(), 'rel1')

        rel = manager.relation('rel2')
        self.assertEqual(rel.id(), 'rel2')
Ejemplo n.º 4
0
    def test_polymorphicRelation(self):
        # tests addPolymorphicRelation/polymorphicRelation

        manager = QgsRelationManager()

        # initially the map should be empty
        self.assertFalse(manager.polymorphicRelations())

        poly_rel1 = self.createPolymorphicRelation()
        poly_rel1.setId('poly_rel1')
        poly_rel1.setName('Poly Rel 1')
        poly_rel2 = self.createPolymorphicRelation()
        poly_rel2.setId('poly_rel2')
        poly_rel2.setName('Poly Rel 2')

        # the relation should be valid now
        self.assertTrue(poly_rel1.isValid())
        self.assertTrue(poly_rel2.isValid())

        manager.addPolymorphicRelation(poly_rel1)
        manager.addPolymorphicRelation(poly_rel2)

        self.assertFalse(
            manager.polymorphicRelation('poly_invalid_id').isValid())
        self.assertTrue(manager.polymorphicRelation('poly_rel1').isValid())
        self.assertTrue(manager.polymorphicRelation('poly_rel2').isValid())
        self.assertTrue(
            manager.polymorphicRelation('poly_rel1').hasEqualDefinition(
                poly_rel1))
        self.assertTrue(
            manager.polymorphicRelation('poly_rel2').hasEqualDefinition(
                poly_rel2))
Ejemplo n.º 5
0
    def test_addRelation(self):
        """ test adding relations to a manager """
        manager = QgsRelationManager()

        relations = manager.relations()
        self.assertEqual(len(relations), 0)

        rel = self.createRelation()
        rel.setId('rel1')
        rel.setName('Relation Number One')
        assert rel.isValid()

        manager.addRelation(rel)

        relations = manager.relations()
        self.assertEqual(len(relations), 1)
        self.assertEqual(relations['rel1'].id(), 'rel1')

        rel = self.createRelation()
        rel.setId('rel2')
        rel.setName('Relation Number Two')
        assert rel.isValid()

        manager.addRelation(rel)

        relations = manager.relations()
        self.assertEqual(len(relations), 2)
        ids = [r.id() for r in list(relations.values())]
        self.assertEqual(set(ids), set(['rel1', 'rel2']))
Ejemplo n.º 6
0
    def create(layer_wrapper: LayerWrapper, relation_manager: QgsRelationManager,
               relation: QgsRelation) -> 'RelationalLayerWrapper':

        referencing_layer: QgsVectorLayer = relation.referencingLayer()
        try:
            other_relation: QgsRelation = [r for r in relation_manager.referencingRelations(referencing_layer)
                                           if r.id() != relation.id()][0]
        except KeyError:
            raise QaavaLayerError(tr('Relation error'), bar_msg(
                tr('Relation {} does not contain referencing another layer', relation.name())))

        relation_layer_wrapper = LayerWrapper.from_qgs_layer(referencing_layer)
        a_pk: QgsField = referencing_layer.fields().toList()[relation.referencingFields()[0]]
        m_pk_a: QgsField = layer_wrapper.get_layer().fields().toList()[relation.referencedFields()[0]]

        fw_m_a = FieldWrapper.from_layer_wrapper(relation_layer_wrapper, a_pk, set(), '')
        fw_a = FieldWrapper.from_layer_wrapper(layer_wrapper, m_pk_a, set(), '')

        other_layer: QgsVectorLayer = other_relation.referencedLayer()
        b_pk = other_layer.fields().toList()[other_relation.referencedFields()[0]]
        m_pk_b = referencing_layer.fields().toList()[other_relation.referencingFields()[0]]

        fw_m_b = FieldWrapper.from_layer_wrapper(relation_layer_wrapper, m_pk_b, set(), '')
        other_layer_wrapper = LayerWrapper.from_qgs_layer(other_layer, relation_layer_wrapper, fw_m_b)

        fw_b = FieldWrapper.from_layer_wrapper(other_layer_wrapper, b_pk, set(), '')

        return RelationalLayerWrapper(referencing_layer.name(), other_layer.name(), layer_wrapper, fw_m_a,
                                      fw_a, fw_m_b, fw_b)
Ejemplo n.º 7
0
    def test_relationByName(self):
        """ test retrieving relations by name"""
        manager = QgsRelationManager()

        rels = manager.relationsByName('does not exist')
        self.assertEqual(rels, [])

        # add some relations
        rel = self.createRelation()
        rel.setRelationId('rel1')
        rel.setRelationName('my relation')
        assert rel.isValid()
        manager.addRelation(rel)
        rel = self.createRelation()
        rel.setRelationId('rel2')
        rel.setRelationName('dupe name')
        assert rel.isValid()
        manager.addRelation(rel)
        rel = self.createRelation()
        rel.setRelationId('rel3')
        rel.setRelationName('dupe name')
        assert rel.isValid()
        manager.addRelation(rel)

        rels = manager.relationsByName('does not exist')
        self.assertEqual(rels, [])

        rels = manager.relationsByName('my relation')
        ids = [r.id() for r in rels]
        self.assertEqual(set(ids), set(['rel1']))

        # case insensitive
        rels = manager.relationsByName('My RelAtion')
        ids = [r.id() for r in rels]
        self.assertEqual(set(ids), set(['rel1']))

        # multiple results
        rels = manager.relationsByName('dupe name')
        ids = [r.id() for r in rels]
        self.assertEqual(set(ids), set(['rel2', 'rel3']))
Ejemplo n.º 8
0
    def test_relationByName(self):
        """ test retrieving relations by name"""
        manager = QgsRelationManager()

        rels = manager.relationsByName('does not exist')
        self.assertEqual(rels, [])

        # add some relations
        rel = self.createRelation()
        rel.setId('rel1')
        rel.setName('my relation')
        assert rel.isValid()
        manager.addRelation(rel)
        rel = self.createRelation()
        rel.setId('rel2')
        rel.setName('dupe name')
        assert rel.isValid()
        manager.addRelation(rel)
        rel = self.createRelation()
        rel.setId('rel3')
        rel.setName('dupe name')
        assert rel.isValid()
        manager.addRelation(rel)

        rels = manager.relationsByName('does not exist')
        self.assertEqual(rels, [])

        rels = manager.relationsByName('my relation')
        ids = [r.id() for r in rels]
        self.assertEqual(set(ids), set(['rel1']))

        # case insensitive
        rels = manager.relationsByName('My RelAtion')
        ids = [r.id() for r in rels]
        self.assertEqual(set(ids), set(['rel1']))

        # multiple results
        rels = manager.relationsByName('dupe name')
        ids = [r.id() for r in rels]
        self.assertEqual(set(ids), set(['rel2', 'rel3']))
Ejemplo n.º 9
0
    def test_setPolymorphicRelations(self):
        # tests polymorphicRelations/setPolymorphicRelations

        manager = QgsRelationManager()

        # initially the map should be empty
        self.assertListEqual(list(manager.polymorphicRelations()), [])

        poly_rel1 = self.createPolymorphicRelation()
        poly_rel1.setId('poly_rel1')
        poly_rel1.setName('Poly Rel 1')
        poly_rel2 = self.createPolymorphicRelation()
        poly_rel2.setId('poly_rel2')
        poly_rel2.setName('Poly Rel 2')
        poly_rel3 = self.createPolymorphicRelation()
        poly_rel3.setId('poly_rel3')
        poly_rel3.setName('Poly Rel 3')

        # the relation should be valid now
        self.assertTrue(poly_rel1.isValid())
        self.assertTrue(poly_rel2.isValid())
        self.assertTrue(poly_rel3.isValid())

        manager.setPolymorphicRelations([poly_rel1, poly_rel2, poly_rel3])

        # the relations should match
        self.assertListEqual(list(manager.polymorphicRelations()),
                             ['poly_rel1', 'poly_rel2', 'poly_rel3'])
        self.assertTrue(manager.polymorphicRelations()['poly_rel1'])
        self.assertTrue(manager.polymorphicRelations()['poly_rel2'])
        self.assertTrue(manager.polymorphicRelations()['poly_rel3'])
        self.assertTrue(manager.polymorphicRelations()
                        ['poly_rel1'].hasEqualDefinition(poly_rel1))
        self.assertTrue(manager.polymorphicRelations()
                        ['poly_rel2'].hasEqualDefinition(poly_rel2))
        self.assertTrue(manager.polymorphicRelations()
                        ['poly_rel3'].hasEqualDefinition(poly_rel3))

        manager.setPolymorphicRelations([poly_rel1])
        self.assertListEqual(list(manager.polymorphicRelations()),
                             ['poly_rel1'])
        manager.setPolymorphicRelations([])
        self.assertListEqual(list(manager.polymorphicRelations()), [])