def test_xo_transaction_is_valid_invalid_space(self): key = signed_object.generate_signing_key() store = global_store_manager.KeyValueStore() # Create the game transaction = XoTransaction({ 'Action': 'CREATE', 'Name': 'game000' }) transaction.sign_object(key) self.assertTrue(transaction.is_valid(store)) transaction.apply(store) self.assertIn('game000', store) self.assertIn('Board', store['game000']) self.assertIn('State', store['game000']) self.assertEquals(store['game000']['State'], 'P1-NEXT') self.assertEquals(store['game000']['Board'], '---------') for space in [0, 10]: transaction = XoTransaction({ 'Action': 'TAKE', 'Name': 'game000', 'Space': space }) transaction.sign_object(key) self.assertFalse(transaction.is_valid(store))
def test_xo_transaction_is_valid_no_game(self): key = signed_object.generate_signing_key() store = global_store_manager.KeyValueStore() transaction = XoTransaction({"Action": "TAKE", "Name": "game000", "Space": 1}) transaction.sign_object(key) self.assertFalse(transaction.is_valid(store))
def test_xo_transaction_is_valid_invalid_action(self): key = signed_object.generate_signing_key() store = global_store_manager.KeyValueStore() transaction = XoTransaction({'Action': 'INVALID', 'Name': 'game000'}) transaction.sign_object(key) self.assertFalse(transaction.is_valid(store))
def test_xo_transaction_is_valid_no_signature(self): store = global_store_manager.KeyValueStore() transaction = XoTransaction({ 'Action': 'CREATE', 'Name': 'game000' }) self.assertFalse(transaction.is_valid(store))
def test_xo_transaction_is_valid_missing_action(self): key = signed_object.generate_signing_key() store = global_store_manager.KeyValueStore() transaction = XoTransaction({"Name": "game000"}) transaction.sign_object(key) self.assertFalse(transaction.is_valid(store))
def test_xo_transaction_is_valid_missing_name(self): key = signed_object.generate_signing_key() store = global_store_manager.KeyValueStore() transaction = XoTransaction({'Action': 'CREATE'}) transaction.sign_object(key) self.assertFalse(transaction.is_valid(store))
def test_xo_transaction_is_valid_take_missing_space(self): key = signed_object.generate_signing_key() store = global_store_manager.KeyValueStore() store.set('game000', '{}') transaction = XoTransaction({'Action': 'TAKE', 'Name': 'game000'}) transaction.sign_object(key) self.assertFalse(transaction.is_valid(store))
def test_xo_transaction_is_valid_create_same_name(self): key = signed_object.generate_signing_key() store = global_store_manager.KeyValueStore() store.set("game000", "{}") transaction = XoTransaction({"Action": "CREATE", "Name": "game000"}) transaction.sign_object(key) self.assertFalse(transaction.is_valid(store))
def test_xo_transaction_is_valid_invalid_action(self): key = signed_object.generate_signing_key() store = global_store_manager.KeyValueStore() transaction = XoTransaction({ 'Action': 'INVALID', 'Name': 'game000' }) transaction.sign_object(key) self.assertFalse(transaction.is_valid(store))
def test_xo_transaction_is_valid_take_missing_space(self): key = signed_object.generate_signing_key() store = global_store_manager.KeyValueStore() store.set('game000', '{}') transaction = XoTransaction({ 'Action': 'TAKE', 'Name': 'game000' }) transaction.sign_object(key) self.assertFalse(transaction.is_valid(store))
def test_xo_transaction_is_valid_invalid_space(self): key = signed_object.generate_signing_key() store = global_store_manager.KeyValueStore() # Create the game transaction = XoTransaction({"Action": "CREATE", "Name": "game000"}) transaction.sign_object(key) self.assertTrue(transaction.is_valid(store)) transaction.apply(store) self.assertIn("game000", store) self.assertIn("Board", store["game000"]) self.assertIn("State", store["game000"]) self.assertEquals(store["game000"]["State"], "P1-NEXT") self.assertEquals(store["game000"]["Board"], "---------") for space in [0, 10]: transaction = XoTransaction({"Action": "TAKE", "Name": "game000", "Space": space}) transaction.sign_object(key) self.assertFalse(transaction.is_valid(store))
def _sendtxn(self, minfo): """ Build a transaction for the update, wrap it in a message with all of the appropriate signatures and post it to the validator """ txn = XoTransaction(minfo=minfo) # add the last transaction submitted to ensure that the ordering # in the journal matches the order in which we generated them if self.LastTransaction: txn.Dependencies = [self.LastTransaction] txn.sign_from_node(self.LocalNode) txnid = txn.Identifier txn.check_valid(self._current_state.State) if not txn.is_valid(self._current_state.State): raise XoClientException('transaction failed to apply') msg = XoTransactionMessage() msg.Transaction = txn msg.SenderID = self.LocalNode.Identifier msg.sign_from_node(self.LocalNode) try: LOGGER.debug('Posting transaction: %s', txnid) result = self.postmsg(msg.MessageType, msg.dump()) except MessageException: return None except: LOGGER.debug('message post failed for some unusual reason') return None # if there was no exception thrown then all transactions should return # a value which is a dictionary with the message that was sent assert result # if the message was successfully posted, then save the transaction # id for future dependencies this could be a problem if the transaction # fails during application self.LastTransaction = txnid txn.apply(self._current_state.State) return txnid
def test_xo_transaction_game_tie(self): player1_key = signed_object.generate_signing_key() player2_key = signed_object.generate_signing_key() store = global_store_manager.KeyValueStore() # Create the game transaction = XoTransaction({'Action': 'CREATE', 'Name': 'game000'}) transaction.sign_object(player1_key) self.assertTrue(transaction.is_valid(store)) transaction.apply(store) self.assertIn('game000', store) self.assertIn('Board', store['game000']) self.assertIn('State', store['game000']) self.assertEqual(store['game000']['State'], 'P1-NEXT') self.assertEqual(store['game000']['Board'], '---------') # Take space 1 transaction = XoTransaction({ 'Action': 'TAKE', 'Name': 'game000', 'Space': 1 }) transaction.sign_object(player1_key) self.assertTrue(transaction.is_valid(store)) transaction.apply(store) self.assertIn('game000', store) self.assertIn('Board', store['game000']) self.assertIn('State', store['game000']) self.assertEqual(store['game000']['State'], 'P2-NEXT') self.assertEqual(store['game000']['Board'], 'X--------') # Take space 2 transaction = XoTransaction({ 'Action': 'TAKE', 'Name': 'game000', 'Space': 2 }) transaction.sign_object(player2_key) self.assertTrue(transaction.is_valid(store)) transaction.apply(store) self.assertIn('game000', store) self.assertIn('Board', store['game000']) self.assertIn('State', store['game000']) self.assertEqual(store['game000']['State'], 'P1-NEXT') self.assertEqual(store['game000']['Board'], 'XO-------') # Take space 4 transaction = XoTransaction({ 'Action': 'TAKE', 'Name': 'game000', 'Space': 4 }) transaction.sign_object(player1_key) self.assertTrue(transaction.is_valid(store)) transaction.apply(store) self.assertIn('game000', store) self.assertIn('Board', store['game000']) self.assertIn('State', store['game000']) self.assertEqual(store['game000']['State'], 'P2-NEXT') self.assertEqual(store['game000']['Board'], 'XO-X-----') # Take space 5 transaction = XoTransaction({ 'Action': 'TAKE', 'Name': 'game000', 'Space': 5 }) transaction.sign_object(player2_key) self.assertTrue(transaction.is_valid(store)) transaction.apply(store) self.assertIn('game000', store) self.assertIn('Board', store['game000']) self.assertIn('State', store['game000']) self.assertEqual(store['game000']['State'], 'P1-NEXT') self.assertEqual(store['game000']['Board'], 'XO-XO----') # Take space 8 transaction = XoTransaction({ 'Action': 'TAKE', 'Name': 'game000', 'Space': 8 }) transaction.sign_object(player1_key) self.assertTrue(transaction.is_valid(store)) transaction.apply(store) self.assertIn('game000', store) self.assertIn('Board', store['game000']) self.assertIn('State', store['game000']) self.assertEqual(store['game000']['State'], 'P2-NEXT') self.assertEqual(store['game000']['Board'], 'XO-XO--X-') # Take space 7 transaction = XoTransaction({ 'Action': 'TAKE', 'Name': 'game000', 'Space': 7 }) transaction.sign_object(player2_key) self.assertTrue(transaction.is_valid(store)) transaction.apply(store) self.assertIn('game000', store) self.assertIn('Board', store['game000']) self.assertIn('State', store['game000']) self.assertEqual(store['game000']['State'], 'P1-NEXT') self.assertEqual(store['game000']['Board'], 'XO-XO-OX-') # Take space 3 transaction = XoTransaction({ 'Action': 'TAKE', 'Name': 'game000', 'Space': 3 }) transaction.sign_object(player1_key) self.assertTrue(transaction.is_valid(store)) transaction.apply(store) self.assertIn('game000', store) self.assertIn('Board', store['game000']) self.assertIn('State', store['game000']) self.assertEqual(store['game000']['State'], 'P2-NEXT') self.assertEqual(store['game000']['Board'], 'XOXXO-OX-') # Take space 6 transaction = XoTransaction({ 'Action': 'TAKE', 'Name': 'game000', 'Space': 6 }) transaction.sign_object(player2_key) self.assertTrue(transaction.is_valid(store)) transaction.apply(store) self.assertIn('game000', store) self.assertIn('Board', store['game000']) self.assertIn('State', store['game000']) self.assertEqual(store['game000']['State'], 'P1-NEXT') self.assertEqual(store['game000']['Board'], 'XOXXOOOX-') # Take space 9 transaction = XoTransaction({ 'Action': 'TAKE', 'Name': 'game000', 'Space': 9 }) transaction.sign_object(player1_key) self.assertTrue(transaction.is_valid(store)) transaction.apply(store) self.assertIn('game000', store) self.assertIn('Board', store['game000']) self.assertIn('State', store['game000']) self.assertEqual(store['game000']['State'], 'TIE') self.assertEqual(store['game000']['Board'], 'XOXXOOOXX')
def test_xo_transaction_is_valid_no_signature(self): store = global_store_manager.KeyValueStore() transaction = XoTransaction({'Action': 'CREATE', 'Name': 'game000'}) self.assertFalse(transaction.is_valid(store))
def test_xo_transaction_game_p1_wins(self): player1_key = signed_object.generate_signing_key() player2_key = signed_object.generate_signing_key() store = global_store_manager.KeyValueStore() # Create the game transaction = XoTransaction({'Action': 'CREATE', 'Name': 'game000'}) transaction.sign_object(player1_key) self.assertTrue(transaction.is_valid(store)) transaction.apply(store) self.assertIn('game000', store) self.assertIn('Board', store['game000']) self.assertIn('State', store['game000']) self.assertEqual(store['game000']['State'], 'P1-NEXT') self.assertEqual(store['game000']['Board'], '---------') # Take space 1 transaction = XoTransaction({ 'Action': 'TAKE', 'Name': 'game000', 'Space': 1 }) transaction.sign_object(player1_key) self.assertTrue(transaction.is_valid(store)) transaction.apply(store) self.assertIn('game000', store) self.assertIn('Board', store['game000']) self.assertIn('State', store['game000']) self.assertEqual(store['game000']['State'], 'P2-NEXT') self.assertEqual(store['game000']['Board'], 'X--------') # Take space 2 transaction = XoTransaction({ 'Action': 'TAKE', 'Name': 'game000', 'Space': 2 }) transaction.sign_object(player2_key) self.assertTrue(transaction.is_valid(store)) transaction.apply(store) self.assertIn('game000', store) self.assertIn('Board', store['game000']) self.assertIn('State', store['game000']) self.assertEqual(store['game000']['State'], 'P1-NEXT') self.assertEqual(store['game000']['Board'], 'XO-------') # Make sure it isn't valid to take space 2 again transaction = XoTransaction({ 'Action': 'TAKE', 'Name': 'game000', 'Space': 2 }) transaction.sign_object(player1_key) self.assertFalse(transaction.is_valid(store)) # Make sure Player 2 can't go on Player 1's turn. transaction = XoTransaction({ 'Action': 'TAKE', 'Name': 'game000', 'Space': 3 }) transaction.sign_object(player2_key) self.assertFalse(transaction.is_valid(store)) # Take space 4 transaction = XoTransaction({ 'Action': 'TAKE', 'Name': 'game000', 'Space': 4 }) transaction.sign_object(player1_key) self.assertTrue(transaction.is_valid(store)) transaction.apply(store) self.assertIn('game000', store) self.assertIn('Board', store['game000']) self.assertIn('State', store['game000']) self.assertEqual(store['game000']['State'], 'P2-NEXT') self.assertEqual(store['game000']['Board'], 'XO-X-----') # Make sure Player 1 can't go on Player 2's turn. transaction = XoTransaction({ 'Action': 'TAKE', 'Name': 'game000', 'Space': 9 }) transaction.sign_object(player1_key) self.assertFalse(transaction.is_valid(store)) # Take space 9 transaction = XoTransaction({ 'Action': 'TAKE', 'Name': 'game000', 'Space': 9 }) transaction.sign_object(player2_key) self.assertTrue(transaction.is_valid(store)) transaction.apply(store) self.assertIn('game000', store) self.assertIn('Board', store['game000']) self.assertIn('State', store['game000']) self.assertEqual(store['game000']['State'], 'P1-NEXT') self.assertEqual(store['game000']['Board'], 'XO-X----O') # Take space 7 transaction = XoTransaction({ 'Action': 'TAKE', 'Name': 'game000', 'Space': 7 }) transaction.sign_object(player1_key) self.assertTrue(transaction.is_valid(store)) transaction.apply(store) self.assertIn('game000', store) self.assertIn('Board', store['game000']) self.assertIn('State', store['game000']) self.assertEqual(store['game000']['State'], 'P1-WIN') self.assertEqual(store['game000']['Board'], 'XO-X--X-O') # Make sure we can't modify the game anymore transaction = XoTransaction({ 'Action': 'TAKE', 'Name': 'game000', 'Space': 3 }) transaction.sign_object(player2_key) self.assertFalse(transaction.is_valid(store))
def test_xo_transaction_game_p1_wins(self): player1_key = signed_object.generate_signing_key() player2_key = signed_object.generate_signing_key() store = global_store_manager.KeyValueStore() # Create the game transaction = XoTransaction({"Action": "CREATE", "Name": "game000"}) transaction.sign_object(player1_key) self.assertTrue(transaction.is_valid(store)) transaction.apply(store) self.assertIn("game000", store) self.assertIn("Board", store["game000"]) self.assertIn("State", store["game000"]) self.assertEquals(store["game000"]["State"], "P1-NEXT") self.assertEquals(store["game000"]["Board"], "---------") # Take space 1 transaction = XoTransaction({"Action": "TAKE", "Name": "game000", "Space": 1}) transaction.sign_object(player1_key) self.assertTrue(transaction.is_valid(store)) transaction.apply(store) self.assertIn("game000", store) self.assertIn("Board", store["game000"]) self.assertIn("State", store["game000"]) self.assertEquals(store["game000"]["State"], "P2-NEXT") self.assertEquals(store["game000"]["Board"], "X--------") # Take space 2 transaction = XoTransaction({"Action": "TAKE", "Name": "game000", "Space": 2}) transaction.sign_object(player2_key) self.assertTrue(transaction.is_valid(store)) transaction.apply(store) self.assertIn("game000", store) self.assertIn("Board", store["game000"]) self.assertIn("State", store["game000"]) self.assertEquals(store["game000"]["State"], "P1-NEXT") self.assertEquals(store["game000"]["Board"], "XO-------") # Make sure it isn't valid to take space 2 again transaction = XoTransaction({"Action": "TAKE", "Name": "game000", "Space": 2}) transaction.sign_object(player1_key) self.assertFalse(transaction.is_valid(store)) # Make sure Player 2 can't go on Player 1's turn. transaction = XoTransaction({"Action": "TAKE", "Name": "game000", "Space": 3}) transaction.sign_object(player2_key) self.assertFalse(transaction.is_valid(store)) # Take space 4 transaction = XoTransaction({"Action": "TAKE", "Name": "game000", "Space": 4}) transaction.sign_object(player1_key) self.assertTrue(transaction.is_valid(store)) transaction.apply(store) self.assertIn("game000", store) self.assertIn("Board", store["game000"]) self.assertIn("State", store["game000"]) self.assertEquals(store["game000"]["State"], "P2-NEXT") self.assertEquals(store["game000"]["Board"], "XO-X-----") # Make sure Player 1 can't go on Player 2's turn. transaction = XoTransaction({"Action": "TAKE", "Name": "game000", "Space": 9}) transaction.sign_object(player1_key) self.assertFalse(transaction.is_valid(store)) # Take space 9 transaction = XoTransaction({"Action": "TAKE", "Name": "game000", "Space": 9}) transaction.sign_object(player2_key) self.assertTrue(transaction.is_valid(store)) transaction.apply(store) self.assertIn("game000", store) self.assertIn("Board", store["game000"]) self.assertIn("State", store["game000"]) self.assertEquals(store["game000"]["State"], "P1-NEXT") self.assertEquals(store["game000"]["Board"], "XO-X----O") # Take space 7 transaction = XoTransaction({"Action": "TAKE", "Name": "game000", "Space": 7}) transaction.sign_object(player1_key) self.assertTrue(transaction.is_valid(store)) transaction.apply(store) self.assertIn("game000", store) self.assertIn("Board", store["game000"]) self.assertIn("State", store["game000"]) self.assertEquals(store["game000"]["State"], "P1-WIN") self.assertEquals(store["game000"]["Board"], "XO-X--X-O") # Make sure we can't modify the game anymore transaction = XoTransaction({"Action": "TAKE", "Name": "game000", "Space": 3}) transaction.sign_object(player2_key) self.assertFalse(transaction.is_valid(store))
def test_xo_transaction_game_p1_wins(self): player1_key = signed_object.generate_signing_key() player2_key = signed_object.generate_signing_key() store = global_store_manager.KeyValueStore() # Create the game transaction = XoTransaction({ 'Action': 'CREATE', 'Name': 'game000' }) transaction.sign_object(player1_key) self.assertTrue(transaction.is_valid(store)) transaction.apply(store) self.assertIn('game000', store) self.assertIn('Board', store['game000']) self.assertIn('State', store['game000']) self.assertEquals(store['game000']['State'], 'P1-NEXT') self.assertEquals(store['game000']['Board'], '---------') # Take space 1 transaction = XoTransaction({ 'Action': 'TAKE', 'Name': 'game000', 'Space': 1 }) transaction.sign_object(player1_key) self.assertTrue(transaction.is_valid(store)) transaction.apply(store) self.assertIn('game000', store) self.assertIn('Board', store['game000']) self.assertIn('State', store['game000']) self.assertEquals(store['game000']['State'], 'P2-NEXT') self.assertEquals(store['game000']['Board'], 'X--------') # Take space 2 transaction = XoTransaction({ 'Action': 'TAKE', 'Name': 'game000', 'Space': 2 }) transaction.sign_object(player2_key) self.assertTrue(transaction.is_valid(store)) transaction.apply(store) self.assertIn('game000', store) self.assertIn('Board', store['game000']) self.assertIn('State', store['game000']) self.assertEquals(store['game000']['State'], 'P1-NEXT') self.assertEquals(store['game000']['Board'], 'XO-------') # Make sure it isn't valid to take space 2 again transaction = XoTransaction({ 'Action': 'TAKE', 'Name': 'game000', 'Space': 2 }) transaction.sign_object(player1_key) self.assertFalse(transaction.is_valid(store)) # Make sure Player 2 can't go on Player 1's turn. transaction = XoTransaction({ 'Action': 'TAKE', 'Name': 'game000', 'Space': 3 }) transaction.sign_object(player2_key) self.assertFalse(transaction.is_valid(store)) # Take space 4 transaction = XoTransaction({ 'Action': 'TAKE', 'Name': 'game000', 'Space': 4 }) transaction.sign_object(player1_key) self.assertTrue(transaction.is_valid(store)) transaction.apply(store) self.assertIn('game000', store) self.assertIn('Board', store['game000']) self.assertIn('State', store['game000']) self.assertEquals(store['game000']['State'], 'P2-NEXT') self.assertEquals(store['game000']['Board'], 'XO-X-----') # Make sure Player 1 can't go on Player 2's turn. transaction = XoTransaction({ 'Action': 'TAKE', 'Name': 'game000', 'Space': 9 }) transaction.sign_object(player1_key) self.assertFalse(transaction.is_valid(store)) # Take space 9 transaction = XoTransaction({ 'Action': 'TAKE', 'Name': 'game000', 'Space': 9 }) transaction.sign_object(player2_key) self.assertTrue(transaction.is_valid(store)) transaction.apply(store) self.assertIn('game000', store) self.assertIn('Board', store['game000']) self.assertIn('State', store['game000']) self.assertEquals(store['game000']['State'], 'P1-NEXT') self.assertEquals(store['game000']['Board'], 'XO-X----O') # Take space 7 transaction = XoTransaction({ 'Action': 'TAKE', 'Name': 'game000', 'Space': 7 }) transaction.sign_object(player1_key) self.assertTrue(transaction.is_valid(store)) transaction.apply(store) self.assertIn('game000', store) self.assertIn('Board', store['game000']) self.assertIn('State', store['game000']) self.assertEquals(store['game000']['State'], 'P1-WIN') self.assertEquals(store['game000']['Board'], 'XO-X--X-O') # Make sure we can't modify the game anymore transaction = XoTransaction({ 'Action': 'TAKE', 'Name': 'game000', 'Space': 3 }) transaction.sign_object(player2_key) self.assertFalse(transaction.is_valid(store))
def test_xo_transaction_game_tie(self): player1_key = signed_object.generate_signing_key() player2_key = signed_object.generate_signing_key() store = global_store_manager.KeyValueStore() # Create the game transaction = XoTransaction({"Action": "CREATE", "Name": "game000"}) transaction.sign_object(player1_key) self.assertTrue(transaction.is_valid(store)) transaction.apply(store) self.assertIn("game000", store) self.assertIn("Board", store["game000"]) self.assertIn("State", store["game000"]) self.assertEquals(store["game000"]["State"], "P1-NEXT") self.assertEquals(store["game000"]["Board"], "---------") # Take space 1 transaction = XoTransaction({"Action": "TAKE", "Name": "game000", "Space": 1}) transaction.sign_object(player1_key) self.assertTrue(transaction.is_valid(store)) transaction.apply(store) self.assertIn("game000", store) self.assertIn("Board", store["game000"]) self.assertIn("State", store["game000"]) self.assertEquals(store["game000"]["State"], "P2-NEXT") self.assertEquals(store["game000"]["Board"], "X--------") # Take space 2 transaction = XoTransaction({"Action": "TAKE", "Name": "game000", "Space": 2}) transaction.sign_object(player2_key) self.assertTrue(transaction.is_valid(store)) transaction.apply(store) self.assertIn("game000", store) self.assertIn("Board", store["game000"]) self.assertIn("State", store["game000"]) self.assertEquals(store["game000"]["State"], "P1-NEXT") self.assertEquals(store["game000"]["Board"], "XO-------") # Take space 4 transaction = XoTransaction({"Action": "TAKE", "Name": "game000", "Space": 4}) transaction.sign_object(player1_key) self.assertTrue(transaction.is_valid(store)) transaction.apply(store) self.assertIn("game000", store) self.assertIn("Board", store["game000"]) self.assertIn("State", store["game000"]) self.assertEquals(store["game000"]["State"], "P2-NEXT") self.assertEquals(store["game000"]["Board"], "XO-X-----") # Take space 5 transaction = XoTransaction({"Action": "TAKE", "Name": "game000", "Space": 5}) transaction.sign_object(player2_key) self.assertTrue(transaction.is_valid(store)) transaction.apply(store) self.assertIn("game000", store) self.assertIn("Board", store["game000"]) self.assertIn("State", store["game000"]) self.assertEquals(store["game000"]["State"], "P1-NEXT") self.assertEquals(store["game000"]["Board"], "XO-XO----") # Take space 8 transaction = XoTransaction({"Action": "TAKE", "Name": "game000", "Space": 8}) transaction.sign_object(player1_key) self.assertTrue(transaction.is_valid(store)) transaction.apply(store) self.assertIn("game000", store) self.assertIn("Board", store["game000"]) self.assertIn("State", store["game000"]) self.assertEquals(store["game000"]["State"], "P2-NEXT") self.assertEquals(store["game000"]["Board"], "XO-XO--X-") # Take space 7 transaction = XoTransaction({"Action": "TAKE", "Name": "game000", "Space": 7}) transaction.sign_object(player2_key) self.assertTrue(transaction.is_valid(store)) transaction.apply(store) self.assertIn("game000", store) self.assertIn("Board", store["game000"]) self.assertIn("State", store["game000"]) self.assertEquals(store["game000"]["State"], "P1-NEXT") self.assertEquals(store["game000"]["Board"], "XO-XO-OX-") # Take space 3 transaction = XoTransaction({"Action": "TAKE", "Name": "game000", "Space": 3}) transaction.sign_object(player1_key) self.assertTrue(transaction.is_valid(store)) transaction.apply(store) self.assertIn("game000", store) self.assertIn("Board", store["game000"]) self.assertIn("State", store["game000"]) self.assertEquals(store["game000"]["State"], "P2-NEXT") self.assertEquals(store["game000"]["Board"], "XOXXO-OX-") # Take space 6 transaction = XoTransaction({"Action": "TAKE", "Name": "game000", "Space": 6}) transaction.sign_object(player2_key) self.assertTrue(transaction.is_valid(store)) transaction.apply(store) self.assertIn("game000", store) self.assertIn("Board", store["game000"]) self.assertIn("State", store["game000"]) self.assertEquals(store["game000"]["State"], "P1-NEXT") self.assertEquals(store["game000"]["Board"], "XOXXOOOX-") # Take space 9 transaction = XoTransaction({"Action": "TAKE", "Name": "game000", "Space": 9}) transaction.sign_object(player1_key) self.assertTrue(transaction.is_valid(store)) transaction.apply(store) self.assertIn("game000", store) self.assertIn("Board", store["game000"]) self.assertIn("State", store["game000"]) self.assertEquals(store["game000"]["State"], "TIE") self.assertEquals(store["game000"]["Board"], "XOXXOOOXX")
def test_xo_transaction_is_valid_no_signature(self): store = global_store_manager.KeyValueStore() transaction = XoTransaction({"Action": "CREATE", "Name": "game000"}) self.assertFalse(transaction.is_valid(store))
def test_xo_transaction_is_valid_invalid_space(self): key = signed_object.generate_signing_key() store = global_store_manager.KeyValueStore() # Create the game transaction = XoTransaction({'Action': 'CREATE', 'Name': 'game000'}) transaction.sign_object(key) self.assertTrue(transaction.is_valid(store)) transaction.apply(store) self.assertIn('game000', store) self.assertIn('Board', store['game000']) self.assertIn('State', store['game000']) self.assertEqual(store['game000']['State'], 'P1-NEXT') self.assertEqual(store['game000']['Board'], '---------') for space in [0, 10]: transaction = XoTransaction({ 'Action': 'TAKE', 'Name': 'game000', 'Space': space }) transaction.sign_object(key) self.assertFalse(transaction.is_valid(store))
def test_xo_transaction_game_tie(self): player1_key = signed_object.generate_signing_key() player2_key = signed_object.generate_signing_key() store = global_store_manager.KeyValueStore() # Create the game transaction = XoTransaction({ 'Action': 'CREATE', 'Name': 'game000' }) transaction.sign_object(player1_key) self.assertTrue(transaction.is_valid(store)) transaction.apply(store) self.assertIn('game000', store) self.assertIn('Board', store['game000']) self.assertIn('State', store['game000']) self.assertEquals(store['game000']['State'], 'P1-NEXT') self.assertEquals(store['game000']['Board'], '---------') # Take space 1 transaction = XoTransaction({ 'Action': 'TAKE', 'Name': 'game000', 'Space': 1 }) transaction.sign_object(player1_key) self.assertTrue(transaction.is_valid(store)) transaction.apply(store) self.assertIn('game000', store) self.assertIn('Board', store['game000']) self.assertIn('State', store['game000']) self.assertEquals(store['game000']['State'], 'P2-NEXT') self.assertEquals(store['game000']['Board'], 'X--------') # Take space 2 transaction = XoTransaction({ 'Action': 'TAKE', 'Name': 'game000', 'Space': 2 }) transaction.sign_object(player2_key) self.assertTrue(transaction.is_valid(store)) transaction.apply(store) self.assertIn('game000', store) self.assertIn('Board', store['game000']) self.assertIn('State', store['game000']) self.assertEquals(store['game000']['State'], 'P1-NEXT') self.assertEquals(store['game000']['Board'], 'XO-------') # Take space 4 transaction = XoTransaction({ 'Action': 'TAKE', 'Name': 'game000', 'Space': 4 }) transaction.sign_object(player1_key) self.assertTrue(transaction.is_valid(store)) transaction.apply(store) self.assertIn('game000', store) self.assertIn('Board', store['game000']) self.assertIn('State', store['game000']) self.assertEquals(store['game000']['State'], 'P2-NEXT') self.assertEquals(store['game000']['Board'], 'XO-X-----') # Take space 5 transaction = XoTransaction({ 'Action': 'TAKE', 'Name': 'game000', 'Space': 5 }) transaction.sign_object(player2_key) self.assertTrue(transaction.is_valid(store)) transaction.apply(store) self.assertIn('game000', store) self.assertIn('Board', store['game000']) self.assertIn('State', store['game000']) self.assertEquals(store['game000']['State'], 'P1-NEXT') self.assertEquals(store['game000']['Board'], 'XO-XO----') # Take space 8 transaction = XoTransaction({ 'Action': 'TAKE', 'Name': 'game000', 'Space': 8 }) transaction.sign_object(player1_key) self.assertTrue(transaction.is_valid(store)) transaction.apply(store) self.assertIn('game000', store) self.assertIn('Board', store['game000']) self.assertIn('State', store['game000']) self.assertEquals(store['game000']['State'], 'P2-NEXT') self.assertEquals(store['game000']['Board'], 'XO-XO--X-') # Take space 7 transaction = XoTransaction({ 'Action': 'TAKE', 'Name': 'game000', 'Space': 7 }) transaction.sign_object(player2_key) self.assertTrue(transaction.is_valid(store)) transaction.apply(store) self.assertIn('game000', store) self.assertIn('Board', store['game000']) self.assertIn('State', store['game000']) self.assertEquals(store['game000']['State'], 'P1-NEXT') self.assertEquals(store['game000']['Board'], 'XO-XO-OX-') # Take space 3 transaction = XoTransaction({ 'Action': 'TAKE', 'Name': 'game000', 'Space': 3 }) transaction.sign_object(player1_key) self.assertTrue(transaction.is_valid(store)) transaction.apply(store) self.assertIn('game000', store) self.assertIn('Board', store['game000']) self.assertIn('State', store['game000']) self.assertEquals(store['game000']['State'], 'P2-NEXT') self.assertEquals(store['game000']['Board'], 'XOXXO-OX-') # Take space 6 transaction = XoTransaction({ 'Action': 'TAKE', 'Name': 'game000', 'Space': 6 }) transaction.sign_object(player2_key) self.assertTrue(transaction.is_valid(store)) transaction.apply(store) self.assertIn('game000', store) self.assertIn('Board', store['game000']) self.assertIn('State', store['game000']) self.assertEquals(store['game000']['State'], 'P1-NEXT') self.assertEquals(store['game000']['Board'], 'XOXXOOOX-') # Take space 9 transaction = XoTransaction({ 'Action': 'TAKE', 'Name': 'game000', 'Space': 9 }) transaction.sign_object(player1_key) self.assertTrue(transaction.is_valid(store)) transaction.apply(store) self.assertIn('game000', store) self.assertIn('Board', store['game000']) self.assertIn('State', store['game000']) self.assertEquals(store['game000']['State'], 'TIE') self.assertEquals(store['game000']['Board'], 'XOXXOOOXX')