def test_get_card_success(self): state_obj = main.State() user_obj = main.User("myusername", "mypassword", main.School.query.get(1)) main.db.session.add_all(( state_obj, user_obj, main.Card(user_obj, 'q1', 'a1'), main.Card(user_obj, 'q2', 'a2'), )) card_obj_1 = main.Card(user_obj, 'q3', 'a3') card_obj_2 = main.Card(user_obj, 'q4', 'a4') main.db.session.add_all(( card_obj_1, card_obj_2, main.Card(user_obj, 'q5', 'a5') )) state_obj.user = user_obj state_obj._State__current_card_seed = 608731 # [3, 4, 5, 2, 1] main.db.session.commit() print(card_obj_1.id, card_obj_2.id) with self.subTest('get first card'): with self.client as c: c.set_cookie('localhost', 'game-state', state_obj.id) response = c.get(flask.url_for('get_card_api', n=1)) self.assert200(response) self.assertEqual(response.json['id'], card_obj_1.id) with self.subTest('get current card'): with self.client as c: c.set_cookie('localhost', 'game-state', state_obj.id) response = c.get(flask.url_for('get_card_api', n=0)) self.assert200(response) self.assertEqual(response.json['id'], card_obj_1.id) with self.subTest('get next card'): with self.client as c: c.set_cookie('localhost', 'game-state', state_obj.id) response = c.get(flask.url_for('get_card_api', n=1)) self.assert200(response) self.assertEqual(response.json['id'], card_obj_2.id) with self.subTest('get current card (next)'): with self.client as c: c.set_cookie('localhost', 'game-state', state_obj.id) response = c.get(flask.url_for('get_card_api', n=0)) self.assert200(response) self.assertEqual(response.json['id'], card_obj_2.id) with self.subTest('overflow iterator'): state_obj._State__current_card_iter = 5 main.db.session.commit() with self.client as c: c.set_cookie('localhost', 'game-state', state_obj.id) response = c.get(flask.url_for('get_card_api', n=1)) self.assert200(response) self.assertIn(response.json['id'], [i.id for i in main.Card.query.all()])
def test_dice(self): ref = main.State() test_count = 6 for i in range(test_count): player_id = random.randint(0, 3) thing_id = random.randint(0, 3) step = random.randint(1, 6) last = ref.pl[player_id][thing_id] ref = ref.dice(player_id, thing_id, step) self.assertEqual(ref.pl[player_id][thing_id], last + step)
def test_answer_no_cards(self): state_obj = main.State() user_obj = main.User("myusername", "mypassword", main.School.query.get(1)) main.db.session.add_all((state_obj, user_obj)) state_obj.user = user_obj main.db.session.commit() with self.client as c: c.set_cookie('localhost', 'game-state', state_obj.id) response = c.post(flask.url_for('answer_card_api'), data={'a': "a1"}) self.assert400(response)
def test_play_empty(self): state_obj = main.State() user_obj = main.User("myusername", "mypassword", main.School.query.get(1)) main.db.session.add_all((state_obj, user_obj)) state_obj.user = user_obj main.db.session.commit() with self.client as c: c.set_cookie('localhost', 'game-state', state_obj.id) response = c.get(flask.url_for('play')) self.assert400(response)
def test_index_logged_in_redirect(self): state_obj = main.State() user_obj = main.User("myusername", "mypassword", main.School.query.get(1)) state_obj.user = user_obj main.db.session.add_all((state_obj, user_obj)) main.db.session.commit() with self.client as c: c.set_cookie('localhost', 'game-state', state_obj.id) response = c.get(flask.url_for('index')) self.assertRedirects(response, flask.url_for('cards'))
def test_pass_turn(self): ref = main.State() self.assertEqual(ref.turn_player(), 0) ref = ref.pass_turn() self.assertEqual(ref.turn_player(), 1) ref = ref.pass_turn() self.assertEqual(ref.turn_player(), 2) ref = ref.pass_turn() self.assertEqual(ref.turn_player(), 3) ref = ref.pass_turn() self.assertEqual(ref.turn_player(), 0)
def test_answer_missing_arg(self): state_obj = main.State() user_obj = main.User("myusername", "mypassword", main.School.query.get(1)) card_obj = main.Card(user_obj, 'q1', 'a1') main.db.session.add_all((state_obj, user_obj, card_obj)) state_obj.user = user_obj state_obj._State__current_card_iter = 0 main.db.session.commit() with self.client as c: c.set_cookie('localhost', 'game-state', state_obj.id) response = c.post(flask.url_for('answer_card_api')) self.assert400(response)
def test_remove_card_get_fail(self): state_obj = main.State() user_obj = main.User("myusername", "mypassword", main.School.query.get(1)) card_obj = main.Card(user_obj, 'q1', 'a1') main.db.session.add_all((state_obj, user_obj, card_obj)) state_obj.user = user_obj main.db.session.commit() with self.client as c: c.set_cookie('localhost', 'game-state', state_obj.id) response = c.get(flask.url_for('remove_card_api'), data={'id': card_obj.id}) self.assert405(response) self.assertEqual(main.Card.query.filter_by(user=user_obj).count(), 1)
def test_state_handler_existing_state(self): state_obj = main.State() main.db.session.add(state_obj) main.db.session.commit() @main.state_handler def func(state): self.assertIs(state, state_obj) return '', 200 with self.app.test_request_context(headers=Headers([('Cookie', 'game-state={};'.format(state_obj.id))])): response = func() self.assertIn("game-state={}".format(state_obj.id), response.headers['set-cookie'])
def test_add_card_get_fail(self): state_obj = main.State() user_obj = main.User("myusername", "mypassword", main.School.query.get(1)) main.db.session.add_all((state_obj, user_obj)) state_obj.user = user_obj main.db.session.commit() with self.client as c: c.set_cookie('localhost', 'game-state', state_obj.id) response = c.get(flask.url_for('add_card_api'), data={ "q": "q1", "a": "a1" }) self.assertEqual(response.status_code, 405)
def test_answer_card_success(self): state_obj = main.State() user_obj = main.User("myusername", "mypassword", main.School.query.get(1)) card_obj = main.Card(user_obj, 'q1', 'a1') main.db.session.add_all((state_obj, user_obj, card_obj)) state_obj.user = user_obj state_obj._State__current_card_iter = 0 main.db.session.commit() with self.client as c: c.set_cookie('localhost', 'game-state', state_obj.id) response = c.post(flask.url_for('answer_card_api'), data={'a': 'a1'}) self.assert200(response) self.assertTrue(response.json['correct']) self.assertIn('score', response.json)
def test_get_card_no_cards_fail(self): state_obj = main.State() user_obj = main.User("myusername", "mypassword", main.School.query.get(1)) main.db.session.add_all((state_obj, user_obj)) state_obj.user = user_obj main.db.session.commit() with self.client as c: c.set_cookie('localhost', 'game-state', state_obj.id) response = c.get(flask.url_for('get_card_api') + '?n=1') self.assertEqual(response.status_code, 204) with self.client as c: c.set_cookie('localhost', 'game-state', state_obj.id) response = c.get(flask.url_for('get_card_api') + '?n=0') self.assertEqual(response.status_code, 204)
def test_require_login_fail(self): state_obj = main.State() main.db.session.add(state_obj) main.db.session.commit() @main.state_handler @main.require_login def func(state): self.fail('app didn\'t abort') with self.app.test_request_context(headers=Headers([('Cookie', 'game-state={};'.format(state_obj.id))])): try: func() except Forbidden: pass else: self.fail('did not raise Forbidden')
def test_state_card(self): state_obj = main.State() user_obj = main.User("myusername", "mypassword", main.School.query.get(1)) main.db.session.add_all((state_obj, user_obj)) state_obj.user = user_obj state_obj._State__current_card_seed = 259720 # [2, 3, 4, 1] main.db.session.add(main.Card(state_obj.user, "card 1", "1")) card_2 = main.Card(state_obj.user, "card 2", "2") main.db.session.add(card_2) main.db.session.add(main.Card(state_obj.user, "card 3", "3")) main.db.session.add(main.Card(state_obj.user, "card 4", "4")) main.db.session.commit() self.assertIs(state_obj.next_card(), card_2) self.assertIsNotNone(state_obj._State__current_card_iter) self.assertIs(state_obj.card, card_2)
def test_not_move(self): ref = main.State() test_count = 6 for i in range(test_count): player_id = random.randint(0, 3) thing_id = random.randint(0, 3) step = random.randint(1, 6) last = ref ref = ref.dice(player_id, thing_id, step) for j in range(4): if j == player_id: continue for thing in range(4): self.assertEqual(ref.pl[j][thing], last.pl[j][thing]) #este pre hraca ktory hybal for thing in range(4): if thing == thing_id: continue self.assertEqual(ref.pl[player_id][thing], last.pl[player_id][thing])
def test_require_login_success(self): state_obj = main.State() main.db.session.add(state_obj) user_obj = main.User("myusername", "mypassword", main.School.query.get(1)) main.db.session.add(user_obj) state_obj.user = user_obj main.db.session.commit() @main.state_handler @main.require_login def func(state): with self.subTest("test_state_passed"): self.assertIs(state, state_obj) self.assertIsNotNone(state.user) return '', 200 with self.app.test_request_context(headers=Headers([('Cookie', 'game-state={};'.format(state_obj.id))])): response = func() with self.subTest('test_state_passed_through'): self.assertIn("game-state={}".format(state_obj.id), response.headers['set-cookie']) self.assert200(response)