def test_json_roundtrip(self): store = SchemaStore() store.add_record(namespaced_schemas.TestRecord) with warnings.catch_warnings(record=True) as warninglist: store.add_record(TestRecord) self.assertEquals(len(warninglist), 1) namespace_instance = namespaced_schemas.TestRecord(a='test') namespace_roundtrip = pyschema.loads(pyschema.dumps(namespace_instance), record_store=store) self.assertTrue(isinstance(namespace_roundtrip, namespaced_schemas.TestRecord)) instance = TestRecord(a='test') roundtrip = pyschema.loads(pyschema.dumps(instance), record_store=store) self.assertTrue(isinstance(roundtrip, TestRecord))
def test_typed_mr(self): task = VanillaTask() luigi.build([task], local_scheduler=True) for line in task.output().open('r'): self.assertTrue("$schema" in line) rec = pyschema.loads(line) self.assertEquals(rec.foo, u"yay") self.assertEquals(rec.barsum, 5)
def test_json_roundtrip(self): store = SchemaStore() store.add_record(namespaced_schemas.TestRecord) with warnings.catch_warnings(record=True) as warninglist: store.add_record(TestRecord) self.assertEquals(len(warninglist), 1) namespace_instance = namespaced_schemas.TestRecord(a='test') namespace_roundtrip = pyschema.loads( pyschema.dumps(namespace_instance), record_store=store) self.assertTrue( isinstance(namespace_roundtrip, namespaced_schemas.TestRecord)) instance = TestRecord(a='test') roundtrip = pyschema.loads(pyschema.dumps(instance), record_store=store) self.assertTrue(isinstance(roundtrip, TestRecord))
def test_fields(self): @pyschema.no_auto_store() class ValidRecord(pyschema.Record): field = Integer() line = '{"field": 8, "invalid_field": 0}' self.assertRaises(ParseError, lambda: pyschema.loads(line, schema=ValidRecord))
def _generic_writer_tests(self, writer, hard_coded_type=None): output = StringIO() writer(None, self.seq(), output) output.seek(0) # remove last \n before splitting output_lines = output.read()[:-1].split('\n') output_records = tuple( pyschema.loads(l, schema=hard_coded_type) for l in output_lines) foos = tuple(rec.foo for rec in output_records) bars = tuple(rec.bar for rec in output_records) self.assertEquals(foos, ("Hej", "Moo", "Tjaba tjena")) self.assertEquals(bars, (10, None, 3)) return output_lines, output_records
def test_full_circle(self): class Foo(Record): bin = Bytes() class MyRecord(Record): a_string = Text() a_float = Float() record = List(SubRecord(Foo)) rec = MyRecord(a_string=u"hej") rec.record = [Foo(bin="bar")] s = dumps(rec) reloaded_obj = loads(s) self.assertEquals(reloaded_obj.a_string, u"hej") self.assertTrue(reloaded_obj.a_float is None) self.assertTrue(reloaded_obj.record[0].bin, "bar")
def _generic_writer_tests(self, writer, hard_coded_type=None): output = StringIO() writer(None, self.seq(), output) output.seek(0) # remove last \n before splitting output_lines = output.read()[:-1].split('\n') output_records = tuple( pyschema.loads(l, schema=hard_coded_type) for l in output_lines) foos = tuple(rec.foo for rec in output_records) bars = tuple(rec.bar for rec in output_records) self.assertEquals( foos, ("Hej", "Moo", "Tjaba tjena") ) self.assertEquals( bars, (10, None, 3) ) return output_lines, output_records
def _roundtrip(self, schema, input_bytes, expected=None): record = schema() record._ = input_bytes serialized = pyschema.dumps(record, attach_schema_name=False) reborn = pyschema.loads(serialized, schema=schema) return serialized, reborn._