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_DocumentCache(tmpdir): cacheFolder = tmpdir.join("puffy").strpath cache = suds.cache.DocumentCache(cacheFolder) assert isinstance(cache, suds.cache.FileCache) assert cache.get("unga1") is None # TODO: DocumentCache class interface seems silly. Its get() operation # returns an XML document while its put() operation takes an XML element. # The put() operation also silently ignores passed data of incorrect type. # TODO: Update this test to no longer depend on the exact input XML data # formatting. We currently expect it to be formatted exactly as what gets # read back from the DocumentCache. content = suds.byte_str("""\ <xsd:element name="Elemento"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:enumeration value="alfa"/> <xsd:enumeration value="beta"/> <xsd:enumeration value="gamma"/> </xsd:restriction> </xsd:simpleType> </xsd:element>""") xml = suds.sax.parser.Parser().parse(suds.BytesIO(content)) cache.put("unga1", xml.getChildren()[0]) readXML = cache.get("unga1") assert isinstance(readXML, suds.sax.document.Document) readXMLElements = readXML.getChildren() assert len(readXMLElements) == 1 readXMLElement = readXMLElements[0] assert isinstance(readXMLElement, suds.sax.element.Element) assert suds.byte_str(str(readXMLElement)) == content
def test_item_expiration(self, tmpdir, monkeypatch, duration, current_time, expect_remove): """See TestFileCache.item_expiration_test_worker() for more info.""" cache = suds.cache.ObjectCache(tmpdir.strpath, **duration) cache.put("silly", InvisibleMan(666)) TestFileCache.item_expiration_test_worker(cache, "silly", monkeypatch, current_time, expect_remove)
def test_item_expiration(self, tmpdir, monkeypatch, duration, current_time, expect_remove): """See TestFileCache.item_expiration_test_worker() for more info.""" cache = suds.cache.FileCache(tmpdir.strpath, **duration) cache.put("unga1", value_p1) TestFileCache.item_expiration_test_worker(cache, "unga1", monkeypatch, current_time, expect_remove)
def test_basic(self, tmpdir): cache = suds.cache.DocumentCache(tmpdir.strpath) assert isinstance(cache, suds.cache.FileCache) assert cache.get("unga1") is None content, element = self.construct_XML() cache.put("unga1", element) self.compare_document_to_content(cache.get("unga1"), content)
def test_item_expiration(self, tmpdir, monkeypatch, duration, current_time, expect_remove): """See TestFileCache.item_expiration_test_worker() for more info.""" cache = suds.cache.DocumentCache(tmpdir.strpath, **duration) content, document = self.construct_XML() cache.put("willy", document) TestFileCache.item_expiration_test_worker(cache, "willy", monkeypatch, current_time, expect_remove)
def test_cache_element(self, tmpdir): cache_item_id = "unga1" cache = suds.cache.DocumentCache(tmpdir.strpath) assert isinstance(cache, 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_custom_cache_location(self): cache_folder = os.path.join( tempfile.gettempdir(), "custom-cache-loc-test") cache = suds.cache.FileCache(cache_folder) assert isinstance(cache, suds.cache.Cache) value = "some value".encode() cache.put("test_key", value) assert cache.get("test_key") == value assert not _is_empty_cache_folder(cache_folder)
def test_repeated_reads(self, tmpdir): cache = 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_basic(self): cache_folder = os.path.join( tempfile.gettempdir(), "custom-cache-loc-document-test") cache = suds.cache.DocumentCache(cache_folder) assert isinstance(cache, suds.cache.FileCache) doc = suds.sax.document.Document() doc.append(suds.sax.element.Element("TestEle1")) cache.put("key1", doc) read1 = cache.get("key1") assert read1.__class__ is suds.sax.document.Document
def test_FileCache_with_empty_cached_content(tmpdir): cacheFolder = tmpdir.strpath cache = suds.cache.FileCache(cacheFolder) cache.put("unga1", value_empty) assert cache.get("unga1") == value_empty assert not _isEmptyCacheFolder(cacheFolder)
def test_FileCache_with_random_utf_character_cached_content(tmpdir): cacheFolder = tmpdir.strpath cache = suds.cache.FileCache(cacheFolder) cache.put("unga1", value_unicode) assert cache.get("unga1") == value_unicode assert not _isEmptyCacheFolder(cacheFolder)
def test_NoCache(): cache = suds.cache.NoCache() assert isinstance(cache, 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 purge() or clear() on a NoCache # instance. pytest.raises(Exception, cache.purge, "id") pytest.raises(Exception, cache.clear)
def test_basic(self): cache_folder = os.path.join( tempfile.gettempdir(), "custom-cache-loc-object-test") cache = suds.cache.ObjectCache(cache_folder) assert isinstance(cache, suds.cache.FileCache) cache.put("key1", DummyObjectForTesting(1)) cache.put("key2", DummyObjectForTesting(2)) read1 = cache.get("key1") read2 = cache.get("key2") assert read1.__class__ is DummyObjectForTesting assert read2.__class__ is DummyObjectForTesting assert read1.x == 1 assert read2.x == 2
def test_basic(self, tmpdir): cache = suds.cache.ObjectCache(tmpdir.strpath) assert isinstance(cache, 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 test_ObjectCache(tmpdir): cacheFolder = tmpdir.join("george carlin").strpath cache = suds.cache.ObjectCache(cacheFolder) assert isinstance(cache, 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 test_NoCache(monkeypatch): cache = suds.cache.NoCache() assert isinstance(cache, 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 assert str(e) == "not-implemented" e = pytest.raises(Exception, cache.clear).value assert str(e) == "not-implemented"
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 = 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_NoCache(monkeypatch): cache = suds.cache.NoCache() assert isinstance(cache, 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_FileCache_version(tmpdir): fakeVersionInfo = "--- fake version info ---" assert suds.__version__ != fakeVersionInfo cacheFolder = tmpdir.join("hitori") versionFile = cacheFolder.join("version") cache = suds.cache.FileCache(cacheFolder.strpath) assert versionFile.read() == suds.__version__ cache.put("unga1", value_p1) versionFile.write(fakeVersionInfo) assert cache.get("unga1") == value_p1 cache2 = suds.cache.FileCache(cacheFolder.strpath) assert _isEmptyCacheFolder(cacheFolder.strpath) assert cache.get("unga1") is None assert cache2.get("unga1") is None assert versionFile.read() == suds.__version__ cache.put("unga1", value_p11) cache.put("unga2", value_p22) versionFile.remove() assert cache.get("unga1") == value_p11 assert cache.get("unga2") == value_p22 cache3 = suds.cache.FileCache(cacheFolder.strpath) assert _isEmptyCacheFolder(cacheFolder.strpath) assert cache.get("unga1") is None assert cache.get("unga2") is None assert cache2.get("unga1") is None assert versionFile.read() == suds.__version__
def test_version(self, tmpdir): fake_version_info = "--- fake version info ---" assert suds.__version__ != fake_version_info version_file = tmpdir.join("version") cache_folder = tmpdir.strpath cache = suds.cache.FileCache(cache_folder) assert version_file.read() == suds.__version__ cache.put("unga1", value_p1) version_file.write(fake_version_info) assert cache.get("unga1") == value_p1 cache2 = 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() == 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 = 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() == suds.__version__
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 = 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_independent_item_expirations(self, tmpdir, monkeypatch): cache = suds.cache.FileCache(tmpdir.strpath, days=1) cache.put("unga1", value_p1) cache.put("unga2", value_p2) cache.put("unga3", value_f2) filepath1 = cache._FileCache__filename("unga1") filepath2 = cache._FileCache__filename("unga2") filepath3 = cache._FileCache__filename("unga3") file_timestamp1 = os.path.getctime(filepath1) file_timestamp2 = file_timestamp1 + 10 * 60 # in seconds file_timestamp3 = file_timestamp1 + 20 * 60 # in seconds file_time1 = datetime.datetime.fromtimestamp(file_timestamp1) file_time1_expiration = file_time1 + cache.duration original_getctime = os.path.getctime def mock_getctime(path): if path == filepath2: return file_timestamp2 if path == filepath3: return file_timestamp3 return original_getctime(path) timedelta = datetime.timedelta monkeypatch.setattr(os.path, "getctime", mock_getctime) monkeypatch.setattr(datetime, "datetime", MockDateTime) MockDateTime.mock_value = file_time1_expiration + timedelta(minutes=15) assert cache._getf("unga2") is None assert os.path.isfile(filepath1) assert not os.path.isfile(filepath2) assert os.path.isfile(filepath3) cache._getf("unga3").close() assert os.path.isfile(filepath1) assert not os.path.isfile(filepath2) assert os.path.isfile(filepath3) MockDateTime.mock_value = file_time1_expiration + timedelta(minutes=25) assert cache._getf("unga1") is None assert not os.path.isfile(filepath1) assert not os.path.isfile(filepath2) assert os.path.isfile(filepath3) assert cache._getf("unga3") is None assert not os.path.isfile(filepath1) assert not os.path.isfile(filepath2) assert not os.path.isfile(filepath3)
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 = 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_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 = 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_cached_content_empty(self, tmpdir): cache_folder = tmpdir.strpath cache = suds.cache.FileCache(cache_folder) cache.put("unga1", value_empty) assert cache.get("unga1") == value_empty assert not _is_empty_cache_folder(cache_folder)
def test_cached_content_unicode(self, tmpdir): cache_folder = tmpdir.strpath cache = suds.cache.FileCache(cache_folder) cache.put("unga1", value_unicode) assert cache.get("unga1") == value_unicode _assert_empty_cache_folder(cache_folder, expected=False)