Add new tests by recording
+"""Recorded tests by MockTestCase"""
from typing import cast
import datetime
import decimal
import unittest
+import django
from django.test.utils import override_settings
from .models import Account, Contact, Unreal
-from tests.test_mock.mocksf import MockJsonRequest, MockTestCase # mock
+from tests.test_mock.mocksf import MockJsonRequest, MockRequest, MockTestCase # mock
+from salesforce.backend import DJANGO_30_PLUS
from salesforce.dbapi.exceptions import SalesforceError
@@ -15,8 +18,47 @@ class TestMock(MockTestCase):
databases = {'salesforce'}
@override_settings(SF_MOCK_MODE='record')
- # @override_settings(SF_MOCK_VERBOSITY=0)
+ @override_settings(SF_MOCK_VERBOSITY=0)
def test_simple_crud(self) -> None:
+ """Record the commands to a variable (quiet verbosity)"""
+ acc = cast(Account, Account.objects.create(name='a'))
+ acc = Account.objects.get(id=acc.id)
+ acc.name = 'b'
+ acc.save()
+ acc.delete()
+ output = django.db.connections['salesforce'].connection._sf_session.mock_recorded
+ self.assertIn('POST mock:///services/data/', output[0])
+ self.assertIn('GET mock:///services/data/', output[1])
+ self.assertIn('PATCH mock:///services/data/', output[2])
+ self.assertIn('DELETE mock:///services/data/', output[3])
+
+ @override_settings(SF_MOCK_MODE='playback')
+ def test_simple_crud_replay(self) -> None:
+ """Replay the recorded commands and verify that also requests are exactly repeated"""
+ optional_sql = '+LIMIT+21' if DJANGO_30_PLUS else ''
+ self.mock_add_expected([
+ MockJsonRequest(
+ "POST mock:///services/data/v51.0/sobjects/Account",
+ req={'Name': 'a'},
+ resp='{"id":"001M000001FgVKlIAN","success":true,"errors":[]}',
+ status_code=201),
+ MockJsonRequest(
+ "GET mock:///services/data/v51.0/query/?q=SELECT+Account.Id%2C+Account.Name%2C+Account.OwnerId+"
+ "FROM+Account+WHERE+Account.Id+%3D+%27001M000001FgVKlIAN%27" + optional_sql,
+ resp='{"totalSize":1,"done":true,"records":['
+ '{"attributes":{"type":"Account","url":"/services/data/v51.0/sobjects/Account/001M000001FgVKlIAN"},'
+ '"Id":"001M000001FgVKlIAN","Name":"a","OwnerId":"005M0000007whduIAA"}]}'),
+ MockJsonRequest(
+ "PATCH mock:///services/data/v51.0/sobjects/Account/001M000001FgVKlIAN",
+ req={'Name': 'b', 'OwnerId': '005M0000007whduIAA'},
+ request_type='',
+ status_code=204),
+ MockRequest(
+ "DELETE mock:///services/data/v51.0/sobjects/Account/001M000001FgVKlIAN",
+ request_type='',
+ status_code=204)
+ ])
+
acc = cast(Account, Account.objects.create(name='a'))
acc = Account.objects.get(id=acc.id)
acc.name = 'b'
