def test_jsonalchemy_toint_usage(self): """Test the usage of ``to_int`` function in real life example. The ``test_toint`` model contains a field which contains an integer subfield. Whenever the record is obtained from ``MARCXML``, the string in mentioned subfield has to be converted to an integer. However, JSONAlchemy fills every absent subfield with a ``None`` value. If the record is not provided with the integer subfield and the built-in ``int`` function is used, the code will crash. The ``to_int`` function used inside definition of ``test_toint`` field prevents it. Here the unprovided subfield is ``999__a``. """ xml = '<collection><record><datafield tag="999" ind1="" ind2= "">' \ '<subfield code="b">Value</subfield></datafield></record>' \ '</collection>' simple_record = translate(xml, SmartJson, master_format='marc', model="test_toint", metadata=self.metadata) assert len(simple_record.__dict__['_dict']['__meta_metadata__'][ '__errors__']) == 0 # Check if it works when the value is provided. xml = '<collection><record><datafield tag="999" ind1="" ind2= "">' \ '<subfield code="a">9999</subfield>' \ '<subfield code="b">Value</subfield></datafield></record>' \ '</collection>' simple_record = translate(xml, SmartJson, master_format='marc', model="test_toint", metadata=self.metadata) self.assertEqual(simple_record['with_integers'][0]['some_int'], 9999)
def test_jsonalchemy_tooldvalue(self): """Test behaviour of ``set_default_value``. In this example, the value provided to the reader in ``d`` subfield is in wrong format. However, the behaviour of ``JSONAlchemy`` in such case is to skip the value. Given the below value of the subfield, the module crashes in ``set_default_value``. The error has been caught. What is the reason behind the mentioned behaviour needs further investigation. """ # Check if it works when the value is provided. xml = '''<collection><record><datafield tag="100" ind1=" " ind2=" "> <subfield code="a">Guy, Bobby</subfield> <subfield code="d">I like trains</subfield> <subfield code="g">ACTIVE</subfield> <subfield code="q">Bobby Guy</subfield> </datafield></record></collection>''' simple_record = translate(xml, SmartJson, master_format='marc', model="test_oldvalue", metadata=self.metadata) self.assertEqual(simple_record['dates']['birth'], None)
def create(cls, data, model="test_versionable", master_format="json", **kwargs): document = translate( data, cls, master_format=master_format, model=model, metadata=self.metadata, **kwargs ) cls.storage_engine.save_one(document.dumps()) document.bind(self.metadata) return document
def test_dumps_hidden(self): data = {"title": "Test Title"} document = translate(data, SmartJson, master_format="json", model="test_hidden", metadata=self.metadata) json = document.dumps() self.assertTrue("title" in json) self.assertTrue("hidden_basic" in json) json = document.dumps(filter_hidden=True) self.assertTrue("title" in json) self.assertFalse("hidden_basic" in json)
def create(cls, data, model='test_versionable', master_format='json', **kwargs): document = translate(data, cls, master_format=master_format, model=model, metadata=self.metadata, **kwargs) cls.storage_engine.save_one(document.dumps()) document.bind(self.metadata) return document
def test_jsonalchemy_toint_usage(self): """Test the usage of ``to_int`` function in real life example. The ``test_toint`` model contains a field which contains an integer subfield. Whenever the record is obtained from ``MARCXML``, the string in mentioned subfield has to be converted to an integer. However, JSONAlchemy fills every absent subfield with a ``None`` value. If the record is not provided with the integer subfield and the built-in ``int`` function is used, the code will crash. The ``to_int`` function used inside definition of ``test_toint`` field prevents it. Here the unprovided subfield is ``999__a``. """ xml = '<collection><record><datafield tag="999" ind1="" ind2= "">' \ '<subfield code="b">Value</subfield></datafield></record>' \ '</collection>' simple_record = translate(xml, SmartJson, master_format='marc', model="test_toint", metadata=self.metadata) assert len(simple_record.__dict__['_dict']['__meta_metadata__'] ['__errors__']) == 0 # Check if it works when the value is provided. xml = '<collection><record><datafield tag="999" ind1="" ind2= "">' \ '<subfield code="a">9999</subfield>' \ '<subfield code="b">Value</subfield></datafield></record>' \ '</collection>' simple_record = translate(xml, SmartJson, master_format='marc', model="test_toint", metadata=self.metadata) self.assertEqual(simple_record['with_integers'][0]['some_int'], 9999)
def test_dumps_hidden(self): data = {'title': 'Test Title'} document = translate(data, SmartJson, master_format='json', model='test_hidden', metadata=self.metadata) json = document.dumps() self.assertTrue('title' in json) self.assertTrue('hidden_basic' in json) json = document.dumps(filter_hidden=True) self.assertTrue('title' in json) self.assertFalse('hidden_basic' in json)