Skip to content

Instantly share code, notes, and snippets.

@artk-dev
Last active Oct 10, 2020
Embed
What would you like to do?
from unittest import TestCase
from mock import patch, Mock
# Uncomment if you experience problems importing the customer class:
#============================
#import sys
#sys.path.append('./')
#=============================
import customer
from customer import Customer
@patch('customer.TinyDB')
class TestCustomer(TestCase):
#Declare test data here
def setUp(self):
self.test_customer_id = '000'
self.test_customer_name = 'Bean'
self.test_email = 'bean@beanlovesteddy.com'
self.test_customer_type = 'Premium'
@patch('customer.Query')
def test_if_function_returns_true_when_customer_exists(self, mock_query, mock_tdb):
test_customer1 = customer.Customer(self.test_customer_id)
mock_tdb.return_value.search.return_value = [{'customer_id':self.test_customer_id}]
result = test_customer1.is_myshop_customer()
assert result==True
@patch('customer.Query')
def test_if_function_returns_false_when_customer_does_not_exist(self, mock_query, mock_tdb):
test_customer1 = customer.Customer(self.test_customer_id)
mock_tdb.return_value.search.return_value = []
result = test_customer1.is_myshop_customer()
assert result==False
@patch('customer.Customer.is_myshop_customer')
def test_new_customer_can_be_added(self, mock_customer_check, mock_tdb):
mock_insert = mock_tdb.return_value.insert
mock_customer_check.return_value = False
test_customer1 = customer.Customer(self.test_customer_id)
result = test_customer1.add_customer(self.test_customer_name, self.test_email, self.test_customer_type)
mock_insert.assert_called()
self.assertEquals(result, "** Customer Added **")
@patch('customer.Customer.is_myshop_customer')
def test_existing_customer_cannot_be_added(self, mock_customer_check, mock_tdb):
mock_insert = mock_tdb.return_value.insert
mock_customer_check.return_value = True
test_customer1 = customer.Customer(self.test_customer_id)
result = test_customer1.add_customer(self.test_customer_name, self.test_email, self.test_customer_type)
mock_insert.assert_not_called()
self.assertEquals(result, f"Customer with ID {self.test_customer_id} already exists!")
@patch('customer.Customer.is_myshop_customer')
def test_existing_customer_can_be_loader(self, mock_customer_check, mock_tdb):
mock_search = mock_tdb.return_value.search
mock_search.return_value = [{"customer_id": self.test_customer_id, "customer_name": self.test_customer_name, "email": self.test_email, "customer_type": self.test_customer_type}]
mock_customer_check.return_value = True
test_customer1 = customer.Customer(self.test_customer_id)
result = test_customer1.load_customer_data()
mock_search.assert_called()
self.assertEquals(test_customer1.customer_name, self.test_customer_name)
self.assertEquals(test_customer1.customer_type, self.test_customer_type)
self.assertEquals(test_customer1.email, self.test_email)
self.assertEquals(result, "** Customer Loaded **")
@patch('customer.Customer.is_myshop_customer')
def test_non_existent_customer_cannot_be_loader(self, mock_customer_check, mock_tdb):
mock_search = mock_tdb.return_value.search
mock_customer_check.return_value = False
test_customer1 = customer.Customer(self.test_customer_id)
result = test_customer1.load_customer_data()
mock_search.assert_not_called()
self.assertEquals(result, f"Customer with ID {self.test_customer_id} does not exist!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment