def testExactlyEnoughInstances(self): test_instances = b'\n'.join([b'{"images": [0, 1], "key": 3}'] * 100) instance_file = io.BytesIO(test_instances) instances = predict_utilities.ReadInstances(instance_file, JSON_FORMAT, limit=100) self.assertEqual(100, len(instances))
def testEmptyLineJson(self): test_instances = (b'{"images": [0, 1], "key": 3}\n\n' b'{"images": [0, 1], "key": 3}') instance_file = io.BytesIO(test_instances) with self.assertRaisesRegex(core_exceptions.Error, 'Empty line is not allowed'): predict_utilities.ReadInstances(instance_file, JSON_FORMAT)
def testTooManyInstances(self): test_instances = b'\n'.join([b'{"images": [0, 1], "key": 3}'] * 101) instance_file = io.BytesIO(test_instances) with self.assertRaisesRegex(core_exceptions.Error, 'no more than 100'): predict_utilities.ReadInstances(instance_file, JSON_FORMAT, limit=100)
def testTooManyInstancesNoLimit(self): test_instances = b'\n'.join([b'{"images": [0, 1], "key": 3}'] * 100) instance_file = io.BytesIO(test_instances) instance_file = io.BytesIO(test_instances) instances = predict_utilities.ReadInstances(instance_file, JSON_FORMAT, limit=None) self.assertEqual(100, len(instances))
def testMultipleJsonInstances(self): test_instances = (b'{"images": [0, 1], "key": 3}\n' b'{"images": [3, 2], "key": 2}\n' b'{"images": [2, 1], "key": 1}') instance_file = io.BytesIO(test_instances) instances = predict_utilities.ReadInstances(instance_file, JSON_FORMAT) expected_instances = [{ 'images': [0, 1], 'key': 3 }, { 'images': [3, 2], 'key': 2 }, { 'images': [2, 1], 'key': 1 }] self.assertEqual(expected_instances, instances)
def testEmptyFile(self): instance_file = io.BytesIO(b'') with self.assertRaisesRegex(core_exceptions.Error, 'No valid instance was found.'): predict_utilities.ReadInstances(instance_file, JSON_FORMAT)
def testMultipleTextInstances(self): instance_file = io.BytesIO(b'2, 3\n4, 5') instances = predict_utilities.ReadInstances(instance_file, TEXT_FORMAT) expected_instances = ['2, 3', '4, 5'] self.assertEqual(expected_instances, instances)
def testTextInstancesTrailingNewline(self): instance_file = io.BytesIO(b'abcd\n') instances = predict_utilities.ReadInstances(instance_file, TEXT_FORMAT) expected_instances = ['abcd'] self.assertEqual(expected_instances, instances)
def testTextInstancesUTF8BOM(self): instance_file = io.BytesIO(b'\xef\xbb\xbfabcd') instances = predict_utilities.ReadInstances(instance_file, TEXT_FORMAT) expected_instances = ['abcd'] self.assertEqual(expected_instances, instances)
def testJsonInstancesTrailingNewline(self): instance_file = io.BytesIO(b'{"images": [0, 1], "key": 3}\n') instances = predict_utilities.ReadInstances(instance_file, JSON_FORMAT) expected_instances = [{'images': [0, 1], 'key': 3}] self.assertEqual(expected_instances, instances)
def testEmptyLineText(self): instance_file = io.BytesIO(b'2, 3\n\n2, 3') with self.assertRaisesRegex(core_exceptions.Error, 'Empty line is not allowed'): predict_utilities.ReadInstances(instance_file, TEXT_FORMAT)
def testNewlineOnlyTEXT(self): instance_file = io.BytesIO(b'\n') with self.assertRaisesRegex(core_exceptions.Error, 'Empty line is not allowed'): predict_utilities.ReadInstances(instance_file, TEXT_FORMAT)