def test_fetch_advanced(self): def request_callback(request): body = json.loads(request.body) self.assertEqual(body["startRow"], 0) self.assertEqual(body["endRow"], 1) self.assertEqual(body["sortBy"], ["cntn_createdOn"]) self.assertEqual(body["criteria"]["operator"], "equals") self.assertEqual(body["criteria"]["fieldName"], "cntn_id") self.assertEqual(body["criteria"]["value"], "test") return (200, {}, json.dumps({"entities": []})) responses.add_callback( responses.GET, 'http://localhost:9999/rest/Content/advanced', callback=request_callback, content_type='application/json', ) slims = Slims("testSlims", "http://localhost:9999", "admin", "admin") entities = slims.fetch("Content", equals("cntn_id", "test"), sort=["cntn_createdOn"], start=0, end=1) self.assertEquals(entities, [])
def test_fetch_by_pk(self): responses.add( responses.GET, 'http://localhost:9999/rest/Content/1', json={ "entities": [{ "pk": 1, "tableName": "Content", "columns": [ { "datatype": "STRING", "name": "cntn_id", "title": "Id", "position": 2, "value": "sample1", "hidden": False, "editable": False }, ] }] }, content_type='application/json', ) slims = Slims("testSlims", "http://localhost:9999", "admin", "admin") entity = slims.fetch_by_pk("Content", 1) self.assertEqual(entity.cntn_id.value, "sample1")
def test_fetch_outgoing_link(self): responses.add( responses.GET, 'http://localhost:9999/rest/Content/1', json={"entities": [{ "pk": 1, "tableName": "Content", "columns": [], "links": [ { "rel": "-rslt_fk_content", "href": "http://localhost:9999/rest/Result?rslt_fk_content=1" }, ]}]}, content_type='application/json', ) responses.add( responses.GET, # responses library filters out GET parameters 'http://localhost:9999/rest/Result', json={"entities": [{ "pk": 2, "tableName": "Result", "columns": []}]}, content_type='application/json', ) slims = Slims("testSlims", "http://localhost:9999", "admin", "admin") entity = slims.fetch_by_pk("Content", 1) self.assertIsInstance(entity.follow("-rslt_fk_content")[0], Record)
def test_fetch_by_pk_nothing_returned(self): responses.add( responses.GET, 'http://localhost:9999/rest/Content/1', json={"entities": []}, content_type='application/json', ) slims = Slims("testSlims", "http://localhost:9999", "admin", "admin") entity = slims.fetch_by_pk("Content", 1) self.assertEqual(entity, None)
def test_adding_flow(self): def execute_first_step(data): pass def add_flow_callback(request): body = json.loads(request.body) self.assertDictEqual( body, {'instance': {'url': 'http://localhost:5000', 'name': 'testSlims'}, 'flow': {'id': 'myFlow', 'name': 'My flow in python', 'usage': 'CONTENT_MANAGEMENT', 'steps': [ {'hidden': False, 'name': 'first step', 'input': {'parameters': [{'type': 'STRING', 'name': 'text', 'label': 'Text'}]}, 'process': {'route': 'myFlow/0', 'asynchronous': False}, 'output': {'parameters': [{'type': 'FILE', 'name': 'file'}]} } ], 'pythonApiFlow': True } }) return (200, {}, json.dumps({})) responses.add_callback( responses.POST, 'http://localhost:9999/rest/external/', callback=add_flow_callback, content_type='application/json', ) slims = Slims("testSlims", "http://localhost:9999", "admin", "admin") slims.add_flow( flow_id="myFlow", name="My flow in python", usage="CONTENT_MANAGEMENT", steps=[ Step( name="first step", action=execute_first_step, input=[ text_input("text", "Text") ], output=[ file_output() ] ) ], testing=True)
def test_fetch_incoming_link(self): responses.add( responses.GET, 'http://localhost:9999/rest/Content/1', json={ "entities": [{ "pk": 1, "tableName": "Content", "columns": [], "links": [ { "rel": "cntn_fk_contentType", "href": "http://localhost:9999/rest/ContentType/2" }, { "rel": "cntn_fk_location", "href": "http://localhost:9999/rest/Location/3" }, ] }] }, content_type='application/json', ) responses.add( responses.GET, 'http://localhost:9999/rest/ContentType/2', json={ "entities": [{ "pk": 2, "tableName": "ContentType", "columns": [] }] }, content_type='application/json', ) responses.add( responses.GET, 'http://localhost:9999/rest/Location/3', json={"entities": []}, content_type='application/json', ) slims = Slims("testSlims", "http://localhost:9999", "admin", "admin") entity = slims.fetch_by_pk("Content", 1) self.assertIsInstance(entity.follow("cntn_fk_contentType"), Record) self.assertEqual(entity.follow("cntn_fk_location"), None)
def test_add(self): slims = Slims("testSlims", "http://localhost:9999", "admin", "admin") responses.add( responses.PUT, 'http://localhost:9999/rest/Content', json={"entities": [{ "pk": 1, "tableName": "Content", "columns": [] }]}, content_type='application/json', ) added = slims.add("Content", {"test": "foo"}) self.assertIsInstance(added, Record)
def test_fetch_unknown_link(self): responses.add( responses.GET, 'http://localhost:9999/rest/Content/1', json={"entities": [{ "pk": 1, "tableName": "Content", "columns": [], "links": []}]}, content_type='application/json', ) slims = Slims("testSlims", "http://localhost:9999", "admin", "admin") entity = slims.fetch_by_pk("Content", 1) self.assertRaises(KeyError, entity.follow, "unknown")
def main(): print("main: hello from python\n", file=sys.stdout, flush=True) slims = Slims("bigtest", "http://127.0.0.1:5703") print("main: bye from python\n", file=sys.stdout, flush=True)
def test_remove_success(self): responses.add( responses.DELETE, 'http://localhost:9999/rest/Content/1', content_type='application/json', ) slims = Slims("testSlims", "http://localhost:9999", "admin", "admin") record = Record({"pk": 1, "tableName": "Content", "columns": []}, slims.slims_api) record.remove()
def test_remove_failure(self): responses.add( responses.DELETE, 'http://localhost:9999/rest/Content/1', content_type='application/json', body="Could not delete", status=400 ) slims = Slims("testSlims", "http://localhost:9999", "admin", "admin") record = Record({"pk": 1, "tableName": "Content", "columns": []}, slims.slims_api) self.assertRaises(Exception, record.remove)
def test_fetch_attachments(self): responses.add( responses.GET, 'http://localhost:9999/rest/attachment/Content/1', json={"entities": [{ "pk": 1, "tableName": "Attachment", "columns": []}]}, content_type='application/json', ) slims = Slims("testSlims", "http://localhost:9999", "admin", "admin") record = Record({"pk": 1, "tableName": "Content", "columns": []}, slims.slims_api) attachments = record.attachments() self.assertIsInstance(attachments[0], Attachment)
def test_download_attachment(self): def repo_request_callback(request): return (200, {}, b"blabla") responses.add_callback( responses.GET, 'http://localhost:9999/rest/repo/1', callback=repo_request_callback, content_type='application/json', ) slims = Slims("testSlims", "http://localhost:9999", "admin", "admin", repo_location="/var/slims/repo") attachment = Attachment(self.attachmentValues, slims.slims_api) temp = tempfile.NamedTemporaryFile() try: attachment.download_to(temp.name) self.assertEqual(b"blabla", temp.read()) finally: temp.close()
def test_update(self): responses.add( responses.POST, 'http://localhost:9999/rest/Content/1', json={"entities": [{ "pk": 1, "tableName": "Content", "columns": [] }]}, content_type='application/json', ) slims = Slims("testSlims", "http://localhost:9999", "admin", "admin") record = Record({"pk": 1, "tableName": "Content", "columns": []}, slims.slims_api) updated = record.update({"test": "foo"}) self.assertIsInstance(updated, Record)
def test_add_attachment(self): def add_attachment_callback(request): body = json.loads(request.body) self.assertDictEqual( body, {"atln_recordPk": 1, "atln_recordTable": "Content", "attm_name": "test.txt", "contents": 'U29tZSB0ZXh0'}) return (200, {"Location": "http://localhost:9999/rest/Attachment/2"}, json.dumps({})) responses.add_callback( responses.POST, 'http://localhost:9999/rest/repo', callback=add_attachment_callback, content_type='application/json', ) slims = Slims("testSlims", "http://localhost:9999", "admin", "admin", repo_location="/var/slims/repo") content = Record(self.contentValues, slims.slims_api) self.assertEqual(2, content.add_attachment("test.txt", b"Some text"))
def test_update_status(self): def request_callback(request): body = json.loads(request.body) self.assertDictEqual( { 'flowRunGuid': 'guid', 'index': 0, 'status': 'FAILED' }, body) return (200, {}, json.dumps({})) responses.add_callback( responses.POST, 'http://localhost:9999/rest/external/status', callback=request_callback, content_type='application/json', ) slims = Slims("testSlims", "http://localhost:9999", "admin", "admin") flowrun = FlowRun(slims.slims_api, 0, {"flowInformation": { "flowRunGuid": "guid" }}) flowrun._update_status(Status.FAILED)
def test_attachment_path_without_repo_location_throws(self): slims = Slims("testSlims", "http://localhost:9999", "admin", "admin") attachment = Attachment(self.attachmentValues, slims.slims_api) self.assertRaises(RuntimeError, attachment.get_local_path)
def test_attachment_path_with_repo_location(self): slims = Slims("testSlims", "http://localhost:9999", "admin", "admin", repo_location="/var/slims/repo") attachment = Attachment(self.attachmentValues, slims.slims_api) self.assertEqual("/var/slims/repo/a/file.txt", attachment.get_local_path())
print("Do nothing") def execute(flow_run): content_to_modify = slims.fetch_by_pk("Content", flow_run.data["content_to_modify"]) content_to_modify.update({"cntn_id": flow_run.data["id"]}) # This environment variable needs to be set only if SLIMS REST is not running on HTTPS os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = 'True' slims = Slims( "slims", "http://127.0.0.1:9999", oauth=True, client_id="c5d4d038-c918-4fca-bfd4-121e415a433c", client_secret= "d83a61a9568940f337632873376cf5c47854617bcfdb3d49b9319ac6c82d65b1") # Whenever SLIMS is not run on the same server as python, local_host="yourIp" # parameter in Slims() method should be set. slims.add_flow( flow_id="updateContentId", name="Update content id", usage="CONTENT_MANAGEMENT", steps=[ Step( name="Select content to update", action=do_nothing, input=[
"length Test" and "weight test" (labels). A patient HAS TO HAVE one and only one result for each test. A BMI Calculation is done based on both. The plotting requires matplotlib (pip install matplotlib) run this script using the underneath command in the folder plotting python main.py """ from __future__ import division from slims.slims import Slims from slims.criteria import equals, is_one_of import matplotlib.pyplot as plt slims = Slims("slims", "http://localhost:9999", "admin", "admin") # Data fetch -> selection of patients and their results patients = slims.fetch("Content", equals("cntp_name", "Patient")) patient_pks = map(lambda patient: patient.pk(), patients) results = slims.fetch("Result", is_one_of("rslt_fk_content", patient_pks)) lengths = [] weights = [] BMI = [] x_axis_values = [] # Identification of the result per test print("Fetching necessary data...") for result in results:
from slims.slims import Slims from slims.step import Step, file_output from slims.output import file_value def execute(): # Make sure the path to the file exists return file_value('C:/Users/User/Downloads/file.txt') slims = Slims("slims", url="http://127.0.0.1:9999/", token="devToken", local_host="0.0.0.0", local_port=5000) slims.add_flow(flow_id="FLOW_0001", name="Download an file from server", usage="CONTENT_MANAGEMENT", steps=[ Step(name="This downloads a file.", action=execute, output=[file_output()]) ])
Launch the script from within the live-report folder with the command python main.py 7777 this way the report can be accessed on http://0.0.0.0:7777 (as shown on the terminal) """ import datetime import web from dateutil.relativedelta import relativedelta from web import form from slims.slims import Slims from slims.criteria import between_inclusive render = web.template.render('templates/') slims = Slims("slims", "http://slimstest.genohm.com/coming", "admin", "admin") # Creation of the register form which allows the user to choose a period of time period = [('days', 'one day'), ('weeks', 'one week'), ('months', 'one month'), ('years', 'one year')] register_form = form.Form( form.Radio("Period", period, description="Number of Time to Display"), form.Button("submit", type="submit", description="Register"), ) urls = ( '/', 'DisplayReport', )
def do_nothing(flow_run): print("Do nothing") def execute(flow_run): content_to_modify = slims.fetch_by_pk("Content", flow_run.data["content_to_modify"]) content_to_modify.update({"cntn_id": flow_run.data["id"]}) # This environment variable needs to be set only if SLIMS REST is not running on HTTPS os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = 'True' slims = Slims("slims", "http://127.0.0.1:9999", oauth=True) # Whenever SLIMS is not run on the same server as python, local_host="yourIp" # parameter in Slims() method should be set. slims.add_flow( flow_id="updateContentId", name="Update content id", usage="CONTENT_MANAGEMENT", steps=[ Step( name="Select content to update", action=do_nothing, input=[ single_choice_with_value_map_input( "content_to_modify", "Content to modify",
When creating a new record in SLims one needs to be careful about the mandatory fields to fill. For example creating a new content may require an ID or not depending on whereas the associate box in its related content type is checked or not. run this script using the underneath command in the folder containing it. python data_modification.py """ from __future__ import print_function from slims.slims import Slims from slims.criteria import equals from slims.content import Status import sys slims = Slims("slims", "http://localhost:9999", "admin", "admin") # Example N_1: Creating a content record; here a Content of content type DNA # in the location Building Test. Requires a location called # "Building test" (of location type Building) and a content type # "DNA" (with use barcode as id) print("Example 1") # Fetch DNA content type dna_type = slims.fetch("ContentType", equals("cntp_name", "DNA")) if not dna_type: sys.exit("No DNA Content type found, can not continue") # Fetch Building Test location (ideally with location type Building, # but this is not mandatory)
run this script using the underneath command in the folder containing it. python data_modification.py """ from slims.slims import Slims from slims.criteria import is_not_null from slims.criteria import equals from slims.criteria import contains from slims.criteria import starts_with from slims.criteria import ends_with from slims.criteria import between_inclusive_match_case from slims.criteria import conjunction from slims.criteria import disjunction from slims.criteria import is_not from slims.util import display_results slims = Slims("slims", "http://localhost:9999", "admin", "admin") # EXAMPLE OF OPERATOR COMBINATIONS # Example N_1: # We fetch Content records without criteria # This means that the fetch includes all the contents in the database # By giving start and end parameters the output is limited to the 10th to the # 30th record to be returned print("Example 1") records = slims.fetch("Content", None, start=10, end=30) # The fields cntn_id and cntn_barCode of the content records will be displayed display_results(records, ["cntn_id", "cntn_barCode"]) # Example N_2: # Fetching content records with the equals criteria;
Launch the script from within the order-submission folder with the command python main.py 7777 this way the report can be accessed on http://localhost:7777 """ from __future__ import print_function import web from web import form from slims.slims import Slims from slims.criteria import is_not from slims.criteria import equals from slims.content import Status render = web.template.render('templates/') slims = Slims("slims", "http://localhost:9999", "admin", "admin") # Populate the comboboxes # Searching for data by fetching order_types = slims.fetch("OrderType", None) content_types = slims.fetch("ContentType", equals("cntp_useBarcodeAsId", True)) locations = slims.fetch("Location", None) requestables = slims.fetch("Requestable", is_not(equals("rqbl_type", "WORKFLOW"))) dic_order_type = {} for order_type in order_types: dic_order_type.update({order_type.rdtp_name.value: order_type}) dic_content_type = {} for content_type in content_types: dic_content_type.update({content_type.cntp_name.value: content_type})