def test_contact_model(self): contact = Contact( self.user, { 'first_name': 'emilio', 'last_name': 'estevez', 'street_address': '1234 fake st', 'city': 'denver', 'state': 'colorado', 'zipcode': '80019' }) contact.insert() self.assertIsInstance(contact, Contact) self.assertIsNotNone(contact.id) self.assertIsNotNone(contact.user_id) self.assertEqual(self.user, contact.user) self.assertEqual('emilio', contact.first_name) self.assertEqual('estevez', contact.last_name) self.assertEqual('1234 fake st', contact.street_address) self.assertEqual('denver', contact.city) self.assertEqual('colorado', contact.state) self.assertEqual('80019', contact.zipcode) self.assertEqual('friend', contact.group) self.assertEqual(None, contact.street_address_2) self.assertEqual(None, contact.phone_number)
def setUp(self): self.app = create_app('testing') self.app_context = self.app.app_context() self.app_context.push() db.create_all() self.client = self.app.test_client() self.user = User(email='*****@*****.**', first_name='joshua', last_name='carey') self.user.insert() self.contact_payload = { 'first_name': 'darrel', 'last_name': 'wadsworth', 'group': 'friend', 'phone_number': '999-999-9999', 'street_address': '45321 example way', 'city': 'Denver', 'state': 'Colorado', 'zipcode': '80000' } self.contact = Contact(self.user, self.contact_payload) self.contact.insert()
def test_user_model_can_delete_record_and_its_contacts(self): user = User( email='*****@*****.**', first_name='joshua', last_name='carey', ) user.insert() self.assertEqual([], user.contacts) contact = Contact( user, { 'first_name': 'emilio', 'last_name': 'estevez', 'group': 'business', 'phone_number': '999-999-9999', 'street_address': '1234 fake st', 'street_address_2': 'unit 100', 'city': 'denver', 'state': 'colorado', 'zipcode': '80019' }) contact.insert() self.assertEqual([contact], user.contacts) user.delete() deleted_user = User.query.filter_by(id=user.id).first() self.assertIsNone(deleted_user) deleted_contact = Contact.query.filter_by(id=user.id).first() self.assertIsNone(deleted_contact)
def test_contact_model_can_update(self): contact = Contact( self.user, { 'first_name': 'emilio', 'last_name': 'estevez', 'group': 'business', 'phone_number': '999-999-9999', 'street_address': '1234 fake st', 'street_address_2': 'unit 100', 'city': 'denver', 'state': 'colorado', 'zipcode': '80019' }) contact.insert() contact.first_name = 'harvey' contact.last_name = 'dent' contact.street_address_2 = '444 avenue way' contact.update() updated_contact = Contact.query.filter_by(id=contact.id).first() self.assertEqual('harvey', updated_contact.first_name) self.assertEqual('dent', updated_contact.last_name) self.assertEqual('444 avenue way', updated_contact.street_address_2)
def setUp(self): self.app = create_app('testing') self.app_context = self.app.app_context() self.app_context.push() db.create_all() self.client = self.app.test_client() self.user = User(email='*****@*****.**', first_name='joshua', last_name='carey') self.user.insert() self.contact_payload = { 'first_name': 'darrel', 'last_name': 'wadsworth', 'group': 'friend', 'phone_number': '999-999-9999', 'street_address': '45321 example way', 'city': 'Denver', 'state': 'Colorado', 'zipcode': '80000' } # creates three contacts (all attributes differentiated by '_i') for user # TODO: good use case for factory bot for i in range(3): payload = deepcopy(self.contact_payload) for key, value in payload.items(): payload[key] = value + f'_{i}' Contact(self.user, payload)
def test_contact_model_missing_last_name(self): try: contact = Contact( self.user, { 'first_name': 'emilio', 'last_name': None, 'street_address': '1234 fake st', 'city': 'denver', 'state': 'colorado', 'zipcode': '80019' }) contact.insert() except IntegrityError: self.assertTrue(True) else: self.assertTrue(False)
def test_contact_model_empty_first_name(self): try: contact = Contact( self.user, { 'first_name': '', 'last_name': 'estevez', 'street_address': '1234 fake st', 'city': 'denver', 'state': 'colorado', 'zipcode': '99999' }) contact.insert() except IntegrityError: self.assertTrue(True) else: self.assertTrue(False)
def test_contact_model_with_non_required_atts(self): contact = Contact( self.user, { 'first_name': 'emilio', 'last_name': 'estevez', 'group': 'business', 'phone_number': '999-999-9999', 'street_address': '1234 fake st', 'street_address_2': 'unit 100', 'city': 'denver', 'state': 'colorado', 'zipcode': '80019' }) contact.insert() self.assertEqual('business', contact.group) self.assertEqual('unit 100', contact.street_address_2) self.assertEqual('999-999-9999', contact.phone_number)
def _create_contact(self, user, data): proceed = True errors = [] proceed, first_name, errors = _validate_field(data, 'first_name', proceed, errors) proceed, last_name, errors = _validate_field(data, 'last_name', proceed, errors) proceed, group, errors = _validate_field(data, 'group', proceed, errors, missing_okay=True) proceed, phone_number, errors = _validate_field(data, 'phone_number', proceed, errors, missing_okay=True) proceed, street_address, errors = _validate_field( data, 'street_address', proceed, errors) proceed, street_address_2, errors = _validate_field(data, 'street_address_2', proceed, errors, missing_okay=True) proceed, city, errors = _validate_field(data, 'city', proceed, errors) proceed, state, errors = _validate_field(data, 'state', proceed, errors) proceed, zipcode, errors = _validate_field(data, 'zipcode', proceed, errors) if proceed: contact = Contact(user, data) contact.insert() return contact, errors else: return None, errors
def test_contact_model_can_delete_record(self): contact = Contact( self.user, { 'first_name': 'emilio', 'last_name': 'estevez', 'group': 'business', 'phone_number': '999-999-9999', 'street_address': '1234 fake st', 'street_address_2': 'unit 100', 'city': 'denver', 'state': 'colorado', 'zipcode': '80019' }) contact.insert() contact.delete() deleted_contact = Contact.query.filter_by(id=contact.id).first() self.assertIsNone(deleted_contact)
class ShowContactTest(unittest.TestCase): def setUp(self): self.app = create_app('testing') self.app_context = self.app.app_context() self.app_context.push() db.create_all() self.client = self.app.test_client() self.user = User(email='*****@*****.**', first_name='joshua', last_name='carey') self.user.insert() self.contact_payload = { 'first_name': 'darrel', 'last_name': 'wadsworth', 'group': 'friend', 'phone_number': '999-999-9999', 'street_address': '45321 example way', 'city': 'Denver', 'state': 'Colorado', 'zipcode': '80000' } self.contact = Contact(self.user, self.contact_payload) self.contact.insert() def tearDown(self): db.session.remove() db.drop_all() self.app_context.pop() def test_happypath_get_contact(self): response = self.client.get(f'/users/{self.user.id}/contacts/{self.contact.id}') self.assertEqual(200, response.status_code) data = json.loads(response.data.decode('utf-8')) assert_payload_field_type_value(self, data, 'success', bool, True) assert_payload_field_type_value(self, data, 'first_name', str, self.contact_payload['first_name']) assert_payload_field_type_value(self, data, 'last_name', str, self.contact_payload['last_name']) assert_payload_field_type_value(self, data, 'group', str, self.contact_payload['group']) assert_payload_field_type_value(self, data, 'phone_number', str, self.contact_payload['phone_number']) assert_payload_field_type_value(self, data, 'street_address', str, self.contact_payload['street_address']) assert_payload_field_type_value(self, data, 'city', str, self.contact_payload['city']) assert_payload_field_type_value(self, data, 'state', str, self.contact_payload['state']) assert_payload_field_type_value(self, data, 'zipcode', str, self.contact_payload['zipcode']) def test_sadpath_invalid_contact_id(self): contact_id = 99999 response = self.client.get(f'/users/{self.user.id}/contacts/{contact_id}') self.assertEqual(400, response.status_code) data = json.loads(response.data.decode('utf-8')) assert_payload_field_type_value(self, data, 'success', bool, False) assert_payload_field_type_value(self, data, 'error', int, 400) assert_payload_field_type_value(self, data, 'errors', list, [f"contact with id: '{contact_id}' not found"]) def test_sadpath_invalid_user_id(self): response = self.client.get(f'/users/99999/contacts/{self.contact.id}') self.assertEqual(404, response.status_code)
class UpdateContactTest(unittest.TestCase): def setUp(self): self.app = create_app('testing') self.app_context = self.app.app_context() self.app_context.push() db.create_all() self.client = self.app.test_client() self.user = User(email='*****@*****.**', first_name='joshua', last_name='carey') self.user.insert() self.contact_payload = { 'first_name': 'darrel', 'last_name': 'wadsworth', 'group': 'friend', 'phone_number': '999-999-9999', 'street_address': '45321 example way', 'city': 'Denver', 'state': 'Colorado', 'zipcode': '80000' } self.update_payload = { 'first_name': 'bruce', 'last_name': 'wayne', 'group': 'associate', 'phone_number': '111-111-1111', 'street_address': 'the bat cave', 'city': 'Pagosa Springs', 'state': 'Colorado', 'zipcode': '12345' } self.contact = Contact(self.user, self.contact_payload) self.contact.insert() def tearDown(self): db.session.remove() db.drop_all() self.app_context.pop() def test_happypath_update_contact(self): payload = deepcopy(self.update_payload) response = self.client.patch( f'/users/{self.user.id}/contacts/{self.contact.id}', json=payload, content_type='application/json') self.assertEqual(200, response.status_code) data = json.loads(response.data.decode('utf-8')) assert_payload_field_type_value(self, data, 'first_name', str, self.update_payload['first_name']) assert_payload_field_type_value(self, data, 'last_name', str, self.update_payload['last_name']) assert_payload_field_type_value(self, data, 'group', str, self.update_payload['group']) assert_payload_field_type_value(self, data, 'phone_number', str, self.update_payload['phone_number']) assert_payload_field_type_value(self, data, 'street_address', str, self.update_payload['street_address']) assert_payload_field_type_value(self, data, 'city', str, self.update_payload['city']) assert_payload_field_type_value(self, data, 'state', str, self.update_payload['state']) assert_payload_field_type_value(self, data, 'zipcode', str, self.update_payload['zipcode']) def test_sadpath_blank_required_field(self): payload = deepcopy(self.update_payload) payload['first_name'] = '' response = self.client.patch( f'/users/{self.user.id}/contacts/{self.contact.id}', json=payload, content_type='application/json') self.assertEqual(400, response.status_code) data = json.loads(response.data.decode('utf-8')) assert_payload_field_type_value(self, data, 'success', bool, False) assert_payload_field_type_value(self, data, 'error', int, 400) assert_payload_field_type_value( self, data, 'errors', list, ["required 'first_name' parameter is blank"]) def test_sadpath_another_blank_required_field(self): payload = deepcopy(self.update_payload) payload['street_address'] = '' response = self.client.patch( f'/users/{self.user.id}/contacts/{self.contact.id}', json=payload, content_type='application/json') self.assertEqual(400, response.status_code) data = json.loads(response.data.decode('utf-8')) assert_payload_field_type_value(self, data, 'success', bool, False) assert_payload_field_type_value(self, data, 'error', int, 400) assert_payload_field_type_value( self, data, 'errors', list, ["required 'street_address' parameter is blank"]) def test_sadpath_update_contact_with_only_one_field(self): payload = deepcopy(self.update_payload) # only update street address del payload['first_name'] del payload['last_name'] del payload['group'] del payload['phone_number'] del payload['city'] del payload['state'] del payload['zipcode'] response = self.client.patch( f'/users/{self.user.id}/contacts/{self.contact.id}', json=payload, content_type='application/json') self.assertEqual(200, response.status_code) data = json.loads(response.data.decode('utf-8')) # only street address should match with update_payload assert_payload_field_type_value(self, data, 'street_address', str, self.update_payload['street_address']) # everything else should match the original payload assert_payload_field_type_value(self, data, 'first_name', str, self.contact_payload['first_name']) assert_payload_field_type_value(self, data, 'last_name', str, self.contact_payload['last_name']) assert_payload_field_type_value(self, data, 'group', str, self.contact_payload['group']) assert_payload_field_type_value(self, data, 'phone_number', str, self.contact_payload['phone_number']) assert_payload_field_type_value(self, data, 'city', str, self.contact_payload['city']) assert_payload_field_type_value(self, data, 'state', str, self.contact_payload['state']) assert_payload_field_type_value(self, data, 'zipcode', str, self.contact_payload['zipcode']) def test_sadpath_invalid_user_id(self): payload = deepcopy(self.update_payload) response = self.client.patch( f'/users/99999/contacts/{self.contact.id}', json=payload, content_type='application/json') self.assertEqual(404, response.status_code) def test_sadpath_invalid_contact_id(self): payload = deepcopy(self.update_payload) contact_id = 99999 response = self.client.patch( f'/users/{self.user.id}/contacts/{contact_id}', json=payload, content_type='application/json') self.assertEqual(400, response.status_code) data = json.loads(response.data.decode('utf-8')) assert_payload_field_type_value(self, data, 'success', bool, False) assert_payload_field_type_value(self, data, 'error', int, 400) assert_payload_field_type_value( self, data, 'errors', list, [f"contact with id: '{contact_id}' not found"])