def test_activity_columns(app):
    with app.app_context():
        """
        Tests for required columns activity
        """
        running = Activity()
        running.id = "1234"
        running.name = "running"
        running.intensity = "abs"  # 600kcal per hour

        running.name = None
        db.session.add(running)
        with pytest.raises(IntegrityError):
            db.session.commit()

        db.session.rollback()

        running.id = None
        db.session.add(running)
        with pytest.raises(IntegrityError):
            db.session.commit()

        db.session.rollback()

        running.intensity = None
        db.session.add(running)
        with pytest.raises(IntegrityError):
            db.session.commit()
def test_activity_record_cascade_on_activity(app):
    with app.app_context():
        running = Activity()
        running.id = "1234"
        running.name = "Running"
        running.intensity = 600  # 600kcal per hour
        db.session.add(running)
        db.session.commit()

        runner = Person()
        runner.id = '4566'
        db.session.add(runner)
        db.session.commit()

        ac = ActivityRecord()
        ac.activity = running
        ac.person = runner
        ac.duration = 3600
        ac.timestamp = datetime.datetime.now()

        db.session.add(ac)
        db.session.commit()

        db.session.delete(running)
        db.session.commit()
        fetched = ActivityRecord.query.filter(
            ActivityRecord.activity_id == running.id).first()
        assert (fetched is None)
def test_activity_record_creation(app):
    with app.app_context():
        running = Activity()
        running.id = "1234"
        running.name = "Running"
        running.intensity = 600  # 600kcal per hour
        db.session.add(running)
        db.session.commit()

        runner = Person()
        runner.id = '4566'
        db.session.add(runner)
        db.session.commit()

        ac = ActivityRecord()
        ac.activity = running
        ac.person = runner
        ac.duration = 3600
        ac.timestamp = datetime.datetime.now()

        db.session.add(ac)
        db.session.commit()

        fetched = ActivityRecord.query.filter(
            ActivityRecord.person_id == runner.id).first()
        assert (fetched.activity == running)

        # extensive cleanup, will work even when cascade might be broken
        db.session.delete(ac)
        db.session.delete(running)
        db.session.delete(runner)