def test_add_styled_spatialite_layer_invalid_layer_error(self, mock_error):
        # Arrange
        display_name = 'test_esu'
        roadnet_dir = os.path.dirname(os.path.abspath(vector_layers.__file__))
        db_path = os.path.join(roadnet_dir, 'database_files',
                               'roadnet_demo.sqlite')

        # Act
        with self.assertRaises(rn_except.InvalidLayerPopupError):
            vl = vector_layers.add_styled_spatialite_layer(
                vlayer_name='this does not exist',
                display_name=display_name,
                db_path=db_path,
                iface=self.iface)
    def test_add_styled_spatialite_layer_invalid_style(self, mock_error):
        display_name = 'esu'
        roadnet_dir = os.path.dirname(os.path.abspath(vector_layers.__file__))
        db_path = os.path.join(roadnet_dir, 'database_files',
                               'roadnet_demo.sqlite')

        vl = vector_layers.add_styled_spatialite_layer(
            vlayer_name='esu',
            display_name=display_name,
            db_path=db_path,
            iface=self.iface,
            style='This does not exist')

        registry = QgsMapLayerRegistry.instance()
        layers = registry.mapLayersByName(display_name)
        self.assertEqual(len(layers), 1,
                         "Failed to load layer with invalid style")
    def test_remove_spatialite_layer_non_existent(self, mock_error):
        # Add a new layer to remove
        display_name = 'esu'
        registry = QgsMapLayerRegistry.instance()
        roadnet_dir = os.path.dirname(os.path.abspath(vector_layers.__file__))
        db_path = os.path.join(roadnet_dir, 'database_files',
                               'roadnet_demo.sqlite')
        vl = vector_layers.add_styled_spatialite_layer(
            vlayer_name='esu',
            display_name=display_name,
            db_path=db_path,
            iface=self.iface)
        registry.removeMapLayer(vl.id())  # Removes layer

        # Act and Assert
        with self.assertRaises(rn_except.QMessageBoxWarningError):
            vector_layers.remove_spatialite_layer(vl, self.iface)
    def setUp(self):
        super(TestRampMclAutoNumbering, self).setUp()
        # Make copy of empty database to work on
        shutil.copy(self.empty_db_path, self.test_db_path)

        # Populate with example data
        conn = db.connect(self.test_db_path)
        curs = conn.cursor()
        try:
            curs.executescript(SQL_SCRIPT)
        finally:
            conn.close()

        # Open connection for tests
        self.db = connect_and_open(self.test_db_path, 'integration_testing')

        # Add vector layer
        self.mcl = vector_layers.add_styled_spatialite_layer(
            'mcl', 'MCL', self.db.databaseName(), self.iface)
    def test_add_styled_spatialite_layer(self):
        display_name = 'esu'
        roadnet_dir = os.path.dirname(os.path.abspath(vector_layers.__file__))
        db_path = os.path.join(roadnet_dir, 'database_files',
                               'roadnet_demo.sqlite')

        vl = vector_layers.add_styled_spatialite_layer(
            vlayer_name='esu',
            display_name=display_name,
            db_path=db_path,
            iface=self.iface)

        registry = QgsMapLayerRegistry.instance()
        layers = registry.mapLayersByName(display_name)
        layer_count = len(layers)
        layer_name = layers[0].name()
        self.assertEqual(layer_count, 1,
                         "One layer was not loaded ({})".format(len(layers)))
        self.assertEqual(
            layer_name, display_name,
            "Loaded name was not {} ({})".format(display_name, layer_name))
    def test_remove_spatialite_layer(self):
        # Add a new layer to remove
        display_name = 'esu'
        registry = QgsMapLayerRegistry.instance()
        roadnet_dir = os.path.dirname(os.path.abspath(vector_layers.__file__))
        db_path = os.path.join(roadnet_dir, 'database_files',
                               'roadnet_demo.sqlite')
        vl = vector_layers.add_styled_spatialite_layer(
            vlayer_name='esu',
            display_name=display_name,
            db_path=db_path,
            iface=self.iface)

        # Act
        vector_layers.remove_spatialite_layer(vl, self.iface)

        # Assert
        layers = registry.mapLayersByName(display_name)
        layer_count = len(layers)
        self.assertEqual(
            layer_count, 0,
            "Layer was not removed ({} layers found)".format(len(layers)))
 def load_mcl_layer(self):
     """
     Add mcl layer for tests that need it.
     """
     self.mcl = vector_layers.add_styled_spatialite_layer(
         'mcl', 'MCL', self.db.databaseName(), self.iface)