Esempio n. 1
0
    def test_WellMarker_setter_and_getter(self):
        # type: () -> None
        """
        Test setter and getter functionality

        :return: Nothing
        :raises AssertionError: Raises Assertion Error if a test fails
        """
        marker = WellMarker.load_all_from_db(self.session)
        self.assertEqual(len(marker), 13)
        test_string = "abcdefghijklmnopqrstuvwxyz1234567890"

        # first part: test setter functionality
        marker[0].comment = "This is a new comment"
        marker[1].comment = 4 * test_string
        marker[2].depth = 123.43
        marker[3].depth = "2345.54"
        with (self.assertRaises(ValueError)):
            marker[4].depth = "Test"
        marker[4].horizon = StratigraphicObject.init_stratigraphy(self.session, "z", 50, False)
        with (self.assertRaises(TypeError)):
            marker[5].horizon = "test"

        # second part: getter functionality
        marker = WellMarker.load_all_from_db(self.session)
        self.assertEqual(marker[0].comment, "This is a new comment")
        self.assertEqual(len(marker[1].comment), 100)
        self.assertEqual(marker[3].comment, "Comment 2")
        self.assertEqual(marker[2].depth, 123.43)
        self.assertEqual(marker[1].depth, 15)
        self.assertEqual(marker[4].horizon.statigraphic_name, "z")
        self.assertEqual(marker[5].horizon.statigraphic_name, "mo")
Esempio n. 2
0
    def test_insertion(self):
        # type: () -> None
        """
        Test the insert functions of class Well

        :return: Nothing
        :raises AssertionError: Raises AssertionError if a test fails
        """
        wells = Well.load_all_from_db(self.session)
        marker_1 = WellMarker(301.43, StratigraphicObject.init_stratigraphy(self.session, "ku"), self.session,
                              "Comment 1")
        marker_2 = WellMarker(351.65, StratigraphicObject.init_stratigraphy(self.session, "mo"), self.session)
        marker_3 = WellMarker(934.23, StratigraphicObject.init_stratigraphy(self.session, "mm"), self.session,
                              "Comment 3")

        wells[0].insert_marker(marker_1)
        self.assertEqual(len(wells[0].marker), 6)
        self.assertEqual(wells[0].marker[5], marker_1)
        self.assertRaises(ValueError, wells[1].insert_marker, marker_3)

        wells[2].insert_multiple_marker([marker_1, marker_2])
        self.assertEqual(len(wells[2].marker), 6)
        self.assertEqual(wells[2].marker[2], marker_1)
        self.assertEqual(wells[2].marker[4], marker_2)
        del wells

        wells = Well.load_all_from_db(self.session)
        self.assertEqual(wells[2].marker[2], marker_1)
        self.assertEqual(wells[2].marker[4], marker_2)
        del wells

        wells = Well.load_all_from_db(self.session)
        self.assertRaises(ValueError, wells[1].insert_multiple_marker, [marker_1, marker_2, marker_3])
Esempio n. 3
0
    def test_WellMarker_to_GeoPoint(self):
        # type: () -> None
        """
        Test WellMarker.to_GeoPoint functionality

        :return: Nothing
        :raises AssertionError: Raises Assertion Error if a test fails
        """
        marker = WellMarker.load_all_from_db(self.session)[0]
        point = marker.to_geopoint()
        self.assertEqual(point.easting, 1234.56)
        self.assertEqual(point.northing, 123.45)
        self.assertEqual(point.altitude, 0.5)
        self.assertEqual(point.name, "Well_1")
        self.assertEqual(point.horizon.statigraphic_name, "mu")
Esempio n. 4
0
    def test_delete_marker(self):
        # type: () -> None
        """
        Test the Well.get_marker_by_depth function

        :return: Nothing
        :raises AssertionError: Raises AssertionError if a test fails
        """
        well = Well.load_by_wellname_from_db("Well_1", self.session)
        marker = well.get_marker_by_depth(16)
        self.assertEqual(marker.horizon.statigraphic_name, "sm")
        self.assertRaises(ValueError, well.get_marker_by_depth, 100)
        well.delete_marker(marker)
        well.save_to_db()
        self.assertEqual(len(well.marker), 4)
        del well

        well = Well.load_by_wellname_from_db("Well_1", self.session)
        self.assertEqual(len(well.marker), 4)

        new_marker = WellMarker(23423, StratigraphicObject.init_stratigraphy(self.session, "so"), self.session,
                                "Nothing")
        self.assertRaises(ValueError, well.delete_marker, new_marker)
        self.assertRaises(TypeError, well.delete_marker, "test")
Esempio n. 5
0
    def run(self) -> bool:
        """
        Save the Well Object(s) to the database
        :return: True if function executed successfully, else False
        """

        self._mutex.lock()
        service = DatabaseService.get_instance()
        service.close_session()
        service.connect()
        session = DatabaseService.get_instance().get_session()

        try:
            onekey = self._data[next(iter(self._data.keys()))]["values"]
            count = len(onekey)

            name = self._selection["name"]
            short_name = self._selection["short_name"]
            east = self._selection["easting"]
            north = self._selection["northing"]
            alt = self._selection["altitude"]
            total_depth = self._selection["total_depth"]
            strat = self._selection["strat"]
            depth_to = self._selection["depth_to"]
            comment = self._selection["comment"]

            reference = ImportService.get_instance().get_crs()
            if reference is None:
                reference = ""
            else:
                reference = reference.toWkt()

            self._logger.debug("Saving with reference system\n{}".format(reference))

            wells = dict()

            maximum = count
            for i in range(count):
                if (self._data[east]["values"][i] == "") and (self._data[north]["values"][i] == ""):
                    continue

                na = self._data[name]["values"][i]
                sn = "" if short_name == "" else self._data[short_name]["values"][i]
                e = float(self._data[east]["values"][i])
                n = float(self._data[north]["values"][i])
                kb = None if alt == "" else float(self._data[alt]["values"][i])
                td = -1 if total_depth == "" else float(self._data[total_depth]["values"][i])
                s = "" if strat == "" else self._data[strat]["values"][i]
                dt = -1 if depth_to == "" else float(self._data[depth_to]["values"][i])
                c = "" if comment == "" else self._data[comment]["values"][i]

                strat_obj = StratigraphicObject.init_stratigraphy(session, s, -1)
                strat_obj.save_to_db()

                marker = WellMarker(dt, strat_obj, session=session, comment=c)
                # marker.save_to_db()
                # point = GeoPoint(None, False if (h is None) else True, reference,
                #                e, n, 0 if (h is None) else h, session, sn, c)

                if na in wells:
                    wells[na]["marker"].append(marker)
                else:
                    wells[na] = {
                        "short_name": sn,
                        "easting": e,
                        "northing": n,
                        "altitude": kb,
                        "total_depth": td,
                        "marker": [marker]
                    }

                    maximum += 1

                if self._cancel:
                    self._logger.debug("Import canceled")
                    self.import_failed.emit(self._message)
                    break

                self.update_progress.emit(100 * i / maximum)

            i = count
            if not self._cancel:
                for well_name in wells:
                    i += 1
                    well = wells[well_name]

                    new_well = Well.load_by_wellname_from_db(well_name, session)
                    if new_well is not None:
                        try:
                            self._logger.debug("Updating existing well: {}".format(new_well))
                            for marker in new_well.marker:
                                WellMarker.delete_from_db(marker, session)

                            new_well.save_to_db()
                            new_well.marker = well["marker"]
                            new_well.short_name = well["short_name"]
                            new_well.easting = well["easting"]
                            new_well.northing = well["northing"]
                            new_well.depth = well["total_depth"]
                        except WellMarkerDepthException as e:
                            self._logger.error("WellMarkerDepthException", "{}\n{}".format(new_well, str(e)))
                            return False
                    else:
                        self._logger.debug("Creating new well with name [{}]".format(well_name))
                        new_well = Well(well_name, well["short_name"], well["total_depth"], reference_system=reference,
                                        easting=well["easting"], northing=well["northing"], altitude=well["altitude"],
                                        session=session)

                        new_well.marker = well["marker"]

                    new_well.save_to_db()

                    self._logger.debug("Saved well:\n{}".format(new_well))

                    self.update_progress.emit(100 * i / maximum)

                    if self._cancel:
                        self._logger.debug("Import canceled")
                        self.import_failed.emit(self._message)
                        break

            if not self._cancel:
                self.update_progress.emit(100)
                self._logger.debug("Lines successfully imported")
                self.import_finished.emit()

        except Exception as e:
            msg = str(ExceptionHandler(e))
            self.import_failed.emit(msg)
            self._logger.error("Error", msg)

        finally:
            service.close_session()

        self._mutex.unlock()
        self.quit()
Esempio n. 6
0
    def setUp(self):
        # type: () -> None
        """
        Initialise a temporary database connection for all test cases and fill the database with test data

        :return: None
        """
        # initialise a in-memory sqlite database
        self.handler = DBHandler(connection="sqlite://", echo=False)
        self.session = self.handler.get_session()

        # add test data to the database
        self.wells = [
            {
                "name": "Well_1",
                "short_name": "W1",
                "comment": "A drilled well",
                "east": 1234.56,
                "north": 123.45,
                "altitude": 10.5,
                "depth": 555,
                "marker": ((10, "mu", 4, ""),
                           (15, "so", 3, "Comment 1"),
                           (16, "sm", 2, ""),
                           (17, "su", 1, "Comment 2"),
                           (5, "mm", 5, "Comment 3"))
            }, {
                "name": "Well_2",
                "short_name": "W2",
                "comment": "",
                "east": 1000.23,
                "north": 2300.34,
                "altitude": 342.23,
                "depth": 341,
                "marker": ((12, "mo", 6, ""),
                           (120, "mm", 5, "Comment 1"),
                           (300, "Fault", 0, "Comment 2"),
                           (320, "mo", 6, ""))
            }, {
                "name": "Well_3",
                "short_name": "W3",
                "comment": "A third well",
                "east": 3454.34,
                "north": 2340.22,
                "altitude": 342.20,
                "depth": 645.21,
                "marker": ((34, "mu", 4, ""),
                           (234, "so", 3, "Comment 1"),
                           (345, "Fault", 0, "Comment 2"),
                           (635, "mu", 4, "Comment 3"))
            }
        ]

        for well in self.wells:
            new_well = Well(well["name"], well["short_name"], well["depth"], "spatial_reference_unknown", well["east"],
                            well["north"], well["altitude"], self.session, "well_set", well["comment"])
            new_well.save_to_db()

            for mark in well["marker"]:
                new_well.marker.append(
                    WellMarker(mark[0], StratigraphicObject.init_stratigraphy(self.session, mark[1], mark[2], False),
                               self.session, well["name"], mark[3]))
                new_well.marker[-1].save_to_db()