mercredi 28 septembre 2016

python/django MagicMock return return value not behaving like I expect

I havr this try/except loop in a model method that I am trying to write tests for...

try:
    wiktionary_url = "http://%s.wiktionary.org/wiki/FILE:%s-%s.ogg" \
        % (self.language.wiktionary_prefix, url_sub_path, self.name)
    wiktionary_page = urllib2.urlopen(wiktionary_url)
    wiktionary_page = fromstring(wiktionary_page.read())
    file_url = wiktionary_page.xpath(
                "//*[contains(concat(' ', @class, ' '), ' fullMedia ')]/a/@href")[0]
    file_number = self.get_num(
        self.definition_set.all()[0].search_existing_audio())
    print file_number + " file num"
    relative_path = '%s/%s%s.ogg' % (path, self.name, file_number)
    full_path = '%s/%s' % (settings.MEDIA_ROOT, relative_path)
    popen("wget -q -O %s 'http:%s'" % (full_path, file_url))
    made = self.definition_set.all()[0].convert_to_mp3(full_path)
except Exception as exc:
    print str(exc)
    return False

and I am trying to write a test for it while mocking some of the function in it.

@patch("lang_api.models.urllib2.urlopen")
@patch("lang_api.models.fromstring")
@patch("lang_api.models.Definition.convert_to_mp3")
@patch("lang_api.models.os.popen")
def test_get_wiktionary_audio(self, popen, convert_to_mp3, fromstring, urlopen):
    # I need to mock another function in the actual method
    fromstring.return_value = MagicMock(return_value='string')
    test = self.things.model['word'].get_wiktionary_audio('en')
    popen.assert_called()
    convert_to_mp3.assert_called()

when I try something similar in the python intepreter it works perfectly. but when this test runs it outputs this error.

sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `wget -q -O /Users/Jeff/Development/langalang/langalang/../media/study_audio/eng/words/table1.ogg 'http:<MagicMock name='fromstring().xpath().__getitem__()' id='4361699216'>''

so my return value statements are not being read properly but I cannot see what is happening.

Aucun commentaire:

Enregistrer un commentaire