|
| 1 | +BATCH FETCH BY UID |
| 2 | +------------------ |
| 3 | + |
| 4 | +Running this test from the buildout directory: |
| 5 | + |
| 6 | + bin/test test_doctests -t batch_fetch |
| 7 | + |
| 8 | + |
| 9 | +Test Setup |
| 10 | +~~~~~~~~~~ |
| 11 | + |
| 12 | +Needed Imports: |
| 13 | + |
| 14 | + >>> import json |
| 15 | + >>> import transaction |
| 16 | + >>> from plone.app.testing import setRoles |
| 17 | + >>> from plone.app.testing import TEST_USER_ID |
| 18 | + >>> from bika.lims import api |
| 19 | + |
| 20 | +Functional Helpers: |
| 21 | + |
| 22 | + >>> def get(url): |
| 23 | + ... browser.open("{}/{}".format(api_url, url)) |
| 24 | + ... return browser.contents |
| 25 | + |
| 26 | + >>> def get_count(response): |
| 27 | + ... data = json.loads(response) |
| 28 | + ... return data.get("count") |
| 29 | + |
| 30 | + >>> def get_items_ids(response, sort=True): |
| 31 | + ... data = json.loads(response) |
| 32 | + ... items = data.get("items") |
| 33 | + ... items = map(lambda it: it["id"], items) |
| 34 | + ... if sort: |
| 35 | + ... return sorted(items) |
| 36 | + ... return list(items) |
| 37 | + |
| 38 | + >>> def get_items_uids(response): |
| 39 | + ... data = json.loads(response) |
| 40 | + ... items = data.get("items") |
| 41 | + ... return sorted(it["uid"] for it in items) |
| 42 | + |
| 43 | +Variables: |
| 44 | + |
| 45 | + >>> portal = self.portal |
| 46 | + >>> portal_url = portal.absolute_url() |
| 47 | + >>> api_url = "{}/@@API/senaite/v1".format(portal_url) |
| 48 | + >>> browser = self.getBrowser() |
| 49 | + >>> setRoles(portal, TEST_USER_ID, ["LabManager", "Manager"]) |
| 50 | + |
| 51 | +Create three Client objects and commit so they are indexed: |
| 52 | + |
| 53 | + >>> c1 = api.create(portal.clients, "Client", title="Alpha Lab", ClientID="AL") |
| 54 | + >>> c2 = api.create(portal.clients, "Client", title="Beta Lab", ClientID="BL") |
| 55 | + >>> c3 = api.create(portal.clients, "Client", title="Gamma Lab", ClientID="GL") |
| 56 | + >>> uid1 = api.get_uid(c1) |
| 57 | + >>> uid2 = api.get_uid(c2) |
| 58 | + >>> uid3 = api.get_uid(c3) |
| 59 | + >>> transaction.commit() |
| 60 | + |
| 61 | + |
| 62 | +Batch fetch two objects by UID |
| 63 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 64 | + |
| 65 | +Passing a comma-separated ``uids`` parameter returns only the matching |
| 66 | +objects in a single request: |
| 67 | + |
| 68 | + >>> response = get("client?uids={},{}".format(uid1, uid2)) |
| 69 | + >>> get_count(response) |
| 70 | + 2 |
| 71 | + >>> get_items_ids(response) |
| 72 | + [u'client-1', u'client-2'] |
| 73 | + |
| 74 | +The third client is not included because its UID was not requested: |
| 75 | + |
| 76 | + >>> "Gamma Lab" in response |
| 77 | + False |
| 78 | + |
| 79 | + |
| 80 | +Batch fetch all three objects |
| 81 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 82 | + |
| 83 | + >>> response = get("client?uids={},{},{}".format(uid1, uid2, uid3)) |
| 84 | + >>> get_count(response) |
| 85 | + 3 |
| 86 | + >>> get_items_ids(response) |
| 87 | + [u'client-1', u'client-2', u'client-3'] |
| 88 | + |
| 89 | + |
| 90 | +Batch fetch a single UID via the uids parameter |
| 91 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 92 | + |
| 93 | +A single-item list is valid and returns one object wrapped in the |
| 94 | +standard batched response (with ``items`` and ``count``): |
| 95 | + |
| 96 | + >>> response = get("client?uids={}".format(uid1)) |
| 97 | + >>> get_count(response) |
| 98 | + 1 |
| 99 | + >>> get_items_ids(response) |
| 100 | + [u'client-1'] |
| 101 | + |
| 102 | + >>> "items" in response |
| 103 | + True |
| 104 | + |
| 105 | + |
| 106 | +Returned UIDs match the requested UIDs |
| 107 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 108 | + |
| 109 | + >>> response = get("client?uids={},{}".format(uid2, uid3)) |
| 110 | + >>> returned = get_items_uids(response) |
| 111 | + >>> returned == sorted([uid2, uid3]) |
| 112 | + True |
| 113 | + |
| 114 | + |
| 115 | +Batch fetch works with complete=1 |
| 116 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 117 | + |
| 118 | +Adding ``complete=1`` wakes up the objects and returns full field data: |
| 119 | + |
| 120 | + >>> response = get("client?uids={},{}&complete=1".format(uid1, uid2)) |
| 121 | + >>> get_count(response) |
| 122 | + 2 |
| 123 | + >>> "Alpha Lab" in response |
| 124 | + True |
| 125 | + >>> "Beta Lab" in response |
| 126 | + True |
| 127 | + |
| 128 | + |
| 129 | +Batch fetch respects portal_type scoping |
| 130 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 131 | + |
| 132 | +When using a typed resource route (``/client``), the portal_type |
| 133 | +constraint is applied alongside the UID filter. UIDs belonging to a |
| 134 | +different portal type are not returned. |
| 135 | + |
| 136 | +Create a SampleType to obtain a non-Client UID: |
| 137 | + |
| 138 | + >>> st = api.create(portal.setup.sampletypes, "SampleType", title="Blood", Prefix="BL") |
| 139 | + >>> uid_st = api.get_uid(st) |
| 140 | + >>> transaction.commit() |
| 141 | + |
| 142 | +Fetching via ``/client?uids=`` with a SampleType UID returns no items: |
| 143 | + |
| 144 | + >>> response = get("client?uids={}".format(uid_st)) |
| 145 | + >>> get_count(response) |
| 146 | + 0 |
| 147 | + |
| 148 | + |
| 149 | +Batch fetch via the generic /search route |
| 150 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 151 | + |
| 152 | +The ``uids`` parameter also works on the generic ``/search`` endpoint |
| 153 | +when combined with ``portal_type``: |
| 154 | + |
| 155 | + >>> response = get( |
| 156 | + ... "search?portal_type=Client&uids={},{}".format(uid1, uid3) |
| 157 | + ... ) |
| 158 | + >>> get_count(response) |
| 159 | + 2 |
| 160 | + >>> get_items_ids(response) |
| 161 | + [u'client-1', u'client-3'] |
0 commit comments