def test_cache_element(self, tmpdir): cache_item_id = "unga1" cache = virtwho.virt.esx.suds.cache.DocumentCache(tmpdir.strpath) assert isinstance(cache, virtwho.virt.esx.suds.cache.FileCache) assert cache.get(cache_item_id) is None content, document = self.construct_XML() cache.put(cache_item_id, document.root()) self.compare_document_to_content(cache.get(cache_item_id), content)
def test_basic(self, tmpdir): cache = virtwho.virt.esx.suds.cache.ObjectCache(tmpdir.strpath) assert isinstance(cache, virtwho.virt.esx.suds.cache.FileCache) assert cache.get("unga1") is None assert cache.get("unga2") is None cache.put("unga1", InvisibleMan(1)) cache.put("unga2", InvisibleMan(2)) read1 = cache.get("unga1") read2 = cache.get("unga2") assert read1.__class__ is InvisibleMan assert read2.__class__ is InvisibleMan assert read1.x == 1 assert read2.x == 2
def open(self, url): """ Open a WSDL schema at the specified I{URL}. First, the WSDL schema is looked up in the I{object cache}. If not found, a new one constructed using the I{fn} factory function and the result is cached for the next open(). @param url: A WSDL URL. @type url: str. @return: The WSDL object. @rtype: I{Definitions} """ cache = self.__cache() id = self.mangle(url, "wsdl") wsdl = cache.get(id) if wsdl is None: wsdl = self.fn(url, self.options) cache.put(id, wsdl) else: # Cached WSDL Definitions objects may have been created with # different options so we update them here with our current ones. wsdl.options = self.options for imp in wsdl.imports: imp.imported.options = self.options return wsdl
def test_file_operation_failure(self, tmpdir, monkeypatch, mock, extra_checks): """ File operation failures such as reading failures or failing to parse data read from such a file should cause no cached object to be found and the related cache file to be removed. """ cache_folder = tmpdir.strpath cache = virtwho.virt.esx.suds.cache.DocumentCache(cache_folder) content1, document1 = self.construct_XML("Eins") content2, document2 = self.construct_XML("Zwei") cache.put("unga1", document1) mock.apply(monkeypatch) assert cache.get("unga1") is None monkeypatch.undo() assert mock.counter == 1 assert extra_checks[0](mock) _assert_empty_cache_folder(cache_folder) mock.reset() assert cache.get("unga1") is None cache.put("unga1", document1) cache.put("unga2", document2) assert mock.counter == 0 assert extra_checks[1](mock) mock.reset() mock.apply(monkeypatch) assert cache.get("unga1") is None monkeypatch.undo() assert mock.counter == 1 assert extra_checks[2](mock) _assert_empty_cache_folder(cache_folder, expected=False) mock.reset() assert cache.get("unga1") is None self.compare_document_to_content(cache.get("unga2"), content2) assert mock.counter == 0 assert extra_checks[3](mock)
def test_NoCache(monkeypatch): cache = virtwho.virt.esx.suds.cache.NoCache() assert isinstance(cache, virtwho.virt.esx.suds.cache.Cache) assert cache.get("id") == None cache.put("id", "something") assert cache.get("id") == None #TODO: It should not be an error to call clear() or purge() on a NoCache # instance. monkeypatch.delitem(locals(), "e", False) e = pytest.raises(Exception, cache.purge, "id").value try: assert str(e) == "not-implemented" finally: del e # explicitly break circular reference chain in Python 3 e = pytest.raises(Exception, cache.clear).value try: assert str(e) == "not-implemented" finally: del e # explicitly break circular reference chain in Python 3
def test_version(self, tmpdir): fake_version_info = "--- fake version info ---" assert virtwho.virt.esx.suds.__version__ != fake_version_info version_file = tmpdir.join("version") cache_folder = tmpdir.strpath cache = virtwho.virt.esx.suds.cache.FileCache(cache_folder) assert version_file.read() == virtwho.virt.esx.suds.__version__ cache.put("unga1", value_p1) version_file.write(fake_version_info) assert cache.get("unga1") == value_p1 cache2 = virtwho.virt.esx.suds.cache.FileCache(cache_folder) _assert_empty_cache_folder(cache_folder) assert cache.get("unga1") is None assert cache2.get("unga1") is None assert version_file.read() == virtwho.virt.esx.suds.__version__ cache.put("unga1", value_p11) cache.put("unga2", value_p22) version_file.remove() assert cache.get("unga1") == value_p11 assert cache.get("unga2") == value_p22 cache3 = virtwho.virt.esx.suds.cache.FileCache(cache_folder) _assert_empty_cache_folder(cache_folder) assert cache.get("unga1") is None assert cache.get("unga2") is None assert cache2.get("unga1") is None assert cache3.get("unga1") is None assert version_file.read() == virtwho.virt.esx.suds.__version__
def test_file_operation_failure(self, tmpdir, monkeypatch, mock, extra_checks): """ Open file operation failures such as reading failures or failing to unpickle the data read from such a file should cause no cached object to be found and the related cache file to be removed. """ cache_folder = tmpdir.strpath cache = virtwho.virt.esx.suds.cache.ObjectCache(cache_folder) cache.put("unga1", InvisibleMan(1)) mock.apply(monkeypatch) assert cache.get("unga1") is None monkeypatch.undo() assert mock.counter == 1 assert extra_checks[0](mock) _assert_empty_cache_folder(cache_folder) mock.reset() assert cache.get("unga1") is None cache.put("unga1", InvisibleMan(1)) cache.put("unga2", InvisibleMan(2)) assert mock.counter == 0 assert extra_checks[1](mock) mock.reset() mock.apply(monkeypatch) assert cache.get("unga1") is None monkeypatch.undo() assert mock.counter == 1 assert extra_checks[2](mock) _assert_empty_cache_folder(cache_folder, expected=False) mock.reset() assert cache.get("unga1") is None assert cache.get("unga2").x == 2 assert mock.counter == 0 assert extra_checks[3](mock)
def test_repeated_reads(self, tmpdir): cache = virtwho.virt.esx.suds.cache.DocumentCache(tmpdir.strpath) content, document = self.construct_XML() cache.put("unga1", document) read_XML = cache.get("unga1").str() assert read_XML == cache.get("unga1").str() assert cache.get(None) is None assert cache.get("") is None assert cache.get("unga2") is None assert read_XML == cache.get("unga1").str()
def test_file_open_failure(self, tmpdir, monkeypatch): """ File open failure should cause no cached object to be found, but any existing underlying cache file should be kept around. """ mock_open = MockFileOpener(fail_open=True) cache_folder = tmpdir.strpath cache = virtwho.virt.esx.suds.cache.DocumentCache(cache_folder) content1, document1 = self.construct_XML("One") content2, document2 = self.construct_XML("Two") assert content1 != content2 cache.put("unga1", document1) mock_open.apply(monkeypatch) assert cache.get("unga1") is None monkeypatch.undo() assert mock_open.counter == 1 _assert_empty_cache_folder(cache_folder, expected=False) self.compare_document_to_content(cache.get("unga1"), content1) mock_open.apply(monkeypatch) assert cache.get("unga2") is None monkeypatch.undo() assert mock_open.counter == 2 _assert_empty_cache_folder(cache_folder, expected=False) self.compare_document_to_content(cache.get("unga1"), content1) assert cache.get("unga2") is None cache.put("unga2", document2) assert mock_open.counter == 2 mock_open.apply(monkeypatch) assert cache.get("unga1") is None monkeypatch.undo() assert mock_open.counter == 3 _assert_empty_cache_folder(cache_folder, expected=False) self.compare_document_to_content(cache.get("unga1"), content1) self.compare_document_to_content(cache.get("unga2"), content2) assert mock_open.counter == 3
def test_file_open_failure(self, tmpdir, monkeypatch): """ File open failure should cause no cached object to be found, but any existing underlying cache file should be kept around. """ mock_open = MockFileOpener(fail_open=True) cache_folder = tmpdir.strpath cache = virtwho.virt.esx.suds.cache.ObjectCache(cache_folder) cache.put("unga1", InvisibleMan(1)) mock_open.apply(monkeypatch) assert cache.get("unga1") is None monkeypatch.undo() assert mock_open.counter == 1 _assert_empty_cache_folder(cache_folder, expected=False) assert cache.get("unga1").x == 1 mock_open.apply(monkeypatch) assert cache.get("unga2") is None monkeypatch.undo() assert mock_open.counter == 2 _assert_empty_cache_folder(cache_folder, expected=False) assert cache.get("unga1").x == 1 assert cache.get("unga2") is None cache.put("unga2", InvisibleMan(2)) assert mock_open.counter == 2 mock_open.apply(monkeypatch) assert cache.get("unga1") is None monkeypatch.undo() assert mock_open.counter == 3 _assert_empty_cache_folder(cache_folder, expected=False) assert cache.get("unga1").x == 1 assert cache.get("unga2").x == 2 assert mock_open.counter == 3
def open(self, url): """ Open an XML document at the specified I{URL}. First, a preparsed document is looked up in the I{object cache}. If not found, its content is fetched from an external source and parsed using the SAX parser. The result is cached for the next open(). @param url: A document URL. @type url: str. @return: The specified XML document. @rtype: I{Document} """ cache = self.__cache() id = self.mangle(url, "document") xml = cache.get(id) if xml is None: xml = self.__fetch(url) cache.put(id, xml) self.plugins.document.parsed(url=url, document=xml.root()) return xml
def test_cached_content_unicode(self, tmpdir): cache_folder = tmpdir.strpath cache = virtwho.virt.esx.suds.cache.FileCache(cache_folder) cache.put("unga1", value_unicode) assert cache.get("unga1") == value_unicode _assert_empty_cache_folder(cache_folder, expected=False)