def test_serialization(self): bibcode = "2017IAUS..325..341B" JSON_fingerprint = "JSON" arxiv_category = "Instrumentation and Methods for Astrophysics" content = "This is a dummy article." bibrecord = BibRecord() bibrecord.data.bibcode = bibcode bibrecord.data.JSON_fingerprint = JSON_fingerprint bibrecord.data.metadata.general.arxivcategories.append(arxiv_category) bibrecord.data.text.body.content = content data = bibrecord.serialize() self.assertEqual(data, '\n\x13{0}\x12\x04{1}\x1a0\n.\n,{2}"\x1c\n\x1a\n\x18{3}'.format(bibcode, JSON_fingerprint, arxiv_category, content)) data_str = str(bibrecord) self.assertEqual(data_str, 'bibcode: "{0}"\nJSON_fingerprint: "{1}"\nmetadata {{\n general {{\n arxivcategories: "{2}"\n }}\n}}\ntext {{\n body {{\n content: "{3}"\n }}\n}}\n'.format(bibcode, JSON_fingerprint, arxiv_category, content)) self.assertNotEqual(data, data_str) recovered_bibrecord = BibRecord.deserializer(data) self.assertTrue(recovered_bibrecord.is_valid()) self.assertEqual(bibrecord.data.bibcode, bibcode) self.assertEqual(bibrecord.data.JSON_fingerprint, JSON_fingerprint) self.assertEqual(bibrecord.data.metadata.general.arxivcategories[0], arxiv_category) self.assertEqual(bibrecord.data.text.body.content, content)
def test_serializer(self): b = BibRecord(bibcode='bibcode') cls, data = b.dump() self.assertEqual('adsmsg.bibrecord.BibRecord', cls) if sys.version_info > (3, ): test_data = b'\n\x07bibcode' else: test_data = '\n\x07bibcode' self.assertEqual( data, test_data) # let's hope the upstream adsmsg doesn't change impl... # dump using default 'json' serializer content_type, encoding, data = serialization.dumps(b) self.assertEqual( '{"__adsmsg__": ["adsmsg.bibrecord.BibRecord", "CgdiaWJjb2Rl"]}', data) # load using our deserializer o = serialization.loads(data, 'application/x-adsmsg', encoding) self.assertTrue(isinstance(o, BibRecord)) self.assertEqual(o.bibcode, 'bibcode') # and arbitrarily nested objects should pose no difficulties test = [{'foo': b}, b, [[b]]] content_type, encoding, data = serialization.dumps(test) o = serialization.loads(data, 'application/x-adsmsg', encoding) for x in (o[0]['foo'], o[1], o[2][0][0]): self.assertTrue(isinstance(x, BibRecord)) self.assertEqual(x.bibcode, "bibcode")
def test_is_valid(self): bibrecord = BibRecord() bibrecord.data.bibcode = "2017IAUS..325..341B" bibrecord.data.JSON_fingerprint = "JSON" bibrecord.data.metadata.general.arxivcategories.append("Instrumentation and Methods for Astrophysics") bibrecord.data.text.body.content = "Article..." self.assertTrue(bibrecord.is_valid())
def test_higher_char(self): b = BibRecord(bibcode=u'\u01b5') cls, data = b.dump() self.assertEqual('adsmsg.bibrecord.BibRecord', cls) self.assertEqual('\n\x02\xc6\xb5', data) b2 = Msg.loads(cls, data) self.assertEqual(b2.bibcode, b.bibcode)
def test_serializer(self): b = BibRecord(bibcode='bibcode') cls, data = b.dump() self.assertEqual('adsmsg.bibrecord.BibRecord', cls) self.assertEqual('\n\x07bibcode', data) b2 = Msg.loads(cls, data) self.assertEqual(b2.bibcode, b.bibcode)
def test_generic_methods(self): # keywords initialization b = BibRecord(bibcode='bibcode') self.assertEqual(b.bibcode, 'bibcode') self.assertEqual(b.data.bibcode, 'bibcode') # acces/set attributes directly b.bibcode = 'foobar' self.assertEqual(b.bibcode, 'foobar') self.assertEqual(b.data.bibcode, 'foobar')
def test_json(self): b = BibRecord(bibcode=u'\u01b5') cls, data = b.dump() x = json.dumps(b.__json__()) z = json.loads(x) data2 = base64.b64decode(z['__adsmsg__'][1]) self.assertEqual(repr(data), repr(data2)) b1 = Msg.loads(cls, data) b2 = Msg.loads(cls, data2) self.assertEqual(b1.bibcode, u'\u01b5') self.assertEqual(b2.bibcode, u'\u01b5')
def test_status(self): b = BibRecord(bibcode='bibcode', status=Status.active) self.assertEqual(b.status, 0) b = BibRecord(bibcode='bibcode', status='active') self.assertEqual(b.status, 0)
def test_toJSON(self): b = BibRecord(bibcode=u'\u01b5') self.assertEqual(b.toJSON(), {'bibcode': u'\u01b5'}) self.assertTrue(isinstance(b.toJSON(), dict)) self.assertEqual('{\n "bibcode": "\\u01b5"\n}', b.toJSON(return_string=True))
def test_utf8(self): b = BibRecord(bibcode=u'\u01b5') ctype, enc, data = serialization.dumps(b) o = serialization.loads(data, 'application/x-adsmsg', 'utf-8') self.assertTrue(isinstance(o, BibRecord)) self.assertEqual(o.bibcode, u'\u01b5')