예제 #1
0
    def test_raise_value_error_if_not_enough_stock_is_present(self):
        stock = [Stock('chocolate', 47)]
        names = ['John', 'Jane']

        with self.assertRaises(ValueError) as context:
            calculate(stock, names)

        self.assertEqual(str(context.exception),
                         'Not enough presents for everyone :(')
예제 #2
0
    def test_randomizes_order_of_presents(self, shuffle):
        stock = [
            Stock('wine', 24),
            Stock('beer', 24),
        ]

        randomized_stock = ['wine', 'beer'] * 24
        expected_stock = ['wine'] * 24 + ['beer'] * 24

        def shuffle_effect(input):
            self.assertEqual(input, expected_stock)
            for n, _ in enumerate(input):
                input[n] = randomized_stock[n]

        shuffle.side_effect = shuffle_effect

        names = ['Jane', 'John']
        calendars = calculate(stock, names)

        janesCalendar = calendars['Jane']
        johnsCalendar = calendars['John']
        self.assertEqual(list(janesCalendar.values()), ['wine'] * 24)
        self.assertEqual(list(johnsCalendar.values()), ['beer'] * 24)

        shuffle.assert_called()
예제 #3
0
    def test_calculate_calendar_from_stock(self):
        stock = [Stock('chocolate', 7), Stock('sweets', 8), Stock('coal', 9)]
        names = ['Jane']

        calendars = calculate(stock, names)

        self.assertEqual(len(calendars), 1)
        self.assertEqual(len(calendars['Jane']), 24)

        calendar = calendars['Jane']
        self.assertEqual(list(calendar.values()).count('chocolate'), 7)
        self.assertEqual(list(calendar.values()).count('sweets'), 8)
        self.assertEqual(list(calendar.values()).count('coal'), 9)
def test_easy():
    assert 4 == calculate('2 + 2')
def test_complex():
    assert 1.5 == calculate('2 * 2 + -2.5')
def test_factorial():
    assert approx(64) == calculate('2 + 5 ! * sin ( 15 + 5 * 3 ) + 2')
def test_hyperfunc():
    assert approx(0.2766) == calculate('-2.5 + sh 100')
def test_unary_expr_easy():
    assert approx(0.5) == calculate('sin 30')
def test_unary_expr_complex():
    assert 4.5 == calculate('2 + sin ( 15 + 5 * 3 ) + 2')
def test_parenthesized_expr():
    assert 12 == calculate('( 2 + 8 / 2 ) * 2')
def test_aprox2():
    assert approx(0.00495) == calculate('495 / 1000 / 100')
def test_priorities():
    assert 10 == calculate('2 + 2 * 2 ** 2')
def test_zero_division():
    with pytest.raises(ZeroDivisionError):
        calculate('8 / 0')
def test_wrong_value():
    with pytest.raises(ValueError):
        calculate('letter')