def test_create_json_default_parser_dot_path(self): protocol, filename = 'file', 'dotpath.tmp/dot.sample.json' content = {"hello": "world"} cabinets.create(f'{protocol}://{filename}', content, parser=True) with open(filename) as fh: data = fh.read() self.assertEqual('{"hello": "world"}', data)
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_create_json(self): filename = 'tmp/sample.json' path = pathlib.Path(filename) cabinets.create(path, {'hello': 'world'}) with open(filename) as fh: data = json.load(fh) self.assertEqual({'hello': 'world'}, data)
def test_create_json_no_parser(self): protocol, filename = 'file', 'tmp/sample.json' content = bytes('{"test":"bytes"}', encoding="utf-8") cabinets.create(f'{protocol}://{filename}', content, parser=False) with open(filename) as fh: data = json.load(fh) self.assertEqual({"test": "bytes"}, data)
def test_create_plain_text(self): protocol, filename = 'file', 'tmp/sample.txt' content = "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" cabinets.create(f'{protocol}://{filename}', content) with open(filename) as fh: data = fh.read() self.assertEqual(content, data)
def test_create_json_custom_parser(self): protocol, filename = 'file', 'tmp/sample.json' content = {"hello": "world"} cabinets.create(f'{protocol}://{filename}', content, parser=MockTextParser) with open(filename) as fh: data = json.load(fh) self.assertDictEqual({"hello": "world", "mock": "parser"}, data)
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_create_plain_text_single_byte_encoding(self): protocol, filename = 'file', 'tmp/sample_single_byte.txt' content = "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" cabinets.create(f'{protocol}://{filename}', content, encoding='iso-8859-1') with open(filename) as fh: data = fh.read() self.assertEqual(content, data)
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_create_text_custom_parser_raises(self): with self.assertRaises(cabinets.CabinetError): cabinets.create('file://tmp/sample.txt', "foo", parser=str) with self.assertRaises(cabinets.CabinetError): cabinets.create('file://tmp/sample.txt', "foo", parser=1) with self.assertRaises(cabinets.CabinetError): cabinets.create('file://tmp/sample.txt', "foo", parser=None)
def test_create_json(self): protocol, filename = 'file', 'tmp/sample.json' cabinets.create(f'{protocol}://{filename}', {'hello': 'world'}) with open(filename) as fh: data = json.load(fh) self.assertEqual({'hello': 'world'}, data)
def test_create_json_default_parser_fails(self): protocol, filename = 'file', 'tmp/sample.json' content = bytes('{"test":"bytes"}', encoding="utf-8") with self.assertRaises(TypeError): cabinets.create(f'{protocol}://{filename}', content, parser=True)