def test_load_processed_files(self): """ Successful load of content. """ test_lines = ( "/dir/ 0", "/dir/file 12345", "/dir/file with spaces 7138743", " /dir/with extra space 12345 ", " \t ", "", "/dir/after empty line 12345", ) expected_value = { "/dir/": 0, "/dir/file": 12345, "/dir/file with spaces": 7138743, "/dir/with extra space": 12345, "/dir/after empty line": 12345, } test_content = StringIO.StringIO("\n".join(test_lines)) mocking.support_with(test_content) mocking.mock(open, mocking.return_value(test_content)) self.assertEquals(expected_value, stem.descriptor.reader.load_processed_files(""))
def test_mirror_mirror_on_the_wall_2(self): def tutorial_example(): from stem.descriptor import parse_file for desc in parse_file(open("/home/atagar/.tor/cached-consensus")): print "found relay %s (%s)" % (desc.nickname, desc.fingerprint) test_file = io.BytesIO( mocking.get_network_status_document_v3( routers=[mocking.get_router_status_entry_v3()], content=True, )) mocking.support_with(test_file) test_file.name = "/home/atagar/.tor/cached-consensus" if is_python_3(): import builtins mocking.mock(open, mocking.return_value(test_file), target_module=builtins) else: mocking.mock(open, mocking.return_value(test_file)) tutorial_example() self.assertEqual( "found relay caerSidi (A7569A83B5706AB1B1A9CB52EFF7D2D32E4553EB)\n", self.stdout.getvalue())
def test_load_processed_files_empty(self): """ Tests the load_processed_files() function with an empty file. """ test_content = StringIO.StringIO("") mocking.support_with(test_content) mocking.mock(open, mocking.return_value(test_content)) self.assertEquals({}, stem.descriptor.reader.load_processed_files(""))
def _mock_open(content): test_content = StringIO.StringIO(content) mocking.support_with(test_content) if stem.prereq.is_python_3(): import builtins mocking.mock(builtins.open, mocking.return_value(test_content), builtins) else: mocking.mock(open, mocking.return_value(test_content))
def test_load_processed_files_malformed_timestamp(self): """ Tests the load_processed_files() function content that is malformed because it has a non-numeric timestamp. """ test_content = StringIO.StringIO("/dir/file 123a") mocking.support_with(test_content) mocking.mock(open, mocking.return_value(test_content)) self.assertRaises(TypeError, stem.descriptor.reader.load_processed_files, "")
def test_load_processed_files_malformed_file(self): """ Tests the load_processed_files() function content that is malformed because it has an invalid file path. """ test_content = StringIO.StringIO("not_an_absolute_file 12345") mocking.support_with(test_content) mocking.mock(open, mocking.return_value(test_content)) self.assertRaises(TypeError, stem.descriptor.reader.load_processed_files, "")
def test_examples(self): """ Run something similar to the examples in the header pydocs. """ # makes a consensus with a couple routers, both with the same nickname entry1 = get_router_status_entry_v3({'s': "Fast"}) entry2 = get_router_status_entry_v3({'s': "Valid"}) content = get_network_status_document_v3(routers=(entry1, entry2), content=True) # first example: parsing via the NetworkStatusDocumentV3 constructor consensus_file = StringIO.StringIO(content) consensus = NetworkStatusDocumentV3(consensus_file.read()) consensus_file.close() for router in consensus.routers: self.assertEqual('caerSidi', router.nickname) # second example: using stem.descriptor.parse_file with support_with(StringIO.StringIO(content)) as consensus_file: for router in stem.descriptor.parse_file( consensus_file, 'network-status-consensus-3 1.0'): self.assertEqual('caerSidi', router.nickname)
def test_examples(self): """ Run something similar to the examples in the header pydocs. """ # makes a consensus with a couple routers, both with the same nickname entry1 = get_router_status_entry_v3({'s': "Fast"}) entry2 = get_router_status_entry_v3({'s': "Valid"}) content = get_network_status_document_v3(routers = (entry1, entry2), content = True) # first example: parsing via the NetworkStatusDocumentV3 constructor consensus_file = StringIO.StringIO(content) consensus = NetworkStatusDocumentV3(consensus_file.read()) consensus_file.close() for router in consensus.routers: self.assertEqual('caerSidi', router.nickname) # second example: using parse_file with support_with(StringIO.StringIO(content)) as consensus_file: for router in parse_file(consensus_file): self.assertEqual('caerSidi', router.nickname)
def test_mirror_mirror_on_the_wall_2(self): def tutorial_example(): from stem.descriptor import parse_file for desc in parse_file(open("/home/atagar/.tor/cached-consensus")): print "found relay %s (%s)" % (desc.nickname, desc.fingerprint) test_file = io.BytesIO(mocking.get_network_status_document_v3( routers = [mocking.get_router_status_entry_v3()], content = True, )) mocking.support_with(test_file) test_file.name = "/home/atagar/.tor/cached-consensus" if is_python_3(): import builtins mocking.mock(open, mocking.return_value(test_file), target_module = builtins) else: mocking.mock(open, mocking.return_value(test_file)) tutorial_example() self.assertEqual("found relay caerSidi (A7569A83B5706AB1B1A9CB52EFF7D2D32E4553EB)\n", self.stdout.getvalue())