def test_predicate(self): file_repository = InMemoryRepository( pk_factory=Vars.key('name'), initial_data=self.sample_content, ) read_sample = list(file_repository.stream(Vars.key("name") == "Fuu")) self.assertEqual(read_sample, [self.sample_content[0]])
def create(self, entity: Entity) -> FileRepository: serializer = Serializer.toml() if entity.singleton else Serializer.yaml() if entity.singleton: file_strategy = SingleFileStrategy( os.path.join(self.base_path, '{}.{}'.format(entity.name, serializer.default_extension)) ) elif entity.parent is None: file_strategy = MultipleFileStrategy( os.path.join(self.base_path, '{}/{{pk}}/main.{}'.format(entity.name, serializer.default_extension)), { 'pk': Vars.key(entity.pk) } ) else: file_strategy = MultipleFileStrategy( os.path.join(self.base_path, '{}/{{parent_id}}/{}.{}'.format( entity.parent.name, entity.name, serializer.default_extension )), { 'parent_id': Vars.key(entity.parent.parent_id), } ) serializer_strategy = OnePerFileStrategy(serializer) if entity.singleton else ManyPerFileStrategy(serializer) pk_factory = (lambda _: 0) if entity.singleton else Vars.key(entity.pk) strategy = ComposedStrategy( file_strategy=file_strategy, serialize_strategy=serializer_strategy, pk_factory=pk_factory, ) return FileRepository(strategy)
def visitBoolean_literal(self, ctx: RSQLParser.Boolean_literalContext): if ctx.TRUE(): return Vars.const(True) elif ctx.FALSE(): return Vars.const(False) else: raise NotImplementedError(ctx)
def visitNumeric_literal(self, ctx: RSQLParser.Numeric_literalContext): textual = ctx.getText() if ctx.DECIMAL_LITERAL(): return Vars.const(float(textual)) elif ctx.INT_LITERAL(): return Vars.const(int(textual)) else: raise NotImplementedError(ctx)
def test_update_all(self): file_repository = InMemoryRepository( pk_factory=Vars.key('name'), initial_data=self.sample_content, ) file_repository.update( Setters.key('name', Vars.const('Updated ') + Vars.key('name')), )
def test_set_and(self): expected = { 'a': 2, 'b': 3, } obj = { 'a': 1 } setter = Setters.key('a', Vars.const(2)) & Setters.key('b', Vars.const(3)) result = setter(obj) self.assertEqual(result, expected)
def test_write_sample_toml(self): original = path.join(path.dirname(__file__), 'multiple/by-parent/') with tempfile.TemporaryDirectory() as output: file_repository = FileRepository.mutable_multiple_file( '{}/{{language}}.toml'.format(output), Serializer.toml(), Vars.key('language') + ':' + Vars.key('version').cast(str)) file_repository.save(self.sample_content) result = filecmp.dircmp(output, original, ignore=['.yaml', '.json']) self.assertFalse(result.diff_files)
def test_set_with_dynamic(self): expected = { 'a': 2, 'b': 1, } obj = { 'a': 1 } setter = Setters.key('b', Vars.key('a')) & Setters.key('a', Vars.const(2)) result = setter(obj) self.assertEqual(result, expected)
def test_read_sample_toml(self): file_repository = FileRepository.multiple_file( path.join(path.dirname(__file__), 'multiple/by-parent/{language}.toml'), Serializer.toml()) read_sample = sorted(list(file_repository.stream()), key=Vars.key("version")) self.assertListEqual(read_sample, self.sample_content)
def test_read_sample(self): file_repository = InMemoryRepository( pk_factory=Vars.key('name'), initial_data=self.sample_content, ) read_sample = list(file_repository.stream()) self.assertEqual(read_sample, self.sample_content)
def test_read_sample_toml(self): file_repository = FileRepository.one_per_file( path.join(path.dirname(__file__), 'multiple/by-name/{name}.toml'), Serializer.toml() ) read_sample = sorted(list(file_repository.stream()), key=Vars.key('name')) self.assertEqual(read_sample, self.sample_content)
def test_remove_all(self): file_repository = InMemoryRepository( pk_factory=Vars.key('name'), initial_data=self.sample_content, ) file_repository.remove(Predicates.ANY) read_sample = list(file_repository.stream()) self.assertEqual(read_sample, [])
def test_write_sample_toml(self): original = path.join(path.dirname(__file__), 'single/sample.toml') with tempfile.NamedTemporaryFile() as output: file_repository = FileRepository.mutable_single_file( output.name, Serializer.toml(), Vars.key('name'), ) file_repository.save(self.sample_content) self.assertTrue(filecmp.cmp(original, output.name, shallow=False))
def test_write_sample_toml(self): original = path.join(path.dirname(__file__), 'multiple/by-name/') with tempfile.TemporaryDirectory() as output: file_repository = FileRepository.mutable_one_per_file( '{}/{{name}}.toml'.format(output), Serializer.toml(), Vars.key('name') ) file_repository.save(self.sample_content) result = filecmp.dircmp(output, original) self.assertFalse(result.diff_files)
def test_remove_all_toml(self): original = path.join(path.dirname(__file__), 'single/sample.toml') with tempfile.NamedTemporaryFile() as output: shutil.copy2(original, output.name) file_repository = FileRepository.mutable_single_file( output.name, Serializer.toml(), Vars.key('name'), ) file_repository.remove(Predicates.ANY) content = list(file_repository.stream()) self.assertEqual(content, [])
def test_eq_same_const(self): var = Vars.const('const') == Vars.const('const') optimized_var = var.optimize() self.assertTrue(Const.is_true(optimized_var))
def test_eq_same_key(self): var = Vars.key('id') == Vars.key('id') optimized_var = var.optimize() self.assertTrue(Const.is_true(optimized_var))
def test_false_and(self): var = Vars.const(False) & Vars.key('any') optimized_var = var.optimize() self.assertTrue(Const.is_false(optimized_var))
def test_or_true(self): var = Vars.key('any') | Vars.const(True) optimized_var = var.optimize() self.assertTrue(Const.is_true(optimized_var))
def test_ne_same_const(self): var = Vars.const('const') != Vars.const('const') optimized_var = var.optimize() self.assertTrue(Const.is_false(optimized_var))
def test_write_sample(self): file_repository = InMemoryRepository(pk_factory=Vars.key('name'), ) file_repository.save(self.sample_content) read_sample = list(file_repository.stream()) self.assertEqual(read_sample, self.sample_content)
def test_ne_same_key(self): var = Vars.key('id') != Vars.key('id') optimized_var = var.optimize() self.assertTrue(Const.is_false(optimized_var))
def test_ne_same_keys(self): var = Vars.keys(('parent', 'id',)) != Vars.keys(('parent', 'id',)) optimized_var = var.optimize() self.assertTrue(Const.is_false(optimized_var))
def visitArray_value(self, ctx: RSQLParser.Array_valueContext): return Vars.const(tuple([self.visit(value) for value in ctx.value()]))
def default_args_factory(template: str) -> Dict[str, Callable[[Any], Any]]: formatter = Formatter() parsed = formatter.parse(template) keys = [key for prefix, key, _, _ in parsed if key is not None] return dict((k, Vars.key(k),) for k in keys)
def visitString_literal(self, ctx: RSQLParser.String_literalContext): if ctx.STRING_LITERAL(): return Vars.const(ast.literal_eval(ctx.getText())) else: raise NotImplementedError(ctx)
def test_set_with_const(self): expected = {'a': 2} obj = {'a': 1} setter = Setters.key('a', Vars.const(2)) result = setter(obj) self.assertEqual(result, expected)
def visitExpression(self, ctx: RSQLParser.ExpressionContext): if ctx.IDENTIFIER(): return Vars.key(ctx.getText()) return self.visit(ctx.value())