def test_encode(self): m = PVLModule(a=PVLGroup(g1=2, g2=3.4), b="c") no_convert = PDSLabelEncoder(convert_group_to_object=False) self.assertRaises(ValueError, no_convert.encode, m) s = """OBJECT = a\r G1 = 2\r G2 = 3.4\r END_OBJECT = a\r B = c\r END\r\n""" self.assertEqual(s, self.e.encode(m)) m = PVLModule( a="b", staygroup=PVLGroup(c="d"), obj=PVLGroup(d="e", f=PVLGroup(g="h")), ) s = """A = b\r GROUP = staygroup\r C = d\r END_GROUP = staygroup\r OBJECT = obj\r D = e\r GROUP = f\r G = h\r END_GROUP = f\r END_OBJECT = obj\r END\r\n""" self.assertEqual(s, self.e.encode(m))
def test_encode(self): m = PVLModule(a=PVLGroup(g1=2, g2=3.4), b='c') no_convert = PDSLabelEncoder(convert_group_to_object=False) self.assertRaises(ValueError, no_convert.encode, m) s = '''OBJECT = a\r G1 = 2\r G2 = 3.4\r END_OBJECT = a\r B = c\r END\r\n''' self.assertEqual(s, self.e.encode(m)) m = PVLModule(a='b', staygroup=PVLGroup(c='d'), obj=PVLGroup(d='e', f=PVLGroup(g='h'))) s = '''A = b\r GROUP = staygroup\r C = d\r END_GROUP = staygroup\r OBJECT = obj\r D = e\r GROUP = f\r G = h\r END_GROUP = f\r END_OBJECT = obj\r END\r\n''' self.assertEqual(s, self.e.encode(m))
def test_encode_aggregation_block(self): g = PVLGroup(a='b', c=PVLGroup(d='e')) no_convert = PDSLabelEncoder(convert_group_to_object=False) self.assertRaises(ValueError, no_convert.encode_aggregation_block, 'key', g) s = '''OBJECT = key\r A = b\r GROUP = c\r D = e\r END_GROUP = c\r END_OBJECT = key''' self.assertEqual(s, self.e.encode_aggregation_block('key', g))
def test_encode_aggregation_block(self): g = PVLGroup(a="b", c=PVLGroup(d="e")) no_convert = PDSLabelEncoder(convert_group_to_object=False) self.assertRaises(ValueError, no_convert.encode_aggregation_block, "key", g) s = """OBJECT = key\r A = b\r GROUP = c\r D = e\r END_GROUP = c\r END_OBJECT = key""" self.assertEqual(s, self.e.encode_aggregation_block("key", g))
def setUp(self): self.e = PDSLabelEncoder()
class TestPDSLabelEncoder(unittest.TestCase): def setUp(self): self.e = PDSLabelEncoder() def test_count_aggs(self): m = PVLModule(a=PVLGroup(), b=PVLObject(), c=PVLObject()) self.assertEqual((2, 1), self.e.count_aggs(m)) def test_is_PDSgroup(self): g = PVLGroup(a='b', c=PVLGroup()) self.assertFalse(self.e.is_PDSgroup(g)) g = PVLGroup({'a': 'b', '^c': 5}) self.assertFalse(self.e.is_PDSgroup(g)) g = PVLGroup({'a': 'b', '^c': 'd'}) self.assertTrue(self.e.is_PDSgroup(g)) g = PVLGroup((('a', 'b'), ('c', 'd'), ('a', 'b2'))) self.assertFalse(self.e.is_PDSgroup(g)) def test_convert_grp_to_obj(self): g = PVLGroup(a='b', c='d') o = PVLObject(a='b', c='d') converted = PVLObject(g) self.assertEqual(o, converted) self.assertIsInstance(converted, PVLObject) def test_encode_aggregation_block(self): g = PVLGroup(a='b', c=PVLGroup(d='e')) no_convert = PDSLabelEncoder(convert_group_to_object=False) self.assertRaises(ValueError, no_convert.encode_aggregation_block, 'key', g) s = '''OBJECT = key\r A = b\r GROUP = c\r D = e\r END_GROUP = c\r END_OBJECT = key''' self.assertEqual(s, self.e.encode_aggregation_block('key', g)) def test_encode_set(self): s = set(['a', "has'apostrophe"]) self.assertRaises(ValueError, self.e.encode_set, s) s = set(['a', 'has\newline']) self.assertRaises(ValueError, self.e.encode_set, s) def test_encode_time(self): t = datetime.time(1, 2) self.assertEqual('01:02Z', self.e.encode_time(t)) t = datetime.time( 13, 14, 15, ) self.assertEqual('13:14:15Z', self.e.encode_time(t)) t = datetime.time(13, 14, 15, tzinfo=datetime.timezone( datetime.timedelta(hours=2))) self.assertRaises(ValueError, self.e.encode_time, t) t = datetime.time(13, 14, 15, tzinfo=datetime.timezone( datetime.timedelta(hours=0))) self.assertRaises(ValueError, self.e.encode_time, t) def test_encode(self): m = PVLModule(a=PVLGroup(g1=2, g2=3.4), b='c') no_convert = PDSLabelEncoder(convert_group_to_object=False) self.assertRaises(ValueError, no_convert.encode, m) s = '''OBJECT = a\r G1 = 2\r G2 = 3.4\r END_OBJECT = a\r B = c\r END\r\n''' self.assertEqual(s, self.e.encode(m)) m = PVLModule(a='b', staygroup=PVLGroup(c='d'), obj=PVLGroup(d='e', f=PVLGroup(g='h'))) s = '''A = b\r GROUP = staygroup\r C = d\r END_GROUP = staygroup\r OBJECT = obj\r D = e\r GROUP = f\r G = h\r END_GROUP = f\r END_OBJECT = obj\r END\r\n''' self.assertEqual(s, self.e.encode(m))
class TestPDSLabelEncoder(unittest.TestCase): def setUp(self): self.e = PDSLabelEncoder() def test_count_aggs(self): m = PVLModule(a=PVLGroup(), b=PVLObject(), c=PVLObject()) self.assertEqual((2, 1), self.e.count_aggs(m)) def test_is_PDSgroup(self): g = PVLGroup(a="b", c=PVLGroup()) self.assertFalse(self.e.is_PDSgroup(g)) g = PVLGroup({"a": "b", "^c": 5}) self.assertFalse(self.e.is_PDSgroup(g)) g = PVLGroup({"a": "b", "^c": "d"}) self.assertTrue(self.e.is_PDSgroup(g)) g = PVLGroup((("a", "b"), ("c", "d"), ("a", "b2"))) self.assertFalse(self.e.is_PDSgroup(g)) def test_convert_grp_to_obj(self): g = PVLGroup(a="b", c="d") o = PVLObject(a="b", c="d") converted = PVLObject(g) self.assertEqual(o, converted) self.assertIsInstance(converted, PVLObject) def test_encode_aggregation_block(self): g = PVLGroup(a="b", c=PVLGroup(d="e")) no_convert = PDSLabelEncoder(convert_group_to_object=False) self.assertRaises(ValueError, no_convert.encode_aggregation_block, "key", g) s = """OBJECT = key\r A = b\r GROUP = c\r D = e\r END_GROUP = c\r END_OBJECT = key""" self.assertEqual(s, self.e.encode_aggregation_block("key", g)) def test_encode_set(self): s = {"a", "has'apostrophe"} self.assertRaises(ValueError, self.e.encode_set, s) s = {"a", "has\newline"} self.assertRaises(ValueError, self.e.encode_set, s) def test_encode_time(self): t = datetime.time(1, 2) self.assertEqual("01:02Z", self.e.encode_time(t)) t = datetime.time( 13, 14, 15, ) self.assertEqual("13:14:15Z", self.e.encode_time(t)) t = datetime.time(13, 14, 15, tzinfo=datetime.timezone( datetime.timedelta(hours=2))) self.assertRaises(ValueError, self.e.encode_time, t) t = datetime.time(13, 14, 15, tzinfo=datetime.timezone( datetime.timedelta(hours=0))) self.assertRaises(ValueError, self.e.encode_time, t) def test_encode(self): m = PVLModule(a=PVLGroup(g1=2, g2=3.4), b="c") no_convert = PDSLabelEncoder(convert_group_to_object=False) self.assertRaises(ValueError, no_convert.encode, m) s = """OBJECT = a\r G1 = 2\r G2 = 3.4\r END_OBJECT = a\r B = c\r END\r\n""" self.assertEqual(s, self.e.encode(m)) m = PVLModule( a="b", staygroup=PVLGroup(c="d"), obj=PVLGroup(d="e", f=PVLGroup(g="h")), ) s = """A = b\r GROUP = staygroup\r C = d\r END_GROUP = staygroup\r OBJECT = obj\r D = e\r GROUP = f\r G = h\r END_GROUP = f\r END_OBJECT = obj\r END\r\n""" self.assertEqual(s, self.e.encode(m))