def _test_delete(self): self.moxer.StubOutWithMock(ArtifactInfo, "get_by_guid", use_mock_anything=True) self.moxer.StubOutWithMock(ArtifactContent, "get_by_guid", use_mock_anything=True) guid = 'blah' ArtifactInfo.get_by_guid(guid).AndReturn(MockEntity(MockKey(name=guid))) ArtifactContent.get_by_guid(guid).AndReturn(MockEntity(MockKey(name=guid))) self.moxer.ReplayAll() ArtifactAccessor.delete(guid) self.moxer.VerifyAll()
def test_delete_nonexistent(self): self.moxer.StubOutWithMock(ArtifactInfo, "get_by_guid", use_mock_anything=True) self.moxer.StubOutWithMock(ArtifactContent, "get_by_guid", use_mock_anything=True) guid = 'blah' ArtifactInfo.get_by_guid(guid) ArtifactContent.get_by_guid(guid) self.moxer.ReplayAll() try: ArtifactAccessor.delete(guid) self.fail("exception expected") except NotFoundException, ex: pass
def get(self, **kw): helper = RequestHelper(self) q = self.request.get("q", None) output = self.request.get("o", None) max_results = int(self.request.get("max", -1)) if not q: helper.error(400, "q not provided.") return q_results = ArtifactContent.all().search(q) json_results = None if output == "short": json_results = {} json_results["count"] = q_results.count() elif output == "id": json_results = {} count = q_results.count() if max_results > 0 and max_results < q_results.count(): count = max_results json_results["count"] = count ids = [] json_results["ids"] = ids results = q_results.fetch(1000) if max_results == -1 else q_results.fetch(max_results) for c in results: ids.append(c.guid) else: json_results = [] if q_results.count(): for content in q_results.fetch(10): info = ArtifactInfo.get_by_guid(content.guid) json_results.append(ArtifactsHelper.artifact_to_hash(info, content)) helper.write_json(json_results)
def _test_delete(self): self.moxer.StubOutWithMock(ArtifactInfo, "get_by_guid", use_mock_anything=True) self.moxer.StubOutWithMock(ArtifactContent, "get_by_guid", use_mock_anything=True) guid = 'blah' ArtifactInfo.get_by_guid(guid).AndReturn(MockEntity( MockKey(name=guid))) ArtifactContent.get_by_guid(guid).AndReturn( MockEntity(MockKey(name=guid))) self.moxer.ReplayAll() ArtifactAccessor.delete(guid) self.moxer.VerifyAll()
def get(cls, rhandler, guid, **kw): helper = RequestHelper(rhandler) artifact_info = ArtifactInfo.get_by_guid(guid) artifact_content = ArtifactContent.get_by_guid(guid) if artifact_info and artifact_content: artifact_hash = ArtifactsHelper.artifact_to_hash(artifact_info, artifact_content) helper.write_json(artifact_hash) else: helper.error(404)
def put(cls, rhandler, guid, **kw): helper = RequestHelper(rhandler) artifact = ArtifactInfo.get_by_guid(guid) if not artifact: helper.error(404) return # removes existing properties props = ArtifactInfo.properties().keys() for prop in props: delattr(artifact, prop) # save artifact ArtifactInfo.save(artifact)
def delete(cls, guid): logger = LoggerFactory.logger(cls.__name__) a_info = ArtifactInfo.get_by_guid(guid) a_content_key = ArtifactContent.get_by_guid(guid) if not (a_info or a_content_key): # neither record found raise NotFoundException("artifact %s" % guid) elif not (a_info and a_content_key): # one record found; one missing logger.warn("artifact %s; missing data; info=%s; content=%s" % (guid, a_info.key().name(), a_content_key)) # I delete what I can keys = [] if a_info: keys.append(a_info) if a_content_key: keys.append(a_content_key) db.delete(keys) # decrease source counter Counters.source_counter(a_info.source.name).decrement()