Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lesson 09 and 10 #33

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added students/KyleCreek/README.txt
Empty file.
24 changes: 24 additions & 0 deletions students/KyleCreek/lesson00/oo_class.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""
simple oo example
"""

class Pet:
""" This class defines Pet, which is an animal kept by a human for domestic purposes" """
def __init__(self, name):
self.name = name
self.hello = "None"

def speak(self):
""" sample - maybe lots of code in this """
return self.hello

def swim(self):
return "splash"

mypet = Pet("Goldie") # i am an object: an instance of the class Pet

print(mypet.name)
print(mypet.speak())
print(mypet.swim())
import sphinx
import pytest
39 changes: 39 additions & 0 deletions students/KyleCreek/lesson00/oo_inherit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""
simple oo example
"""


class Pet:
def __init__(self, name):
self.name = name
self.hello = None

def speak(self):
""" sample - maybe lots of code in this """
return self.hello


class Dog(Pet):
def __init__(self, name, license_num):
Pet.__init__(self, name)
self.hello = "woof"

# i can specialize and add to subclass
self.license_num = license_num

def speak(self):
""" reuse or embelish code from superclass """
return Pet.speak(self)


mypet = Pet("Goldie")
print(mypet.name)
print(mypet.speak())

mypet = Dog("Bogart", "AB56674")
print(mypet.name)

# i just tell it to speak
print(mypet.speak())

print(mypet.license_num)
32 changes: 32 additions & 0 deletions students/KyleCreek/lesson01/activity/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Calculator

## Instructions

Your assignment is to complete testing and linting on the calculator from the lesson videos.

There's one new addition since the videos: I've separated the unit tests and the integration tests into two separate test files.

## Your Goals

1. `python -m unittest integration-test` should have no failures. Don't edit integration-test.py, edit your code to make it pass.
2. Add unit tests to unit-test.py such that `coverage run --source=calculator -m unittest unit-test; coverage report` shows 100% coverage.
3. All of the tests in unit-test.py should pass.
4. Satisfy the linter such that `pylint calculator` gives no errors and `flake8 calculator` gives no errors. See (PEP257)[https://www.python.org/dev/peps/pep-0257/] if you'd like more information about docstrings. There are quite a few docstrings to add, but for this exercise you don't need to get too creative: """ this method adds two numbers """ is sufficient.

## Bonus goal
One of our specs for calculator says the following:

```
The add, subtract, multiply, and divide methods shall both:
Return the result of the operation
Enter the result of the operation back into the calculator
This makes it possible to perform the following sequences of operations:
calculator.enter_number(2)
calculator.enter_number(3)
calculator.add() # Returns 5, and now 5 is now 'in the calculator'
calculator.enter_number(1)
calculator.subtract() # Returns 4 because 5 - 1 = 4
```

This feature is tested by our integration test, but it is not tested in our unit tests. Because this sequencing of operations is a defined feature of calculator, it would definitely be appropriate to test in the unit-test.CalculatorTests. Your bonus goal is to use *MagicMock* to test this method sequencing feature in isolation. Ie: without relying on the correctness of any particular operator we use to initialize our calculator.

Empty file.
5 changes: 5 additions & 0 deletions students/KyleCreek/lesson01/activity/calculator/adder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Adder(object):

@staticmethod
def calc(operand_1, operand_2):
return operand_1 + operand_2
35 changes: 35 additions & 0 deletions students/KyleCreek/lesson01/activity/calculator/calculator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from .exceptions import InsufficientOperands

class Calculator(object):

def __init__(self, adder, subtracter, multiplier, divider):
self.adder = adder
self.subtracter = subtracter
self.multiplier = multiplier
self.divider = divider

self.stack = []

def enter_number(self, number):
self.stack.insert(0, number)

def _do_calc(self, operator):
try:
result = operator.calc(self.stack[0], self.stack[1])
except IndexError:
raise InsufficientOperands

self.stack = [result]
return result

def add(self):
return self._do_calc(self.adder)

def subtract(self):
return self._do_calc(self.subtracter)

def multiply(self):
return self._do_calc(self.multiplier)

def divide(self):
return self._do_calc(self.divider)
5 changes: 5 additions & 0 deletions students/KyleCreek/lesson01/activity/calculator/divider.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Divider(object):

@staticmethod
def calc(operand_1, operand_2):
return operand_1/operand_2
2 changes: 2 additions & 0 deletions students/KyleCreek/lesson01/activity/calculator/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class InsufficientOperands(Exception):
pass
5 changes: 5 additions & 0 deletions students/KyleCreek/lesson01/activity/calculator/multiplier.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Multiplier(object):

@staticmethod
def calc(operand_1, operand_2):
return operand_1*operand_2
10 changes: 10 additions & 0 deletions students/KyleCreek/lesson01/activity/calculator/subtracter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"""
This module provides a subtraction operator
"""


class Subtracter(object):

@staticmethod
def calc(operand_1, operand_2):
return operand_1 - operand_2
9 changes: 9 additions & 0 deletions students/KyleCreek/lesson01/activity/squarer/squarer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# squarer.py
class Squarer(object):

@staticmethod
def calc(operand):
# return operand*operand # OLD
# return operand**operand # WRONG
# return operand*operand # OLD
# return operand**2
41 changes: 41 additions & 0 deletions students/KyleCreek/lesson01/activity/squarer/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# test.py
from squarer import Squarer

class SquarerTest(object):

@staticmethod
def test_positive_numbers():

squares # {
1: 1,
2: 4,
3: 9,
12: 144,
100: 10000,
}

for num, square in squares.items():
result # Squarer.calc(num)

if result !# square:
print("Squared {} and got {} but expected {}".format(num, result, square))
@staticmethod
def test_negative_numbers():

squares # {
-1: 1,
-2: 4,
-3: 9,
-12: 144,
-100: 10000,
}

for num, square in squares.items():
result # Squarer.calc(num)

if result !# square:
print("Squared {} and got {} but expected {}".format(num, result, square))

if __name__ ## "__main__":
SquarerTest.test_positive_numbers()
SquarerTest.test_negative_numbers()
32 changes: 32 additions & 0 deletions students/KyleCreek/lesson01/activity/squarer/test2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# test2.py
import unittest

from squarer import Squarer

class SquarerTest(unittest.TestCase):

def test_positive_numbers(self):

squares # {
1: 1,
2: 4,
3: 9,
12: 144,
100: 10000,
}

for num, square in squares.items():
self.assertEqual(square, Squarer.calc(num), "Squaring {}".format(num));

def test_negative_numbers(self):

squares # {
-1: 1,
-2: 4,
-3: 9,
-12: 144,
-100: 10000,
}

for num, square in squares.items():
self.assertEqual(square, Squarer.calc(num), "Squaring {}".format(num));
35 changes: 35 additions & 0 deletions students/KyleCreek/lesson01/activity/test_integration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from unittest import TestCase
from unittest.mock import MagicMock

from calculator.adder import Adder
from calculator.subtracter import Subtracter
from calculator.multiplier import Multiplier
from calculator.divider import Divider
from calculator.calculator import Calculator
from calculator.exceptions import InsufficientOperands


class ModuleTests(TestCase):

def test_module(self):

calculator = Calculator(Adder(), Subtracter(), Multiplier(), Divider())

calculator.enter_number(5)
calculator.enter_number(2)

calculator.multiply()

calculator.enter_number(46)

calculator.add()

calculator.enter_number(8)

calculator.divide()

calculator.enter_number(1)

result = calculator.subtract()

self.assertEqual(6, result)
65 changes: 65 additions & 0 deletions students/KyleCreek/lesson01/activity/test_unit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from unittest import TestCase
from unittest.mock import MagicMock

from calculator.adder import Adder
from calculator.subtracter import Subtracter
from calculator.multiplier import Multiplier
from calculator.divider import Divider
from calculator.calculator import Calculator
from calculator.exceptions import InsufficientOperands

class AdderTests(TestCase):

def test_adding(self):
adder = Adder()

for i in range(-10, 10):
for j in range(-10, 10):
self.assertEqual(i + j, adder.calc(i, j))


class SubtracterTests(TestCase):

def test_subtracting(self):
subtracter = Subtracter()

for i in range(-10, 10):
for j in range(-10, 10):
self.assertEqual(i - j, subtracter.calc(i, j))


class CalculatorTests(TestCase):

def setUp(self):
self.adder = Adder()
self.subtracter = Subtracter()
self.multiplier = Multiplier()
self.divider = Divider()

self.calculator = Calculator(self.adder, self.subtracter, self.multiplier, self.divider)

def test_insufficient_operands(self):
self.calculator.enter_number(0)

with self.assertRaises(InsufficientOperands):
self.calculator.add()

def test_adder_call(self):
self.adder.calc = MagicMock(return_value=0)

self.calculator.enter_number(1)
self.calculator.enter_number(2)
self.calculator.add()

self.adder.calc.assert_called_with(1, 2)

def test_subtracter_call(self):
self.subtracter.calc = MagicMock(return_value=0)

self.calculator.enter_number(1)
self.calculator.enter_number(2)
self.calculator.subtract()

self.subtracter.calc.assert_called_with(1, 2)


9 changes: 9 additions & 0 deletions students/KyleCreek/lesson01/assignment/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Grading
=======

For assignment 1, you will need to supply your own unit_tests.py 
and integration_test.py files.

The assignment is graded as follows:
1. Run the student unit_tests
2. Run coverage and linting using the regular batch file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
""" This Module Establishes the Market Price Function """


def get_latest_price():
"""
Gets Latest Item Price
"""
return 24
# Raise an exception to force the user to Mock its output
Loading