old_contents: """
  Test the test_bulk_user_org_email_optout management command
  """
  from __future__ import absolute_import
  
  import os
  import tempfile
  from contextlib import contextmanager
  
  import mock
  import pytest
  from django.core.management import call_command
  
  pytestmark = pytest.mark.django_db
  
  
  CSV_DATA = """1,UniversityX
  2,CollegeX
  3,StateUX
  """
  
  
  @contextmanager
  def _create_test_csv(csv_data):
      """
      Context manager to create and populate a CSV file - and delete it after usage.
      """
      __, file_name = tempfile.mkstemp(text=True)
      with open(file_name, 'w') as file_pointer:
          file_pointer.write(csv_data.encode('utf-8'))
      try:
          yield file_name
      finally:
          os.unlink(file_name)
  
  
  @mock.patch('openedx.core.djangoapps.user_api.management.commands.bulk_user_org_email_optout.log.info')
  def test_successful_dry_run(mock_logger):
      """
      Run the command with default states for a successful initial population
      """
      with _create_test_csv(CSV_DATA) as tmp_csv_file:
          args = ['--dry_run', '--optout_csv_path={}'.format(tmp_csv_file)]
          call_command('bulk_user_org_email_optout', *args)
          assert mock_logger.call_count == 3
          mock_logger.assert_any_call(u"Read %s opt-out rows from CSV file '%s'.", 3, tmp_csv_file)
          mock_logger.assert_any_call(
              u'Attempting opt-out for rows (%s, %s) through (%s, %s)...', '1', 'UniversityX', '3', 'StateUX'
          )
          mock_logger.assert_any_call(
              'INSERT INTO user_api_userorgtag (`user_id`, `org`, `key`, `value`, `created`, `modified`) \
  VALUES (1,"UniversityX","email-optin","False",NOW(),NOW()),(2,"CollegeX","email-optin","False",NOW(),NOW()),\
  (3,"StateUX","email-optin","False",NOW(),NOW()) ON DUPLICATE KEY UPDATE value="False", modified=NOW();')
  
new_contents: """
  Test the test_bulk_user_org_email_optout management command
  """
  from __future__ import absolute_import
  
  import io
  import os
  import tempfile
  from contextlib import contextmanager
  
  import mock
  import pytest
  from django.core.management import call_command
  
  pytestmark = pytest.mark.django_db
  
  
  CSV_DATA = u"""1,UniversityX
  2,CollegeX
  3,StateUX
  """
  
  
  @contextmanager
  def _create_test_csv(csv_data):
      """
      Context manager to create and populate a CSV file - and delete it after usage.
      """
      __, file_name = tempfile.mkstemp(text=True)
      with io.open(file_name, 'w') as file_pointer:
          file_pointer.write(csv_data)
      try:
          yield file_name
      finally:
          os.unlink(file_name)
  
  
  @mock.patch('openedx.core.djangoapps.user_api.management.commands.bulk_user_org_email_optout.log.info')
  def test_successful_dry_run(mock_logger):
      """
      Run the command with default states for a successful initial population
      """
      with _create_test_csv(CSV_DATA) as tmp_csv_file:
          args = ['--dry_run', '--optout_csv_path={}'.format(tmp_csv_file)]
          call_command('bulk_user_org_email_optout', *args)
          assert mock_logger.call_count == 3
          mock_logger.assert_any_call(u"Read %s opt-out rows from CSV file '%s'.", 3, tmp_csv_file)
          mock_logger.assert_any_call(
              u'Attempting opt-out for rows (%s, %s) through (%s, %s)...', '1', 'UniversityX', '3', 'StateUX'
          )
          mock_logger.assert_any_call(
              'INSERT INTO user_api_userorgtag (`user_id`, `org`, `key`, `value`, `created`, `modified`) \
  VALUES (1,"UniversityX","email-optin","False",NOW(),NOW()),(2,"CollegeX","email-optin","False",NOW(),NOW()),\
  (3,"StateUX","email-optin","False",NOW(),NOW()) ON DUPLICATE KEY UPDATE value="False", modified=NOW();')
  
subject: Fix CSV writing in test BOM-639 (#21762)