def test_territory_exchange_with_resources_canceled(self): arthur = User.objects.get(username='******') brian = User.objects.get(username='******') aglax = Territory.objects.get(name='Aglax') efea = Territory.objects.get(name='Efea') offeror_r = Resources(currency=499) offeror_r.save() exchange = Exchange(offeror=arthur, offeror_territory=aglax, offeror_resources=offeror_r, offeree=brian, offeree_territory=efea) self.assertEqual(aglax.owner, arthur) self.assertEqual(efea.owner, brian) self.assertEqual(arthur.player.resources.currency, 1000) self.assertEqual(brian.player.resources.currency, 0) exchange.offer(user=arthur) self.assertEqual(arthur.player.resources.currency, 501) self.assertEqual(brian.player.resources.currency, 0) exchange.reject(user=brian) self.assertEqual(exchange.state, exchange.REJECTED) self.assertEqual(aglax.owner, arthur) self.assertEqual(efea.owner, brian) self.assertEqual(arthur.player.resources.currency, 1000) self.assertEqual(brian.player.resources.currency, 0)
def test_exchange_invalid_user(self): arthur = User.objects.get(username='******') brian = User.objects.get(username='******') dickens = User.objects.get(username='******') offeror_r = Resources(currency=100) offeror_r.save() offeree_r = Resources(agricultural=10) offeree_r.save() exchange = Exchange(offeror=arthur, offeror_resources=offeror_r, offeree=brian, offeree_resources=offeree_r) with self.assertRaisesRegexp(ValidationError, "“brian” is not the offeror of this exchange."): exchange.offer(user=brian) exchange.offer(user=arthur) with self.assertRaisesRegexp(ValidationError, "“arthur” is not the offeree of this exchange."): exchange.accept(user=arthur) with self.assertRaisesRegexp(ValidationError, "“arthur” is not the offeree of this exchange."): exchange.reject(user=arthur) with self.assertRaisesRegexp(ValidationError, "“brian” is not the offeror of this exchange."): exchange.cancel(user=brian) self.assertEqual(exchange.state, exchange.WAITING)
def test_simple_exchange_accept(self): arthur = User.objects.get(username='******') brian = User.objects.get(username='******') dickens = User.objects.get(username='******') offeror_r = Resources(currency=100) offeror_r.save() offeree_r = Resources(agricultural=10) offeree_r.save() exchange = Exchange(offeror=arthur, offeror_resources=offeror_r, offeree=brian, offeree_resources=offeree_r) arthur.player.resources.currency = 5 arthur.player.resources.save() with self.assertRaisesRegexp(ValidationError, "Offeror “arthur” lack resources to offer this exchange."): exchange.offer(user=arthur) arthur.player.resources.currency = 1000 arthur.player.resources.save() exchange.offer(user=arthur) self.assertEqual(arthur.player.resources.currency, 900) brian.player.resources.agricultural = 3 brian.player.resources.save() with self.assertRaisesRegexp(ValidationError, "Offeree “brian” lack resources to accept this exchange."): exchange.accept(user=brian) brian.player.resources.agricultural = 11 brian.player.resources.save() self.assertEqual(exchange.state, exchange.WAITING) self.assertEqual(arthur.player.resources.currency, 900) self.assertEqual(arthur.player.resources.agricultural, 0) self.assertEqual(brian.player.resources.currency, 0) self.assertEqual(brian.player.resources.agricultural, 11) self.assertTrue(exchange.accept(user=brian)) self.assertEqual(exchange.state, exchange.ACCEPTED) self.assertEqual(arthur.player.resources.currency, 900) self.assertEqual(arthur.player.resources.agricultural, 10) self.assertEqual(brian.player.resources.currency, 100) self.assertEqual(brian.player.resources.agricultural, 1)
def test_public_exchange_offering_accept_reject(self): arthur = User.objects.get(username='******') brian = User.objects.get(username='******') patsy = User.objects.get(username='******') offeror_r = Resources(currency=499) offeror_r.save() offeree_r = Resources(agricultural=1) offeree_r.save() exchange = Exchange(offeror=arthur, offeror_resources=offeror_r, offeree_resources=offeree_r)
def test_simple_exchange_cancel(self): arthur = User.objects.get(username='******') brian = User.objects.get(username='******') dickens = User.objects.get(username='******') offeror_r = Resources(currency=100) offeror_r.save() offeree_r = Resources(agricultural=10) offeree_r.save() exchange = Exchange(offeror=arthur, offeror_resources=offeror_r, offeree=brian, offeree_resources=offeree_r) self.assertEqual(brian.player.resources.currency, 0) self.assertEqual(brian.player.resources.agricultural, 11) self.assertEqual(arthur.player.resources.currency, 1000) self.assertEqual(arthur.player.resources.agricultural, 0) exchange.offer(user=arthur) self.assertEqual(exchange.state, exchange.WAITING) self.assertEqual(brian.player.resources.currency, 0) self.assertEqual(brian.player.resources.agricultural, 11) self.assertEqual(arthur.player.resources.currency, 900) self.assertEqual(arthur.player.resources.agricultural, 0) self.assertTrue(exchange.cancel(user=arthur)) self.assertEqual(exchange.state, exchange.CANCELED) self.assertEqual(brian.player.resources.currency, 0) self.assertEqual(brian.player.resources.agricultural, 11) self.assertEqual(arthur.player.resources.currency, 1000) self.assertEqual(arthur.player.resources.agricultural, 0) with self.assertRaisesRegexp(ValidationError, "This exchange is not waiting for response."): exchange.accept(user=brian) with self.assertRaisesRegexp(ValidationError, "This exchange is not waiting for response."): exchange.reject(user=brian) with self.assertRaisesRegexp(ValidationError, "This exchange is not waiting for response."): exchange.cancel(user=arthur)
def test_bond_lending(self): arthur = User.objects.get(username='******') brian = User.objects.get(username='******') offeror_r = Resources(currency=2000) offeror_r.save() offeree_r = Resources(currency=1000) offeree_r.save() # Arthur offers: $2000 as Bond in up to 2 turns in exchange of Brian # sending $1000. exchange = Exchange(offeror=brian, offeror_resources=offeror_r, offeror_as_bond=True, offeror_as_bond_maturity=2, offeree=arthur, offeree_resources=offeree_r) self.assertEqual(brian.player.resources.currency, 0) self.assertEqual(arthur.player.resources.currency, 1000) exchange.offer(user=brian) self.assertEqual(brian.player.resources.currency, 0) self.assertEqual(arthur.player.resources.currency, 1000) exchange.accept(user=arthur) self.assertEqual(exchange.state, exchange.ACCEPTED) self.assertEqual(brian.player.resources.currency, 1000) self.assertEqual(arthur.player.resources.currency, 0) bond = Bond.objects.filter(borrower=brian)[0] self.assertEqual(bond.state, bond.PENDING) with self.assertRaisesRegexp(ValidationError, "“arthur” is not the borrower of this bond."): bond.pay(user=arthur) with self.assertRaisesRegexp(ValidationError, "Offeror “brian” lack resources to offer this exchange."): bond.pay(user=brian) brian.player.resources.currency = 2000 brian.player.resources.save() # Need to request Bond again, to get updated Brian information bond = Bond.objects.filter(borrower=brian)[0] self.assertEqual(bond.state, bond.PENDING) bond.pay(user=brian) self.assertEqual(bond.state, bond.PAID) with self.assertRaisesRegexp(ValidationError, "This bond is not pending."): bond.pay(user=brian)
def test_empty_exchange(self): arthur = User.objects.get(username='******') brian = User.objects.get(username='******') offeror_r = Resources(currency=0) offeror_r.save() offeree_r = Resources(agricultural=0) offeree_r.save() exchange = Exchange(offeror=arthur, offeror_resources=offeror_r, offeree=brian, offeree_resources=offeree_r) with self.assertRaisesRegexp(ValidationError, "Empty exchange."): exchange.offer(user=arthur)
def test_bond_payment(self): arthur = User.objects.get(username='******') brian = User.objects.get(username='******') brian.player.resources.currency = 1000 brian.player.resources.save() offeror_r = Resources(currency=450) offeror_r.save() offeree_r = Resources(currency=311) offeree_r.save() # Arthur offers: $2000 as Bond in up to 2 turns in exchange of Brian # sending $1000. exchange = Exchange(offeror=brian, offeror_resources=offeror_r, offeror_as_bond=True, offeror_as_bond_maturity=2, offeree=arthur, offeree_resources=offeree_r, offeree_as_bond=True, offeree_as_bond_maturity=2) self.assertEqual(brian.player.resources.currency, 1000) self.assertEqual(arthur.player.resources.currency, 1000) exchange.offer(user=brian) self.assertEqual(brian.player.resources.currency, 1000) self.assertEqual(arthur.player.resources.currency, 1000) exchange.accept(user=arthur) self.assertEqual(exchange.state, exchange.ACCEPTED) self.assertEqual(brian.player.resources.currency, 1000) self.assertEqual(arthur.player.resources.currency, 1000) brian_bond = Bond.objects.filter(borrower=brian)[0] self.assertEqual(brian_bond.state, brian_bond.PENDING) arthur_bond = Bond.objects.filter(borrower=arthur)[0] self.assertEqual(arthur_bond.state, arthur_bond.PENDING) brian_bond.pay(user=brian) arthur_bond.pay(user=arthur) self.assertEqual(brian_bond.state, brian_bond.PAID) self.assertEqual(arthur_bond.state, arthur_bond.PAID) arthur = User.objects.get(username='******') brian = User.objects.get(username='******') self.assertEqual(brian.player.resources.currency, 1000 - 450 + 311) self.assertEqual(arthur.player.resources.currency, 1000 - 311 + 450)
def test_exchange_between_the_same_player(self): arthur = User.objects.get(username='******') brian = User.objects.get(username='******') offeror_r = Resources(currency=1) offeror_r.save() offeree_r = Resources(agricultural=1) offeree_r.save() exchange = Exchange(offeror=arthur, offeror_resources=offeror_r, offeree=arthur, offeree_resources=offeree_r) with self.assertRaisesRegexp(ValidationError, "Offeror and offeree cannot be the same."): exchange.offer(user=arthur)
def test_simple_ask_for_donation_cancel(self): arthur = User.objects.get(username='******') brian = User.objects.get(username='******') offeree_r = Resources(agricultural=1) offeree_r.save() exchange = Exchange(offeror=arthur, offeree=brian, offeree_resources=offeree_r) self.assertEqual(arthur.player.resources.agricultural, 0) self.assertEqual(brian.player.resources.agricultural, 11) exchange.offer(user=arthur) self.assertEqual(arthur.player.resources.agricultural, 0) self.assertEqual(brian.player.resources.agricultural, 11) exchange.cancel(user=arthur) self.assertEqual(exchange.state, exchange.CANCELED) self.assertEqual(arthur.player.resources.agricultural, 0) self.assertEqual(brian.player.resources.agricultural, 11)
def test_simple_donation_cancel(self): arthur = User.objects.get(username='******') brian = User.objects.get(username='******') offeror_r = Resources(currency=1) offeror_r.save() exchange = Exchange(offeror=arthur, offeror_resources=offeror_r, offeree=brian) self.assertEqual(arthur.player.resources.currency, 1000) self.assertEqual(brian.player.resources.currency, 0) exchange.offer(user=arthur) self.assertEqual(arthur.player.resources.currency, 999) self.assertEqual(brian.player.resources.currency, 0) exchange.cancel(user=arthur) self.assertEqual(exchange.state, exchange.CANCELED) self.assertEqual(arthur.player.resources.currency, 1000) self.assertEqual(brian.player.resources.currency, 0)
def test_fail_bond_of_bond(self): arthur = User.objects.get(username='******') brian = User.objects.get(username='******') offeror_r = Resources(currency=499) offeror_r.save() bond = Bond(holder=arthur, borrower=brian) bond.save() exchange = Exchange(offeror=arthur, offeror_bond=bond, offeror_as_bond=True, offeree=brian) with self.assertRaisesRegexp(ValidationError, "Cannot build a Bond of Bond."): exchange.offer(user=arthur) exchange = Exchange(offeror=arthur, offeree=brian, offeree_bond=bond, offeree_as_bond=True) with self.assertRaisesRegexp(ValidationError, "Cannot build a Bond of Bond."): exchange.offer(user=arthur)
def test_territory_buy_reject(self): arthur = User.objects.get(username='******') brian = User.objects.get(username='******') aglax = Territory.objects.get(name='Aglax') efea = Territory.objects.get(name='Efea') offeree_r = Resources(agricultural=10) offeree_r.save() exchange = Exchange(offeror=arthur, offeror_territory=aglax, offeree=brian, offeree_resources=offeree_r) self.assertEqual(aglax.owner, arthur) self.assertEqual(arthur.player.resources.agricultural, 0) self.assertEqual(brian.player.resources.agricultural, 11) exchange.offer(user=arthur) self.assertEqual(arthur.player.resources.agricultural, 0) self.assertEqual(brian.player.resources.agricultural, 11) exchange.reject(user=brian) self.assertEqual(exchange.state, exchange.REJECTED) self.assertEqual(aglax.owner, arthur) self.assertEqual(arthur.player.resources.agricultural, 0) self.assertEqual(brian.player.resources.agricultural, 11)
def test_bond_payment(self): arthur = User.objects.get(username='******') brian = User.objects.get(username='******') gumby = User.objects.get(username='******') brian.player.resources.currency = 1000 brian.player.resources.save() offeror_r = Resources(currency=450) offeror_r.save() offeree_r = Resources(currency=311) offeree_r.save() exchange = Exchange(offeror=brian, offeror_resources=offeror_r, offeror_as_bond=True, offeror_as_bond_maturity=2, offeree=arthur, offeree_resources=offeree_r, offeree_as_bond=True, offeree_as_bond_maturity=2) self.assertEqual(brian.player.resources.currency, 1000) self.assertEqual(arthur.player.resources.currency, 1000) exchange.offer(user=brian) self.assertEqual(brian.player.resources.currency, 1000) self.assertEqual(arthur.player.resources.currency, 1000) exchange.accept(user=arthur) self.assertEqual(exchange.state, exchange.ACCEPTED) self.assertEqual(brian.player.resources.currency, 1000) self.assertEqual(arthur.player.resources.currency, 1000) brian_bond = Bond.objects.filter(borrower=brian)[0] self.assertEqual(brian_bond.state, brian_bond.PENDING) arthur_bond = Bond.objects.filter(borrower=arthur)[0] self.assertEqual(arthur_bond.state, arthur_bond.PENDING) exchange = Exchange(offeror=brian, offeror_bond=brian_bond, offeree=arthur) with self.assertRaisesRegexp(ValidationError, ("Cannot exchange a Bond with its holder. " "To pay the bond, use the payment section.")): exchange.offer(user=brian) exchange = Exchange(offeror=brian, offeror_bond=arthur_bond, offeree=arthur) with self.assertRaisesRegexp(ValidationError, ("“brian” is not the borrower of this Bond.")): exchange.offer(user=brian) exchange = Exchange(offeror=brian, offeree=arthur, offeree_bond=brian_bond) with self.assertRaisesRegexp(ValidationError, "“arthur” is not the holder of this Bond."): exchange.offer(user=brian) exchange = Exchange(offeror=brian, offeree=arthur, offeree_bond=arthur_bond) with self.assertRaisesRegexp(ValidationError, "Cannot exchange a Bond with its holder."): exchange.offer(user=brian) brian_bond.pay(user=brian) arthur_bond.pay(user=arthur) exchange = Exchange(offeror=gumby, offeree=arthur, offeree_bond=arthur_bond) with self.assertRaisesRegexp(ValidationError, "This Bond is not pending."): exchange.offer(user=gumby) exchange = Exchange(offeror=arthur, offeror_bond=arthur_bond, offeree=gumby) with self.assertRaisesRegexp(ValidationError, "This Bond is not pending."): exchange.offer(user=arthur) self.assertEqual(brian_bond.state, brian_bond.PAID) self.assertEqual(arthur_bond.state, arthur_bond.PAID) arthur = User.objects.get(username='******') brian = User.objects.get(username='******') self.assertEqual(brian.player.resources.currency, 1000 - 450 + 311) self.assertEqual(arthur.player.resources.currency, 1000 - 311 + 450)
def test_bond_payment_while_offering(self): arthur = User.objects.get(username='******') brian = User.objects.get(username='******') gumby = User.objects.get(username='******') brian.player.resources.currency = 1000 brian.player.resources.save() gumby.player.resources.currency = 455 gumby.player.resources.save() offeror_r = Resources(currency=450) offeror_r.save() offeree_r = Resources(currency=311) offeree_r.save() exchange = Exchange(offeror=brian, offeror_resources=offeror_r, offeror_as_bond=True, offeror_as_bond_maturity=2, offeree=arthur, offeree_resources=offeree_r, offeree_as_bond=True, offeree_as_bond_maturity=2) exchange.offer(user=brian) exchange.accept(user=arthur) brian_bond = Bond.objects.filter(borrower=brian)[0] self.assertEqual(brian_bond.holder, arthur) self.assertEqual(brian_bond.borrower, brian) self.assertEqual(brian_bond.state, brian_bond.PENDING) arthur_bond = Bond.objects.filter(borrower=arthur)[0] self.assertEqual(arthur_bond.holder, brian) self.assertEqual(arthur_bond.borrower, arthur) self.assertEqual(arthur_bond.state, arthur_bond.PENDING) exchange = Exchange(offeror=gumby, offeree=arthur, offeree_bond=arthur_bond) exchange.offer(user=gumby) self.assertEqual(exchange.state, exchange.WAITING) self.assertEqual(arthur_bond.holder, brian) self.assertEqual(arthur_bond.borrower, arthur) arthur_bond.pay(user=arthur) brian = User.objects.get(username='******') arthur = User.objects.get(username='******') gumby = User.objects.get(username='******') self.assertEqual(brian.player.resources.currency, 1000 + 311) self.assertEqual(arthur.player.resources.currency, 1000 - 311) self.assertEqual(gumby.player.resources.currency, 455) with self.assertRaisesRegexp(ValidationError, "This Bond is not pending."): exchange.accept(user=arthur) exchange = Exchange(offeror=gumby, offeree=brian, offeree_bond=brian_bond) exchange.offer(user=gumby) exchange.accept(user=brian) # Now brian's Bond became Gumby's Bond gumby_bond = Bond.objects.filter(borrower=gumby)[0] self.assertEqual(gumby_bond.holder, arthur) self.assertEqual(gumby_bond.borrower, gumby) gumby_bond.pay(user=gumby) arthur = User.objects.get(username='******') brian = User.objects.get(username='******') gumby = User.objects.get(username='******') self.assertEqual(brian.player.resources.currency, 1000 + 311) self.assertEqual(arthur.player.resources.currency, 1000 - 311 + 450) self.assertEqual(gumby.player.resources.currency, 5)