Example #1
0
def test_all_ready(client):
    run_db_script("test_all_ready.sql")

    res = client.get('/ask-if-ready', follow_redirects=True)

    assert b"READY" in res.data
    assert b"NOT" not in res.data
Example #2
0
def test_serve_question(client):
    run_db_script("test_serve_question.sql")

    with startup.get_app().app_context():  # For DB access

        # Test a random question
        # TODO: Test all questions?
        question = cah.db.get_db().execute(
            'SELECT question FROM current_question').fetchone()['question']

        question = html_escape(question)
        question = bytes(question, 'utf-8')

        # Case for regular player (player 1 or 2 in sql script)
        with client.session_transaction() as test_session:
            test_session['username'] = "******"

        res = client.get('/question-phase', follow_redirects=True)

        assert question in res.data

        # Case for judge (player 3 in sql script)
        with client.session_transaction() as test_session:
            test_session['username'] = "******"

        res = client.get('/question-phase', follow_redirects=True)
        assert b"the best answer" in res.data
        assert question in res.data
Example #3
0
def test_submit_answer_not_all_answered(client):
    # Borrowing from the first test. It sets up
    # three players, one of which hasn't answered.
    # This is exactly what we need for this test too.
    run_db_script("test_get_answers.sql")
    with client.session_transaction() as test_session:
        test_session['username'] = "******"

    res = client.post('/submit-answer',
                      data={"answer": "THIS IS A TEST"},
                      follow_redirects=True)

    with startup.get_app().app_context():
        recs = cah.db.get_db().execute(
            "SELECT name, answer FROM players").fetchall()

    # Check in both the db and the response data
    # since we don't render from db information
    [ player_2_answer ] = [ x['answer'] for x in recs \
                            if x['name'] == "player 2"]

    assert "A TEST" in player_2_answer
    assert b"A TEST" in res.data

    # Check that we didn't modify db in an incorrect manner
    [ player_3_rec ] = [ x['answer'] for x in recs  \
                         if x['name'] == "player 3" ]

    assert None == player_3_rec
Example #4
0
def test_select_winner(client):
  run_db_script("test_select_winner.sql")
  
  with client.session_transaction() as test_session:
    test_session['username'] = '******' # Set to judge in sql file

  res = client.post('/select-winner',
              data = {"answer-button":"Player 1 answer"},
              follow_redirects = True)
  assert b'<td> player 1 </td>' in res.data
  assert b'<td> 7 </td>' in res.data
Example #5
0
def test_get_answers(client):
    # TODO: Should /get-answers really be dependent on a session?
    run_db_script("test_get_answers.sql")

    with client.session_transaction() as test_session:
        test_session['username'] = "******"

    res = client.get('/get-answers')

    # TODO: Right now, get-answers doesn't give the answer
    #       for the player within the current session.
    #       This would go away with the removal of session
    #       dependence.
    # assert b'PLAYER 1 ANSWER' in res.data
    # TODO: Check for no nulls
    assert b'PLAYER 2 ANSWER' in res.data
Example #6
0
def test_ready(client):

    run_db_script("test_ready.sql")

    with client.session_transaction() as test_session:
        test_session['username'] = '******'

    res = client.post('/submit-ready', follow_redirects=True)
    assert b'asdf is ready' in res.data

    with startup.get_app().app_context():

        # Check ready and answer flags are properly set
        rec = cah.db.get_db().execute("SELECT ready, answer FROM players"
                                      " WHERE name = 'asdf'").fetchone()
        assert rec['ready'] == 1
        assert rec['answer'] == None
Example #7
0
def test_all_answered(client):
    run_db_script("test_all_answered.sql")

    with client.session_transaction() as test_session:
        test_session['username'] = "******"
    res = client.get('/all-answered')

    assert b'ALL ANSWERED' in res.data

    with startup.get_app().app_context():
        cah.db.get_db().execute("UPDATE players"
                                " SET answer = NULL"
                                " WHERE name = 'player 3'")
        cah.db.get_db().commit()

    res = client.get('/all-answered')

    assert b'NOT ALL ANSWERED' in res.data
Example #8
0
def test_judge_answered(client):
  run_db_script("test_judge_answered.sql")
  
  res = client.get('/judge-answered', follow_redirects = True)

  assert b'DID NOT ANSWER' in res.data

  with startup.get_app().app_context():
    cah.db.get_db().execute(
      "UPDATE players"
      " SET ANSWER = 'Player 3 answer'"
      " WHERE judge = 1"
    )
    cah.db.get_db().commit()

  res = client.get('/judge-answered', follow_redirects = True)

  assert b'ANSWERED' in res.data    
Example #9
0
def test_ready_with_updates(client):
    # Similar to the test above, but all playrs will be ready
    # once the post is sent, so the judge and question should
    # be updated.
    run_db_script("test_ready_with_updates.sql")

    # Get all information before our post switches to
    # a new round, which involves a lot of changes to
    # the database.
    initial_judge = ""
    initial_question = ""
    inital_player_recs = []
    with startup.get_app().app_context():

        initial_judge = cah.db.get_db().execute(
            'SELECT name FROM players'
            ' WHERE judge = 1').fetchone()['name']

        initial_question = cah.db.get_db().execute(
            'SELECT question FROM current_question').fetchone()['question']
        with client.session_transaction() as test_session:
            test_session['username'] = "******"

        res = client.post('/submit-ready', follow_redirects=True)
        assert b'asdf is ready' in res.data

        # Check ready and answer flags are properly set
        rec = cah.db.get_db().execute("SELECT ready, answer FROM players"
                                      " WHERE name = 'asdf'").fetchone()
        assert rec['ready'] == 1
        assert rec['answer'] == None

        # Check that the question has changed.
        new_question = cah.db.get_db().execute(
            "SELECT question FROM current_question").fetchone()['question']

        assert new_question != initial_question

        # Check that the judge has changed.
        new_judge = cah.db.get_db().execute(
            "SELECT name FROM players"
            " WHERE judge = 1").fetchone()['name']

        assert new_judge != initial_judge