Example #1
0
 def execute(self, session):
     vector1 = Vector(start_point='A', end_point='B')
     vector2 = Vector(start_point='D', end_point='C')
     session.add_all(
         [vector1, vector2,
          Angle(vector1=vector1, vector2=vector2)])
     session.commit()
Example #2
0
 def shape(self, args):
     shape = args[0]
     points = args[1]
     if shape == 'line':
         if len(points) < 2:
             raise ValueError(
                 "The shape 'line' should have 2 points or more")
         vectors = [
             Vector(start_point=point1, end_point=point2)
             for point1, point2 in combinations(points, 2)
         ]
         angles = [
             Angle(vector1=vec1, vector2=vec2, size=180)
             for vec1, vec2 in combinations(vectors, 2)
         ]
         res = frozenset(chain(vectors, angles))
         return res
     elif shape == 'polygon':
         if len(points) < 3:
             raise ValueError(
                 "The shape 'polygon' should have 3 points or more")
         vectors = [
             Vector(start_point=points[i],
                    end_point=points[(i + 1) % len(points)])
             for i in range(len(points))
         ]
         angles = [
             Angle(vector1=vectors[i],
                   vector2=vectors[(i + 1) % len(vectors)])
             for i in range(len(vectors))
         ]
         res = frozenset(chain(vectors, angles))
         return res
     raise ValueError(f"Unknown shape: '{shape}'")
Example #3
0
def test_ReverseAngle_on_null_empty_size_not_adding(sqlalchemy_information,
                                                    memory_session):
    angle = Angle(vector1=Vector(start_point='A', end_point='B'),
                  vector2=Vector(start_point='C', end_point='D'))

    memory_session.add(angle)
    memory_session.commit()

    tested_rule = ReverseAngle()

    sqlalchemy_information.execute(tested_rule)

    assert len(memory_session.query(Angle).all()) == 1
Example #4
0
def test_SumVectors_on_second_empty_adds_nothing(sqlalchemy_information,
                                                 memory_session):
    vector1 = Vector(start_point='A', end_point='B', length=2)
    vector2 = Vector(start_point=vector1.end_point, end_point='C')
    angle = Angle(vector1=vector1, vector2=vector2, size=180)

    memory_session.add_all([angle, vector1, vector2])
    memory_session.commit()

    tested_rule = SumVectors()

    sqlalchemy_information.execute(tested_rule)

    assert len(memory_session.query(Angle).all()) == 1
    assert len(memory_session.query(Vector).all()) == 2
Example #5
0
def test_ReverseAngle(sqlalchemy_information, memory_session):
    angle = Angle(vector1=Vector(start_point='A', end_point='B'),
                  vector2=Vector(start_point='C', end_point='D'),
                  size=30)

    memory_session.add(angle)
    memory_session.commit()

    tested_rule = ReverseAngle()

    sqlalchemy_information.execute(tested_rule)

    assert len(memory_session.query(Angle).all()) == 2
    result = memory_session.query(Angle).filter_by(
        vector1=angle.vector2, vector2=angle.vector1).first()
    assert result.size == 360 - angle.size
Example #6
0
def test_SumAngles_on_second_empty_adds_nothing(sqlalchemy_information,
                                                memory_session):
    angle1 = Angle(vector1=Vector(start_point='A', end_point='B'),
                   vector2=Vector(start_point='C', end_point='D'),
                   size=30)
    angle2 = Angle(vector1=angle1.vector2,
                   vector2=Vector(start_point='E', end_point='F'))

    memory_session.add_all([angle1, angle2])
    memory_session.commit()

    tested_rule = SumAngles()

    sqlalchemy_information.execute(tested_rule)

    assert len(memory_session.query(Angle).all()) == 2
Example #7
0
def test_SumVectors(sqlalchemy_information, memory_session):
    vector1 = Vector(start_point='A', end_point='B', length=2)
    vector2 = Vector(start_point=vector1.end_point, end_point='C', length=3)
    angle = Angle(vector1=vector1, vector2=vector2, size=180)

    memory_session.add_all([angle, vector1, vector2])
    memory_session.commit()

    tested_rule = SumVectors()

    sqlalchemy_information.execute(tested_rule)

    assert len(memory_session.query(Angle).all()) == 1
    assert len(memory_session.query(Vector).all()) == 3
    result = memory_session.query(Vector).filter_by(
        start_point=vector1.start_point, end_point=vector2.end_point).first()
    assert result.length == vector1.length + vector2.length
Example #8
0
 def execute(self, session: Session):
     vectors = session.query(Vector).all()
     mapper = lambda vec: Vector(start_point=vec.end_point,
                                 end_point=vec.start_point,
                                 length=vec.length)
     new_vectors = list(map(mapper, vectors))
     session.add_all(new_vectors)
     session.commit()
Example #9
0
def test_SumAngles(sqlalchemy_information, memory_session):
    angle1 = Angle(vector1=Vector(start_point='A', end_point='B'),
                   vector2=Vector(start_point='C', end_point='D'),
                   size=30)
    angle2 = Angle(vector1=angle1.vector2,
                   vector2=Vector(start_point='E', end_point='F'),
                   size=60)

    memory_session.add_all([angle1, angle2])
    memory_session.commit()

    tested_rule = SumAngles()

    sqlalchemy_information.execute(tested_rule)

    assert len(memory_session.query(Angle).all()) == 3
    result = memory_session.query(Angle).filter_by(
        vector_id1=angle1.vector_id1, vector_id2=angle2.vector_id2).first()
    assert result.size == angle1.size + angle2.size
Example #10
0
def test_ReverseVector(sqlalchemy_information, memory_session):
    vector = Vector(start_point='A', end_point='B', length=1)

    memory_session.add(vector)
    memory_session.commit()

    tested_rule = ReverseVector()

    sqlalchemy_information.execute(tested_rule)

    assert len(memory_session.query(Vector).all()) == 2
    result = memory_session.query(Vector).get(vector.id)
    assert result.length == vector.length
Example #11
0
 def execute(self, session: Session):
     vector1 = aliased(Vector, name="vector1")
     vector2 = aliased(Vector, name="vector2")
     angle = aliased(Angle)
     vectors = session.query(vector1.start_point, vector2.end_point, vector1.length + vector2.length)\
     .filter(and_(angle.size == 180, vector1.end_point == vector2.start_point,
                  angle.vector_id1 == vector1.id, angle.vector_id2 == vector2.id,
                  None != vector1.length, None != vector2.length
                  )).all()
     mapper = lambda vec: Vector(
         start_point=vec[0], end_point=vec[1], length=vec[2])
     new_vectors = list(map(mapper, vectors))
     session.add_all(new_vectors)
     session.commit()
Example #12
0
 def execute(self, session):
     session.add(Vector(start_point='A', end_point='B'))
     session.commit()
Example #13
0
 def execute(self, session):
     vector = Vector(start_point='A', end_point='B')
     session.add_all([vector, Angle(vector1=vector, size=-1)])
     session.commit()
Example #14
0
 def execute(self, session):
     session.add(Angle(vector2=Vector(start_point='B', end_point='C')))
     session.commit()