rumdrums
rumdrums

Reputation: 1382

Python: Mock Opening File, return Actual File

I need to test a call to gzip.open, but I need it to supply it with an actual test file with test data in it to read. I've seen several very similar questions, but none of them work as expected.

This is the code I'm testing:

with gzip.open(local_file_path,'r') as content:
    for line in content:
        try:
            if line.startswith('#'):
                continue
            line_data = line.split('\t')
            request_key = line_data[LINE_FORMAT['date']]
            request_key += '-' + line_data[LINE_FORMAT['time']][:-3]
            request_key += '-' + line_data[LINE_FORMAT['source_ip']]
            if request_key in result.keys():
                result[request_key] += 1
            else:
                result[request_key] = 1
            num_requests += 1

        except Exception, e:
            print ("[get_outstanding_requesters] \t\tError to process line: %s"%line)

I think the problem is related to the issues discussed here because the code treats the file as an iterator, but none of the workarounds discussed have worked for me.

I've tried variations on this:

test_data = open('test_access_log').read()
m = mock.mock_open(read_data=test_data)
m.return_value.__iter__ = lambda self:self
m.return_value.__next__ = lambda self: self.readline()
with mock.patch('gzip.open', m):
    with gzip.open('asdf') as f:
       for i in f:
         print i

Which results in:

TypeError: iter() returned non-iterator of type 'MagicMock'

I'm using Python 2.7. I'm tearing my hair out over this one. Is my only solution to forget trying to use an iterator (actual files can be very large, which is why I'm trying to avoid doing so?)

Upvotes: 0

Views: 1305

Answers (1)

rumdrums
rumdrums

Reputation: 1382

This is working:

import unittest
import mock

test_data = open('test_access_log').read()
m = mock.mock_open(read_data=test_data)
m.return_value.__iter__.return_value = test_data.splitlines()
with mock.patch('gzip.open', m):
  with gzip.open('test_access_log') as f:
    for i in f:
      print i

Thanks to bash-shell.net

Upvotes: 1

Related Questions