def test_ingest_publish_dry_run_from_cli(self): # ensure nothing exists self.assertEqual(models.Journal.objects.count(), 0) self.assertEqual(models.Article.objects.count(), 0) self.assertEqual(models.ArticleVersion.objects.count(), 0) args = [self.nom, '--ingest+publish', '--id', self.msid, '--version', self.version, self.ajson_fixture1, '--dry-run'] errcode, stdout = self.call_command(*args) self.assertEqual(errcode, 0) # ensure nothing was created self.assertEqual(models.Journal.objects.count(), 0) self.assertEqual(models.Article.objects.count(), 0) self.assertEqual(models.ArticleVersion.objects.count(), 0) # ensure response is json and well-formed result = json.loads(stdout.getvalue()) self.assertTrue(utils.has_all_keys(result, ['status', 'id', 'datetime', 'message'])) # ensure response data is correct self.assertEqual(result['status'], 'published') ajson = json.load(open(self.ajson_fixture1, 'r')) self.assertEqual(result['datetime'], ajson['article']['published']) self.assertEqual(result['message'], "(dry-run)")
def test_ingest_publish_dry_run_from_cli(self): # ensure nothing exists self.assertEqual(models.Journal.objects.count(), 0) self.assertEqual(models.Article.objects.count(), 0) self.assertEqual(models.ArticleVersion.objects.count(), 0) args = [ self.nom, '--ingest+publish', '--id', self.msid, '--version', self.version, self.ajson_fixture1, '--dry-run' ] errcode, stdout = self.call_command(*args) self.assertEqual(errcode, 0) # ensure nothing was created self.assertEqual(models.Journal.objects.count(), 0) self.assertEqual(models.Article.objects.count(), 0) self.assertEqual(models.ArticleVersion.objects.count(), 0) # ensure response is json and well-formed result = json.loads(stdout) self.assertTrue( utils.has_all_keys(result, ['status', 'id', 'datetime', 'dry-run'])) # ensure response data is correct self.assertEqual(result['status'], 'validated') self.assertEqual(result['dry-run'], True) ajson = json.load(open(self.ajson_fixture1, 'r')) self.assertEqual(result['datetime'], ajson['article']['published'])
def test_ingest_publish_from_cli(self): args = [self.nom, '--ingest+publish', '--id', self.msid, '--version', self.version, self.ajson_fixture1] errcode, stdout = self.call_command(*args) self.assertEqual(errcode, 0) # article has been ingested av = models.ArticleVersion.objects.get(article__manuscript_id=self.msid, version=self.version) # article has been published self.assertTrue(av.published()) # ensure response is json and well-formed result = json.loads(stdout.getvalue()) self.assertTrue(utils.has_all_keys(result, ['status', 'id', 'datetime'])) # ensure response data is correct self.assertEqual(result['status'], 'published') self.assertEqual(result['datetime'], utils.ymdhms(av.datetime_published))
def test_publish_from_cli(self): args = [self.nom, '--ingest', '--id', self.msid, '--version', self.version, self.ajson_fixture1] errcode, stdout = self.call_command(*args) self.assertEqual(errcode, 0) # article has been ingested self.assertEqual(models.ArticleVersion.objects.count(), 1) args = [self.nom, '--publish', '--id', self.msid, '--version', self.version] errcode, stdout = self.call_command(*args) self.assertEqual(errcode, 0) # ensure response is json result = json.loads(stdout.getvalue()) self.assertTrue(utils.has_all_keys(result, ['status', 'id', 'datetime'])) self.assertEqual(result['status'], 'published')
def test_ingest_from_cli(self): "ingest script requires the --ingest flag and a source of data" args = [self.nom, '--ingest', '--id', self.msid, '--version', self.version, self.ajson_fixture1] errcode, stdout = self.call_command(*args) self.assertEqual(errcode, 0) # article has been ingested self.assertEqual(models.ArticleVersion.objects.count(), 1) # message returned is json encoded with all the right keys and values result = json.loads(stdout.getvalue()) self.assertTrue(utils.has_all_keys(result, ['status', 'id', 'datetime'])) self.assertEqual(result['status'], 'ingested') # the date and time is roughly the same as right now, ignoring microseconds expected_datetime = utils.ymdhms(utils.utcnow()) self.assertEqual(result['datetime'][:20], expected_datetime[:20]) self.assertEqual(result['datetime'][-6:], expected_datetime[-6:])
def test_has_all_keys(self): data = {'a': 1, 'b': 2} cases = [ (data, ['a'], True), (data, ['a', 'b'], True), (data, ['a', 'b', 'c'], False), (data, [0], False), (data, [0, 1], False), (data, [self], False), ] for case, args, expected in cases: try: self.assertEqual(utils.has_all_keys(case, args), expected) except AssertionError: print case, args, expected raise
def test_validate_without_force_flag(self): "an invalid dry ingest fails obviously" # article exists ajson = json.load(open(self.ajson_fixture_v1, 'r')) ajson_ingestor.ingest_publish(ajson) # attempt validation of given data args = [ self.nom, '--ingest', '--dry-run', '--id', self.msid, '--version', 1, self.ajson_fixture_v1 ] # no force flag present errcode, stdout = self.call_command(*args) self.assertEqual(errcode, 1) # 1 = error # error resp has a bucket of info resp = json.loads(stdout) self.assertTrue( utils.has_all_keys(resp, ['code', 'message', 'comment', 'trace']))
def test_ingest_publish_from_cli(self): args = [ self.nom, '--ingest+publish', '--id', self.msid, '--version', self.version, self.ajson_fixture1 ] errcode, stdout = self.call_command(*args) self.assertEqual(errcode, 0) # article has been ingested av = models.ArticleVersion.objects.get( article__manuscript_id=self.msid, version=self.version) # article has been published self.assertTrue(av.published()) # ensure response is json and well-formed result = json.loads(stdout) self.assertTrue( utils.has_all_keys(result, ['status', 'id', 'datetime'])) # ensure response data is correct self.assertEqual(result['status'], 'published') self.assertEqual(result['datetime'], utils.ymdhms(av.datetime_published))
def test_publish_from_cli(self): args = [ self.nom, '--ingest', '--id', self.msid, '--version', self.version, self.ajson_fixture1 ] errcode, stdout = self.call_command(*args) self.assertEqual(errcode, 0) # article has been ingested self.assertEqual(models.ArticleVersion.objects.count(), 1) args = [ self.nom, '--publish', '--id', self.msid, '--version', self.version ] errcode, stdout = self.call_command(*args) self.assertEqual(errcode, 0) # ensure response is json result = json.loads(stdout) self.assertTrue( utils.has_all_keys(result, ['status', 'id', 'datetime'])) self.assertEqual(result['status'], 'published')
def test_ingest_from_cli(self): "ingest script requires the --ingest flag and a source of data" args = [ self.nom, '--ingest', '--id', self.msid, '--version', self.version, self.ajson_fixture1 ] errcode, stdout = self.call_command(*args) self.assertEqual(errcode, 0) # article has been ingested self.assertEqual(models.ArticleVersion.objects.count(), 1) # message returned is json encoded with all the right keys and values result = json.loads(stdout) self.assertTrue( utils.has_all_keys(result, ['status', 'id', 'datetime'])) self.assertEqual(result['status'], 'ingested') # the date and time is roughly the same as right now, ignoring microseconds expected_datetime = utils.utcnow() actual_datetime = utils.todt(result['datetime']) delta = expected_datetime - actual_datetime threshold = 2 # seconds self.assertTrue(delta.seconds <= threshold)