Exemplo n.º 1
0
 def testTest0002(self):
     """It turned out that binary comission was not paid for user when a binary matrix got full.
        in testHandleFullMatrixOfUser. In this test, we go to the failure point and execute the test
        function line-by-line."""
     parentUserId = User.objects.create(username="******", sponsor=MasterUser.Get()).id
     Controller().createNewBinaryPosition(parentUserId)
     numberOfUsers = BinaryTree()._getNumberOfNodesToReturn() - 2
     self._createBinaryPositions(numberOfUsers, parentUserId)
     lastUserId = User.objects.create(username="******", sponsor=User.Get(parentUserId)).id
     # This was the failed line:
     # Controller().createNewBinaryPosition(lastUserId)
     self.assertTrue(User.Get(lastUserId).addNewActiveBinaryPosition())
     # Check situation before payment
     self.assertEqual(- Controller().price, User.Get(parentUserId).binary_money)
     Controller().payBinaryPrice(lastUserId)
     self.assertEqual(- Controller().price, User.Get(parentUserId).binary_money)
     # This line did noy pay commision for parent user. Extend it, check line-by-line
     # Controller()._handleFullMatrix(User.Get(lastUserId).active_binary_position)
     position = User.Get(lastUserId).active_binary_position
     top = BinaryTree().getMatrixTop(position)
     self.assertTrue(not top.owner.isMaster())
     if not top.owner.isMaster():
         Controller().payBinaryCommission(top.owner.id)
     # OK, this was the error. payBinaryCommission was called, saved the new money state,
     # but this line re-saved the old state.
     # top.owner.save()
     self.assertEqual(- Controller().price + Controller().commission, User.Get(parentUserId).binary_money)
Exemplo n.º 2
0
 def testHandleFullMatrixOfUser(self):
     activeId = MasterUser.Get().active_binary_position.id
     self.assertTrue(MasterUser.Get().active_binary_position)
     self.assertEqual(MasterUser.Get().id, 1)
     parentUserId = User.objects.create(username="******", sponsor=MasterUser.Get()).id
     Controller().createNewBinaryPosition(parentUserId)
     self.assertTrue(User.Get(parentUserId).active_binary_position)
     numberOfUsers = BinaryTree()._getNumberOfNodesToReturn() - 2
     self._createBinaryPositions(numberOfUsers, parentUserId)
     self.assertEqual(- Controller().price, User.Get(parentUserId).binary_money)
     lastUserId = User.objects.create(username="******", sponsor=User.Get(parentUserId)).id
     self.assertEqual(activeId, MasterUser.Get().active_binary_position.id)
     self.assertEqual(User.Get(lastUserId).sponsor.id, parentUserId)
     Controller().createNewBinaryPosition(lastUserId)
     self.assertEqual(User.Get(lastUserId).sponsor.id, parentUserId)
     self.assertEqual(activeId, MasterUser.Get().active_binary_position.id)
     self.assertEqual((numberOfUsers + 2) * Controller().price - Controller().commission, MasterUser.Get().binary_money)
     # With this move, the parent's matrix got full. Sponsor should get commission, no new fee
     # should be paid after the repositioning.
     # This line has been failed. Test0002 was used for debugging.
     self.assertEqual(- Controller().price + Controller().commission, User.Get(parentUserId).binary_money)
     # Check if the new position for the master has been created.
     # It must be the rightmost node at one level below the full matrix.
     self.assertEqual(User.Get(parentUserId).active_binary_position.parent.owner.id, MasterUser.Get().id)
     self.assertTrue(MasterUser.Get().active_binary_position.right)
     self.assertEqual(MasterUser.Get().active_binary_position.right.id, User.Get(parentUserId).active_binary_position.id)
Exemplo n.º 3
0
 def testHandleFullMatrixOfMaster(self):
     numberOfUsers = BinaryTree()._getNumberOfNodesToReturn() - 2
     self._createBinaryPositions(numberOfUsers, MasterUser.Get().id)
     self.assertEqual(numberOfUsers * Controller().price, MasterUser.Get().binary_money)
     user = User.objects.create(username="******", sponsor=MasterUser.Get())
     Controller().createNewBinaryPosition(user.id)
     # With this move, the master's matrix got full. Master should not get commission,
     # only the fees.
     self.assertEqual((numberOfUsers + 1) * Controller().price, MasterUser.Get().binary_money)
     # Check if the new position for the master has been created.
     # It must be a completely new matrix.
     self.assertEqual(MasterUser.Get().active_binary_position.parent, None)
Exemplo n.º 4
0
 def setUp(self):
     self.root = DummyNode(1)
     self.logic = BinaryTree()
Exemplo n.º 5
0
class Tests(TestCase):
    def setUp(self):
        self.root = DummyNode(1)
        self.logic = BinaryTree()

    def testPlaceNodeCatchEmptyNodes(self):
        notEmptyNode = 1

        with self.assertRaises(BinaryTree.NodeIsEmpty):
            self.logic.placeNode(None, notEmptyNode)

        with self.assertRaises(BinaryTree.NodeIsEmpty):
            self.logic.placeNode(notEmptyNode, None)

    def testPlaceNodeRecursiveLogicCatchEmpties(self):
        with self.assertRaises(BinaryTree.QueueIsEmpty):
            queue = deque([])
            self.logic._placeNodeRecursiveLogic(queue)

        with self.assertRaises(BinaryTree.NodeIsEmpty):
            queue = deque([None])
            self.logic._placeNodeRecursiveLogic(queue)

    def testPlaceNodeRecursiveLogicTestLeftPlacement(self):
        queue = deque([self.root])
        (actualNode, side) = self.logic._placeNodeRecursiveLogic(queue)
        self.assertEqual(side, "left")
        self.assertEqual(actualNode, self.root)

    def testPlaceNodeRecursiveLogicTestRightPlacement(self):
        self.root.left = DummyNode(2)
        queue = deque([self.root])
        (actualNode, side) = self.logic._placeNodeRecursiveLogic(queue)
        self.assertEqual(side, "right")
        self.assertEqual(actualNode, self.root)

    def testPlaceNodeRecursiveLogicTestOneLevelDown(self):
        self.root.left = DummyNode(2)
        self.root.right = DummyNode(3)
        self.root.left.left = DummyNode(4)
        queue = deque([self.root])
        (actualNode, side) = self.logic._placeNodeRecursiveLogic(queue)
        self.assertEqual(side, "right")
        self.assertEqual(actualNode, self.root.left)

    def testPlaceNodeRecursiveLogicTestOneLevelDownThreeNodesRight(self):
        self.root.left = DummyNode(2)
        self.root.right = DummyNode(3)
        self.root.left.left = DummyNode(4)
        self.root.left.right = DummyNode(5)
        queue = deque([self.root])
        (actualNode, side) = self.logic._placeNodeRecursiveLogic(queue)
        self.assertEqual(side, "left")
        self.assertEqual(actualNode, self.root.right)

    def testPlaceNodeSaves(self):
        newNode = DummyNode(2)
        self.assertFalse(self.root.saved)
        self.assertEqual(self.root.left, None)
        self.logic.placeNode(self.root, newNode)
        self.assertEqual(self.root.left, newNode)
        self.assertTrue(self.root.saved)

    def testCommitNewNode(self):
        node = DummyNode(1)
        newNode = DummyNode(2)
        newNode.sponsor = node

        with self.assertRaises(BinaryTree.LogicError):
            self.logic._commitNewNode(None, "left", node)

        with self.assertRaises(BinaryTree.LogicError):
            self.logic._commitNewNode(node, "", node)

        with self.assertRaises(BinaryTree.NodeIsEmpty):
            self.logic._commitNewNode(node, "left", None)

        with self.assertRaises(BinaryTree.LogicError):
            self.logic._commitNewNode(node, "atyala", node)

        self.logic._commitNewNode(node, "left", newNode)
        self.assertTrue(node.left)
        self.assertEqual(node.left.id, 2)
        self.assertFalse(node.right)
        self.assertEqual(newNode.sponsor, node)

        node = DummyNode(1)
        newNode.sponsor = node
        self.logic._commitNewNode(node, "right", newNode)
        self.assertTrue(node.right)
        self.assertEqual(node.right.id, 2)
        self.assertFalse(node.left)
        self.assertEqual(newNode.sponsor, node)

    def _testPersistencyCreate(self):
        user = User.objects.create(username="******")
        self.root = BinaryPosition.objects.create(name="pos1", owner=user)
        newNode = BinaryPosition.objects.create(name="pos2", owner=user)
        self.assertEqual(self.root.left, None)
        self.logic.placeNode(self.root, newNode)
        self.assertEqual(self.root.left, newNode)

    def testPersistency(self):
        self._testPersistencyCreate()
        self.root = BinaryPosition.objects.get(name="pos1")
        newNode = BinaryPosition.objects.get(name="pos2")
        self.assertTrue(self.root)
        self.assertTrue(newNode)
        self.assertEqual(self.root.left, newNode)

    def testGetNumberOfNodesToReturn(self):
        self.assertEqual(1, self.logic._getNumberOfNodesToReturn(1))
        self.assertEqual(3, self.logic._getNumberOfNodesToReturn(2))
        self.assertEqual(7, self.logic._getNumberOfNodesToReturn(3))

    def testSumOfSquares(self):
        self.assertEqual(1, self.logic._sumOfSquares(1))
        self.assertEqual(3, self.logic._sumOfSquares(2))
        self.assertEqual(7, self.logic._sumOfSquares(3))


    def testIsMatrixFull(self):
        self.logic._levelsOfFullMatrix = 2
        node = DummyNode(1)
        self.assertFalse(self.logic.isMatrixFull(node))
        node.left = DummyNode(2)
        self.assertFalse(self.logic.isMatrixFull(node))
        node.right = DummyNode(3)
        self.assertTrue(self.logic.isMatrixFull(node))

    def _testHandleFullMatrixOnePlacement(self):
        self.logic._levelsOfFullMatrix = 2

        with self.assertRaises(BinaryTree.NodeIsEmpty):
            self.logic._handleFullMatrix(None)
        # Set up test
        self.logic.placeNode(self.root, DummyNode(2))
        self.logic.placeNode(self.root, DummyNode(3))
        self.logic.placeNode(self.root.left, DummyNode(4))
        self.logic.placeNode(self.root.left, DummyNode(5))
        self.assertFalse(self.root.isCommissionPaid)
        # Test body
        self.assertFalse(self.root.right.left)
        self.logic._handleFullMatrix(self.root.left.right)
        self.assertEqual(self.root.right.left, self.root.left.right)
        self.assertTrue(self.root.isCommissionPaid)

    def _testPlaceNodeFullMatrix(self):
        self.logic._levelsOfFullMatrix = 2
        # Test that in case of not full matrix, isMatrixFull is false
        queue = deque([self.root])
        (node, side, isMatrixFull) = self.logic._placeNodeRecursiveLogic(queue, 1)
        self.assertEqual(node, self.root)
        self.assertEqual(side, "left")
        self.assertFalse(isMatrixFull)
        # Test that in case of not full matrix, isMatrixFull is true
        self.root.left = DummyNode(2)
        queue = deque([self.root])
        (node, side, isMatrixFull) = self.logic._placeNodeRecursiveLogic(queue, 1)
        self.assertEqual(node, self.root)
        self.assertEqual(side, "right")
        self.assertTrue(isMatrixFull)

    def testGetMatrixTop(self):
        self.logic._levelsOfFullMatrix = 2
        self.logic.placeNode(self.root, DummyNode(2))
        self.logic.placeNode(self.root.left, DummyNode(3))
        self.assertEqual(self.root, self.logic.getMatrixTop(self.root.left))
        self.logic._levelsOfFullMatrix = 3
        self.logic.placeNode(self.root, DummyNode(4))
        self.logic.placeNode(self.root.right, DummyNode(5))
        self.assertEqual(self.root, self.logic.getMatrixTop(self.root.right.left))
        # Test if we are too close to the parent: we cannot go beyont teh parentmost node (master),
        # in this case, the method must return None.
        self.assertEqual(None, self.logic.getMatrixTop(self.root.right))

    def testTreeToJson_OneNode(self):
        node = DummyNode(1, owner=DummyUser("user1"))
        json = self.logic.treeToJson(node)
        self.assertEqual("{'children': [], 'data': {}, 'id': '1', 'name': 'user1 (None)'}", str(json))

    def testTreeToJson_TwoNodes(self):
        user1 = DummyUser("user1")
        node1 = DummyNode(1, owner=user1)
        node2 = DummyNode(2, parent=node1, owner=DummyUser("user2", user1))
        node1.left = node2
        json = self.logic.treeToJson(node1)
        self.assertEqual("{'children': [{'children': [], 'data': {}, 'id': '2', 'name': 'user2 (user1)'}], 'data': {}, 'id': '1', 'name': 'user1 (None)'}", str(json))