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)
def test_activity_record_cascade_many(app): with app.app_context(): # make sure we can have many records for one person # and many records for one activity p1 = Person(id="333") p2 = Person(id="444") a1 = Activity(id="123", name="act1", intensity=200) a2 = Activity(id="456", name="act2", intensity=200) entities = [p1, p2, a1, a2] p1a1 = ActivityRecord() p1a1.person = p1 p1a1.activity = a1 p1a1.duration = 100 p1a1.timestamp = datetime.datetime.now() entities.append(p1a1) p1a2 = ActivityRecord() p1a2.person = p1 p1a2.activity = a2 p1a2.duration = 100 p1a2.timestamp = datetime.datetime.now() entities.append(p1a2) p2a1 = ActivityRecord() p2a1.person = p2 p2a1.activity = a1 p2a1.duration = 100 p2a1.timestamp = datetime.datetime.now() entities.append(p2a1) p2a2 = ActivityRecord() p2a2.person = p2 p2a2.activity = a2 p2a2.duration = 100 p2a2.timestamp = datetime.datetime.now() entities.append(p2a2) for i in entities: db.session.add(i) db.session.commit() db.session.delete(p1) db.session.delete(a1) db.session.commit() fetched = ActivityRecord.query.filter(Person.id == p1.id).first() assert (fetched is None) fetched = ActivityRecord.query.filter(Activity.id == a1.id).first() assert (fetched is None) fetched = ActivityRecord.query.filter(Person.id == p2.id).first() assert (fetched is not None) fetched = ActivityRecord.query.filter(Activity.id == a2.id).first() assert (fetched is not None) for i in entities: db.session.delete(i) db.session.commit()
def test_activity_unique(app): with app.app_context(): same_id = "123456" a1 = Activity(id=same_id, name="Running", intensity=600) a2 = Activity(id=same_id, name="Running-hard", intensity=800) db.session.add(a1) db.session.add(a2) with pytest.raises(IntegrityError): db.session.commit()
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_creation(app): with app.app_context(): activity = Activity(id="123", name="Running", intensity=600) db.session.add(activity) db.session.commit() # And with optional fields a = Activity(id="345", name="Running-hard", intensity=800) a.description = "A harder exercise containing a continuous high heart beat running training" db.session.add(a) db.session.commit() db.session.delete(activity) db.session.delete(a) db.session.commit()