def test_unsupported_bulk_create(self): """Unsupported bulk_create: "Errors should never pass silently." """ objects = [ Contact(last_name='sf_test a'), Contact(last_name='sf_test b') ] self.assertRaises(AssertionError, Contact.objects.bulk_create, objects)
def setUpClass(cls): """Add contact if less than 2 exist""" super(BasicSOQLRoTest, cls).setUpClass() some_contacts = Contact.objects.all()[:2] if len(some_contacts) < 2: for i in range(2 - len(some_contacts)): contact = Contact(first_name='sf_test demo', last_name='Test %d' %i) contact.save() if User.objects.count() == 0: user = User(Username=current_user) user.save()
def test_unsupported_bulk_create(self): """ Unsupported bulk_create: "Errors should never pass silently." """ if not DJANGO_14: self.skipTest('Django 1.3 has no bulk operations.') objects = [ Contact(LastName='sf_test a'), Contact(LastName='sf_test b') ] self.assertRaises(AssertionError, Contact.objects.bulk_create, objects)
def test_insert_date(self): """Test inserting a date. """ now = timezone.now().replace(microsecond=0) contact = Contact( first_name='Joe', last_name='Freelancer', email_bounced_date=now) contact.save() try: self.assertEqual(refresh(contact).email_bounced_date, now) finally: contact.delete()
def test_foreign_key(self): """Verify that the owner of an Contact is the currently logged admin. """ current_sf_user = User.objects.get(Username=current_user) test_contact = Contact(first_name='sf_test', last_name='my') test_contact.save() try: contact = Contact.objects.filter(owner=current_sf_user)[0] user = contact.owner # This user can be e.g. '[email protected]'. self.assertEqual(user.Username, current_user) finally: test_contact.delete()
def test_not_null_related(self): """ Verify conditions `isnull` for foreign keys: filter(Account=None) filter(Account__isnull=True) and nested in Q(...) | Q(...). """ test_contact = Contact(first_name='sf_test', last_name='my') test_contact.save() try: contacts = Contact.objects.filter(Q(account__isnull=True) | Q(account=None), account=None, account__isnull=True, first_name='sf_test') self.assertEqual(len(contacts), 1) finally: test_contact.delete()
def test_insert_date(self): """ Test inserting a date. """ now = round_datetime_utc(datetime.datetime.utcnow()) contact = Contact( FirstName = 'Joe', LastName = 'Freelancer', EmailBouncedDate=now.replace(tzinfo=pytz.utc)) contact.save() try: self.assertEqual(refresh(contact).EmailBouncedDate, now) finally: contact.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_date_comparison(self): """ Test that date comparisons work properly. """ today = round_datetime_utc(datetime.datetime(2013, 8, 27)) yesterday = today - datetime.timedelta(days=1) tomorrow = today + datetime.timedelta(days=1) contact = Contact(FirstName='sf_test', LastName='date', EmailBouncedDate=today) contact.save() try: contacts1 = Contact.objects.filter(EmailBouncedDate__gt=yesterday) self.assertEqual(len(contacts1), 1) contacts2 = Contact.objects.filter(EmailBouncedDate__gt=tomorrow) self.assertEqual(len(contacts2), 0) finally: contact.delete()
def test_update_date(self): """Test updating a date. """ # removed microseconds only for easy compare in the test, no problem now = timezone.now().replace(microsecond=0) contact = Contact(first_name='sf_test', last_name='my') contact.save() contact = refresh(contact) try: contact.email_bounced_date = now contact.save() self.assertEqual(refresh(contact).email_bounced_date, now) finally: contact.delete()
def test_double_delete(self): """ Test that repeated delete of the same object is ignored the same way like "DELETE FROM Contact WHERE Id='deleted yet'" would do. """ contact = Contact(last_name='sf_test', owner=User.objects.get(Username=current_user)) contact.save() contact_id = contact.pk Contact(pk=contact_id).delete() # Id of a deleted object or a too small valid Id shouldn't raise Contact(pk=contact_id).delete() # Simulate the same with obsoleted oauth session # It is not possible to use salesforce.auth.expire_token() to simulate # expiration because it forces reauhentication before the next request salesforce.auth.oauth_data[sf_alias]['access_token'] = '* something invalid *' Contact(pk=contact_id).delete() # Id of completely deleted item or fake but valid item. Contact(pk='003000000000000AAA').delete()
def test_expired_auth_id(self): """Test the code for expired auth ID for multiple SF databases. No similar test exists for a single db. """ self.assertGreaterEqual(len(sf_databases), 1) objects = [] for db in sf_databases: c = Contact(last_name='sf_test %s' % db) c.save(using=db) objects.append(c) try: # simulate that a request with invalid/expired auth ID re-authenticates # and succeeds. for db in sf_databases: salesforce.auth.oauth_data[db]['access_token'] += 'simulated invalid/expired' for x in objects: self.assertTrue(refresh(x)) finally: for x in objects: x.delete()
def test_expired_auth_id(self): """ Test the code for expired auth ID for multiple SF databases. No similar test exists for a single db. """ self.assertGreaterEqual(len(sf_databases), 1) objects = [] for db in sf_databases: c = Contact(last_name='sf_test %s' % db) c.save(using=db) objects.append(c) try: # simulate that a request with invalid/expired auth ID re-authenticates # and succeeds. for db in sf_databases: salesforce.auth.oauth_data[db]['access_token'] += 'simulated invalid/expired' for x in objects: self.assertTrue(refresh(x)) finally: for x in objects: x.delete()
def test_generic_type_field(self): """Test that a generic foreign key can be filtered by type name and the type name can be referenced. """ test_contact = Contact(first_name='sf_test', last_name='my') test_contact.save() note_1 = Note(title='note for Lead', parent_id=self.test_lead.pk) note_2 = Note(title='note for Contact', parent_id=test_contact.pk) note_1.save() note_2.save() try: self.assertEqual( Note.objects.filter(parent_type='Contact')[0].parent_type, 'Contact') self.assertEqual( Note.objects.filter(parent_type='Lead')[0].parent_type, 'Lead') note = Note.objects.filter(parent_type='Contact')[0] parent_model = getattr(salesforce.testrunner.example.models, note.parent_type) parent_object = parent_model.objects.get(pk=note.parent_id) self.assertEqual(parent_object.pk, note.parent_id) finally: note_1.delete() note_2.delete() test_contact.delete()
def test_update_date(self): """ Test updating a date. """ now = timezone.now().replace(microsecond=0) contact = Contact(first_name = 'sf_test', last_name='my') contact.save() contact = refresh(contact) try: contact.email_bounced_date = now contact.save() self.assertEqual(refresh(contact).email_bounced_date, now) finally: contact.delete()
def test_generic_type_field(self): """Test that a generic foreign key can be filtered by type name and the type name can be referenced. """ test_contact = Contact(first_name='sf_test', last_name='my') test_contact.save() note_1 = Note(title='note for Lead', parent_id=self.test_lead.pk) note_2 = Note(title='note for Contact', parent_id=test_contact.pk) note_1.save() note_2.save() try: self.assertEqual(Note.objects.filter(parent_type='Contact')[0].parent_type, 'Contact') self.assertEqual(Note.objects.filter(parent_type='Lead')[0].parent_type, 'Lead') note = Note.objects.filter(parent_type='Contact')[0] parent_model = getattr(salesforce.testrunner.example.models, note.parent_type) parent_object = parent_model.objects.get(pk=note.parent_id) self.assertEqual(parent_object.pk, note.parent_id) finally: note_1.delete() note_2.delete() test_contact.delete()
def test_multiple_sf_databases(self): """Test a connection to two sf sandboxes of the same organization. """ other_db = [db for db in sf_databases if db != sf_alias][0] c1 = Contact(last_name='sf_test 1') c2 = Contact(last_name='sf_test 2') c1.save() c2.save(using=other_db) try: user1 = refresh(c1).owner user2 = refresh(c2).owner username1 = user1.Username username2 = user2.Username # Verify different usernames, like it is usual in sandboxes self.assertNotEqual(user1._state.db, user2._state.db) self.assertNotEqual(username1, username2) expected_user2 = connections[other_db].settings_dict['USER'] self.assertEqual(username1, current_user) self.assertEqual(username2, expected_user2) finally: c1.delete() c2.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()
def setUp(self): """ Create our test lead record. """ def add_obj(obj): obj.save() self.objs.append(obj) # self.test_lead = Lead( FirstName="User", LastName="Unittest General", Email=test_email, Status='Open', Company="Some company, Ltd.", ) self.objs = [] self.test_lead.save() if not default_is_sf: add_obj(Contact(LastName='Test contact 1')) add_obj(Contact(LastName='Test contact 2')) add_obj(User(Username=current_user))
def test_multiple_sf_databases(self): """ Test a connection to two sf databases with the same user. (with sandboxes of the same organization) """ other_db = [db for db in sf_databases if db != sf_alias][0] c1 = Contact(last_name='sf_test 1') c2 = Contact(last_name='sf_test 2') c1.save() c2.save(using=other_db) try: user1 = refresh(c1).owner user2 = refresh(c2).owner username1 = user1.Username username2 = user2.Username # Verify different, but similar usernames like usual in sandboxes self.assertNotEqual(user1._state.db, user2._state.db) self.assertNotEqual(username1, username2) self.assertEqual(username1.split('@')[0], username2.split('@')[0]) finally: c1.delete() c2.delete()
def setUp(self): """Create our test lead record. """ def add_obj(obj): obj.save() self.objs.append(obj) # self.test_lead = Lead( FirstName="User" + uid, LastName="Unittest General", Email=test_email, Status='Open', Company="Some company, Ltd.", ) self.objs = [] self.test_lead.save() # This is only for demonstration that some test can be run even with # non SFDC database SALESFORCE_DB_ALIAS, even if the test expects some # contacts and the current user, but most of tests can pass only on SFDC. if not default_is_sf: add_obj(Contact(last_name='Test contact 1')) add_obj(Contact(last_name='Test contact 2')) add_obj(User(Username=current_user))
def test_default_specified_by_sf(self): """ Verify that an object with a field with default value specified by some Salesforce code can be inserted. (The default is used only for a field unspecified in SF REST API, but not for None or any similar value. It was a pain for some unimportant foreign keys that don't accept null. """ # Verify a smart default is used. contact = Contact(FirstName = 'sf_test', LastName='my') contact.save() try: self.assertEqual(refresh(contact).Owner.Username, current_user) finally: contact.delete() # Verify that an explicit value is possible for this field. other_user_obj = User.objects.exclude(Username=current_user)[0] contact = Contact(FirstName = 'sf_test', LastName='your', Owner=other_user_obj) contact.save() try: self.assertEqual( refresh(contact).Owner.Username, other_user_obj.Username) finally: contact.delete()
def test_double_delete(self): """Test that repeated delete of the same object is ignored the same way like "DELETE FROM Contact WHERE Id='deleted yet'" would do. """ contact = Contact(last_name='sf_test', owner=User.objects.get(Username=current_user)) contact.save() contact_id = contact.pk Contact(pk=contact_id).delete() # Id of a deleted object or a too small valid Id shouldn't raise Contact(pk=contact_id).delete() # Simulate the same with obsoleted oauth session # It is not possible to use salesforce.auth.expire_token() to simulate # expiration because it forces reauhentication before the next request salesforce.auth.oauth_data[sf_alias]['access_token'] = '* something invalid *' Contact(pk=contact_id).delete() # Id of completely deleted item or fake but valid item. Contact(pk='003000000000000AAA').delete()
def test_insert_date(self): """Test inserting a date. """ now = timezone.now().replace(microsecond=0) contact = Contact(first_name='Joe', last_name='Freelancer', email_bounced_date=now) contact.save() try: self.assertEqual(refresh(contact).email_bounced_date, now) finally: contact.delete()
def test_insert_date(self): """ Test inserting a date. """ now = round_datetime_utc(datetime.datetime.utcnow()) contact = Contact(FirstName='Joe', LastName='Freelancer', EmailBouncedDate=now.replace(tzinfo=pytz.utc)) contact.save() try: self.assertEqual(refresh(contact).EmailBouncedDate, now) finally: contact.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_not_null_related(self): """Verify conditions `isnull` for foreign keys: filter(Account=None) filter(Account__isnull=True) and nested in Q(...) | Q(...). """ test_contact = Contact(first_name='sf_test', last_name='my') test_contact.save() try: contacts = Contact.objects.filter(Q(account__isnull=True) | Q(account=None), account=None, account__isnull=True, first_name='sf_test') self.assertEqual(len(contacts), 1) finally: test_contact.delete()
def test_date_comparison(self): """Test that date comparisons work properly. """ today = datetime.datetime(2013, 8, 27) if settings.USE_TZ: today = timezone.make_aware(today, pytz.utc) yesterday = today - datetime.timedelta(days=1) tomorrow = today + datetime.timedelta(days=1) contact = Contact(first_name='sf_test' + uid, last_name='date', email_bounced_date=today) contact.save() try: contacts1 = Contact.objects.filter(email_bounced_date__gt=yesterday, first_name='sf_test' + uid) self.assertEqual(len(contacts1), 1) contacts2 = Contact.objects.filter(email_bounced_date__gt=tomorrow, first_name='sf_test' + uid) self.assertEqual(len(contacts2), 0) finally: contact.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_default_specified_by_sf(self): """Verify insert of object with a field with default value on create by SF. (The default is used only for a field unspecified in SF REST API, but not for None or any similar value. It was a pain for some unimportant foreign keys that don't accept null.) """ # Verify a smart default is used. contact = Contact(first_name='sf_test', last_name='my') contact.save() try: self.assertEqual(refresh(contact).owner.Username, current_user) finally: contact.delete() # Verify that an explicit value is possible for this field. other_user_obj = User.objects.exclude(Username=current_user)[0] contact = Contact(first_name='sf_test', last_name='your', owner=other_user_obj) contact.save() try: self.assertEqual( refresh(contact).owner.Username, other_user_obj.Username) finally: contact.delete()