def test_read_create_json(self): protocol, filename = 'file', 'test.json' data = {'I': {'am': ['nested', 1, 'object', None]}} cabinets.create(f'{protocol}://{filename}', data) cabinets.create(f'{protocol}://{filename}', data) result = cabinets.read(f'{protocol}://{filename}') self.assertDictEqual(data, result)
def test_read_plain_text_custom_parser(self): protocol = 'file' filename = os.path.join(self.fixture_path, 'sample_small.txt') data = cabinets.read(f'{protocol}://{filename}', parser=MockTextParser) self.assertIsInstance(data, dict) expected = {'mock-parser': "I am sample text!"} self.assertDictEqual(expected, data)
def test_read_plain_text_no_parser(self): protocol = 'file' filename = os.path.join(self.fixture_path, 'sample_small.txt') data = cabinets.read(f'{protocol}://{filename}', parser=False) self.assertIsInstance(data, bytes) expected = bytes("I am sample text!", encoding='utf-8') self.assertEqual(expected, data)
def test_read_plain_text_default_parser(self): protocol = 'file' filename = os.path.join(self.fixture_path, 'sample_small.txt') data = cabinets.read(f'{protocol}://{filename}', parser=True) self.assertIsInstance(data, str) expected = "I am sample text!" self.assertEqual(expected, data)
def test_read_plain_text_single_byte_encoding(self): protocol = 'file' filename = os.path.join(self.fixture_path, 'sample_single_byte.txt') data = cabinets.read(f'{protocol}://{filename}', encoding='iso-8859-1') expected = "I am sample text!\nThis file has more than one line.\n" \ "Hey look, a panda.\n\nNow it's a new paragraph. This line has " \ "two sentences.\n" self.assertEqual(expected, data)
def test_read_plain_text(self): protocol = 'file' filename = os.path.join(self.fixture_path, 'sample.txt') data = cabinets.read(f'{protocol}://{filename}') expected = "I am sample text!\nThis file has more than one line.\n" \ "Hey look, a panda.\n\nその鶏のサイズを見てください\nNow it's a " \ "new paragraph. This line has two sentences.\n🤯🦄\n" self.assertEqual(expected, data)
def test_read_plain_text_default_parser_dot_path(self): protocol = 'file' filename = os.path.join(self.fixture_path, 'dotpath.example', 'dot.sample.txt') data = cabinets.read(f'{protocol}://{filename}', parser=True) self.assertIsInstance(data, str) expected = "I am sample text from a file with dots (.) in the path!" self.assertEqual(expected, data)
def send_notification_with_manual_data(path_courier_data): try: courier_message = cabinets.read(path_courier_data) except AttributeError as e: print(f"Attribute error: {e}") return courier_message["event"] = EVENT_ID response = send(**courier_message) print(f"response: {response}")
def test_read_create_pickle(self): protocol, filename = 'file', 'test.pickle' data = { 'I': { 'am': ['nested', 1 + 2j, 'object', None], 'purple': SimpleNamespace(egg=True, fish=42) } } cabinets.create(f'{protocol}://{filename}', data) result = cabinets.read(f'{protocol}://{filename}') self.assertDictEqual(data, result)
def test_read_create(self): self.client = boto3.client('s3') self.client.create_bucket(Bucket=self._bucket) protocol, filename = 's3', f'{self._bucket}/test.yml' data = {'I': {'am': ['nested', 1, 'object', None]}} cabinets.create(f'{protocol}://{filename}', data) result = cabinets.read(f'{protocol}://{filename}') self.assertDictEqual(data, result) # clean up file in mocked s3 cabinets.delete(f'{protocol}://{filename}')
def test_read_create_with_different_region(self): self.client = boto3.client('s3', 'us-east-2') self.client.create_bucket( Bucket=self._bucket, CreateBucketConfiguration={'LocationConstraint': 'us-east-2'}) protocol, filename = 's3', f'{self._bucket}/test.yml' data = {'I': {'am': ['nested', 1, 'object', None]}} cabinets.create(f'{protocol}://{filename}', data) result = cabinets.read(f'{protocol}://{filename}') self.assertDictEqual(data, result) # clean up file in mocked s3 cabinets.delete(f'{protocol}://{filename}')
def test_read_text_custom_parser_raises(self): with self.assertRaises(cabinets.CabinetError): cabinets.read(os.path.join(self.fixture_path, 'sample.txt'), parser=str) with self.assertRaises(cabinets.CabinetError): cabinets.read(os.path.join(self.fixture_path, 'sample.txt'), parser=1) with self.assertRaises(cabinets.CabinetError): cabinets.read(os.path.join(self.fixture_path, 'sample.txt'), parser=None)
def test_read_json(self): protocol = 'file' filename = os.path.join(self.fixture_path, 'sample.json') data = cabinets.read(f'{protocol}://{filename}') self.assertEqual({'hello': 'world'}, data)
def test_read_json(self): filename = os.path.join(self.fixture_path, 'sample.json') path = pathlib.Path(filename) data = cabinets.read(path) self.assertEqual({'hello': 'world'}, data)