def test_load(self): """Load redirects from specified file or self.file_name.""" rd1 = Redirect('/one', '/two', 302) rd2 = Redirect('/three', '/four', 302) rd3 = Redirect('/five', '/six', 302) rdf = RedirectsFile('/tmp/foo.json') rdf.add_redirect(rd1) rdf.add_redirect(rd2) rdf.add_redirect(rd3) json_file = StringIO() json_file.write(json.dumps([rd.to_JSON() for rd in rdf.redirects])) json_file.seek(0) rdf2 = RedirectsFile('/tmp/foo.json') m = mock.mock_open() with mock.patch('builtins.open', m, create=True): m.return_value = json_file rdf2.load('/tmp/bar.json') m.assert_called_with('/tmp/bar.json', 'r', encoding='utf-8') json_file = StringIO() json_file.write(json.dumps([rd.to_JSON() for rd in rdf.redirects])) json_file.seek(0) rdf2 = RedirectsFile('/tmp/foo.json') m = mock.mock_open() with mock.patch('builtins.open', m, create=True): m.return_value = json_file rdf2.load() m.assert_called_with('/tmp/foo.json', 'r', encoding='utf-8')