def get(self, board_id=None): """ View a board. """ if board_id: board = Board.get(id=board_id) if isinstance(board, dict): return { 'status': 'fail', 'message': 'The board does not exist.', 'help': 'Ensure board_id is of an existent board.' }, 404 else: return { 'status': 'success', 'data': { 'board': board.view() } }, 200 else: boards = Board.get_all() if isinstance(boards, dict): return { 'status': 'fail', 'message': 'No boards exist.', 'help': 'Ensure there are boards in the database.' }, 404 else: return { 'status': 'success', 'data': { 'boards': [board.view() for board in boards] } }, 200
def post(self): """ Create a board. """ payload = request.get_json() required = ['members'] result = validate_json(required, payload, empty=True) if isinstance(result, bool) is False: return { 'status': 'fail', 'message': 'Members list required.', 'help': 'It can be empty if only oneself is a member.' }, 400 else: members = [User.get(id=i) for i in payload['members']] for i in members: if isinstance(i, dict): return { 'status': 'fail', 'message': 'The user does not exist.', 'missing_user': payload['members'][members.index(i)] }, 404 board = Board() board.insert('members', members) return {'status': 'success', 'data': {'board': board.view()}}, 201
def test_delete_board(self): self.board1.save() self.estate1.save() self.unit1.save() board1 = Board.get(id=1) board1.insert('estates_owned', [Estate.get(id=1)]) board1.units_owned.append(Unit.get(id=1)) self.assertEqual(True, Board.get(id=1).delete()) self.assertEqual(True, isinstance(Board.get(id=1), dict))
def test_add_and_remove_users_from_board(self): self.board1.save() self.user1.save() self.user2.save() board1 = Board.get(id=1) board1.insert('members', [User.get(id=1), User.get(id=2)]) self.assertEqual(2, len(Board.get(id=1).members)) board1.remove('members', id=1) self.assertEqual(1, len(Board.get(id=1).members))
def setUp(self): """ Set up test application. """ self.app = create_app('testing') self.client = self.app.test_client() self.client.headers = {'Authorization': getenv('TEST_TOKEN')} self.app_context = self.app.app_context() self.app_context.push() db.drop_all() db.create_all() self.board1 = Board() self.board2 = Board() self.conversation1 = Conversation() self.conversation2 = Conversation() self.deposit1 = Deposit() self.deposit2 = Deposit() self.estate1 = Estate(address="Random Address 1") self.estate2 = Estate(address="Random Address 2") self.estate3 = Estate() self.headers = {'Authorization': getenv('TEST_TOKEN')} self.message1 = Message(sender=1, content='Random Content') self.message2 = Message(sender=2, content='Random Content') self.message3 = Message() self.payment1 = Payment() self.payment2 = Payment() self.role1 = Role(title='basic') self.role2 = Role(title='admin') self.role3 = Role(title='super_admin') self.role4 = Role() self.unit1 = Unit(name="Random Unit 1") self.unit2 = Unit(name="Random Unit 2") self.unit3 = Unit() self.user1 = User(name="First1 Middle1 Last1", phone_number="000 12 3456781", email="*****@*****.**", password=digest('ABC123!@#')) self.user2 = User(name="First2 Middle2 Last2", phone_number="000 12 3456782", email="*****@*****.**", password=digest('ABC123!@#')) self.user3 = User() self.user_new_data1 = {'phone_number': "000 12 3456783"} self.user_new_data2 = {'bad_field': "random"} self.wallet1 = Wallet() self.wallet2 = Wallet() self.user1_dict = { "name": "First1 Middle1 Last1", "phone_number": "000 12 3456781", "email": "*****@*****.**", "password": "******" } self.conversation3_dict = {"participants": [1, 2]} self.message4_dict = {"content": "New Message."} self.board3_dict = {'members': [1, 2]}
def test_add_and_remove_estates_owned_to_board(self): self.board1.save() self.estate1.save() self.estate2.save() board1 = Board.get(id=1) board1.insert('estates_owned', [Estate.get(id=1), Estate.get(id=2)]) estates = Board.get(id=1).estates_owned self.assertEqual("Random Address 1", estates[0].address) self.assertEqual("Random Address 2", estates[1].address) self.assertEqual(2, len(estates)) self.assertEqual(True, isinstance(estates[0], Estate)) self.assertEqual(True, isinstance(estates[1], Estate)) board1.remove('estates_owned', id=1) self.assertEqual(1, len(Board.get(id=1).estates_owned))
def test_add_and_remove_units_owned_to_board(self): self.board1.save() self.unit1.save() self.unit2.save() board1 = Board.get(id=1) board1.insert('units_owned', [Unit.get(id=1), Unit.get(id=2)]) units = Board.get(id=1).units_owned self.assertEqual("Random Unit 1", units[0].name) self.assertEqual("Random Unit 2", units[1].name) self.assertEqual(2, len(units)) self.assertEqual(True, isinstance(units[0], Unit)) self.assertEqual(True, isinstance(units[1], Unit)) board1.remove('units_owned', id=1) self.assertEqual(1, len(Board.get(id=1).units_owned))
def test_get_estate_board(self): self.estate1.save() self.board1.save() board1 = Board.get(id=1) board1.insert('estates_owned', [Estate.get(id=1)]) estate1 = Estate.get(id=1) self.assertEqual(1, estate1.board_id)
def test_chess_board_from_uuid(): """ Test a python-chess Board can be fully recovered from a Board model instance by providing the game uuid """ # Create board and make two moves chess_board = chess.Board() board_instance = Board.from_fen(chess_board.fen()) board_instance.save() first_move = "e2e4" second_move = "e7e5" services.move_piece(board_instance, first_move[:2], first_move[2:], chess_board=chess_board) services.move_piece(board_instance, second_move[:2], second_move[2:], chess_board=chess_board) new_chess_board = services.chess_board_from_uuid(board_instance.game_uuid) assert chess_board.ep_square == int(new_chess_board.ep_square) assert chess_board.castling_rights == int(new_chess_board.castling_rights) assert chess_board.turn == new_chess_board.turn assert chess_board.fullmove_number == new_chess_board.fullmove_number assert chess_board.halfmove_clock == new_chess_board.halfmove_clock assert chess_board.move_stack == new_chess_board.move_stack assert chess_board.fen() == new_chess_board.fen() assert chess_board.board_fen() == new_chess_board.board_fen()
def test_add_remove_conversation_from_board(self): self.board1.save() self.conversation1.save() board1 = Board.get(id=1) board1.insert('conversation', Conversation.get(id=1)) self.assertEqual(True, isinstance(board1.conversation, Conversation)) board1.remove('conversation') self.assertEqual(None, board1.conversation)
def test_board_from_fen(): chess_board = chess.Board() fen = chess_board.fen() board = Board.from_fen(fen) assert board.ep_square is None assert board.halfmove_clock == 0 assert board.fullmove_number == 1 assert board.castling_xfen == "KQkq"
def test_insert_wrong_field(self): self.user1.save() self.board1.save() user1 = User.get(id=1) excepted = { "message": "Ensure the field passed is valid.", "help": "The field should be an attribute of the object." } actual = user1.insert('random', Board.get_all()) self.assertDictEqual(excepted, actual)
def test_insert_many_not_list(self): self.user1.save() self.board1.save() user1 = User.get(id=1) excepted = { "message": "Ensure objects passed are as a list.", "help": "This eases updating of (one)many-to-many fields" } actual = user1.insert('boards', Board.get(id=1)) self.assertDictEqual(excepted, actual)
def test_add_and_remove_board_from_user(self): self.user1.save() self.board1.save() self.board2.save() user1 = User.get(id=1) self.assertEqual(0, len(user1.boards)) user1.insert('boards', Board.get_all()) self.assertEqual(2, len(user1.boards)) user1.remove('boards', id=1) self.assertEqual(1, len(user1.boards)) user1.remove('boards') self.assertEqual(0, len(user1.boards))
def test_add_and_remove_board_estate_resident(self): self.board1.save() self.estate1.save() self.user1.save() self.unit1.save() unit1 = Unit.get(id=1) unit1.insert('resident', User.get(id=1)) unit1.insert('board', Board.get(id=1)) unit1.insert('estate', Estate.get(id=1)) self.assertEqual(True, isinstance(unit1.board, Board)) self.assertEqual(True, isinstance(unit1.resident, User)) self.assertEqual(True, isinstance(unit1.estate, Estate)) unit1.remove('resident') unit1.remove('board') unit1.remove('estate') self.assertEqual(None, unit1.board) self.assertEqual(None, unit1.estate) self.assertEqual(None, unit1.resident)
def seed_test_data(): """ Seed data to be used for testing. """ db.drop_all() db.create_all() payment_1 = Payment() payment_2 = Payment() payment_3 = Payment() payment_4 = Payment() payment_1.insert('deposits', [Deposit( amount=1000.0), Deposit(amount=500.0)]) payment_2.insert('deposits', [Deposit( amount=1000.0), Deposit(amount=500.0)]) payment_3.insert('deposits', [Deposit(amount=1000.0)]) payment_4.insert('deposits', [Deposit(amount=1000.0)]) wallet_1 = Wallet() wallet_2 = Wallet() wallet_3 = Wallet() wallet_4 = Wallet() wallet_1.insert('payments', [payment_1, payment_2]) wallet_2.insert('payments', [payment_3]) wallet_3.insert('payments', [payment_4]) estate1 = Estate(address='Casterly Rock') estate2 = Estate(address='Dorne') estate3 = Estate(address='Dragonstone') estate2.insert('payment', wallet_2.payments[0]) estate3.insert('payment', wallet_3.payments[0]) unit1 = Unit(name='No. 1') unit2 = Unit(name='No. 2') unit3 = Unit(name='No. 1') unit4 = Unit(name='No. 2') unit5 = Unit(name='No. 3') unit1.insert('payment', wallet_1.payments[0]) unit2.insert('payment', wallet_1.payments[1]) estate1.insert('estate_units', [unit1, unit2]) estate2.insert('estate_units', [unit3]) estate3.insert('estate_units', [unit4, unit5]) board1 = Board() board2 = Board() board1.insert('estates_owned', [estate1, estate2]) board2.insert('estates_owned', [estate3]) board1.insert('units_owned', [unit5]) user0 = User( name="Test User", phone_number="000 00 0000000", email="*****@*****.**", password=digest('ABC123!@#1')) user1 = User( name="Jaime Lannister", phone_number="000 12 3456781", email="*****@*****.**", password=digest('ABC123!@#1')) user2 = User( name="Oberyn Martell", phone_number="000 12 3456782", email="*****@*****.**", password=digest('ABC123!@#2')) user3 = User( name="Daenerys Targaryen", phone_number="000 12 3456783", email="*****@*****.**", password=digest('ABC123!@#3')) conversation1 = Conversation() conversation2 = Conversation() message1 = Message(sender=user1.name, content='Content 1') message2 = Message(sender=user2.name, content='Content 2') message3 = Message(sender=user3.name, content='Content 3') conversation1.insert('messages', [message1, message2]) conversation2.insert('messages', [message3]) user0.insert('boards', [board1]) user1.insert('boards', [board1]) user2.insert('boards', [board1]) user3.insert('boards', [board2]) user1.insert('units', [unit1, unit2]) user2.insert('units', [unit3]) user3.insert('units', [unit4]) user0.insert('wallet', wallet_4) user1.insert('wallet', wallet_1) user2.insert('wallet', wallet_2) user3.insert('wallet', wallet_3) user1.insert('conversations', [conversation1, conversation2]) user2.insert('conversations', [conversation1, conversation2]) user3.insert('conversations', [conversation1]) role1 = Role(title='admin') role2 = Role(title='basic') role3 = Role(title='super_admin') role3.insert('users', [user0, user1]) role2.insert('users', [user0, user1, user2, user3]) role1.insert('users', [user0, user3]) role1.save() role2.save() role3.save() print('\nTest Data Seeded.\n')
def test_get_many_boards(self): self.assertTrue(isinstance(Board.get_all(), dict)) self.board1.save() self.board2.save() self.assertEqual(2, len(Board.get_all()))
def test_board_model_string(): board = Board() assert str(board) == board.fen
def test_get_empty_move_stack(): board = Board() assert board.move_stack == []
def test_get_board(self): self.board1.save() self.assertTrue(isinstance(Board.get(id=1), Board))
def test_board_exists(self): self.board1.save() self.assertEqual(True, Board.check_exists(id=1)) self.assertEqual(False, Board.check_exists(id=2))
import pytest import chess from api import services from api.models import Board, Game, Result JWT_TOKEN = { "refresh": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoicmVmcmVzaCIsImV4cCI6MTU5NTIzMjMxMCwianRpIjoiMTViM2ZiZGNhODJlNDBiMDkyNTBiYzA5ZTlkODQwMmYiLCJ1c2VyX2lkIjoxfQ.3UZIhcS4X14zb9V7wRnf0G3TJ1f7G6UMijThokvOD_M", "access": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNTk1MTQ1OTIyLCJqdGkiOiI1ODBjNmM3ZDcyNTk0NmJmOWJiNGY1YmMyMzcyMjY0MiIsInVzZXJfaWQiOjF9.44RKjIbKXmWqjzJA0TtpbnBTt3-3tAMxUP1EZDMJais", } CHESS_BOARD = chess.Board() BOARD_INSTANCE = Board.from_fen(CHESS_BOARD.fen()) @pytest.fixture def users(): user_one = services.User.objects.create( username="******", name="Walter Hartwell White", email="*****@*****.**", password="******", is_active=True, ) user_two = services.User.objects.create( username="******", name="Jesse Bruce Pinkman", email="*****@*****.**",