Пример #1
0
def test_relation(app):
    '''A simple test of adding an object with relation
    '''

    map1 = Map(name="map1")
    beam1 = Beam(name="beam1", map=map1)

    # The relation has been already established
    assert map1 is beam1.map
    assert [beam1] == map1.beams

    # The primary and foreign keys are still None
    assert map1.map_id is None
    assert beam1.beam_id is None
    assert beam1.input_map_id is None

    with app.app_context():
        sa.session.add(map1)
        sa.session.commit()

        # The primary keys are assigned
        assert map1.map_id is not None
        assert beam1.beam_id is not None

        # The foreign key is correctly set
        assert map1.map_id == beam1.input_map_id

    with app.app_context():
        map1 = Map.query.filter_by(name='map1').first()
        beam1 = Beam.query.filter_by(name='beam1').first()

        # The relation is preserved in a different app context
        assert map1 is beam1.map
        assert beam1 is map1.beams[0]
        assert map1.map_id == beam1.input_map_id
Пример #2
0
def test_add(app):
    '''A simple test of adding an object with a date field
    '''

    # date_posted needs to be initialized with a datetime.date
    date_posted = datetime.date(2019, 2, 23)
    map1 = Map(name="map1", date_posted=date_posted)

    with app.app_context():
        sa.session.add(map1)
        sa.session.commit()

    with app.app_context():
        map1 = Map.query.filter_by(name='map1').first()
        assert datetime.date(2019, 2, 23) == map1.date_posted
Пример #3
0
def test_add_raise(app):
    '''A simple test of adding an object with a wrong type
    '''

    # It is not impossible to instnaiate a date field with a wrong
    # type, e.g, str
    map1 = Map(name="map1", date_posted="2019-02-13")

    with app.app_context():

        # It is also possible to add
        sa.session.add(map1)

        # However, it is not possible to commit
        with pytest.raises(sqlalchemy.exc.StatementError):
            sa.session.commit()
Пример #4
0
def test_python_object(app):
    '''A simple test about Python object
    '''

    map1 = Map(name="map1")

    with app.app_context():
        sa.session.add(map1)
        sa.session.commit()

        map1_ = Map.query.filter_by(name='map1').first()

        # the query returns the same Python object
        assert map1 is map1_

    with app.app_context():
        map1_ = Map.query.filter_by(name='map1').first()

        # In a different app context, no longer the same Python object
        assert map1 is not map1_
Пример #5
0
def test_primary_key(app):
    '''A simple test about the primary key
    '''

    map1 = Map(name="map1")

    # The primary key (map_id) is None at this point
    assert map1.map_id is None

    with app.app_context():
        sa.session.add(map1)
        sa.session.commit()

        # After the commit, map_id is automatically assigned
        map_id = map1.map_id
        assert map_id is not None

    with app.app_context():

        # The object can be retrived by the map_id in another context
        map1 = Map.query.filter_by(map_id=map_id).first()
        assert 'map1' == map1.name
Пример #6
0
def test_backref(app):

    # the operations here don't need be within the app context.

    # 1. create a map
    map1 = Map(name="map1")

    # no beams are associated with the map
    assert [] == map1.beams


    # 2. create a beam with the map
    beam1 = Beam(name="beam1", map=map1)

    # the map is associated with the beam
    assert map1 is beam1.map

    # the beam is referenced back by the map
    assert [beam1] == map1.beams

    # the beam has no child beam
    assert 0 == len(beam1.child_beams)


    # 3. create another beam with the map and the beam
    beam2 = Beam(name="beam2", map=map1, parent_beam=beam1)

    # the map is also associated with the 2nd beam
    assert map1 is beam2.map

    # the two beams are referenced back by the map
    assert [beam1, beam2] == map1.beams

    # the 1st beam is referenced by the 2nd beam
    assert beam1 is beam2.parent_beam

    # the 2nd beam is referenced back by the 1st beam
    assert [beam2] == beam1.child_beams
Пример #7
0
def test_simple(app):
    '''A simple test of adding an object
    '''

    with app.app_context():

        # save the initial number of the maps to compare later
        nmaps = len(Map.query.all())

    # this instantiation doesn't need be within a app context
    map1 = Map(name="map1")

    with app.app_context():
        sa.session.add(map1)
        sa.session.commit()

    with app.app_context():

        # test the number of the maps is increased by one
        assert (nmaps + 1) == len(Map.query.all())

        # the new map can be retrieved in a different app context
        map1_ = Map.query.filter_by(name='map1').first()
        assert isinstance(map1_, Map)