def test_time_field(self): #Test for ticket #12059: TimeField wrongly handling datetime.datetime object. d = Donut(name='Apple Fritter') d.baked_time = datetime.datetime(year=2007, month=4, day=20, hour=16, minute=19, second=59) d.save() d2 = Donut.objects.get(name='Apple Fritter') self.assertEqual(d2.baked_time, datetime.time(16, 19, 59))
def test_boolean_type(self): d = Donut(name='Apple Fritter') self.assertFalse(d.is_frosted) self.assertTrue(d.has_sprinkles is None) d.has_sprinkles = True self.assertTrue(d.has_sprinkles) d.save() d2 = Donut.objects.get(name='Apple Fritter') self.assertFalse(d2.is_frosted) self.assertTrue(d2.has_sprinkles)
def add_donut(token): body = request.get_json() donut_title = body.get('donut_title', None) if donut_title is None: abort(422) try: donut = Donut(title=donut_title) donut.insert() return jsonify({ 'status': 200, 'success': True, }) except Exception: abort(422)
def initialize_database(app, db): """Drop and restore database in a consistent state""" with app.app_context(): db.drop_all() db.create_all() # create beers db.session.add(Beer('ipa', 10)) db.session.add(Beer('pilsner', 8)) db.session.add(Beer('lager', 8)) db.session.add(Beer('stout', 7)) db.session.commit() # create donuts db.session.add(Donut('jelly')) db.session.add(Donut('glazed')) db.session.add(Donut('chocolate')) db.session.add(Donut('bavarian')) db.session.commit()
def setUp(self): """Make demo data.""" Donut.query.delete() donut = Donut(**DONUT_DATA) db.session.add(donut) db.session.commit() self.donut = donut
def test_error_on_timezone(self): """Regression test for #8354: the MySQL and Oracle backends should raise an error if given a timezone-aware datetime object.""" dt = datetime.datetime(2008, 8, 31, 16, 20, tzinfo=tzinfo.FixedOffset(0)) d = Donut(name='Bear claw', consumed_at=dt) self.assertRaises(ValueError, d.save)
def create_donut(): """Add a donut, and return data about the new donut. Return JSON like: {donut: [{id, flavor, rating, size, image}]} """ data = request.json donut = Donut( flavor=data['flavor'], rating=data['rating'], size=data['size'], image=data['image'] or None, ) db.session.add(donut) db.session.commit() return (jsonify(donut=donut.to_dict()), 201)
def test_tz_awareness_mysql(self): # Regression test for #8354: the MySQL backend should raise an error if given # a timezone-aware datetime object. if settings.DATABASES[DEFAULT_DB_ALIAS][ 'ENGINE'] == 'django.db.backends.mysql': dt = datetime.datetime(2008, 8, 31, 16, 20, tzinfo=tzinfo.FixedOffset(0)) d = Donut(name='Bear claw', consumed_at=dt) self.assertRaises(ValueError, d.save)
def test_date_type(self): d = Donut(name='Apple Fritter') d.baked_date = datetime.date(year=1938, month=6, day=4) d.baked_time = datetime.time(hour=5, minute=30) d.consumed_at = datetime.datetime(year=2007, month=4, day=20, hour=16, minute=19, second=59) d.save() d2 = Donut.objects.get(name='Apple Fritter') self.assertEqual(d2.baked_date, datetime.date(1938, 6, 4)) self.assertEqual(d2.baked_time, datetime.time(5, 30)) self.assertEqual(d2.consumed_at, datetime.datetime(2007, 4, 20, 16, 19, 59))
# from app import app from models import db, Donut db.drop_all() db.create_all() c1 = Donut( flavor="cherry", size="large", rating=5, ) c2 = Donut( flavor="chocolate", size="small", rating=9, image="https://bargainhuntingmomma.files.wordpress.com/2014/05/donut.jpg") db.session.add_all([c1, c2]) db.session.commit()