def test_retrieve_content(self, mock_request): mock_request.return_value = get_mock_response( 200, '{"id": 100, "slug": "resource1"}' ) resource = Resource.get(project_slug='project1', slug='resource1') mock_request.return_value = get_mock_response( 200, '{"content": "string1\\nstring2\\nstring3"}' ) content = resource.retrieve_content() assert content == 'string1\nstring2\nstring3'
def test_delete_existing_object(self, mock_request): mock_request.return_value = get_mock_response( 200, '{"id": 100, "slug": "slug"}' ) obj = DummyModel.get(slug='slug') mock_request.return_value = get_mock_response( 200, '{}' ) obj.delete() assert mock_request.call_count == 2
def test_update(self, mock_request): """Test the update of a Resource when no 'content' parameter is set.""" mock_request.return_value = get_mock_response( 200, '{"id": 100, "slug": "resource1"}' ) resource = Resource.get(project_slug='project1', slug='resource1') mock_request.return_value = get_mock_response( 200, '{"content": "only_one_string"}' ) resource.save( name='Resource--1', ) assert resource.name == 'Resource--1'
def test_get_translation(self, mock_request): mock_request.return_value = get_mock_response( 200, u'{"content": {"Master_key": "τεστ"}}' ) obj = Translation.get(project_slug='project1', slug='resource1', lang='el') assert obj.lang == 'el' assert obj.content == {'Master_key': u'τεστ'}
def test_saving_existing_object_updates_it(self, mock_request): mock_request.return_value = get_mock_response( 200, '{"id": 100, "slug": "slug"}' ) obj = DummyModel.get(slug='slug') mock_request.return_value = get_mock_response( 200, '{"id": 100, "slug": "slug", ' '"description": "description", "name": "name"}' ) obj.save( description='description', name='name' ) assert obj.name == 'name' assert obj.description == 'description' assert mock_request.call_count == 2
def test_get_populates_object(self, mock_request): mock_request.return_value = get_mock_response( 200, '{"id": 100, "slug": "slug"}' ) obj = DummyModel.get(slug='slug') assert obj.id == 100 assert obj.slug == 'slug' assert obj._populated_fields == {"id": 100, "slug": "slug"}
def test_get_populates_object(self, mock_request): mock_request.return_value = get_mock_response( 200, '{"id": 100, "slug": "project1"}' ) obj = Project.get(slug='project1') assert obj.id == 100 assert obj.slug == 'project1' assert '{}'.format(obj) == '[Project slug=project1]'
def test_get_populates_object(self, mock_request): mock_request.return_value = get_mock_response( 200, '{"id": 100, "slug": "resource1"}' ) obj = Resource.get(project_slug='project1', slug='resource1') assert obj.id == 100 assert obj.slug == 'resource1' assert '{}'.format(obj) == '[Resource slug=resource1]'
def test_update_content(self, mock_request): """Test the update of a Resource when a 'content' parameter is set. The `content` parameter needs an additional API request, so it is handled separately. """ mock_request.return_value = get_mock_response( 200, '{"id": 100, "slug": "resource1"}' ) resource = Resource.get(project_slug='project1', slug='resource1') mock_request.return_value = get_mock_response( 200, '{"content": "only_one_string"}' ) resource.save( name='Resource--1', content='only_one_string' ) assert resource.content == 'only_one_string'
def test_saving_empty_object_does_not_make_request(self, mock_request): mock_request.return_value = get_mock_response( 200, '{"id": 100, "slug": "slug"}' ) obj = DummyModel.get(slug='slug') obj.save() # The mock request should only have been called once, # for `.get()`. The next call, `.save()` should not result # to a request, because no attribute was actually set assert mock_request.call_count == 1
def test_save_content(self, mock_request): mock_request.return_value = get_mock_response( 200, '{"content": "string1\\nstring2\\nstring3"}' ) resource = Resource(project_slug='project1', slug='resource1') resource.save( name='Resource1', content='string1\\nstring2\\nstring3' ) assert resource.slug == 'resource1' assert resource.content == 'string1\\nstring2\\nstring3'
def test_put_translation(self, mock_request): mock_request.return_value = get_mock_response( 200, '{\ "strings_added": 1,\ "strings_updated": 0,\ "strings_delete": 0,\ "redirect": ""\ }' ) content = { 'Master_key': 'τεστ' } translation = Translation( project_slug='project1', slug='resource1', lang='el' ) translation.save(content=content) assert translation.lang == 'el' assert translation.content == {'Master_key': 'τεστ'}
def test_saving_new_object_creates_it(self, mock_request): mock_request.return_value = get_mock_response( 200, '{"id": 100, "description": "description"}' ) # Method 1: set attributes, then save() obj = DummyModel(slug='slug') obj.name = 'name' obj.description = 'description' obj.save() assert obj.name == 'name' assert obj.description == 'description' # Method 2: save() at once with all attributes obj = DummyModel(slug='slug') obj.save( name='name', description='description' ) assert obj.name == 'name' assert obj.description == 'description'
def test_get_populates_object(self, mock_request): mock_response = """{ "en": { "reviewed_percentage": "0%", "completed": "100%", "untranslated_words": 0, "last_commiter": "diegobz", "reviewed": 0, "translated_entities": 2165, "translated_words": 16629, "last_update": "2014-02-20 14:25:06", "untranslated_entities": 0 }, }""" mock_request.return_value = get_mock_response( 200, mock_response, ) obj = Statistics.get(project_slug='sample_project', slug='new_resource') assert obj == mock_response