def test_article_protocol(self): "a request for an article that exists returns a 200 successful request with correct content-type" fixture = join(FIXTURE_DIR, "bp-post-to-elife.json") logic.add_result(json.load(open(fixture, "r"))) resp = self.c.get(self.article_url) self.assertEqual(resp.status_code, 200) self.assertEqual(resp["Content-Type"], settings.ELIFE_CONTENT_TYPE)
def test_article_protocol_data(self): "a request for article data returns a valid response" fixture = join(FIXTURE_DIR, "bp-post-to-elife.json") logic.add_result(json.load(open(fixture, "r"))) resp = self.c.get(self.article_url).json() for row in resp["items"]: self.assertTrue( utils.has_only_keys(row, logic.PROTOCOL_DATA_KEYS.values()))
def test_article_protocol_elife_ctype(self): "a request for an article with a custom elife content type, gets the same content type in the response" fixture = join(FIXTURE_DIR, "bp-post-to-elife.json") logic.add_result(json.load(open(fixture, "r"))) resp = self.c.get(self.article_url, HTTP_ACCEPT=settings.ELIFE_CONTENT_TYPE) self.assertEqual(resp.status_code, 200) self.assertEqual(resp["Content-Type"], settings.ELIFE_CONTENT_TYPE)
def test_add_result_bad_item(self): "a result with a bad item is not discarded entirely" fixture = join(FIXTURE_DIR, "bp-post-to-elife.json") result = json.load(open(fixture, "r")) del result["data"][0][ "URI"] # fails validation 'all keys must be present' logic.add_result(result) self.assertEqual(logic.row_count(), 3) # 5 rows, 3 that are protocols self.assertEqual(models.ArticleProtocol.objects.count(), 5)
def test_add_result_twice(self): "adding a result set twice does updates" fixture = join(FIXTURE_DIR, "bp-post-to-elife.json") fixture = json.load(open(fixture, "r")) logic.add_result(fixture) self.assertEqual(logic.row_count(), 3) # 6 rows, 3 that are protocols self.assertEqual(models.ArticleProtocol.objects.count(), 6) logic.add_result(fixture) self.assertEqual(logic.row_count(), 3) self.assertEqual(models.ArticleProtocol.objects.count(), 6)
def test_protocol_data_valid(self): "article protocol data we're returning is valid." fixture = join(FIXTURE_DIR, "bp-post-to-elife.json") msid = logic.add_result(json.load(open(fixture, "r")))["msid"] for row in logic.protocol_data(msid)["items"]: self.assertTrue( utils.has_only_keys(row, logic.PROTOCOL_DATA_KEYS.values()))
def test_protocol_data(self): "a list of article protocol data is returned" fixture = join(FIXTURE_DIR, "bp-post-to-elife.json") msid = logic.add_result(json.load(open(fixture, "r")))["msid"] data = logic.protocol_data(msid) self.assertEqual(models.ArticleProtocol.objects.count(), 6) # 6 in fixture ... self.assertTrue(isinstance(data["items"], list)) self.assertEqual(data["total"], 3) # ... only 3 that are protocols self.assertEqual(len(data["items"]), 3)
def test_add_result_retval_with_failures(self): "`add_result` returns a map of results, including failures" fixture = join(FIXTURE_DIR, "bp-post-to-elife.json") result = json.load(open(fixture, "r")) del result["data"][0][ "URI"] # fails validation 'all keys must be present' results = logic.add_result(result) failure = results["failed"][0] expected_failure = "ProcessingError: 'KeyError' thrown with message \"'URI'\" on data: {'ProtocolSequencingNumber': 's4-1', 'ProtocolTitle': 'Antibodies', 'IsProtocol': False, 'ProtocolStatus': 0, 'msid': 12345}" self.assertEqual(logic.format_error(failure), expected_failure)
def test_protocol_data_empty(self): "an empty list of article protocol data is returned" fixture = join(FIXTURE_DIR, "bp-post-to-elife.json") msid = logic.add_result(json.load(open(fixture, "r")))["msid"] # leaving only non-protocol data models.ArticleProtocol.objects.filter(is_protocol=True).delete() data = logic.protocol_data(msid) self.assertEqual(models.ArticleProtocol.objects.count(), 3) self.assertTrue(isinstance(data["items"], list)) self.assertEqual(data["total"], 0) self.assertEqual(len(data["items"]), 0)
def test_add_result_retval(self): "`add_result` returns a map of results" fixture = join(FIXTURE_DIR, "bp-post-to-elife.json") results = logic.add_result(json.load(open(fixture, "r"))) self.assertTrue( utils.has_all_keys(results, ["msid", "successful", "failed"])) self.assertTrue( all([ isinstance(results["successful"], list), isinstance(results["failed"], list), len(results["successful"]) == 6, len(results["failed"]) == 0, ]))
def test_article_protocol_head(self): "a HEAD request for an article that exists returns, 200 successful request" fixture = join(FIXTURE_DIR, "bp-post-to-elife.json") logic.add_result(json.load(open(fixture, "r"))) resp = self.c.head(self.article_url) self.assertEqual(resp.status_code, 200)
def test_add_result(self): "an entire result from BP can be processed, validated and inserted" fixture = join(FIXTURE_DIR, "bp-post-to-elife.json") logic.add_result(json.load(open(fixture, "r"))) self.assertEqual(logic.row_count(), 3) # 6 rows, 3 that are protocols self.assertEqual(models.ArticleProtocol.objects.count(), 6)