11import os
22import unittest
3-
3+ import json
44import requests
55
66import sbol2 as sbol
@@ -213,6 +213,7 @@ def test_uri2url_spoofed(self):
213213
214214 @unittest .skipIf (password is None , "No password supplied" )
215215 def test_attach_file (self ):
216+ sbol2 .Config .setOption ('sbol_typed_uris' , False )
216217 doc = sbol2 .Document ()
217218 doc .displayId = 'test_attachment'
218219 doc .name = 'test attachment'
@@ -222,11 +223,44 @@ def test_attach_file(self):
222223 sbh = sbol2 .PartShop (TEST_RESOURCE )
223224 sbh .login (username , password )
224225 sbh .submit (doc , overwrite = 1 )
225- md_uri = '{}/user/{}/{}/{}/{}' .format (sbh .getURL (), sbh .getUser (),
226- doc .displayId , md .displayId ,
227- md .version )
226+ md_uri = '{}/user/{}/{}/{}/{}' .format (sbh .getURL (), sbh .getUser (), doc .displayId ,
227+ md .displayId , md .version )
228228 sbh .attachFile (md_uri , CRISPR_LOCATION )
229229
230+ # SBH will autogenerate an Attachment object, so now we query to find out
231+ # what the URI of that Attachment object is so we can download it
232+ query = '''
233+ PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
234+ PREFIX dcterms: <http://purl.org/dc/terms/>
235+ PREFIX dc: <http://purl.org/dc/elements/1.1/>
236+ PREFIX sbh: <http://wiki.synbiohub.org/wiki/Terms/synbiohub#>
237+ PREFIX prov: <http://www.w3.org/ns/prov#>
238+ PREFIX sbol: <http://sbols.org/v2#>
239+ PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
240+ PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
241+ PREFIX purl: <http://purl.obolibrary.org/obo/>
242+ SELECT DISTINCT ?attachment_uri
243+ WHERE {{
244+ <{}> sbol:attachment ?attachment_uri
245+ }}''' .format (md_uri )
246+ response = sbh .sparqlQuery (query )
247+ content = json .loads (response .content )
248+ attachment_uri = content ['results' ]['bindings' ][0 ]['attachment_uri' ]['value' ]
249+ # Save to default filename
250+ sbh .downloadAttachment (attachment_uri )
251+ self .assertTrue (os .path .exists ('./crispr_example.xml' ))
252+ os .remove ('crispr_example.xml' )
253+ # Save to new file name
254+ sbh .downloadAttachment (attachment_uri , 'foo.xml' )
255+ self .assertTrue (os .path .exists ('./foo.xml' ))
256+ os .remove ('foo.xml' )
257+ # Confirm error handling
258+ sbh .remove (attachment_uri )
259+ with self .assertRaises (sbol2 .SBOLError ) as cm :
260+ sbh .downloadAttachment (attachment_uri )
261+ e = cm .exception
262+ self .assertEqual (e .error_code (), sbol2 .SBOLErrorCode .SBOL_ERROR_NOT_FOUND )
263+
230264 def test_search_general (self ):
231265 sbh = sbol2 .PartShop (TEST_RESOURCE_MAIN )
232266 # sbh.login(username, password)
0 commit comments