def test_update_customer(): """ GIVEN CreateCustomerCommand with a valid properties author, title and content WHEN the execute method is called THEN a new Customer must exist in the database with the same attributes """ customer = Customer(first_name="Opemipo", last_name="Durodola", phone_number="08182104309", email="*****@*****.**", address="Row 3 House 6 Staff Qtrs", city="Lagos").create_customer() cmd = UpdateCustomerCommand(id=customer.id, update={ "first_name": "Emmanuel", "last_name": "Caster", "phone_number": "08182104309", "email": "*****@*****.**", "address": "Row 3 House 6 Staff Qtrs", "city": "Lagos" }) cmd.execute() db_customer = Customer.get(customer.id) assert db_customer["first_name"] == cmd.update["first_name"] assert db_customer["last_name"] == cmd.update["last_name"] assert db_customer["phone_number"] == cmd.update["phone_number"] assert db_customer["email"] == cmd.update["email"] assert db_customer["address"] == cmd.update["address"] assert db_customer["city"] == cmd.update["city"]
def delete_all_rows(): """delete_all_rows() Deletes all data from the Customer table so we can have fresh tests. """ with DATABASE.transaction(): Customer.delete().execute() LOGGER.info("Clear all data in Customer table.")
def _create_customer(cls, user_id, server_id): session = get_session() customer = Customer(user_id=user_id, server_id=server_id) session.add(customer) session.flush() session.commit() return customer
def add_customer(body): if request.is_json: first_name = body.get('first_name') last_name = body.get('last_name') email = body.get('email') phone_number = body.get('phone_number') new_customer = Customer(first_name=first_name, last_name=last_name, email=email, phone_number=phone_number) existing_customer = Customer.query\ .filter(Customer.first_name == first_name)\ .filter(Customer.last_name == last_name)\ .filter(Customer.email == email)\ .filter(Customer.phone_number == phone_number)\ .one_or_none() if existing_customer is None: try: db.session.add(new_customer) db.session.commit() return make_response('New customer was successfully created', 201) except IntegrityError: return make_response('That email/phone number already exists', 409) else: return make_response( f'Customer, {first_name} {last_name}, already exists', 409)
def test_update_customer(client): """ GIVEN ID of customer stored in the database WHEN endpoint /update-customer/<id-of-customer>/ is called THEN it should update """ data = { 'first_name': 'Emmanuel', 'last_name': 'Caster', 'phone_number': '08182104309', 'email': '*****@*****.**', 'address': 'Row 3 House 6 Staff Qtrs', 'city': 'Ogun', } customer = Customer(first_name="Opemipo", last_name="Durodola", phone_number="08182104309", email="*****@*****.**", address="Row 3 House 6 Staff Qtrs", city="Lagos").create_customer() response = client.put( f'/update-customer/{customer.id}/', data=json.dumps(data), content_type='application/json', ) validate_payload(response.json, 'UpdateCustomer.json')
def execute(self) -> Customer: try: Customer.get_by_email(self.email) raise AlreadyExists except NotFound: pass customer = Customer( first_name=self.first_name, last_name=self.last_name, phone_number=self.phone_number, email=self.email, address=self.address, city=self.city ).create_customer( ) return customer
def all() -> Generator: with open(DATA_STORE_PATH, 'r') as file: for line in file.readlines(): json_data = json.loads(line) geo_location = GeoLocation(float(json_data['longitude']), float(json_data['latitude'])) customer_obj = Customer(json_data['user_id'], json_data['name'], geo_location) yield customer_obj
def setup(self): self.customer = Customer("1") self.transaction = Transaction( { "id": "15887", "customer_id": "528", "load_amount": "$3318.47", "time": "2000-01-01T00:00:00Z", } )
class TestCustomer: @pytest.fixture(autouse=True) def set_up(self): self.customer = Customer("528") def test_create_customer(self): customer_id = "1" customer = Customer(customer_id) assert customer assert customer.customer_id == customer_id def test_add_transaction(self): input_data = [ { "id": "15887", "customer_id": "528", "load_amount": "$1000.00", "time": "2000-01-01T00:00:00Z", }, { "id": "30081", "customer_id": "528", "load_amount": "$1000.00", "time": "2000-01-01T01:01:22Z", }, { "id": "26540", "customer_id": "528", "load_amount": "$1000.00", "time": "2000-01-02T02:02:44Z", }, ] for data in input_data: transaction = Transaction(data) self.customer.perform_transaction(transaction) assert isinstance(self.customer.transaction_list, list) len(self.customer.transaction_list) == len(data) for transaction in self.customer.transaction_list: assert transaction assert isinstance(transaction, Transaction)
def test_list_customers(): """ GIVEN 2 customers stored in the database WHEN the execute method is called THEN it should return 2 customers """ Customer(first_name="Opemipo", last_name="Durodola", phone_number="08182104309", email="*****@*****.**", address="Row 3 House 6 Staff Qtrs", city="Lagos").create_customer() Customer(first_name="Emmanuel", last_name="Caster", phone_number="08182104309", email="*****@*****.**", address="Row 3 House 6 Staff Qtrs", city="Lagos").create_customer() query = ListCustomersQuery() assert len(query.execute()) == 2
def add_customer(customer_id, name, last_name, home_address, phone_number, email_address, status, credit_limit): """add_customer(customer_id, name, last_name, home_address, phone_number, email_address, status, credit_limit) Add customer info into DB based on provided params. :param customer_id str: Unique customer ID :param name str: First name of customer :param last_name str: Last name of customer :param str: Phone number of customer :param email_address str: Email address of customer :param status str: Active or Inactive customer :param credit_limit int: Credit limit for customer :return None :rtype None """ try: with DATABASE.transaction(): Customer.create( customer_id=customer_id, name=name, last_name=last_name, home_address=home_address, phone_number=phone_number, email_address=email_address, status=status, credit_limit=credit_limit) LOGGER.info("Database add successful") except peewee.IntegrityError: LOGGER.error("Unique constraint failed on %s", customer_id)
def quote_dependencies(test_db): new_customer = Customer(first_name='John', last_name='Doe', email='*****@*****.**', phone_number='(000) 000-0000') new_product = Product(name='Tables', description='10 seater', quantity=5, price=500) test_db.session.add(new_customer) test_db.session.add(new_product) test_db.session.commit()
def test_get_customer_by_id(): """ GIVEN ID customers stored in the database WHEN the execute method is called on GetCustomerByIDQuery with id set THEN it should return the customer with the same id """ customer = Customer(first_name="Opemipo", last_name="Durodola", phone_number="08182104309", email="*****@*****.**", address="Row 3 House 6 Staff Qtrs", city="Lagos").create_customer() query = GetCustomerByIDQuery(id=customer.id) assert query.execute()["id"] == customer.id
def test_delete_customer(): """ GIVEN ID customers stored in the database WHEN the execute method is called on DeleteCustomer with id set THEN it should deleted """ customer = Customer(first_name="Opemipo", last_name="Durodola", phone_number="08182104309", email="*****@*****.**", address="Row 3 House 6 Staff Qtrs", city="Lagos").create_customer() query = DeleteCustomer(id=customer.id) assert query.execute() == "Deleted"
def test_add_new_customer_to_database(test_db): new_customer = Customer(first_name='John', last_name='Doe', email='*****@*****.**', phone_number='(000) 000-0000') test_db.session.add(new_customer) test_db.session.commit() customer = Customer.query\ .filter(Customer.first_name == 'John')\ .filter(Customer.last_name == 'Doe')\ .filter(Customer.email == '*****@*****.**')\ .filter(Customer.phone_number == '(000) 000-0000')\ .one_or_none() assert customer is not None
def test_get_customer(client): """ GIVEN ID of customer stored in the database WHEN endpoint /customer/<id-of-customer>/ is called THEN it should return Customer in json format matching schema """ customer = Customer(first_name="Opemipo", last_name="Durodola", phone_number="08182104309", email="*****@*****.**", address="Row 3 House 6 Staff Qtrs", city="Lagos").create_customer() response = client.get( f'/customer/{customer.id}/', content_type='application/json', ) validate_payload(response.json, 'Customer.json')
def test_create_customer_already_exists(): """ GIVEN CreateCustomerCommand with a email of some customer in database WHEN the execute method is called THEN the AlreadyExists exception must be raised """ Customer(first_name="Emmanuel", last_name="Caster", phone_number="08182104309", email="*****@*****.**", address="Row 3 House 6 Staff Qtrs", city="Lagos").create_customer() cmd = CreateCustomerCommand(first_name="Opemipo", last_name="Durodola", phone_number="08182104309", email="*****@*****.**", address="Row 3 House 6 Staff Qtrs", city="Lagos") with pytest.raises(AlreadyExists): cmd.execute()
def test_create_customer(): """ GIVEN CreateCustomerCommand with a valid properties author, title and content WHEN the execute method is called THEN a new Customer must exist in the database with the same attributes """ cmd = CreateCustomerCommand(first_name="Emmanuel", last_name="Caster", phone_number="08182104309", email="*****@*****.**", address="Row 3 House 6 Staff Qtrs", city="Lagos") customer = cmd.execute() db_customer = Customer.get_by_email(customer.email) assert db_customer["first_name"] == customer.first_name assert db_customer["last_name"] == customer.last_name assert db_customer["phone_number"] == customer.phone_number assert db_customer["email"] == customer.email assert db_customer["address"] == customer.address assert db_customer["city"] == customer.city
def set_up(self): self.customer = Customer("528")
↓↓.........................↓↓↓↓↓↓↓↓↓...↓↓↓↓↓↓↓...............................↓↓ ↓↓..............................↓↓↓↓↓↓↓↓↓↓...................................↓↓ ↓↓..........................↓↓↓↓↓....↓↓↓↓↓↓↓↓↓...............................↓↓ ↓↓............↓↓.↓↓↓↓↓↓↓↓↓↓↓↓↓............↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓..................↓↓ ↓↓............↓↓.↓↓..↓↓↓↓.....................↓↓↓↓↓↓↓↓↓↓↓↓↓↓.................↓↓ ↓↓..............↓↓↓↓↓↓............................↓↓.↓↓↓↓↓↓↓.................↓↓ ↓↓.................. ......................↓↓ ↓↓.................. ↑↑↑ ↑↑↑ ↑↑↑↑↑↑↑ ↑↑↑↑↑↑↑ .......................↓↓ ↓↓.................. ↑↑↑ ↑↑↑ ↑↑↑ ↑↑↑↑ ↑↑↑ ↑↑↑↑.....................↓↓ ↓↓.................. ↑↑↨ ↑↑↑ ↑↑↨ ↨↑↑ ↑↑↨ ↨↑↑......................↓↓ ↓↓.................. ↨↑↨ ↑↨↑ ↨↑↨ ↨↑↨ ↨↑↨ ↨↑↨......................↓↓ ↓↓.................. ↑↨↑ ↨↑↨ ↨↨↑↨↑↨↨↑↑↨ ↨↨↑↨↑↨↨↑↑↨.....................↓↓ ↓↓.................. ↨↑↨ ↨↨↨ ↨↨↨ ↨↨↨ ↨↨↨ ↨↨↨....................↓↓ ↓↓.................. :↨: ↨↨: ↨↨: :↨↨ ↨↨: :::....................↓↓ ↓↓................... ::↨↨:↨ :↨: :↨: :↨: :::....................↓↓ ↓↓.................... :::: ::: ::: ::: :::....................↓↓ ↓↓...................... : : : ::::::: ....................↓↓ ↓↓...........................................................................↓↓ ↓↓←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←↓↓ ↓↓→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→↓↓ ↓↓ init_db.py Created by Durodola Opemipo 2021 ↓↓ ↓↓ Personal Email : <*****@*****.**> ↓↓ ↓↓ Telephone Number: +2348182104309 ↓↓ ↓↓→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→→↓↓ ↓↓←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←←↓↓ """ from src.models import Customer if __name__ == '__main__': Customer.create_model()
def test_invalid_customer_geolocation(self, geolocation, random_name): random_id = random.randint(500, 999) with pytest.raises(ValueError): Customer(random_id, random_name, geolocation)
def test_invalid_customer_name(self, customer_name): random_id = random.randint(500, 999) with pytest.raises(ValueError): Customer(random_id, customer_name, GeoLocation(-9.742744, 51.999447))
def test_invalid_customer_id(self, customer_id, random_name): with pytest.raises(ValueError): Customer(customer_id, random_name, GeoLocation(-9.742744, 51.999447))
def test_valid_customer(self, user_id, name, geo_location): Customer(user_id, name, geo_location)
class TestCustomer: """ Unit tests for the Customer model. """ @pytest.mark.parametrize( 'user_id, name, geo_location', [(901, 'Ronaldo', GeoLocation(-9.742744, 51.999447)), (178, 'Roger Waters', GeoLocation(-7.11167, 53.74452))]) def test_valid_customer(self, user_id, name, geo_location): Customer(user_id, name, geo_location) @pytest.mark.parametrize('customer_obj, user_id', [ (Customer(901, 'Ronaldo', GeoLocation(-9.742744, 51.999447)), 901), (Customer(178, 'Roger Waters', GeoLocation(-7.11167, 53.74452)), 178) ]) def test_customer_user_id(self, customer_obj, user_id): assert customer_obj.user_id == user_id @pytest.mark.parametrize( 'customer_obj, name', [(Customer(901, 'Ronaldo', GeoLocation(-9.742744, 51.999447)), 'Ronaldo'), (Customer(178, 'Roger Waters', GeoLocation( -7.11167, 53.74452)), 'Roger Waters')]) def test_customer_name(self, customer_obj, name): assert customer_obj.name == name @pytest.mark.parametrize( 'customer_obj, geolocation', [(Customer(901, 'Ronaldo', GeoLocation( -9.742744, 51.999447)), GeoLocation(-9.742744, 51.999447)), (Customer(178, 'Roger Waters', GeoLocation( -7.11167, 53.74452)), GeoLocation(-7.11167, 53.74452)), (Customer(903, 'Jim Waters', GeoLocation( -72.11167, 44.74452)), GeoLocation(-72.11167, 44.74452))]) def test_customer_geo_location(self, customer_obj, geolocation): assert isinstance(customer_obj.geo_location, GeoLocation) assert customer_obj.geo_location == geolocation @pytest.mark.parametrize('customer_id', [ None, 'abc', 'random-string', '', pytest, range, (), (5, 9, 10), {}, {90, 43, 901}, dict, pytest, [56, 901], [{ 'abc': 1, 'efg': 2, 'lmo': 3 }], { '1': 903, '2': ['xyz'] } ]) def test_invalid_customer_id(self, customer_id, random_name): with pytest.raises(ValueError): Customer(customer_id, random_name, GeoLocation(-9.742744, 51.999447)) @pytest.mark.parametrize('customer_name', [ None, 123, 8.001, '', (), (5, 9, 10), {}, {90, 43, 901}, range, dict, float, pytest, [56, 901], [{ 'abc': 1, 'efg': 2, 'lmo': 3 }], { '1': 903, '2': ['xyz'] } ]) def test_invalid_customer_name(self, customer_name): random_id = random.randint(500, 999) with pytest.raises(ValueError): Customer(random_id, customer_name, GeoLocation(-9.742744, 51.999447)) @pytest.mark.parametrize('geolocation', [ None, 123, 8.001, '', (), (5, 9, 10), {}, {90, 43, 901}, range, dict, float, pytest, [{ 'abc': 1, 'efg': 2, 'lmo': 3 }], [56, 901], { '1': 903, '2': ['xyz'] } ]) def test_invalid_customer_geolocation(self, geolocation, random_name): random_id = random.randint(500, 999) with pytest.raises(ValueError): Customer(random_id, random_name, geolocation)
def execute(self) -> Customer: customer = Customer.delete_customer(self.id) return customer
def execute(self) -> Customer: customer = Customer.update_customer(id=self.id, updates=self.update) return customer
def database(): Customer.create_model() yield Customer.destroy_model()
def test_create_customer(self): customer_id = "1" customer = Customer(customer_id) assert customer assert customer.customer_id == customer_id
def execute(self) -> Customer: customer = Customer.get(self.id) return customer