from __future__ import absolute_import

import pytest
import os
import mock
import json

import app.mapping


with open(os.path.join(os.path.dirname(__file__), 'fixtures/mappings/services.json')) as f:
    _services_mapping_definition = json.load(f)


@pytest.fixture(scope="function")
def services_mapping():
    """Provide a services mapping fixture, and patch it into the global singleton getter."""

    mock_services_mapping_getter_patch = mock.patch('app.mapping.get_services_mapping')
    mock_services_mapping_getter = mock_services_mapping_getter_patch.start()
    mock_services_mapping_getter.return_value = app.mapping.Mapping(_services_mapping_definition, 'services')

    yield mock_services_mapping_getter.return_value

    mock_services_mapping_getter_patch.stop()

from __future__ import absolute_import

import pytest
import os
import mock
import json

import app.mapping


with open(os.path.join(os.path.dirname(__file__), 'fixtures/mappings/services.json')) as f:
    _services_mapping_definition = json.load(f)


@pytest.fixture(scope="function")
def services_mapping():
    """Provide a services mapping fixture, and patch it into the global singleton getter."""

    with mock.patch('app.mapping.get_services_mapping') as mock_services_mapping_getter:
        mock_services_mapping_getter.return_value = app.mapping.Mapping(_services_mapping_definition, 'services')
        yield mock_services_mapping_getter.return_value

Use with block to start/stop the patch context manager.
Use with block to start/stop the patch context manager.

 - this is less code, hopefully is just as clear why we need to 'yield'
   rather than just 'return'.

https://trello.com/c/OpWI068M/380-after-g9-go-live-removal-of-old-filters-from-search-api-mapping

  from __future__ import absolute_import
  
  import pytest
  import os
  import mock
  import json
  
  import app.mapping
  
  
  with open(os.path.join(os.path.dirname(__file__), 'fixtures/mappings/services.json')) as f:
      _services_mapping_definition = json.load(f)
  
  
  @pytest.fixture(scope="function")
  def services_mapping():
      """Provide a services mapping fixture, and patch it into the global singleton getter."""
  
+     with mock.patch('app.mapping.get_services_mapping') as mock_services_mapping_getter:
-     mock_services_mapping_getter_patch = mock.patch('app.mapping.get_services_mapping')
-     mock_services_mapping_getter = mock_services_mapping_getter_patch.start()
-     mock_services_mapping_getter.return_value = app.mapping.Mapping(_services_mapping_definition, 'services')
+         mock_services_mapping_getter.return_value = app.mapping.Mapping(_services_mapping_definition, 'services')
+         yield mock_services_mapping_getter.return_value
  
-     yield mock_services_mapping_getter.return_value
- 
-     mock_services_mapping_getter_patch.stop()
- 
Use with block to start/stop the patch context manager.
## Code Before:
from __future__ import absolute_import

import pytest
import os
import mock
import json

import app.mapping


with open(os.path.join(os.path.dirname(__file__), 'fixtures/mappings/services.json')) as f:
    _services_mapping_definition = json.load(f)


@pytest.fixture(scope="function")
def services_mapping():
    """Provide a services mapping fixture, and patch it into the global singleton getter."""

    mock_services_mapping_getter_patch = mock.patch('app.mapping.get_services_mapping')
    mock_services_mapping_getter = mock_services_mapping_getter_patch.start()
    mock_services_mapping_getter.return_value = app.mapping.Mapping(_services_mapping_definition, 'services')

    yield mock_services_mapping_getter.return_value

    mock_services_mapping_getter_patch.stop()

## Instruction:
Use with block to start/stop the patch context manager.
## Code After:
from __future__ import absolute_import

import pytest
import os
import mock
import json

import app.mapping


with open(os.path.join(os.path.dirname(__file__), 'fixtures/mappings/services.json')) as f:
    _services_mapping_definition = json.load(f)


@pytest.fixture(scope="function")
def services_mapping():
    """Provide a services mapping fixture, and patch it into the global singleton getter."""

    with mock.patch('app.mapping.get_services_mapping') as mock_services_mapping_getter:
        mock_services_mapping_getter.return_value = app.mapping.Mapping(_services_mapping_definition, 'services')
        yield mock_services_mapping_getter.return_value
