OLD | NEW |
(Empty) | |
| 1 # Copyright 2016 Google Inc. All Rights Reserved. |
| 2 # |
| 3 # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 # you may not use this file except in compliance with the License. |
| 5 # You may obtain a copy of the License at |
| 6 # |
| 7 # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 # |
| 9 # Unless required by applicable law or agreed to in writing, software |
| 10 # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 # See the License for the specific language governing permissions and |
| 13 # limitations under the License. |
| 14 |
| 15 from __future__ import absolute_import |
| 16 |
| 17 import sys |
| 18 import unittest2 |
| 19 from expects import expect, equal, raise_error |
| 20 |
| 21 from google.api.control import money, messages |
| 22 |
| 23 |
| 24 class TestCheckValid(unittest2.TestCase): |
| 25 _BAD_CURRENCY = messages.Money(currencyCode='this-is-bad') |
| 26 _MISMATCHED_UNITS = ( |
| 27 messages.Money(currencyCode='JPY', units=-1, nanos=1), |
| 28 messages.Money(currencyCode='JPY', units=1, nanos=-1), |
| 29 ) |
| 30 _NANOS_OOB = messages.Money(currencyCode='EUR', units=0, nanos=9999999999) |
| 31 _OK = ( |
| 32 messages.Money(currencyCode='JPY', units=1, nanos=1), |
| 33 messages.Money(currencyCode='JPY', units=-1, nanos=-1), |
| 34 messages.Money(currencyCode='EUR', units=0, nanos=money.MAX_NANOS), |
| 35 ) |
| 36 |
| 37 def test_should_fail_if_not_really_money(self): |
| 38 expect(lambda: money.check_valid(object())).to(raise_error(ValueError)) |
| 39 expect(lambda: money.check_valid(None)).to(raise_error(ValueError)) |
| 40 |
| 41 def test_should_fail_when_no_currency_is_set(self): |
| 42 expect(lambda: money.check_valid(messages.Money())).to( |
| 43 raise_error(ValueError)) |
| 44 |
| 45 def test_should_fail_when_the_currency_is_bad(self): |
| 46 expect(lambda: money.check_valid(self._BAD_CURRENCY)).to( |
| 47 raise_error(ValueError)) |
| 48 |
| 49 def test_should_fail_when_the_units_and_nanos_are_mismatched(self): |
| 50 for m in self._MISMATCHED_UNITS: |
| 51 expect(lambda: money.check_valid(m)).to(raise_error(ValueError)) |
| 52 |
| 53 def test_should_fail_when_nanos_are_oob(self): |
| 54 expect(lambda: money.check_valid(self._NANOS_OOB)).to( |
| 55 raise_error(ValueError)) |
| 56 |
| 57 def test_should_succeed_for_ok_instances(self): |
| 58 for m in self._OK: |
| 59 money.check_valid(m) |
| 60 |
| 61 |
| 62 class TestAdd(unittest2.TestCase): |
| 63 _SOME_YEN = messages.Money(currencyCode='JPY', units=3, nanos=0) |
| 64 _SOME_YEN_DEBT = messages.Money(currencyCode='JPY', units=-2, nanos=-1) |
| 65 _SOME_MORE_YEN = messages.Money(currencyCode='JPY', units=1, nanos=3) |
| 66 _SOME_USD = messages.Money(currencyCode='USD', units=1, nanos=0) |
| 67 _INT64_MAX = sys.maxint |
| 68 _INT64_MIN = -sys.maxint - 1 |
| 69 _LARGE_YEN = messages.Money(currencyCode='JPY', units=_INT64_MAX -1, nanos=0
) |
| 70 _LARGE_YEN_DEBT = messages.Money(currencyCode='JPY', units=-_INT64_MAX + 1,
nanos=0) |
| 71 |
| 72 def test_should_fail_if_non_money_is_used(self): |
| 73 testfs = [ |
| 74 lambda: money.add(self._SOME_YEN, object()), |
| 75 lambda: money.add(object(), self._SOME_USD), |
| 76 lambda: money.add(None, self._SOME_USD), |
| 77 lambda: money.add(self._SOME_YEN, None), |
| 78 ] |
| 79 for testf in testfs: |
| 80 expect(testf).to(raise_error(ValueError)) |
| 81 |
| 82 def test_should_fail_on_currency_mismatch(self): |
| 83 testf = lambda: money.add(self._SOME_YEN, self._SOME_USD) |
| 84 expect(testf).to(raise_error(ValueError)) |
| 85 |
| 86 def test_should_fail_on_unallowed_positive_overflows(self): |
| 87 testf = lambda: money.add(self._SOME_YEN, self._LARGE_YEN) |
| 88 expect(testf).to(raise_error(OverflowError)) |
| 89 |
| 90 def test_should_allow_positive_overflows(self): |
| 91 overflowing = money.add(self._SOME_YEN, self._LARGE_YEN, |
| 92 allow_overflow=True) |
| 93 expect(overflowing.units).to(equal(self._INT64_MAX)) |
| 94 expect(overflowing.nanos).to(equal(money.MAX_NANOS)) |
| 95 |
| 96 def test_should_fail_on_unallowed_negative_overflows(self): |
| 97 testf = lambda: money.add(self._SOME_YEN_DEBT, self._LARGE_YEN_DEBT) |
| 98 expect(testf).to(raise_error(OverflowError)) |
| 99 |
| 100 def test_should_allow_negative_overflows(self): |
| 101 overflowing = money.add(self._SOME_YEN_DEBT, self._LARGE_YEN_DEBT, |
| 102 allow_overflow=True) |
| 103 expect(overflowing.units).to(equal(self._INT64_MIN)) |
| 104 expect(overflowing.nanos).to(equal(-money.MAX_NANOS)) |
| 105 |
| 106 def test_should_add_ok_when_nanos_have_same_sign(self): |
| 107 the_sum = money.add(self._SOME_YEN, self._SOME_YEN) |
| 108 expect(the_sum.units).to(equal(2 * self._SOME_YEN.units)) |
| 109 |
| 110 def test_should_add_ok_when_nanos_have_different_signs(self): |
| 111 the_sum = money.add(self._SOME_YEN, self._SOME_YEN_DEBT) |
| 112 want_units = self._SOME_YEN_DEBT.units + self._SOME_YEN.units - 1 |
| 113 expect(the_sum.units).to(equal(want_units)) |
| 114 expect(the_sum.nanos).to(equal(money.MAX_NANOS)) |
| 115 the_sum = money.add(self._SOME_MORE_YEN, self._SOME_YEN_DEBT) |
| 116 want_units = self._SOME_YEN_DEBT.units + self._SOME_YEN.units - 1 |
| 117 expect(the_sum.units).to(equal(want_units)) |
| 118 expect(the_sum.nanos).to(equal(1 - money.MAX_NANOS)) |
OLD | NEW |