def main(): config = pg_config.Config.from_env(os.environ) session = pg.connect( "host={host} user={username} password={password} dbname={database}". format_map({ "host": config.host, "username": config.username, "password": config.password, "database": config.database })) dao = Dao(session) ps = point_set.PointSet() ps.name = "Test Point Set" ps.created = utils.now() point_list = create_random_points(100, 100) ps.points = point_list try: ps = dao.save_point_set(ps) except Exception as e: sys.stderr.write("error saving point_set: {}\n".format(e, )) os.exit(1) print(str(ps)) try: ps = dao.get_point_set_by_id(ps.id) except Exception as e: sys.stderr.write("error getting point_set of id = {}: {}\n".format( ps.id, e)) os.exit(1) print(str(ps))
def main(): config = dao_config.Config.from_env(os.environ) dao = dao_module.create_dao(config) ps = point_set.PointSet() ps.points = create_random_points(10, 10) ps.created = utils.now() ps.name = "Hello SQLAlchemy" ps = dao.save_or_update_point_set(ps) ps = dao.get_point_set_by_id(ps.id) print(utils.to_json(ps))
def get_point_set_by_id(self, id: int): sql = "select id, name, created from point_set where id = %s" cursor = self.__session.cursor() try: cursor.execute(sql, (id, )) result_set = cursor.fetchone() ps = point_set.PointSet() ps.id = result_set[0] ps.name = result_set[1] ps.created = result_set[2] ps.points = self.get_points_by_point_set_id(id) return ps finally: cursor.close()
def main(): point_set = ps.PointSet() point_set.name = "My Point Set" point_set.id = 40 point_set.created = point_utils.now() for i in range(0, 10): point = p.Point() point.point_set_id = point_set.id point.x = random.random() point.y = random.random() point.x = random.random() point_set.points.append(point) #sys.stdout.write(str(point_set)) sys.stdout.write(point_utils.to_json(point_set))
def test_point_set_number_of_points(self): ps = point_set.PointSet() self.assertEqual( ps.number_of_points(), 0, "expected number_of_points to be 0 on newly instantiated point_set" ) ps.points = [] self.assertEqual( ps.number_of_points(), 0, "expected number_of_points to be 0 on point_set.points on empty point set" ) ps.points = [point.Point()] self.assertEqual( ps.number_of_points(), 1, "expected number_of_points to be 1 after adding a point")
def test_point_set_to_dict(self): ps = point_set.PointSet() ps.created = point_utils.now() ps.name = "Test Case" ps.id = 448 point_one = point.Point.from_dict({ "x": 409.39, "y": 348.44, "z": 54.33, "point_set_id": 448 }) ps.points = [point_one] data = ps.to_json_dict() date_string = data["created"] date_time = point_utils.string_to_datetime(date_string) self.assertEqual(data["id"], ps.id, "expected the id to be the same") self.assertEqual(data["name"], ps.name, "expected the name to be the same") self.assertEqual(date_time, ps.created, "expected created at date to match")