def test_redeclaration_of_less_time(self): """ test re-declarations of time. E.g., if there is already covered declared time, it should not be possible to save comment with less declared time than is already covered by transactions. -Should not be possible to change the declared time to smaller if there are investments already.- """ self.assertEqual( self.comment.claimed_hours, Decimal('1.5') ) self.assertEqual( self.comment.assumed_hours, Decimal('6.5') ) self.comment.invest(0.1, 'eur', self.investor) self.comment.text =""" - {1.0},{?0.1} for coming up with basic class structure, - {?2.5} for implementation, - {?3.5} for testing. Here is the result so far: https://github.com/wefindx/infty2.0/commit/9f096dc54f94c31eed9558eb32ef0858f51b1aec """ self.comment.save() self.assertEqual( self.comment.claimed_hours, Decimal('1.0') ) self.assertTrue( self.comment.assumed_hours-Decimal('6.1') < Decimal('1E-15') ) self.assertEqual( self.comment.invested(), Decimal('0.1') ) self.assertEqual( self.comment.matched(), Decimal('0.1') ) self.assertEqual( self.comment.donated(), Decimal('0.0') ) # Balances self.assertEqual( ContributionCertificate.user_matched(self.doer), Decimal('0.05') ) self.assertEqual( ContributionCertificate.user_matched(self.investor), Decimal('0.05') ) self.assertEqual( ContributionCertificate.user_unmatched(self.doer), Decimal('0.0') ) self.assertEqual( ContributionCertificate.user_unmatched(self.investor), Decimal('0.0') )
def test_future_investment_multiparty(self): """ DOER 8.0 ĥ [---------------------------------------------------------] INVESTOR1 INVESTOR2 INVESTOR3 1.0 ḥ 1.0 ḥ 6.0 ḥ [--------][--------][-------------------------------------] CONTRIBUTIONS [-FUTURE-][-FUTURE-][---------------FUTURE----------------] ( .unmatched ) ( .unmatched ) ( .unmatched ) """ self.assertEqual( self.comment2.remains(), Decimal('8.0') ) self.tx1 = self.comment2.invest(1.0, 'eur', self.investor) self.tx2 = self.comment2.invest(1.0, 'eur', self.investor2) self.tx3 = self.comment2.invest(6.0, 'eur', self.investor3) self.assertEqual( self.comment2.invested(), Decimal('8.0') ) self.assertEqual( self.comment2.remains(), Decimal('0.0') ) # [PRESENT] self.assertEqual( self.comment2.matched(), Decimal('0.0') ) # [FUTURE] self.assertEqual( self.comment2.donated(), Decimal('8.0') ) # .matched_hours by investors must be zero. self.assertEqual(self.comment2.matched(by=self.investor),Decimal('0.0')) self.assertEqual(self.comment2.matched(by=self.investor2),Decimal('0.0')) self.assertEqual(self.comment2.matched(by=self.investor3),Decimal('0.0')) # .donated_hours by investors must be 1/2,1/2,6/2 respectively. self.assertEqual(self.comment2.donated(by=self.investor),Decimal('1.0')/Decimal(2)) self.assertEqual(self.comment2.donated(by=self.investor2),Decimal('1.0')/Decimal(2)) self.assertEqual(self.comment2.donated(by=self.investor3),Decimal('6.0')/Decimal(2)) # .donated_hours by doer must be 4, or 1/2+1/2+6/2, as doer gets 50%. self.assertEqual( self.comment2.donated(by=self.doer), Decimal('4.0') ) # Balances self.assertEqual( ContributionCertificate.user_matched(self.doer), Decimal('0.0') ) self.assertEqual( ContributionCertificate.user_matched(self.investor), Decimal('0.0') ) self.assertEqual( ContributionCertificate.user_matched(self.investor2), Decimal('0.0') ) self.assertEqual( ContributionCertificate.user_matched(self.investor3), Decimal('0.0') ) self.assertEqual( ContributionCertificate.user_unmatched(self.doer), Decimal('4.0') ) self.assertEqual( ContributionCertificate.user_unmatched(self.investor), Decimal('0.5') ) self.assertEqual( ContributionCertificate.user_unmatched(self.investor2), Decimal('0.5') ) self.assertEqual( ContributionCertificate.user_unmatched(self.investor3), Decimal('3.0') )
def test_mixed_present_future_investment_multiparty(self): """ "Mixed Present-Future Investment Multiparty : there is a couple of investors ,and the total amount of investment is larger than the time declared. (needs to simulate that if there is re-declaration of time, where there is more time declared, then the new ContributionCer- tificates are generated, so as to preserve priority of investors' sequence. E.g., if small extra time is declared, the newly crated ContributionCertificate is for the earlier investments.) DOER 1.5 h 6.5 ĥ [------------][-------------------------------------------] INVESTOR1 INVESTOR2 INVESTOR3 1.0 ḥ 1.0 ḥ 6.0 ḥ [--------][--------][-------------------------------------] CONTRIBUTIONS [----P---][P-][-F--][--------------FUTURE-----------------] """ self.assertEqual(self.comment.claimed_hours, Decimal('1.5')) self.assertEqual(self.comment.assumed_hours, Decimal('6.5')) self.assertEqual(self.comment.remains(), Decimal('8.0')) self.tx1 = self.comment.invest(1.0, 'eur', self.investor) self.assertEqual(self.comment.invested(), Decimal('1.0')) self.assertEqual(self.comment.remains(), Decimal('7.0')) self.assertEqual(self.comment.donated(), Decimal('0.0')) self.assertEqual(self.comment.contributions(), 2) self.assertEqual( self.comment.claimed_hours - self.comment.matched(), Decimal(0.5) ) self.assertEqual( self.comment.assumed_hours - self.comment.donated(), Decimal(6.5) ) self.assertEqual(self.comment.remains(), Decimal('7.0')) self.tx2 = self.comment.invest(1.0, 'eur', self.investor2) self.assertEqual(self.comment.invested(), Decimal('2.0')) self.assertEqual(self.comment.remains(), Decimal('6.0')) self.assertEqual(self.comment.contributions(), 6) self.assertEqual(self.comment.donated(), Decimal('0.5')) self.tx3 = self.comment.invest(6.0, 'eur', self.investor3) self.assertEqual(self.comment.invested(), Decimal('8.0')) self.assertEqual(self.comment.remains(), Decimal('0.0')) self.assertEqual(self.comment.donated(), Decimal('6.5')) self.assertEqual( self.comment.remains(), Decimal('0.0') ) self.assertEqual( self.comment.matched(), Decimal('1.5') ) self.assertEqual( self.comment.donated(), Decimal('6.5') ) # Balances self.assertEqual( ContributionCertificate.user_matched(self.doer), Decimal('0.75') ) self.assertEqual( ContributionCertificate.user_matched(self.investor), Decimal('0.5') ) self.assertEqual( ContributionCertificate.user_matched(self.investor2), Decimal('0.25') ) self.assertEqual( ContributionCertificate.user_matched(self.investor3), Decimal('0.0') ) self.assertEqual( ContributionCertificate.user_unmatched(self.doer), Decimal('3.25') ) self.assertEqual( ContributionCertificate.user_unmatched(self.investor), Decimal('0.0') ) self.assertEqual( ContributionCertificate.user_unmatched(self.investor2), Decimal('0.25') ) self.assertEqual( ContributionCertificate.user_unmatched(self.investor3), Decimal('3.0') )
def test_future_investment_one_investor(self): """ DOER 8.0 ĥ [---------------------------------------------------------] INVESTOR 1.0 ħ [--------] CONTRIBUTIONS [-FUTURE-] ( .unmatched ) Now, let's say that the doer updates the comment to show the progress (changes the .assumed_hours to .claimed_hours) 8.0 ĥ [---------------------------------------------------------] COMMENT UPDATE | \ / v DOER 0.4 h 7.6 ĥ [--][-----------------------------------------------------] INVESTOR 1.0 ħ [--------] CONTRIBUTIONS [-FUTURE-] ( .unmatched ) 0.4 ḥ [--] HAPPENS (1) We go through old certificates with broken=False, matched=False, (2) Make new child certificates with broken=False, matched=True, (3) and at the points of overflow, broken=False, matched=False. (4) invalidate parent certificates. """ # We have correct initial state: self.assertEqual( self.comment2.claimed_hours, Decimal(0.0) ) self.assertEqual( self.comment2.assumed_hours, Decimal(8.0) ) self.assertEqual( self.comment2.remains(), Decimal(8.0) ) # An investor comes to make an investment of 1.0 self.tx = self.comment2.invest(1.0, 'eur', self.investor) # Both investor and doer get a certificate of investment. self.assertEqual( ContributionCertificate.objects.filter(comment_snapshot__comment=self.comment2).count(), 2 ) # Invested amount 1.0 self.assertEqual( Decimal('1.0'), self.comment2.invested() ) # Remains to invest 7.0 self.assertEqual( self.comment2.remains(), Decimal('7.0') ) # None is matched yet. self.assertEqual( self.comment2.matched(), Decimal('0.0') ) # Both investor and doer should by now have zero matched time. self.assertEqual( ContributionCertificate.user_matched(self.doer), Decimal('0.0') ) self.assertEqual( ContributionCertificate.user_matched(self.investor), Decimal('0.0') ) # Both doer and investor should have '0.5' h each as 'unmatched' self.assertEqual( ContributionCertificate.user_unmatched(self.doer), Decimal('0.5') ) self.assertEqual( ContributionCertificate.user_unmatched(self.investor), Decimal('0.5') ) # Let's say, the doer updates comment to show progress. self.comment2.text = """ - {0.4}{?7.6} for testing. Here is the result so far: https://wiki.mindey.com/shared/screens/7e402349b3c2e3a626b5d25fd.png https://wiki.mindey.com/shared/screens/92fe2c4d8c795e4ff39884622.png """ self.comment2.save() # The matched time is 0.4 self.assertEqual( self.comment2.matched(), Decimal('0.4') ) # The donated time is 0.6 self.assertEqual( self.comment2.donated(), Decimal('0.6') ) # Invested should be 1.0 as before self.assertEqual( self.comment2.invested(), Decimal('1.0') ) # Remains the same amount to invest: self.assertTrue( Decimal('7.0')-self.comment2.remains() < Decimal('1E-15') ) # Number of certificates for the comment should be: self.assertEqual( ContributionCertificate.objects.filter( transaction__comment=self.comment2 ).count(), 6 ) # Two of them broken: self.assertEqual( ContributionCertificate.objects.filter( transaction__comment=self.comment2, broken=True ).count(), 2 ) # Four of them not broken: self.assertEqual( ContributionCertificate.objects.filter( transaction__comment=self.comment2, broken=False ).count(), 4 ) # Two of unbroken those received by doer: self.assertEqual( ContributionCertificate.objects.filter( transaction__comment=self.comment2, received_by=self.doer, broken=False ).count(), 2 ) # Two of unbroken those received by the investor: self.assertEqual( ContributionCertificate.objects.filter( transaction__comment=self.comment2, received_by=self.investor, broken=False ).count(), 2 ) # Doer has 0.2, and investor also 0.2 matched hours. # Two of unbroken those received by the investor, summing to 0.2: self.assertEqual( Decimal(ContributionCertificate.objects.filter( transaction__comment=self.comment2, received_by=self.investor, matched=True, broken=False ).aggregate(total=Sum('hours')).get('total') or 0), Decimal('0.2') ) # Two of unbroken those received by the doer, summing to 0.2: self.assertEqual( Decimal(ContributionCertificate.objects.filter( transaction__comment=self.comment2, received_by=self.doer, matched=True, broken=False ).aggregate(total=Sum('hours')).get('total') or 0), Decimal('0.2') ) # Doer has 0.2 matched hours self.assertEqual( ContributionCertificate.user_matched(self.doer), Decimal('0.2') ) # Investor has 0.2 matched hours self.assertEqual( ContributionCertificate.user_matched(self.investor), Decimal('0.2') )