def test_software(self): for agent_foobar in self.models(limit=1): db.session.add(agent_foobar) # create some software version tags software_version_objects = [] for software_name in ("foo", "bar", "baz"): software = Software() software.agents = [agent_foobar] software.software = software_name software_version = SoftwareVersion() software_version.software = software software_version.version = "1" software_version.rank = 1 software_version_objects.append((software.software, software_version.version)) agent_foobar.software_versions.append(software_version) db.session.commit() agent_id = agent_foobar.id db.session.remove() agent = Agent.query.filter_by(id=agent_id).first() self.assertIsNotNone(agent) agent_software_versions = list( (str(i.software.software), str(i.version)) for i in agent.software_versions) software_version_objects.sort() agent_software_versions.sort() self.assertListEqual(agent_software_versions, software_version_objects)
def test_software(self): for agent_foobar in self.models(limit=1): db.session.add(agent_foobar) # create some software version tags software_version_objects = [] for software_name in ("foo", "bar", "baz"): software = Software() software.agents = [agent_foobar] software.software = software_name software_version = SoftwareVersion() software_version.software = software software_version.version = "1" software_version.rank = 1 software_version_objects.append( (software.software, software_version.version)) agent_foobar.software_versions.append(software_version) db.session.commit() agent_id = agent_foobar.id db.session.remove() agent = Agent.query.filter_by(id=agent_id).first() self.assertIsNotNone(agent) agent_software_versions = list( (str(i.software.software), str(i.version)) for i in agent.software_versions) software_version_objects.sort() agent_software_versions.sort() self.assertListEqual(agent_software_versions, software_version_objects)
def test_insert(self): # A job can not be created without a jobtype, create one first jobtype = JobType() jobtype.name = "foo" jobtype.description = "this is a job type" jobtype.classname = "Foobar" jobtype.code = dedent(""" class Foobar(JobType): pass""").encode("utf-8") jobtype.mode = JobTypeLoadMode.OPEN db.session.add(jobtype) job = Job() job.job_type = jobtype software = Software() software.jobs = [job] software.software = "foo" db.session.add_all([job, software]) db.session.commit() job_id = job.id software_id = software.id db.session.remove() software = Software.query.filter_by(id=software_id).first() self.assertEqual(software.jobs[0].id, job_id) self.assertEqual(software.software, "foo") self.assertEqual(software.version, "any")
def test_unique(self): # A job can not be created without a jobtype, create one first jobtype = JobType() jobtype.name = "foo" jobtype.description = "this is a job type" jobtype.classname = "Foobar" jobtype.code = dedent(""" class Foobar(JobType): pass""").encode("utf-8") db.session.add(jobtype) job = Job() job.job_type = jobtype software = Software() software.software = "foo" requirementA = JobSoftwareRequirement() requirementB = JobSoftwareRequirement() requirementA.job = job requirementA.software = software requirementB.job = job requirementB.software = software db.session.add_all([job, requirementA, requirementB]) with self.assertRaises(DatabaseError): db.session.commit()
def test_null(self): with self.assertRaises(DatabaseError): model = Software() db.session.add(model) db.session.commit() db.session.remove() with self.assertRaises(DatabaseError): tag = Software() tag.software = "foo" db.session.add(model) db.session.commit()
def test_software_unique(self): for agent_foobar in self.models(limit=1): softwareA = Software() softwareA.agent = [agent_foobar] softwareA.software = "foo" softwareA.version = "1.0.0" softwareB = Software() softwareB.agent = [agent_foobar] softwareB.software = "foo" softwareB.version = "1.0.0" db.session.add_all([softwareA, softwareB]) with self.assertRaises(DatabaseError): db.session.commit() db.session.rollback()
def test_null(self): with self.assertRaises(DatabaseError): model = JobSoftwareRequirement() db.session.add(model) db.session.commit() db.session.remove() with self.assertRaises(DatabaseError): software = Software() software.software = "foo" requirement = JobSoftwareRequirement() requirement.software = software db.session.add(requirement) db.session.commit()
def schema(): """ Returns the basic schema of :class:`.Software` .. http:get:: /api/v1/software/schema HTTP/1.1 **Request** .. sourcecode:: http GET /api/v1/software/schema HTTP/1.1 Accept: application/json **Response** .. sourcecode:: http HTTP/1.1 200 OK Content-Type: application/json { "id": "INTEGER", "software": "VARCHAR(64)" } :statuscode 200: no error """ return jsonify(Software.to_schema())
def test_unique(self): job = Job() softwareA = Software() softwareB = Software() softwareA.jobs = [job] softwareA.software = "foo" softwareB.jobs = [job] softwareB.software = "foo" db.session.add_all([job, softwareA, softwareB]) with self.assertRaises(DatabaseError): db.session.commit()
def add_software(): software = Software(software=request.form["software"]) db.session.add(software) db.session.commit() flash("Software %s created" % software.software) return redirect(url_for("single_software_ui", software_id=software.id), SEE_OTHER)
def test_insert(self): # A job can not be created without a jobtype, create one first jobtype = JobType() jobtype.name = "foo" jobtype.description = "this is a job type" jobtype_version = JobTypeVersion() jobtype_version.jobtype = jobtype jobtype_version.version = 1 jobtype_version.classname = "Foobar" jobtype_version.code = (""" class Foobar(JobType): pass""").encode("utf-8") db.session.add(jobtype_version) queue = JobQueue() queue.name = "FooQueue" job = Job() job.title = "Test Job" job.jobtype_version = jobtype_version job.queue = queue # Software requirement needs a software first software = Software() software.software = "foo" requirement = JobSoftwareRequirement() requirement.job = job requirement.software = software db.session.add(job) db.session.commit() job_id = job.id requirement_id = requirement.id requirement2 = JobSoftwareRequirement.query.\ filter_by(id=requirement_id).first() self.assertEqual(requirement.job.id, job_id) self.assertEqual(requirement2.software.software, "foo") self.assertEqual(requirement2.min_version, None) self.assertEqual(requirement2.max_version, None)
def test_software_schema(self): response = self.client.get("/api/v1/software/schema") self.assert_ok(response) self.assertEqual(response.json, Software.to_schema())
def put(self, software_rq): """ A ``PUT`` to this endpoint will create a new software tag under the given URI or update an existing software tag if one exists. Renaming existing software tags via this call is supported, but when creating new ones, the included software name must be equal to the one in the URI. You should only call this by id for overwriting an existing software tag or if you have a reserved software id. There is currently no way to reserve a tag id. .. http:put:: /api/v1/software/<str:softwarename> HTTP/1.1 **Request** .. sourcecode:: http PUT /api/v1/software/blender HTTP/1.1 Accept: application/json { "software": "blender" } **Response** .. sourcecode:: http HTTP/1.1 201 CREATED Content-Type: application/json { "id": 4, "software": "blender", "versions": [] } **Request** .. sourcecode:: http PUT /api/v1/software/blender HTTP/1.1 Accept: application/json { "software": "blender", "version": [ { "version": "1.69" } ] } **Response** .. sourcecode:: http HTTP/1.1 201 CREATED Content-Type: application/json { "id": 4, "software": "blender", "versions": [ { "version": "1.69", "id": 1, "rank": 100 } ] } :statuscode 200: an existing software tag was updated :statuscode 201: a new software tag was created :statuscode 400: there was something wrong with the request (such as invalid columns being included) """ if isinstance(software_rq, STRING_TYPES): if g.json["software"] != software_rq: return jsonify(error="""The name of the software must be equal to the one in the URI."""), BAD_REQUEST software = Software.query.filter_by(software=software_rq).first() else: software = Software.query.filter_by(id=software_rq).first() new = False if software else True if not software: software = Software() # This is only checked when creating new software. Otherwise, # renaming is allowed if g.json["software"] != software_rq: return jsonify(error="""The name of the software must be equal to the one in the URI."""), BAD_REQUEST # If this endpoint specified by id, make sure to create the new # software under this same id, too if isinstance(software_rq, int): software.id = software_rq software.software = g.json["software"] if "versions" in g.json: software.versions = [] db.session.flush() versions = extract_version_dicts(g.json) current_rank = 100 for version_dict in versions: version_dict.setdefault("rank", current_rank) version = SoftwareVersion(**version_dict) version.software = software current_rank = max(version.rank, current_rank) + 100 db.session.add(software) try: db.session.commit() except DatabaseError: return jsonify(error="Database error"), INTERNAL_SERVER_ERROR software_data = software.to_dict() logger.info("created software %s: %r", software.id, software_data) return jsonify(software_data), CREATED if new else OK
def post(self): """ A ``POST`` to this endpoint will create a new software tag. A list of versions can be included. If the software item already exists the listed versions will be added to the existing ones. Versions with no explicit rank are assumed to be the newest version available. Users should not mix versions with an explicit rank with versions without one. .. http:post:: /api/v1/software/ HTTP/1.1 **Request** .. sourcecode:: http POST /api/v1/software/ HTTP/1.1 Accept: application/json { "software": "blender" } **Response (new software item create)** .. sourcecode:: http HTTP/1.1 201 CREATED Content-Type: application/json { "id": 4, "software": "blender", "versions": [] } :statuscode 201: a new software item was created :statuscode 400: there was something wrong with the request (such as invalid columns being included) :statuscode 409: a software tag with that name already exists """ # Collect versions to add to the software object # Note: This can probably be done a lot simpler with generic parsing # of relations try: versions = extract_version_dicts(g.json) except VersionParseError as e: return jsonify(error=e.args[0]), BAD_REQUEST software = Software.query.filter_by(software=g.json["software"]).first() if software: return (jsonify(error="Software %s already exixts" % g.json["software"]), CONFLICT) software = Software(**g.json) current_rank = 100 for version_dict in versions: version_dict.setdefault("rank", current_rank) version = SoftwareVersion(**version_dict) version.software = software current_rank = max(version.rank, current_rank) + 100 db.session.add(software) try: db.session.commit() except DatabaseError: return jsonify(error="Database error"), INTERNAL_SERVER_ERROR software_data = software.to_dict() logger.info("created software %s: %r", software.id, software_data) return jsonify(software_data), CREATED