From d97ecf660adeb1ed480d14703543d6473687d657 Mon Sep 17 00:00:00 2001 From: Jose Contreras Date: Tue, 12 Apr 2016 01:02:20 -0400 Subject: [PATCH 1/4] Insert id before put to fix missing id error. --- app/controllers/project.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/controllers/project.py b/app/controllers/project.py index be3dd41..ed82fb7 100644 --- a/app/controllers/project.py +++ b/app/controllers/project.py @@ -71,6 +71,7 @@ class ProjectController(RestController): 'When embedding a client in a salesforce project, please ' + 'include its salesforce id.') if client: + client_data.update({'id': client.id}) client = ClientController().put(client.id, client_data, {}) else: client = ClientController().post(client_data, {}) @@ -89,12 +90,14 @@ class ProjectController(RestController): address = place.address if place else None if address: + address_data.update({'id': address.id}) address = AddressController().put(address.id, address_data, {}) else: address = AddressController().post(address_data, {}) place_data.update({'address_id': address.id}) if place: + place_data.update({'id': place.id}) place = PlaceController().put(place.id, place_data, {}) else: place = PlaceController().post(place_data, {}) @@ -119,6 +122,7 @@ class ProjectController(RestController): contact_data['sales_force_id'])\ .first() if contact: + contact_data.update({'id':contact.id}) contact = ContactController().put( contact.id, contact_data, {}) else: -- GitLab From 32974619c76ff67cbdf2fb62506dc723a6ff8008 Mon Sep 17 00:00:00 2001 From: Jose Contreras Date: Tue, 12 Apr 2016 12:15:22 -0400 Subject: [PATCH 2/4] Add tests for embedded put with existing Salesforce objects --- app/tests/test_project.py | 89 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/app/tests/test_project.py b/app/tests/test_project.py index fbe0a62..f5949d6 100644 --- a/app/tests/test_project.py +++ b/app/tests/test_project.py @@ -259,6 +259,34 @@ class TestSalesForceProject(UnprotectedRestTestCase): Client.name == client_data['name']))\ .first()) + def test_post_sf_existing_client(self): + """Tests /project/?form=salesforce POST with an existing client.""" + client = self.env.client + client_data = client.get_dictionary() + client_data.pop('id', None) + place = self.env.place + data = { + 'sales_force_id': 'test', + 'name': 'test', + 'state': 'pending', + 'client': client_data, + 'place': place.get_dictionary(), + 'address': place.address.get_dictionary(), + 'contacts': [], + 'contact_methods': [], + 'notes': []} + response_data = self._test_post(data, {'form': 'salesforce'}) + # Check that the client was modified as expected. + self.assertTrue( + db.session.query(Client)\ + .join(Project, Project.client_id == Client.id) + .filter(and_( + Project.id == response_data['id'], + Client.id == client.id, + Client.name == client_data['name']))\ + .first()) + + def test_post_sf_no_client(self): """Tests /project/?form=salesforce POST with no client. This should 400. @@ -367,6 +395,39 @@ class TestSalesForceProject(UnprotectedRestTestCase): .filter(and_(*filters))\ .first()) + def test_post_sf_existing_place(self): + """Tests /project/?form=salesforce POST with an existing place and + address. + """ + place = self.env.place + place_data = place.get_dictionary() + place_data.pop('id', None) + address_data = place.address.get_dictionary() + address_data.pop('id', None) + data = { + 'sales_force_id': 'test', + 'name': 'test', + 'state': 'pending', + 'client': self.env.client.get_dictionary(), + 'place': place_data, + 'address': address_data, + 'contacts': [], + 'contact_methods': [], + 'notes': []} + response_data = self._test_post(data, {'form': 'salesforce'}) + + filters = [ + Project.id == response_data['id'], + Place.id == place.id, + Address.id == place.address.id] + + self.assertTrue( + db.session.query(Address)\ + .join(Place, Place.address_id == Address.id)\ + .join(Project, Project.place_id == Place.id)\ + .filter(and_(*filters))\ + .first()) + def test_post_sf_no_place(self): """Tests /project/?form=salesforce POST with no place. It should 400. """ @@ -468,6 +529,34 @@ class TestSalesForceProject(UnprotectedRestTestCase): Contact.name == contact_data['name']))\ .first()) + def test_post_sf_existing_contacts(self): + """Tests /project/?form=salesforce POST with an existing contact.""" + place = self.env.place + contact = self.env.contact + contact_data = contact.get_dictionary() + contact_data.pop('id', None) + data = { + 'sales_force_id': 'test', + 'name': 'test', + 'state': 'pending', + 'client': self.env.client.get_dictionary(), + 'place': place.get_dictionary(), + 'address': place.address.get_dictionary(), + 'contacts': [contact_data], + 'contact_methods': [], + 'notes': []} + response_data = self._test_post(data, {'form': 'salesforce'}) + self.assertTrue( + db.session.query(Contact)\ + .join(ProjectContact, ProjectContact.contact_id == Contact.id)\ + .join(Project, ProjectContact.project_id == Project.id)\ + .filter(and_( + Project.id == response_data['id'], + Contact.id == contact.id, + Contact.sales_force_id == contact_data['sales_force_id'], + Contact.name == contact_data['name']))\ + .first()) + def test_post_sf_no_contacts(self): """Tests /project/?form=salesforce POST with no contacts. It should 400. -- GitLab From 1e0ce47dae942e153e7f0f1bda5c00d370c0a43f Mon Sep 17 00:00:00 2001 From: Jose Contreras Date: Tue, 12 Apr 2016 12:33:59 -0400 Subject: [PATCH 3/4] Pop id from models in tests with existing embedded salesforce objects and remove repeated test cases. --- app/tests/test_project.py | 94 +++------------------------------------ 1 file changed, 5 insertions(+), 89 deletions(-) diff --git a/app/tests/test_project.py b/app/tests/test_project.py index f5949d6..2f08768 100644 --- a/app/tests/test_project.py +++ b/app/tests/test_project.py @@ -236,34 +236,8 @@ class TestSalesForceProject(UnprotectedRestTestCase): """Tests /project/?form=salesforce POST with a modified client.""" client = self.env.client client_data = client.get_dictionary() - client_data.update({'name': 'foo'}) - place = self.env.place - data = { - 'sales_force_id': 'test', - 'name': 'test', - 'state': 'pending', - 'client': client_data, - 'place': place.get_dictionary(), - 'address': place.address.get_dictionary(), - 'contacts': [], - 'contact_methods': [], - 'notes': []} - response_data = self._test_post(data, {'form': 'salesforce'}) - # Check that the client was modified as expected. - self.assertTrue( - db.session.query(Client)\ - .join(Project, Project.client_id == Client.id) - .filter(and_( - Project.id == response_data['id'], - Client.id == client.id, - Client.name == client_data['name']))\ - .first()) - - def test_post_sf_existing_client(self): - """Tests /project/?form=salesforce POST with an existing client.""" - client = self.env.client - client_data = client.get_dictionary() client_data.pop('id', None) + client_data.update({'name': 'foo'}) place = self.env.place data = { 'sales_force_id': 'test', @@ -286,7 +260,6 @@ class TestSalesForceProject(UnprotectedRestTestCase): Client.name == client_data['name']))\ .first()) - def test_post_sf_no_client(self): """Tests /project/?form=salesforce POST with no client. This should 400. @@ -355,9 +328,12 @@ class TestSalesForceProject(UnprotectedRestTestCase): """ place = self.env.place place_data = place.get_dictionary() + place_data.pop('id', None) new_place_data = {'name': 'foo'} place_data.update(new_place_data) address_data = place.address.get_dictionary() + address_data.pop('id', None) + new_address_data = { 'street_address': '111 N Fake St', 'city': 'Raleigh', @@ -395,39 +371,6 @@ class TestSalesForceProject(UnprotectedRestTestCase): .filter(and_(*filters))\ .first()) - def test_post_sf_existing_place(self): - """Tests /project/?form=salesforce POST with an existing place and - address. - """ - place = self.env.place - place_data = place.get_dictionary() - place_data.pop('id', None) - address_data = place.address.get_dictionary() - address_data.pop('id', None) - data = { - 'sales_force_id': 'test', - 'name': 'test', - 'state': 'pending', - 'client': self.env.client.get_dictionary(), - 'place': place_data, - 'address': address_data, - 'contacts': [], - 'contact_methods': [], - 'notes': []} - response_data = self._test_post(data, {'form': 'salesforce'}) - - filters = [ - Project.id == response_data['id'], - Place.id == place.id, - Address.id == place.address.id] - - self.assertTrue( - db.session.query(Address)\ - .join(Place, Place.address_id == Address.id)\ - .join(Project, Project.place_id == Place.id)\ - .filter(and_(*filters))\ - .first()) - def test_post_sf_no_place(self): """Tests /project/?form=salesforce POST with no place. It should 400. """ @@ -506,35 +449,8 @@ class TestSalesForceProject(UnprotectedRestTestCase): place = self.env.place contact = self.env.contact contact_data = contact.get_dictionary() - contact_data.update({'name': 'foo'}) - data = { - 'sales_force_id': 'test', - 'name': 'test', - 'state': 'pending', - 'client': self.env.client.get_dictionary(), - 'place': place.get_dictionary(), - 'address': place.address.get_dictionary(), - 'contacts': [contact_data], - 'contact_methods': [], - 'notes': []} - response_data = self._test_post(data, {'form': 'salesforce'}) - self.assertTrue( - db.session.query(Contact)\ - .join(ProjectContact, ProjectContact.contact_id == Contact.id)\ - .join(Project, ProjectContact.project_id == Project.id)\ - .filter(and_( - Project.id == response_data['id'], - Contact.id == contact.id, - Contact.sales_force_id == contact_data['sales_force_id'], - Contact.name == contact_data['name']))\ - .first()) - - def test_post_sf_existing_contacts(self): - """Tests /project/?form=salesforce POST with an existing contact.""" - place = self.env.place - contact = self.env.contact - contact_data = contact.get_dictionary() contact_data.pop('id', None) + contact_data.update({'name': 'foo'}) data = { 'sales_force_id': 'test', 'name': 'test', -- GitLab From 4e6a23629a5cb4cf360e61282d36c7844f312073 Mon Sep 17 00:00:00 2001 From: Jose Contreras Date: Tue, 12 Apr 2016 13:12:53 -0400 Subject: [PATCH 4/4] Remove unnecessary default return value,None, in pop --- app/tests/test_project.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/tests/test_project.py b/app/tests/test_project.py index 2f08768..80834ef 100644 --- a/app/tests/test_project.py +++ b/app/tests/test_project.py @@ -236,7 +236,7 @@ class TestSalesForceProject(UnprotectedRestTestCase): """Tests /project/?form=salesforce POST with a modified client.""" client = self.env.client client_data = client.get_dictionary() - client_data.pop('id', None) + client_data.pop('id') client_data.update({'name': 'foo'}) place = self.env.place data = { @@ -328,11 +328,11 @@ class TestSalesForceProject(UnprotectedRestTestCase): """ place = self.env.place place_data = place.get_dictionary() - place_data.pop('id', None) + place_data.pop('id') new_place_data = {'name': 'foo'} place_data.update(new_place_data) address_data = place.address.get_dictionary() - address_data.pop('id', None) + address_data.pop('id') new_address_data = { 'street_address': '111 N Fake St', @@ -449,7 +449,7 @@ class TestSalesForceProject(UnprotectedRestTestCase): place = self.env.place contact = self.env.contact contact_data = contact.get_dictionary() - contact_data.pop('id', None) + contact_data.pop('id') contact_data.update({'name': 'foo'}) data = { 'sales_force_id': 'test', -- GitLab