from nose.tools import eq_
from mock import MagicMock, patch

from buffpy.models.profile import Profile, PATHS

mocked_response = {
  'name': 'me',
  'service': 'twiter',
  'id': 1
}

def test_profile_schedules_getter():
  '''
    Test schedules gettering from buffer api
  '''

  mocked_api = MagicMock()
  mocked_api.get.return_value = '123'

  profile = Profile(mocked_api, mocked_response)

  eq_(profile.schedules, '123')
  mocked_api.get.assert_called_once_with(url = PATHS['GET_SCHEDULES'] % 1)

def test_profile_schedules_setter():
  '''
    Test schedules setter from buffer api
  '''

  mocked_api = MagicMock()
  mocked_api.get.return_value = '123'

  profile = Profile(mocked_api, mocked_response)

  profile.schedules = {
      'times': ['mo']
  }

  mocked_api.post.assert_called_once_with(url=PATHS['UPDATE_SCHEDULES'] % 1,
      data='schedules[0][times][]=mo&')

def test_profile_updates():
  '''
    Test updates relationship with a profile
  '''

  mocked_api = MagicMock()

  with patch('buffpy.models.profile.Updates') as mocked_updates:
    profile = Profile(api=mocked_api, raw_response={'id': 1})
    updates = profile.updates

    mocked_updates.assert_called_once_with(api=mocked_api, profile_id=1)

from unittest.mock import MagicMock, patch

from buffpy.models.profile import Profile, PATHS


mocked_response = {
    "name": "me",
    "service": "twiter",
    "id": 1
}


def test_profile_schedules_getter():
    """ Should retrieve profiles from buffer's API. """

    mocked_api = MagicMock()
    mocked_api.get.return_value = "123"

    profile = Profile(mocked_api, mocked_response)

    assert profile.schedules == "123"
    mocked_api.get.assert_called_once_with(url=PATHS["GET_SCHEDULES"].format("1"))


def test_profile_schedules_setter():
    """ Should update profile's schedules. """

    mocked_api = MagicMock()
    mocked_api.get.return_value = "123"

    profile = Profile(mocked_api, mocked_response)

    profile.schedules = {
        "times": ["mo"]
    }

    mocked_api.post.assert_called_once_with(
        url=PATHS["UPDATE_SCHEDULES"].format("1"),
        data="schedules[0][times][]=mo&")


def test_profile_updates():
    """ Should properly call buffer's updates. """

    mocked_api = MagicMock()

    with patch("buffpy.models.profile.Updates") as mocked_updates:
        profile = Profile(api=mocked_api, raw_response={"id": 1})

        assert profile.updates
        mocked_updates.assert_called_once_with(api=mocked_api, profile_id=1)

Migrate profile tests to pytest
Migrate profile tests to pytest

- from nose.tools import eq_
- from mock import MagicMock, patch
+ from unittest.mock import MagicMock, patch
  
  from buffpy.models.profile import Profile, PATHS
  
+ 
  mocked_response = {
-   'name': 'me',
+     "name": "me",
-   'service': 'twiter',
+     "service": "twiter",
-   'id': 1
+     "id": 1
  }
  
+ 
  def test_profile_schedules_getter():
+     """ Should retrieve profiles from buffer's API. """
-   '''
-     Test schedules gettering from buffer api
-   '''
  
-   mocked_api = MagicMock()
+     mocked_api = MagicMock()
-   mocked_api.get.return_value = '123'
+     mocked_api.get.return_value = "123"
  
-   profile = Profile(mocked_api, mocked_response)
+     profile = Profile(mocked_api, mocked_response)
  
-   eq_(profile.schedules, '123')
+     assert profile.schedules == "123"
-   mocked_api.get.assert_called_once_with(url = PATHS['GET_SCHEDULES'] % 1)
+     mocked_api.get.assert_called_once_with(url=PATHS["GET_SCHEDULES"].format("1"))
+ 
  
  def test_profile_schedules_setter():
+     """ Should update profile's schedules. """
-   '''
-     Test schedules setter from buffer api
-   '''
  
-   mocked_api = MagicMock()
+     mocked_api = MagicMock()
-   mocked_api.get.return_value = '123'
+     mocked_api.get.return_value = "123"
  
-   profile = Profile(mocked_api, mocked_response)
+     profile = Profile(mocked_api, mocked_response)
  
-   profile.schedules = {
+     profile.schedules = {
-       'times': ['mo']
+         "times": ["mo"]
-   }
+     }
  
-   mocked_api.post.assert_called_once_with(url=PATHS['UPDATE_SCHEDULES'] % 1,
+     mocked_api.post.assert_called_once_with(
+         url=PATHS["UPDATE_SCHEDULES"].format("1"),
-       data='schedules[0][times][]=mo&')
+         data="schedules[0][times][]=mo&")
+ 
  
  def test_profile_updates():
+     """ Should properly call buffer's updates. """
-   '''
-     Test updates relationship with a profile
-   '''
  
-   mocked_api = MagicMock()
+     mocked_api = MagicMock()
  
-   with patch('buffpy.models.profile.Updates') as mocked_updates:
+     with patch("buffpy.models.profile.Updates") as mocked_updates:
-     profile = Profile(api=mocked_api, raw_response={'id': 1})
+         profile = Profile(api=mocked_api, raw_response={"id": 1})
-     updates = profile.updates
  
+         assert profile.updates
-     mocked_updates.assert_called_once_with(api=mocked_api, profile_id=1)
+         mocked_updates.assert_called_once_with(api=mocked_api, profile_id=1)
  
Migrate profile tests to pytest
## Code Before:
from nose.tools import eq_
from mock import MagicMock, patch

from buffpy.models.profile import Profile, PATHS

mocked_response = {
  'name': 'me',
  'service': 'twiter',
  'id': 1
}

def test_profile_schedules_getter():
  '''
    Test schedules gettering from buffer api
  '''

  mocked_api = MagicMock()
  mocked_api.get.return_value = '123'

  profile = Profile(mocked_api, mocked_response)

  eq_(profile.schedules, '123')
  mocked_api.get.assert_called_once_with(url = PATHS['GET_SCHEDULES'] % 1)

def test_profile_schedules_setter():
  '''
    Test schedules setter from buffer api
  '''

  mocked_api = MagicMock()
  mocked_api.get.return_value = '123'

  profile = Profile(mocked_api, mocked_response)

  profile.schedules = {
      'times': ['mo']
  }

  mocked_api.post.assert_called_once_with(url=PATHS['UPDATE_SCHEDULES'] % 1,
      data='schedules[0][times][]=mo&')

def test_profile_updates():
  '''
    Test updates relationship with a profile
  '''

  mocked_api = MagicMock()

  with patch('buffpy.models.profile.Updates') as mocked_updates:
    profile = Profile(api=mocked_api, raw_response={'id': 1})
    updates = profile.updates

    mocked_updates.assert_called_once_with(api=mocked_api, profile_id=1)

## Instruction:
Migrate profile tests to pytest
## Code After:
from unittest.mock import MagicMock, patch

from buffpy.models.profile import Profile, PATHS


mocked_response = {
    "name": "me",
    "service": "twiter",
    "id": 1
}


def test_profile_schedules_getter():
    """ Should retrieve profiles from buffer's API. """

    mocked_api = MagicMock()
    mocked_api.get.return_value = "123"

    profile = Profile(mocked_api, mocked_response)

    assert profile.schedules == "123"
    mocked_api.get.assert_called_once_with(url=PATHS["GET_SCHEDULES"].format("1"))


def test_profile_schedules_setter():
    """ Should update profile's schedules. """

    mocked_api = MagicMock()
    mocked_api.get.return_value = "123"

    profile = Profile(mocked_api, mocked_response)

    profile.schedules = {
        "times": ["mo"]
    }

    mocked_api.post.assert_called_once_with(
        url=PATHS["UPDATE_SCHEDULES"].format("1"),
        data="schedules[0][times][]=mo&")


def test_profile_updates():
    """ Should properly call buffer's updates. """

    mocked_api = MagicMock()

    with patch("buffpy.models.profile.Updates") as mocked_updates:
        profile = Profile(api=mocked_api, raw_response={"id": 1})

        assert profile.updates
        mocked_updates.assert_called_once_with(api=mocked_api, profile_id=1)
