def test_admin(self): """Test that mainly improves code coverage.""" # Log in as a superuser user = django.contrib.auth.models.User.objects.create_user('fredsu', '*****@*****.**', 'passwd') user.is_superuser = True user.is_staff = True user.save() self.client.login(username='******', password='******') account = Account(Name = 'sf_test account') account.save() response = self.client.get('/') response = self.client.get('/search/') response = self.client.post('/search/', {'query': 'test account'}) self.assertIn(b'sf_test account', response.content) response = self.client.post('/admin/example/account/') response = self.client.post('/admin/example/contact/') response = self.client.post('/admin/example/lead/') response = self.client.post('/admin/example/pricebook/') response = self.client.post('/admin/') self.assertIn('PricebookEntries', response.rendered_content) account.delete() user.delete()
def test_escape_single_quote(self): """ Test that single quotes in strings used in filtering a QuerySet are escaped properly. """ account_name = '''Dr. Evil's Giant "Laser", LLC''' account = Account(Name=account_name) account.save() try: self.assertTrue(Account.objects.filter(Name=account_name).exists()) finally: account.delete()
def test_lead_conversion(self): """ Create a Lead object within Salesforce and try to convert it, convert/merge it with the information from a duplicit Lead, then clean all the generated objects. """ lead = Lead(FirstName="Foo", LastName="Bar", Company="django-salesforce", Street='Test Avenue 45') lead.save() lead2 = Lead(FirstName="Foo", LastName="Bar", Company="django-salesforce", Phone='123456789') lead2.save() ret = None try: # convert the first Lead ret = convert_lead(lead, doNotCreateOpportunity=True) # print("Response from convertLead: " + # ', '.join('%s: %s' % (k, v) for k, v in sorted(ret.items()))) expected_names = set(('accountId', 'contactId', 'leadId', 'opportunityId', 'success')) self.assertEqual(set(ret), expected_names) self.assertEqual(ret['success'], 'true') # merge the new Account with the second Lead ret2 = convert_lead(lead2, doNotCreateOpportunity=True, accountId=ret['accountId']) account = Account.objects.get(pk=ret['accountId']) # verify that account is merged self.assertEqual(ret2['accountId'], account.pk) self.assertEqual(account.BillingStreet, 'Test Avenue 45') self.assertEqual(account.Phone, '123456789') finally: # Cleaning up... if ret: # Deleting the Account object will also delete the related Contact # and Opportunity objects. try: account = Account.objects.get(pk=ret['accountId']) except Exception: # pylint:disable=broad-except # this allows to recycle the account even if the queryset code is broken account = Account(pk=ret['accountId']) account._state.db = lead._state.db account.delete() lead.delete() # FYI, ret['leadId'] == lead.pk lead2.delete()
def test_bulk_update(self): """Create two Contacts by one request in one command, find them. """ account_0, account_1 = [Account(Name='test' + uid), Account(Name='test' + uid)] account_0.save() account_1.save() try: request_count_0 = salesforce.backend.driver.request_count Account.objects.filter(pk=account_0.pk).update(Name="test2" + uid) Account.objects.filter(pk__in=[account_1.pk]).update(Name="test2" + uid) Account.objects.filter(pk__in=Account.objects.filter(Name='test2' + uid)).update(Name="test3" + uid) request_count_1 = salesforce.backend.driver.request_count self.assertEqual(Account.objects.filter(Name='test3' + uid).count(), 2) self.assertEqual(request_count_1, request_count_0 + 4) finally: account_0.delete() account_1.delete()
def test_bulk_create(self): """Create two Contacts by one request in one command and find them. """ account = Account(Name='test bulk') account.save() try: objects = [Contact(last_name='sf_test a', account=account), Contact(last_name='sf_test b', account=account)] request_count_0 = salesforce.backend.driver.request_count ret = Contact.objects.bulk_create(objects) request_count_1 = salesforce.backend.driver.request_count self.assertEqual(request_count_1, request_count_0 + 1) self.assertEqual(len(Contact.objects.filter(account=account)), 2) finally: account.delete()
def test_insert_date(self): """ Test inserting a date. """ self.skipTest("Need to find a suitable *standard* model field to test datetime inserts.") now = datetime.datetime.now() account = Account( FirstName = 'Joe', LastName = 'Freelancer', LastLogin = now, IsPersonAccount = False, ) account.save() saved = Account.objects.get(pk=account.pk) self.assertEqual(saved.LastLogin, now) self.assertEqual(saved.IsPersonAccount, False) saved.delete()
def test_account_insert_delete(self): """Test insert and delete an account (normal or personal SF config) """ if settings.PERSON_ACCOUNT_ACTIVATED: test_account = Account(FirstName='IntegrationTest', LastName='Account') else: test_account = Account(Name='IntegrationTest Account') test_account.save() try: accounts = Account.objects.filter(Name='IntegrationTest Account') self.assertEqual(len(accounts), 1) finally: test_account.delete()
def test_admin(self): """Test that mainly improves code coverage.""" # Log in as a superuser user = django.contrib.auth.models.User.objects.create_user( 'fredsu', '*****@*****.**', 'passwd') user.is_superuser = True user.is_staff = True user.save() self.client.login(username='******', password='******') account = Account(Name='sf_test account') account.save() response = self.client.get('/') response = self.client.get('/search/') response = self.client.post('/search/', {'query': 'test account'}) self.assertIn(b'sf_test account', response.content) response = self.client.post('/admin/example/account/') response = self.client.post('/admin/example/contact/') response = self.client.post('/admin/example/lead/') response = self.client.post('/admin/example/pricebook/') response = self.client.post('/admin/') self.assertIn('PricebookEntries', response.rendered_content) account.delete() user.delete()
def test_escape_single_quote(self): """Test single quotes in strings used in a filter Verity that they are escaped properly. """ account_name = '''Dr. Evil's Giant\\' "Laser", LLC''' account = Account(Name=account_name) account.save() try: self.assertTrue(Account.objects.filter(Name=account_name).exists()) finally: account.delete()
def test_foreign_key_column(self): """Verify filtering by a column of related parent object. """ test_account = Account(Name='sf_test account') test_account.save() test_contact = Contact(first_name='sf_test', last_name='my', account=test_account) test_contact.save() try: contacts = Contact.objects.filter(account__Name='sf_test account') self.assertEqual(len(contacts), 1) finally: test_contact.delete() test_account.delete()
def test_escape_single_quote_in_raw_query(self): """Test that manual escaping within a raw query is not double escaped. """ account_name = '''Dr. Evil's Giant\\' "Laser", LLC''' account = Account(Name=account_name) account.save() manually_escaped = '''Dr. Evil\\'s Giant\\\\\\' "Laser", LLC''' try: retrieved_account = Account.objects.raw( "SELECT Id, Name FROM Account WHERE Name = '%s'" % manually_escaped)[0] self.assertEqual(account_name, retrieved_account.Name) finally: account.delete()
def test_simple_select_related(self): """Verify that simple_selct_related does not require additional queries. """ test_account = Account(Name='sf_test account') test_account.save() test_contact = Contact(first_name='sf_test', last_name='my', account=test_account) test_contact.save() req_count_0 = salesforce.backend.driver.request_count try: qs = Contact.objects.filter(account__Name='sf_test account').simple_select_related('account') contacts = list(qs) req_count_1 = salesforce.backend.driver.request_count [x.account.Name for x in contacts] req_count_2 = salesforce.backend.driver.request_count self.assertEqual(req_count_1, req_count_0 + 2) self.assertEqual(req_count_2, req_count_1) self.assertGreaterEqual(len(contacts), 1) finally: test_contact.delete() test_account.delete()
def test_bulk_create(self): """Create two Contacts by one request in one command and find them. """ account = Account(Name='test bulk') account.save() try: objects = [ Contact(last_name='sf_test a', account=account), Contact(last_name='sf_test b', account=account) ] request_count_0 = salesforce.backend.driver.request_count ret = Contact.objects.bulk_create(objects) if DJANGO_110_PLUS: # test that can return ids from bulk_create self.assertTrue(ret and all(x.pk for x in ret)) request_count_1 = salesforce.backend.driver.request_count self.assertEqual(request_count_1, request_count_0 + 1) self.assertEqual(len(Contact.objects.filter(account=account)), 2) finally: account.delete()