API

Unittest/django support

ApiCheckerMixin

class drf_api_checker.unittest.ApiCheckerMixin[source]

Mixin to enable API contract check

How to use it:

  • implement get_fixtures() to create data for test. It should returns a dictionary
  • use self.assert<METHOD>(url) to check urls contract

Example:

class TestAPIAgreements(ApiCheckerMixin, TestCase):
def get_fixtures(self):
return {‘customer’: CustomerFactory()}
def test_customer_detail(self):
url = reverse(“customer-detail”, args=[self.get_fixture(‘customer’).pk]) self.assertGET(url)

ApiCheckerBase

class drf_api_checker.unittest.ApiCheckerBase[source]

Custom _type_, intended to be used as metaclass. It will create a test for each url defined in URLS in the format test__<normalized_url_path>, if a method with the same name is found the creation is skipped reading this as an intention to have a custom test for that url.

class TestAPIIntervention(TestCase, metaclass=ApiCheckerBase):
URLS = [
reverse(“intervention-list”), reverse(“intervention-detail”, args=[101]),

]

def get_fixtures(cls):
return {‘intervention’: InterventionFactory(id=101),
‘result’: ResultFactory(), }

running this code will produce…

… test_url__api_v2_interventions (etools.applications.partners.tests.test_api.TestAPIIntervention) … ok test_url__api_v2_interventions_101 (etools.applications.partners.tests.test_api.TestAPIIntervention) … ok …

pytest support

@frozenfixture

drf_api_checker.pytest.frozenfixture(fixture_name=<function default_fixture_name>)[source]

@contract

drf_api_checker.pytest.contract(recorder_class=<class 'drf_api_checker.recorder.Recorder'>, allow_empty=False, name=None, method='get', checks=None, debug=False, **kwargs)[source]

Internals

Recorder

class drf_api_checker.recorder.Recorder(data_dir, owner=None, headers_to_check=None, fixture_file=None, as_user=None)[source]

ForeignKeysCollector

class drf_api_checker.collector.ForeignKeysCollector(using)[source]