def test_default_article_fields(self, patched): """ articles should have their properties set to None for each key in fl, if the response has no data. """ sr = SolrResponse(self.response) sr.docs = [{"id": 1}] sr.fl = ['id', 'bibstem'] self.assertEqual(sr.articles[0].id, 1) self.assertEqual(sr.articles[0].bibstem, None) self.assertEqual(patched.call_count, 0)
def test_articles(self): """ the article attribute should be read-only, and set the first time it is called """ sr = SolrResponse(self.response.text) self.assertIsNone(sr._articles) self.assertEqual(len(sr.articles), 28) self.assertEqual(sr._articles, sr.articles) self.assertEqual(sr.articles[0].doi, [u'10.1126/science.174.4005.142']) with self.assertRaises(AttributeError): sr.articles = 'this should be read-only'
def test_load_http_response(self): """ the classmethod load_http_response() should return an instansiated SolrResponse class """ sr = SolrResponse.load_http_response(self.response) self.assertIsInstance(sr, SolrResponse) self.assertIsInstance(sr, APIResponse) self.assertEqual(sr.response, self.response) # Response with a non-200 return code should raise self.response.status_code = 500 with self.assertRaises(APIResponseError): SolrResponse.load_http_response(self.response)
def test_init(self): """ ensure that an init of SolrResponse has the expected keys, and that if critical data are missing then raise SolrResponseParseError """ sr = SolrResponse(self.response.text) self.assertIn('responseHeader', sr.json) self.assertIn('response', sr.json) self.assertEqual(sr.numFound, 28) malformed_text = self.response.text.replace( 'responseHeader', 'this_is_now_malformed_data', ) with self.assertRaises(SolrResponseParseError): SolrResponse(malformed_text)