Repository: proteanhq/protean

Issue:
Handle escaped quotes in values
`Dog.create(id=4, name='d\'Artagnan', owner='John', age=5)`

results in:
```
src/protean/core/entity.py:691: in create
    entity._validate_unique()
src/protean/core/entity.py:804: in _validate_unique
    if self.exists(excludes, **{filter_key: lookup_value}):
src/protean/core/entity.py:669: in exists
    return bool(results)
src/protean/core/entity.py:331: in __bool__
    return bool(self.all())
src/protean/core/entity.py:293: in all
    results = adapter._filter_objects(self._criteria, self._page, self._per_page, order_by)
src/protean/impl/repository/dict_repo.py:104: in _filter_objects
    items = list(self._filter(criteria, self.conn['data'][self.model_name]).values())
src/protean/impl/repository/dict_repo.py:84: in _filter
    input_db = self._evaluate_lookup(child[0], child[1], negated, input_db)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <protean.impl.repository.dict_repo.Adapter object at 0x10572bb70>, key = 'name', value = "d'Artagnan", negated = False
db = {1: {'age': 2, 'id': 1, 'name': 'Athos', 'owner': 'John'}, 2: {'age': 3, 'id': 2, 'name': 'Porthos', 'owner': 'John'}, 3: {'age': 4, 'id': 3, 'name': 'Aramis', 'owner': 'John'}}

    def _evaluate_lookup(self, key, value, negated, db):
        """Extract values from DB that match the given criteria"""
        results = {}
        for record_key, record_value in db.items():
            match = True
    
            stripped_key, lookup_class = self._extract_lookup(key)
            lookup = lookup_class(record_value[stripped_key], value)
    
            if negated:
                match &= not eval(lookup.as_expression())
            else:
>               match &= eval(lookup.as_expression())
E                 File "<string>", line 1
E                   'Athos' == 'd'Artagnan'
E                                        ^
E               SyntaxError: invalid syntax

src/protean/impl/repository/dict_repo.py:63: SyntaxError
```


Discussion:

Patch:
diff --git a/src/protean/impl/repository/dict_repo.py b/src/protean/impl/repository/dict_repo.py
index a2eaac7f..7f8f01ce 100644
--- a/src/protean/impl/repository/dict_repo.py
+++ b/src/protean/impl/repository/dict_repo.py
@@ -329,19 +329,23 @@ class DefaultDictLookup(BaseLookup):
     def process_source(self):
         """Return source with transformations, if any"""
         if isinstance(self.source, str):
-            return "'%s'" % self.source
+            # Replace single and double quotes with escaped single-quote
+            self.source = self.source.replace("'", "\'").replace('"', "\'")
+            return "\"{source}\"".format(source=self.source)
         return self.source
 
     def process_target(self):
         """Return target with transformations, if any"""
         if isinstance(self.target, str):
-            return "'%s'" % self.target
+            # Replace single and double quotes with escaped single-quote
+            self.target = self.target.replace("'", "\'").replace('"', "\'")
+            return "\"{target}\"".format(target=self.target)
         return self.target
 
     def as_expression(self):
-        return '%s %s %s' % (self.process_source(),
-                             operators[self.lookup_name],
-                             self.process_target())
+        return '{source} {op} {target}'.format(source=self.process_source(),
+                                               op=operators[self.lookup_name],
+                                               target=self.process_target())
 
 
 @DictProvider.register_lookup


Test patch:
diff --git a/tests/core/test_entity.py b/tests/core/test_entity.py
index 8dc00c60..00f5fd25 100644
--- a/tests/core/test_entity.py
+++ b/tests/core/test_entity.py
@@ -276,7 +276,7 @@ class TestEntity:
         Dog.create(id=1, name='Athos', owner='John', age=2)
         Dog.create(id=2, name='Porthos', owner='John', age=3)
         Dog.create(id=3, name='Aramis', owner='John', age=4)
-        Dog.create(id=4, name='dArtagnan', owner='John', age=5)
+        Dog.create(id=4, name='d\'Artagnan', owner='John', age=5)
 
         # Perform update
         updated_count = Dog.query.filter(age__gt=3).update(owner='Jane')
@@ -298,7 +298,7 @@ class TestEntity:
         Dog.create(id=1, name='Athos', owner='John', age=2)
         Dog.create(id=2, name='Porthos', owner='John', age=3)
         Dog.create(id=3, name='Aramis', owner='John', age=4)
-        Dog.create(id=4, name='dArtagnan', owner='John', age=5)
+        Dog.create(id=4, name='d\'Artagnan', owner='John', age=5)
 
         # Perform update
         updated_count = Dog.query.filter(age__gt=3).update_all({'owner': 'Jane'})
@@ -320,7 +320,7 @@ class TestEntity:
         Dog.create(id=1, name='Athos', owner='John', age=2)
         Dog.create(id=2, name='Porthos', owner='John', age=3)
         Dog.create(id=3, name='Aramis', owner='John', age=4)
-        Dog.create(id=4, name='dArtagnan', owner='John', age=5)
+        Dog.create(id=4, name='d\'Artagnan', owner='John', age=5)
 
         # Perform update
         updated_count = Dog.query.filter(age__gt=3).update_all(owner='Jane')
@@ -559,7 +559,7 @@ class TestEntity:
         Dog.create(id=1, name='Athos', owner='John', age=2)
         Dog.create(id=2, name='Porthos', owner='John', age=3)
         Dog.create(id=3, name='Aramis', owner='John', age=4)
-        Dog.create(id=4, name='dArtagnan', owner='John', age=5)
+        Dog.create(id=4, name='d\'Artagnan', owner='John', age=5)
 
         dogs = Dog.query.all()
         assert dogs.total == 4
@@ -574,7 +574,7 @@ class TestEntity:
         Dog.create(id=1, name='Athos', owner='John', age=2)
         Dog.create(id=2, name='Porthos', owner='John', age=3)
         Dog.create(id=3, name='Aramis', owner='John', age=4)
-        Dog.create(id=4, name='dArtagnan', owner='John', age=5)
+        Dog.create(id=4, name='d\'Artagnan', owner='John', age=5)
 
         # Perform update
         deleted_count = Dog.query.filter(age__gt=3).delete_all()
@@ -599,7 +599,7 @@ class TestEntity:
         Dog.create(id=1, name='Athos', owner='John', age=2)
         Dog.create(id=2, name='Porthos', owner='John', age=3)
         Dog.create(id=3, name='Aramis', owner='John', age=4)
-        Dog.create(id=4, name='dArtagnan', owner='John', age=5)
+        Dog.create(id=4, name='d\'Artagnan', owner='John', age=5)
 
         # Perform update
         deleted_count = Dog.query.filter(age__gt=3).delete()
@@ -627,6 +627,20 @@ class TestEntity:
         query = Dog.query.filter(owner='John')
         assert isinstance(query, QuerySet)
 
+    def test_escaped_quotes_in_values(self):
+        """Test that escaped quotes in values are handled properly"""
+
+        Dog.create(name='Athos', owner='John', age=2)
+        Dog.create(name='Porthos', owner='John', age=3)
+        Dog.create(name='Aramis', owner='John', age=4)
+
+        dog1 = Dog.create(name="d'Artagnan1", owner='John', age=5)
+        dog2 = Dog.create(name="d\'Artagnan2", owner='John', age=5)
+        dog3 = Dog.create(name="d\"Artagnan3", owner='John', age=5)
+        dog4 = Dog.create(name='d\"Artagnan4', owner='John', age=5)
+
+        assert all(dog is not None for dog in [dog1, dog2, dog3, dog4])
+    
     def test_override(self):
         """Test overriding methods from Entity"""
         class ImmortalDog(Entity):