You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The way you have this now, you are going to have to call get_subjects to test make_index, so in order for tests to pass, you need stable data to test against via live API call (slow, flaky) or to use something like VCR to mock what that result would be. Same for attach_subjects, which invokes make_index
This kind of setup leads to hard to write/setup tests that are hard to maintain when something changes. (I have some similar problems in collectionspace-mapper which has this same overall problem without involving Thor)
You can greatly simplify the testing by sorta complicating the code. I'm not 100% sure what's going on with your *args here, and undoubtedly this will not straight up work if dropped in, but here's an sketchy idea of what I mean: https://gist.github.com/kspurgin/94c20bfe513304951d6fcc30abc85ff1
It takes the idea of putting your code's actual behavior into Plain Old Ruby Objects (POROs) and mixes in some semi/pseudo dependency injection so the POROs can be called in isolation from one another. I.e. in production, they call on each other like the invoke s you have now, but in testing you can pass in basic test data easily.
Then the Thor commands are just dumb wrappers around already tested external code, or your own tested code.
The text was updated successfully, but these errors were encountered:
moduleCommonclassSubjectGetterdefself.callAspace_Client.client.use_global_repositorypage=1data=[]response=Aspace_Client.client.get('subjects',query: {page: page,page_size: 100})last_page=response.result['last_page']whilepage <= last_pageresponse=Aspace_Client.client.get('subjects',query: {page: page,page_size: 100})data << response.result['results']page += 1enddata.flattenendendclassSubjectIndexMakerdefself.call(data=SubjectGetter.call)index={}data.eachdo |record|
index[record['title'].gsub(" -- ","--")]=record['uri']endindexendendclassSubjectAttacherdefself.call(data,field,index=SubjectIndexMaker.call)data.eachdo |record|
# sets the variable to empty array if the referenced array is nil; otherwise sets the variable to the array# this makes it so that this doesn't override the array if it already exists - it would instead add to the arraysubjects_refs=record["subjects__refs"].nil? ? [] : record["subjects__refs"]record[field].each{|entity| subjects_refs << index[entity]}record["subjects__refs"]=subjects_refsenddataendendclassSubjects < Thordesc'get_subjects','retrieve API response of all subject data in ASpace'defget_subjects(*args)SubjectGetter.call(args)enddesc'make_index','create the following index - "title:uri"'defmake_index(*args)SubjectIndexMaker.callenddesc"attach_subjects","attach subjects refs to object by matching values from the given field. assumes DATA is an array of hashes, FIELD is a string"defattach_subjects(data,field)SubjectAttacher.call(data,field)endendend#----- separate spec fileRSpec.describeCommon::SubjectIndexMakerdodescribe'.call'doit'returns index'do# now it is easy to text what this does with different input# because you can just put test input in the testdata='paste/type data in expected format here'expected='paste/type data in expected format here'expect(Common::SubjectIndexMaker.call(data)).toeq(expected)endendend#----- separate spec fileRSpec.describeCommon::SubjectAttacherdodescribe'.call'doit'returns index'do# now it is easy to text what this does with different input# because you can just put test input in the testdata='paste/type data in expected format here'field='fieldname'index='paste/type data in expected format here'expected='paste/type data in expected format here'expect(Common::SubjectAttacher.call(data,field,index)).toeq(expected)endendend
The text was updated successfully, but these errors were encountered: