def test_setter_and_getter(self): # type: () -> None """ Test the setter and getter functions of class GeoPoint :return: Nothing :raises AssertionError: Raises AssertionError if a test fails """ # points = GeoPoint.load_all_from_db(self.session) points = GeoPoint.load_all_without_lines_from_db(self.session) self.assertEqual( len(points), len(self.points), "Wrong point length ({}). Should be {}.".format( len(points), len(self.points))) points[0].easting = 1 points[1].northing = 2 points[2].altitude = 3 points[2].use_z() points[3].horizon = StratigraphicObject.init_stratigraphy( self.session, "so", 10, False) points[0].name = "point set name" points[1].del_z() for point in points: point.save_to_db() del points points = GeoPoint.load_all_without_lines_from_db(self.session) self.assertEqual( len(points), len(self.points), "Wrong point length ({}). Should be {}.".format( len(points), len(self.points))) self.assertEqual( points[0].easting, 1, "Wrong easting value ({}). Should be {}.".format( points[0].easting, 1)) self.assertEqual( points[1].northing, 2, "Wrong northing value ({}). Should be {}.".format( points[1].northing, 2)) self.assertEqual( points[2].altitude, 3, "Wrong altitude value ({}). Should be {}.".format( points[2].altitude, 3)) self.assertTrue( points[2].has_z, "Third point has no z-value...") # Third point got z-value self.assertEqual( points[3].horizon.statigraphic_name, "so", "Wrong horizon ({}). Should be {}.".format( points[3].horizon.statigraphic_name, "so")) self.assertEqual( points[0].name, "point set name", "Wrong point name ({}). Should be {}.".format( points[0].name, "point set name")) self.assertFalse(points[1].has_z, "Second point has a z-value...") self.assertEqual( points[1].altitude, 0, "Wrong altitude value ({}). Should be {}.".format( points[1].altitude, 0))
def test_setter_and_getter(self): # type: () -> None """ Test the setter and getter functionality :return: Nothing :raises AssertionError: Raises Assertion Error when a test fails """ point = GeoPoint.load_all_from_db(self.session)[0] point.properties[0].property_value = 342 point.properties[1].property_value = "345.34" point.properties[2].property_value = "Test" point.properties[1].name = "some text information" point.properties[1].comment = "unused" point.properties[2].property_type = PropertyTypes.FLOAT with self.assertRaises(ValueError): point.properties[0].property_value = "string" with self.assertRaises(ValueError): point.properties[0].property_value = 234.34 del point point = GeoPoint.load_all_from_db(self.session)[0] self.assertEqual(342, point.properties[0].property_value) self.assertEqual(345.34, point.properties[1].property_value) self.assertEqual(None, point.properties[2].property_value) # type changed to PropertyTypes.FLOAT point.properties[2].property_type = PropertyTypes.STRING self.assertEqual("Test", point.properties[2].property_value) self.assertEqual("some text information", point.properties[1].name) self.assertEqual("unused", point.properties[1].comment)
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 point = GeoPoint(StratigraphicObject.init_stratigraphy(self.session, "mu", 1), True, "", 1, 2, 3, self.session, "test", "") point.save_to_db() point.add_property(Property(0, PropertyTypes.INT, "test prop", "test unit", self.session)) point.add_property(Property(0, PropertyTypes.FLOAT, "test prop 2", "test unit 2", self.session)) point.add_property(Property(0, PropertyTypes.STRING, "test prop 3", "test unit 3", self.session))
def test_add_and_delete_properties(self): # type: () -> None """ Test the add_property and delete_property function :return: Nothing :raises AssertionError: Raises AssertionError if a test fails """ point = GeoPoint.load_all_from_db(self.session)[0] point.add_property( Property(0, PropertyTypes.INT, "test prop", "test unit", self.session)) point.add_property( Property(0, PropertyTypes.INT, "test prop 2", "test unit 2", self.session)) self.assertRaises(TypeError, point.add_property, "string") del point point = GeoPoint.load_all_from_db(self.session)[0] self.assertEqual(2, len(point.properties)) self.assertEqual("test prop", point.properties[0].property_name) self.assertEqual("test prop 2", point.properties[1].property_name) self.assertEqual("test unit", point.properties[0].property_unit) self.assertEqual("test unit 2", point.properties[1].property_unit) self.assertTrue(point.has_property("test prop 2")) self.assertEqual("test unit 2", point.get_property("test prop 2").property_unit) prop = point.properties[0] point.delete_property(prop) self.assertRaises(TypeError, point.delete_property, "string") self.assertRaises(ValueError, point.delete_property, prop) self.assertEqual(1, len(point.properties)) self.assertEqual("test prop 2", point.properties[0].property_name) self.assertEqual("test unit 2", point.properties[0].property_unit) del point point = GeoPoint.load_all_from_db(self.session)[0] self.assertEqual(1, len(point.properties)) self.assertEqual("test prop 2", point.properties[0].property_name) self.assertEqual("test unit 2", point.properties[0].property_unit)
def to_geopoint(self) -> GeoPoint: """ Returns the current well marker as GeoPoint :return: the current well marker as GeoPoint """ easting = self.well.easting northing = self.well.northing altitude = self.well.altitude - self.depth return GeoPoint(self.horizon, True, self.well.reference_system, easting, northing, altitude, self.session, self.well.well_name, self.comment)
def test_init(self): # type: () -> None """ Test the initialisation of the class :return: Nothing :raises AssertionError: Raises AssertionError if a test fails """ point = GeoPoint.load_all_from_db(self.session)[0] self.assertEqual(3, len(point.properties)) self.assertEqual("test prop", point.properties[0].property_name) self.assertEqual("test prop 2", point.properties[1].property_name) self.assertEqual("test prop 3", point.properties[2].property_name) self.assertEqual("test unit", point.properties[0].property_unit) self.assertEqual("test unit 2", point.properties[1].property_unit) self.assertEqual("test unit 3", point.properties[2].property_unit) self.assertEqual(PropertyTypes.INT, point.properties[0].property_type) self.assertEqual(PropertyTypes.FLOAT, point.properties[1].property_type) self.assertEqual(PropertyTypes.STRING, point.properties[2].property_type)
def run(self): """ Save the Properties to the database :return: Nothing, emits Qt Signals """ self._mutex.lock() service = DatabaseService.get_instance() service.close_session() service.connect() session = service.get_session() failed_imports = 0 try: onekey = self._data[next(iter(self._data.keys()))]["values"] # import json # self._logger.info(json.dumps(onekey, indent=2)) count = len(onekey) id_col = self._selection["id"] 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)) for i in range(count): self.update_progress.emit(100 * i / count) try: _id = int(self._data[id_col]["values"][i]) except ValueError: self._logger.warn("No id specified for current data set at line {}".format(i), only_logfile=True) failed_imports += 1 continue if (_id is None) or (_id < 0): self._logger.warn("Unknown Geopoint ID found: [{}]".format(_id)) failed_imports += 1 continue try: point = GeoPoint.load_by_id_from_db(_id, session) if point is None: self._logger.warn("No Geopoint with ID [{}] found".format(_id)) failed_imports += 1 continue point.reference_system = reference # add / update properties for item in self._properties: self._logger.debug("Property: {}".format(item)) if point.has_property(item.name): p = point.get_property(item.name) p.property_unit = item.unit p.value = self._data[item.name]["values"][i] p.property_type = item.property_type else: p = Property(value=self._data[item.name]["values"][i], property_name=item.name, _type=item.property_type, property_unit=item.unit, session=session) point.add_property(p) self._logger.debug("point: {}".format(point)) point.save_to_db() except DatabaseRequestException: self._logger.warn("Cannot find Geopoint with ID [{}]. Skipping property import".format(_id)) failed_imports += 1 if self._cancel: self._logger.debug("Import canceled") self.import_failed.emit(self._message) break if not self._cancel: if failed_imports > 0: self.import_finished_with_warnings.emit("Could not import {} properties.".format(failed_imports)) self.update_progress.emit(100) self.import_finished.emit() except Exception as e: msg = str(ExceptionHandler(e)) self.import_failed.emit(msg) self._logger.error("Error", msg, to_messagebar=True) finally: service.close_session() self._mutex.unlock() self.quit()
def run(self): """ Save the Point 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"] # import json # self._logger.info(json.dumps(onekey, indent=2)) count = len(onekey) east = self._selection["easting"] north = self._selection["northing"] alt = self._selection["altitude"] strat = self._selection["strat"] age = self._selection["strat_age"] set_name = self._selection["set_name"] 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)) lines = dict() line = 0 maximum = count for i in range(count): _id = None if "gln_id" in self._data: try: _id = int(self._data["gln_id"]["values"][i]) except ValueError: pass if (self._data[east]["values"][i] == "") and (self._data[north]["values"][i] == ""): line += 1 continue e = float(self._data[east]["values"][i]) n = float(self._data[north]["values"][i]) h = None if alt == "" else float(self._data[alt]["values"][i]) s = "" if strat == "" else self._data[strat]["values"][i] a = -1 if age == "" else float(self._data[age]["values"][i]) sn = "" if set_name == "" else self._data[set_name]["values"][i] c = "" if comment == "" else self._data[comment]["values"][i] strat_obj = StratigraphicObject.init_stratigraphy(session, s, a) point = GeoPoint(None, False if (h is None) else True, reference, e, n, 0 if (h is None) else h, session, sn, c) # add / update properties for item in self._properties: self._logger.debug("Property: {}".format(item)) if point.has_property(item.name): p = point.get_property(item.name) p.property_unit = item.unit p.value = self._data[item.name]["values"][i] p.property_type = item.property_type else: p = Property(value=self._data[item.name]["values"][i], property_name=item.name, _type=item.property_type, property_unit=item.unit, session=session) point.add_property(p) if _id is not None: point.line_id = _id self._logger.debug("line point: {}".format(point)) if line in lines: lines[line]["points"].append(point) else: lines[line] = { "id": _id, "strat": strat_obj, "points": [point], "name": sn, "comment": c } maximum += 1 i += 1 self.update_progress.emit(100 * i / maximum) if self._cancel: self._logger.debug("Import canceled") self.import_failed.emit(self._message) break i = count if not self._cancel: for l in lines: i += 1 line = lines[l] closed = False if (len(line["points"]) > 1) and (line["points"][0] == line["points"][-1]): closed = True line["points"].pop() new_line = None if line["id"] is not None: new_line = Line.load_by_id_from_db(line["id"], session) if new_line is not None: for point in new_line.points: AbstractDBObject.delete_from_db(point, session) del point new_line.closed = closed new_line.points = line["points"] new_line.horizon = line["strat"] new_line.name = line["name"] new_line.comment = line["comment"] self._logger.debug("Updated existing line") if line["id"] is None or new_line is None: # new_line is None ? not in database -> create a new one new_line = Line(closed, line["strat"], line["points"], session, line["name"], line["comment"]) self._logger.debug("Created new line") new_line.save_to_db() self._logger.debug("Line: {}".format(str(new_line))) 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()
def run(self): """ Save the Point 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 = service.get_session() try: onekey = self._data[next(iter(self._data.keys()))]["values"] # import json # self._logger.info(json.dumps(onekey, indent=2)) count = len(onekey) east = self._selection["easting"] north = self._selection["northing"] alt = self._selection["altitude"] strat = self._selection["strat"] age = self._selection["strat_age"] set_name = self._selection["set_name"] 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)) for i in range(count): self.update_progress.emit(100 * i / count) if (self._data[east]["values"][i] == "") and (self._data[north]["values"][i] == ""): continue _id = None if "gpt_id" in self._data: try: _id = int(self._data["gpt_id"]["values"][i]) except ValueError: pass e = float(self._data[east]["values"][i]) n = float(self._data[north]["values"][i]) h = None if alt == "" else float(self._data[alt]["values"][i]) s = "" if strat == "" else self._data[strat]["values"][i] a = -1 if age == "" else float(self._data[age]["values"][i]) sn = "" if set_name == "" else self._data[set_name]["values"][i] c = "" if comment == "" else self._data[comment]["values"][i] strat_obj = StratigraphicObject.init_stratigraphy(session, s, a) if (_id is not None) and (_id > -1): point = GeoPoint.load_by_id_from_db(_id, session) point.easting = e point.northing = n point.altitude = h point.del_z() if h is None else point.use_z() point.reference_system = reference point.horizon = strat_obj point.name = sn point.comment = c self._logger.debug("point update") else: point = GeoPoint(strat_obj, False if (h is None) else True, reference, e, n, 0 if (h is None) else h, session, sn, c) self._logger.debug("new point") # add / update properties for item in self._properties: self._logger.debug("Property: {}".format(item)) if point.has_property(item.name): p = point.get_property(item.name) p.property_unit = item.unit p.value = self._data[item.name]["values"][i] p.property_type = item.property_type else: p = Property(value=self._data[item.name]["values"][i], property_name=item.name, _type=item.property_type, property_unit=item.unit, session=session) point.add_property(p) self._logger.debug("point: {}".format(point)) point.save_to_db() 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("Points 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()
def test_delete_point(self): # type: () -> None """ Test the deletion of a point. /1/ the point itself /2/ delete by coordinates /3/ test auto-removal of doubled points after deletion :return: Nothing :raises AssertionError: Raises AssertionError if a test fails """ line_query = self.session.query(Line).filter_by(id=2) count = line_query.count() self.assertEqual( count, 1, "Get more than one expected result for line-id-request ({})". format(count)) line = line_query.one() line.session = self.session line.delete_point(line.points[2]) # save deletion and reload line, test afterwards line.save_to_db() del count del line del line_query line = self.session.query(Line).filter_by(id=2).one() self.assertEqual( len(line.points), 3, "Wrong Nr of points ({}), should be {}".format( len(line.points), 3)) # test exception handling self.assertRaises(TypeError, line.delete_point, "string") self.assertRaises( ValueError, line.delete_point, GeoPoint(None, False, "", 1, 2, 0, self.session, "", "")) del line # /2/ test deletion by coordinates line_query = self.session.query(Line).filter_by(id=3) count = line_query.count() self.assertEqual( count, 1, "Get more than one expected result for line-id-request ({})". format(count)) line = line_query.one() line.session = self.session line.delete_point_by_coordinates(1214704.933941905, 641092.8288590391, 0) # save deletion and reload line, test afterwards line.save_to_db() del count del line del line_query line = self.session.query(Line).filter_by(id=3).one() self.assertEqual( len(line.points), 4, "Wrong Nr of points ({}), should be {}".format( len(line.points), 4)) self.assertEqual( line.points[1].id, 10, "First point before deleted point should have id {} but has {}". format(10, line.points[1].id)) self.assertEqual( line.points[2].id, 12, "First point after deleted point should have id {} but has {}". format(12, line.points[2].id)) # test exception handling self.assertRaises(ValueError, line.delete_point_by_coordinates, 123, 456, "test") self.assertRaises(ValueError, line.delete_point_by_coordinates, 123, 456, 789) # /3/ test auto-removal of doubled points after deletion line_query = self.session.query(Line).filter_by(id=4) count = line_query.count() self.assertEqual( count, 1, "Get more than one expected result for line-id-request ({})". format(count)) line = line_query.one() line.session = self.session line.delete_point_by_coordinates(1191579.1097525698, 648030.5761158396, 0) # save deletion and reload line, test afterwards line.save_to_db() del count del line del line_query line = self.session.query(Line).filter_by(id=4).one() self.assertEqual( len(line.points), 3, "Wrong Nr of points ({}), should be {}".format( len(line.points), 3)) self.assertEqual( line.points[1].id, 15, "First point before deleted point should have id {} but has {}". format(15, line.points[1].id)) self.assertEqual( line.points[2].id, 18, "First point after deleted point should have id {} but has {}". format(18, line.points[2].id))
def test_insert_multiple(self): # type: () -> None """ Test the insertion of multiple points. Although test remove of doubled values in a line. :return: Nothing :raises AssertionError: Raises AssertionError if a test fails """ insert_point_1 = GeoPoint( StratigraphicObject.init_stratigraphy(self.session, "mu"), False, "", 1204200, 620800, 0, self.session) insert_point_2 = GeoPoint( StratigraphicObject.init_stratigraphy(self.session, "mu"), False, "", 1204500, 621200, 0, self.session) insert_point_3 = GeoPoint( StratigraphicObject.init_stratigraphy(self.session, "mu"), False, "", 1204700, 621000, 0, self.session) insert_point_4 = GeoPoint( StratigraphicObject.init_stratigraphy(self.session, "mu"), False, "", 1204700, 621000, 0, self.session) points = [ insert_point_1, insert_point_2, insert_point_3, insert_point_4 ] line_query = self.session.query(Line).filter_by(id=1) count = line_query.count() self.assertEqual( count, 1, "Get more than one expected result for line-id-request ({})". format(count)) line = line_query.one() line.session = self.session line.insert_points(points, 1) line.save_to_db() # point is inserted, now delete insert details del count del insert_point_1, insert_point_2, insert_point_3 del line del line_query # test the insertion-process line_query = self.session.query(Line).filter_by(id=1) count = line_query.count() self.assertEqual( count, 1, "Get more than one expected result for line-id-request ({})". format(count)) line = line_query.one() # 20 Point initially, 2 removed, new point are Nr 19-21 -> id=19 to 21 # !!!ATTENTION!!! counting starts with 1 not 0 in sqlite-DB! # line-pos should be 1, 2 and 3 self.assertEqual( line.points[1].id, 19, "Wrong id ({}) for new point (should be {})".format( line.points[1].id, 19)) self.assertEqual( line.points[2].id, 20, "Wrong id ({}) for new point (should be {})".format( line.points[2].id, 20)) self.assertEqual( line.points[3].id, 21, "Wrong id ({}) for new point (should be {})".format( line.points[3].id, 21)) self.assertEqual( line.points[4].id, 2, "Wrong id ({}) for point after insert (should be {})".format( line.points[4].id, 2)) self.assertEqual( line.points[1].line_pos, 1, "Wrong position of the new point ({}) in the line (should be {})". format(line.points[1].line_pos, 1)) self.assertEqual( line.points[2].line_pos, 2, "Wrong position of the new point ({}) in the line (should be {})". format(line.points[2].line_pos, 2)) self.assertEqual( line.points[3].line_pos, 3, "Wrong position of the new point ({}) in the line (should be {})". format(line.points[3].line_pos, 3)) self.assertEqual( line.points[1].easting, 1204200, "Wrong easting ({} / should be {})".format(line.points[1].easting, 1204200)) self.assertEqual( line.points[1].northing, 620800, "Wrong northing ({} / should be {})".format( line.points[1].northing, 620800)) self.assertEqual( line.points[1].altitude, 0, "Wrong altitude ({} / should be {})".format( line.points[1].altitude, 0)) self.assertEqual( line.points[1].has_z, False, "Wrong has_z ({} / should be {})".format(line.points[1].has_z, False)) # test Exception handling self.assertRaises(ValueError, line.insert_points, points, "abc") points.append("cde") self.assertRaises(TypeError, line.insert_points, points, 2)
def test_insert_one(self): # type: () -> None """ Test the insertion of one point. Additionally test get_point_index(point) function :return: Nothing :raises AssertionError: Raises AssertionError if a test fails """ insert_point = GeoPoint( StratigraphicObject.init_stratigraphy(self.session, "mu"), False, "", 1204200, 620800, 0, self.session) line_query = self.session.query(Line).filter_by(id=1) count = line_query.count() self.assertEqual( count, 1, "Get more than one expected result for line-id-request ({})". format(count)) line = line_query.one() line.session = self.session line.insert_point(insert_point, 1) self.assertEqual(line.points[1].line_pos, 1) # point is inserted, now delete insert details del count del line del line_query # test the insertion-process line_query = self.session.query(Line).filter_by(id=1) count = line_query.count() self.assertEqual( count, 1, "Get more than one expected result for line-id-request ({})". format(count)) line = line_query.one() # 20 Point initially, 2 removed, new point is Nr 19 -> id=19 # !!!ATTENTION!!! counting starts with 1 not 0 in sqlite-DB! # line-pos and get_point_index should be 1 self.assertEqual( line.points[1].id, 19, "Wrong id ({}) for new point (should be {})".format( line.points[1].id, 19)) self.assertEqual( line.points[1].line_pos, 1, "Wrong position of the new point ({}) in the line (should be {})". format(line.points[1].line_pos, 1)) self.assertEqual( line.get_point_index(insert_point), 1, "Wrong get_point_index(...) value ({}) in the line (should be {})". format(line.get_point_index(insert_point), 1)) self.assertEqual( line.points[1].easting, 1204200, "Wrong easting ({} / should be {})".format(line.points[1].easting, 1204200)) self.assertEqual( line.points[1].northing, 620800, "Wrong northing ({} / should be {})".format( line.points[1].northing, 620800)) self.assertEqual( line.points[1].altitude, 0, "Wrong altitude ({} / should be {})".format( line.points[1].altitude, 0)) self.assertEqual( line.points[1].has_z, False, "Wrong has_z ({} / should be {})".format(line.points[1].has_z, False)) # test Exception handling self.assertRaises(TypeError, line.insert_point, "string", 1) self.assertRaises(ValueError, line.insert_points, insert_point, "abc")
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() # handler = DBHandler( # connection="sqlite:////Users/stephan/Documents/data.db", # debug=False) # handler = DBHandler(connection="sqlite:///D:\\data.db", debug=False) # add test data to the database self.lines = [{ "closed": False, "horizon": "mu", "age": 3, "update": False, "points": ((1204067.0548148106, 634617.5980860253), (1204067.0548148106, 620742.1035724243), (1215167.4504256917, 620742.1035724243), (1215167.4504256917, 634617.5980860253), (1204067.0548148106, 634617.5980860253)), "name": "Line_1" }, { "closed": True, "horizon": "so", "age": 2, "update": True, "points": ((1179553.6811741155, 647105.5431482664), (1179553.6811741155, 626292.3013778647), (1194354.20865529, 626292.3013778647), (1194354.20865529, 647105.5431482664)), "name": "Line_2" }, { "closed": False, "horizon": "mm", "age": 4, "update": True, "points": ((1179091.1646903288, 712782.8838459781), (1161053.0218226474, 667456.2684348812), (1214704.933941905, 641092.8288590391), (1228580.428455506, 682719.3123998424), (1218405.0658121984, 721108.1805541387)), "name": "Line_3" }, { "closed": False, "horizon": "mo", "age": 5, "update": True, "points": ((1149490.1097279799, 691044.6091080031), (1149490.1097279799, 648030.5761158396), (1191579.1097525698, 648030.5761158396), (1149490.1097279799, 648030.5761158396), (1191579.1097525698, 691044.6091080031), (1149490.1097279799, 691044.6091080031)), "name": "Line_2" }] for line in self.lines: points = list() for point in line["points"]: points.append( GeoPoint(None, False, "", point[0], point[1], 0, self.session, line["name"], "")) new_line = Line( line["closed"], StratigraphicObject.init_stratigraphy(self.session, line["horizon"], line["age"], line["update"]), points, self.session, line["name"], "") new_line.save_to_db()
def test_loading(self): # type: () -> None """ Test the loading functions of the GeoPoint class /1/ load all from database /2/ load by name /3/ load in extent :return: Nothing :raises AssertionError: Raises AssertionError if a test fails """ # /1/ load all from database points = GeoPoint.load_all_from_db(self.session) pnts_count = len(self.points) for line in self.lines: pnts_count += len(line["points"]) # 2 points have been automatically deleted and the lines will be closed pnts_count -= 2 self.assertEqual( len(points), pnts_count, "Wrong point count ({}). Should be {}.".format( len(points), pnts_count)) self.assertEqual( points[0].id, 1, "First point should have id {}, but has {}.".format( 1, points[0].id)) self.assertTrue( math.fabs(float(points[-1].easting) - 1191579.1097525698) < float_precision, "Wrong easting difference to large ( |{} - {}| = {} > {}).".format( float(points[-1].easting), 1191579.1097525698, math.fabs(float(points[-1].easting) - 1191579.1097525698), float_precision)) self.assertTrue( math.fabs(float(points[-1].northing) - 691044.6091080031) < float_precision, "Wrong northing difference to large ( |{} - {}| = {} > {}).". format(float(points[-1].northing), 691044.6091080031, math.fabs(float(points[-1].northing) - 691044.6091080031), float_precision)) del points points = GeoPoint.load_all_without_lines_from_db(self.session) pnts_count = len( self.points ) # only points which doesn"t belong to a line are loaded self.assertEqual( len(points), pnts_count, "Wrong point count ({}). Should be {}.".format( len(points), pnts_count)) self.assertEqual( points[0].id, 1, "First point should have id {}, but has {}.".format( 1, points[0].id)) self.assertTrue( math.fabs(float(points[-1].easting) - 1273456) < float_precision, "Wrong easting difference to large ( |{} - {}| = {} > {}).".format( float(points[-1].easting), 1273456, math.fabs(float(points[-1].easting) - 1273456), float_precision)) self.assertTrue( math.fabs(float(points[-1].northing) - 5449672) < float_precision, "Wrong northing difference to large ( |{} - {}| = {} > {}).". format(float(points[-1].northing), 5449672, math.fabs(float(points[-1].northing) - 5449672), float_precision)) self.assertEqual( points[-1].horizon.statigraphic_name, "mu", "Wrong horizon ({}). Should be {}.".format( points[-1].horizon.statigraphic_name, "mu")) del points # /2/ load by name points = GeoPoint.load_by_name_from_db("", self.session) pnts_count = len(self.points) # Added line name as points set name, so we have to comment the line points out... # for line in self.lines: # pnts_count += len(line["points"]) # 2 points will be automatically deleted and the lines will be closed # pnts_count -= 2 # 2 points have another name (obviously they have a name) pnts_count -= 2 self.assertEqual( len(points), pnts_count, "Wrong point count ({}). Should be {}.".format( len(points), pnts_count)) self.assertEqual( points[0].id, 1, "First point should have id {}, but has {}.".format( 1, points[0].id)) self.assertTrue( math.fabs(float(points[-1].easting) - 1254367) < float_precision, "Wrong easting difference to large ( |{} - {}| = {} > {}).".format( float(points[-1].easting), 1254367, math.fabs(float(points[-1].easting) - 1254367), float_precision)) self.assertTrue( math.fabs(float(points[-1].northing) - 5443636) < float_precision, "Wrong northing difference to large ( |{} - {}| = {} > {}).". format(float(points[-1].northing), 5443636, math.fabs(float(points[-1].northing) - 5443636), float_precision)) del points points = GeoPoint.load_by_name_without_lines_from_db("", self.session) pnts_count = len(self.points) # 2 points have another name (obviously they have a name) pnts_count -= 2 self.assertEqual( len(points), pnts_count, "Wrong point count ({}). Should be {}.".format( len(points), pnts_count)) self.assertEqual( points[0].id, 1, "First point should have id {}, but has {}.".format( 1, points[0].id)) self.assertTrue( math.fabs(float(points[-1].easting) - 1254367) < float_precision, "Wrong easting difference to large ( |{} - {}| = {} > {}).".format( float(points[-1].easting), 1254367, math.fabs(float(points[-1].easting) - 1254367), float_precision)) self.assertTrue( math.fabs(float(points[-1].northing) - 5443636) < float_precision, "Wrong northing difference to large ( |{} - {}| = {} > {}).". format(float(points[-1].northing), 5443636, math.fabs(float(points[-1].northing) - 5443636), float_precision)) self.assertEqual( points[-1].horizon.statigraphic_name, "so", "Wrong horizon ({}). Should be {}.".format( points[-1].horizon.statigraphic_name, "so")) del points # /3/ load points in given extent # x -> 1174000 - 1200000 # y -> 613500 - 651000 # should return points of 2 lines with line-ids 2 (all points) and 4 (1 point) points = GeoPoint.load_in_extent_from_db(self.session, 1174000, 1200000, 613500, 651000) self.assertEqual( len(points), 5, "Wrong number of points ({}), should be {}".format(len(points), 5)) self.assertTrue( math.fabs(float(points[0].easting) - 1179553.6811741155) < float_precision, "Wrong easting difference to large ( |{} - {}| = {} > {}).".format( float(points[0].easting), 1179553.6811741155, math.fabs(float(points[0].easting) - 1179553.6811741155), float_precision)) self.assertTrue( math.fabs(float(points[0].northing) - 647105.5431482664) < float_precision, "Wrong northing difference to large ( |{} - {}| = {} > {}).". format(float(points[0].northing), 647105.5431482664, math.fabs(float(points[0].northing) - 647105.5431482664), float_precision)) self.assertEqual( points[0].horizon.statigraphic_name, "so", "Wrong horizon ({}). Should be {}.".format( points[-1].horizon.statigraphic_name, "so")) del points points = GeoPoint.load_in_extent_without_lines_from_db( self.session, 0, 1, 0, 1) self.assertEqual( len(points), 0, "Wrong number of points ({}), should be {}".format(len(points), 0)) del points