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)
Example #2
0
 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]])
Example #3
0
 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_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_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)
Example #6
0
 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)
Example #7
0
 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)
Example #8
0
 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, [])
Example #9
0
 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_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)
Example #11
0
 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)
Example #12
0
 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_ne_same_key(self):
     var = Vars.key('id') != Vars.key('id')
     optimized_var = var.optimize()
     self.assertTrue(Const.is_false(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 visitExpression(self, ctx: RSQLParser.ExpressionContext):
     if ctx.IDENTIFIER():
         return Vars.key(ctx.getText())
     return self.visit(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)
Example #19
0
 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)