def test_json_wall(self): app = test_app.test_client() with app.session_transaction() as sess: db.drop_all() db.create_all() q = db.session.query(User).filter(User.email == '*****@*****.**') user = q.first() if user is None: example = User() example.firstname = 'userwall' example.lastname = 'theWall' example.email = '*****@*****.**' example.dateofbirth = datetime.datetime(2020, 10, 5) example.is_admin = True example.set_password('daddysflownacrosstheocean') db.session.add(example) db.session.commit() q = db.session.query(User).filter(User.email == '*****@*****.**') user = q.first() q = db.session.query(Story).filter(Story.author_id == user.id) story = q.first() if story is None: example = Story() example.text = 'We dont need no education We dont need no...All in all you re just another brick in the wall' example.likes = 42 example.dislikes = 1 example.dicenumber = 6 example.author_id = user.id db.session.add(example) example = Story() example.text = 'Leaving just a memory...Snapshot in the family album...Daddy what else did you leave for me?' example.likes = 42 example.dislikes = 0 example.dicenumber = 4 example.author_id = user.id db.session.add(example) db.session.commit() q = db.session.query(Story).filter(Story.author_id == user.id) stories = [] thewalltest = Wall(user) for s in q: s: Story thewalltest.add_story(s) stories.append({ 'story_id': s.id, 'text': s.text, 'likes': s.likes, 'dislikes': s.dislikes }) reply = app.get('/thewall/' + str(user.id)) body = json.loads(str(reply.data, 'utf8')) self.assertEqual( body, { "id": user.id, "firstname": user.firstname, "lastname": user.lastname, "email": user.email, "stories": stories # thewalltest.stories }) wall_repl = Wall() wall_repl.acquire_from_json(reply) self.assertEqual(thewalltest.id, wall_repl.id, "Json acquire fail")
def create_app(test=False): app = Flask(__name__, static_url_path='/static') app.config['WTF_CSRF_SECRET_KEY'] = 'A SECRET KEY' app.config['SECRET_KEY'] = 'ANOTHER ONE' app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///storytellers.db' if test: app.config['TESTING'] = True app.config['CELERY_ALWAYS_EAGER'] = True app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:' app.config['WTF_CSRF_ENABLED'] = False app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False celery = celeryApp.make_celery(app) celeryApp.celery = celery for bp in blueprints: app.register_blueprint(bp) bp.app = app db.init_app(app) login_manager.init_app(app) db.create_all(app=app) with app.app_context(): # Create first admin user. q = db.session.query(User).filter(User.email == '*****@*****.**') user = q.first() if user is None: example = User() example.firstname = 'Admin' example.lastname = 'Admin' example.email = '*****@*****.**' example.dateofbirth = datetime.datetime(2020, 10, 5) example.is_admin = True example.set_password('admin') db.session.add(example) db.session.commit() example = Story() example.title = 'My first story!' example.rolls_outcome = '[["bike", "static/Mountain/bike.PNG"], ["bus", "static/Mountain/bus.PNG"]]' example.text = 'With my bike, I am faster than a bus!!!!' example.theme = 'Mountain' example.published = 1 example.likes = 42 example.dislikes = 5 example.author_id = 1 db.session.add(example) db.session.commit() # Create dice sets if missing. themes = retrieve_themes() if not themes: die1 = Die( ['angry', 'bag', 'bike', 'bird', 'crying', 'moonandstars'], "N/A") die2 = Die(['bus', 'coffee', 'happy', 'letter', 'paws', 'plate'], "N/A") die3 = Die( ['caravan', 'clock', 'drink', 'mouth', 'tulip', 'whale'], "N/A") die4 = Die( ['baloon', 'bananas', 'cat', 'icecream', 'pencil', 'phone'], "N/A") dice_set = DiceSet([die1, die2, die3], "Mountain") store_dice_set(dice_set) dice_set = DiceSet([die2, die3, die4], "Late night") store_dice_set(dice_set) dice_set = DiceSet([die3, die1, die4], "Travelers") store_dice_set(dice_set) dice_set = DiceSet([die2, die1, die4], "Youth") store_dice_set(dice_set) die = Die(["1", "2", "3"], "test_theme") dice_set = DiceSet([die], "test_theme") return app