def app(): _app = create_app(TestConfig) ctx = _app.test_request_context() ctx.push() yield _app ctx.pop()
def test_init(self): global _app if _app is None: tested_app = create_app(debug=True) _app = tested_app else: tested_app = _app with tested_app.test_client() as client: db = Database() db.initDiceSets("dice/resources") db.remove_all_dice_in_set("basic") db.remove_all_dice_in_set("halloween") db.initDiceCollection("basic") db.initDiceCollection("halloween") reply = client.get("/") body = json.loads(str(reply.data, 'utf8')) expected = {"message": "App started"} self.assertEqual(body, expected)
def test_available_sets(self): global _app if _app is None: tested_app = create_app(debug=True) _app = tested_app else: tested_app = _app with tested_app.test_client() as client: db = Database() db.initDiceSets("dice/resources") db.remove_all_dice_in_set("basic") db.remove_all_dice_in_set("halloween") db.initDiceCollection("basic") db.initDiceCollection("halloween") # get available dice sets reply = client.get("/dicesets") body = json.loads(str(reply.data, 'utf8')) available_dicesets = body["available_dicesets"] self.assertEqual(len(available_dicesets), 2)
def test_production_config(): app = create_app(ProdConfig) assert app.config['ENV'] == 'prod' assert app.config['DEBUG'] is False assert app.config['DEBUG_TB_ENABLED'] is False assert app.config['ASSETS_DEBUG'] is False
def test_dev_config(): app = create_app(DevConfig) assert app.config['ENV'] == 'dev' assert app.config['DEBUG'] is True assert app.config['ASSETS_DEBUG'] is True
#!/usr/bin/env python # -*- coding: utf-8 -*- import os from flask_script import Manager, Shell, Server from flask_script.commands import Clean, ShowUrls from flask_migrate import MigrateCommand from dice.app import create_app from dice.models.user import User from dice.settings import DevConfig, ProdConfig from dice.database import db if os.environ.get("DICE_ENV") == 'prod': app = create_app(ProdConfig) else: app = create_app(DevConfig) HERE = os.path.abspath(os.path.dirname(__file__)) TEST_PATH = os.path.join(HERE, 'tests') manager = Manager(app) def _make_context(): """Return context dict for a shell session so you can access app, db, and the User model by default. """ return {'app': app, 'db': db, 'User': User} @manager.command
def test_roll(self): global _app if _app is None: tested_app = create_app(debug=True) _app = tested_app else: tested_app = _app with tested_app.test_client() as client: db = Database() db.initDiceSets("dice/resources") db.remove_all_dice_in_set("basic") db.remove_all_dice_in_set("halloween") db.initDiceCollection("basic") db.initDiceCollection("halloween") # non-integer dice number reply = client.get('/rolldice/pippo/basic') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(reply.status_code, 200) self.assertEqual( body, { "message": "Dice number needs to be an integer!", "dice_set": "", "roll": [], }) # wrong dice number (<0) reply = client.get('/rolldice/0/basic') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(reply.status_code, 200) self.assertEqual(body, { "message": "Wrong dice number!", "dice_set": "", "roll": [], }) # wrong dice number (> len diceset) reply = client.get('/rolldice/12/basic') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(reply.status_code, 200) self.assertEqual(body, { "message": "Wrong dice number!", "dice_set": "", "roll": [], }) # non-existing dice set reply = client.get('/rolldice/6/pippo') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(reply.status_code, 200) self.assertEqual( body, { "message": "Dice set pippo doesn't exist!", "dice_set": "", "roll": [], }) # correct roll reply = client.get('/rolldice/5/basic') body = json.loads(str(reply.data, 'utf8')) self.assertEqual(reply.status_code, 200) dice_set = body["dice_set"] roll = body["roll"] self.assertEqual(dice_set, "basic") self.assertEqual(len(roll), 5)