Repository: scieloorg/catalogmanager

Issue:
Corrigir put_attachment
![captura de tela 2018-04-16 as 11 31 01](https://user-images.githubusercontent.com/505143/38815168-ad0744cc-4169-11e8-91fe-38c5e5842940.png)

Bug:
- Não precisar executar `self.db_manager.read` porque dentro de self.db_manager.put_attachment já está sendo executado isso.

- `self.db_manager.read` deve ser executado após `self.db_manager.put_attachment` para ter os dados atualizado com o resultado da ação de `self.db_manager.put_attachment`


relacionado com #2 e #6

Comments:

Patch:
diff --git a/catalog_persistence/databases.py b/catalog_persistence/databases.py
index cce5884..954921e 100644
--- a/catalog_persistence/databases.py
+++ b/catalog_persistence/databases.py
@@ -87,7 +87,9 @@ class InMemoryDBManager(BaseDBManager):
         return doc
 
     def update(self, id, document):
-        self.database.update({id: document})
+        _document = self.read(id)
+        _document.update(document)
+        self.database.update({id: _document})
 
     def delete(self, id):
         self.read(id)
@@ -151,11 +153,7 @@ class InMemoryDBManager(BaseDBManager):
         return list(doc.get(self._attachments_key, {}).keys())
 
     def attachment_exists(self, id, file_id):
-        doc = self.read(id)
-        return (
-            doc.get(self._attachments_key) and
-            doc[self._attachments_key].get(file_id)
-        )
+        return file_id in self.list_attachments(id)
 
 
 class CouchDBManager(BaseDBManager):
@@ -255,11 +253,7 @@ class CouchDBManager(BaseDBManager):
         return list(doc.get(self._attachments_key, {}).keys())
 
     def attachment_exists(self, id, file_id):
-        doc = self.read(id)
-        return (
-            doc.get(self._attachments_key) and
-            doc[self._attachments_key].get(file_id)
-        )
+        return file_id in self.list_attachments(id)
 
 
 class DatabaseService:
@@ -398,17 +392,18 @@ class DatabaseService:
         Erro:
         DocumentNotFound: documento não encontrado na base de dados.
         """
-        read_record = self.db_manager.read(document_id)
         self.db_manager.put_attachment(document_id,
                                        file_id,
                                        content,
                                        file_properties)
+        document = self.db_manager.read(document_id)
         document_record = {
-            'document_id': read_record['document_id'],
-            'document_type': read_record['document_type'],
-            'created_date': read_record['created_date'],
+            'document_id': document['document_id'],
+            'document_type': document['document_type'],
+            'content': document['content'],
+            'created_date': document['created_date']
         }
-        self._register_change(document_record, ChangeType.UPDATE, file_id)
+        self.update(document_id, document_record)
 
     def get_attachment(self, document_id, file_id):
         """


Test patch:
diff --git a/catalog_persistence/tests/test_databases.py b/catalog_persistence/tests/test_databases.py
index 575d003..9916c25 100644
--- a/catalog_persistence/tests/test_databases.py
+++ b/catalog_persistence/tests/test_databases.py
@@ -201,22 +201,48 @@ def test_put_attachment_to_document(setup, database_service, xml_test):
     )
 
 
-@patch.object(DatabaseService, '_register_change')
-def test_put_attachment_to_document_register_change(mocked_register_change,
+@patch.object(DatabaseService, 'update')
+def test_put_attachment_to_document_update(mocked_update,
                                                     setup,
                                                     database_service,
                                                     xml_test):
-    article_record = get_article_record({'Test': 'Test7'})
+    article_record = get_article_record({'Test': 'Test9'})
     database_service.register(
         article_record['document_id'],
         article_record
     )
+    attachment_id = "filename"
+    database_service.put_attachment(
+        document_id=article_record['document_id'],
+        file_id=attachment_id,
+        content=xml_test.encode('utf-8'),
+        file_properties={
+            'content_type': "text/xml",
+            'content_size': len(xml_test)
+        }
+    )
     record = database_service.read(article_record['document_id'])
     document_record = {
         'document_id': record['document_id'],
         'document_type': record['document_type'],
-        'created_date': record['created_date'],
+        'content': record['content'],
+        'created_date': record['created_date']
     }
+    mocked_update.assert_called_with(
+        article_record['document_id'], document_record)
+
+
+def test_put_attachment_to_document_update_dates(setup,
+                                        database_service,
+                                        xml_test):
+    article_record = get_article_record({'Test': 'Test9'})
+    database_service.register(
+        article_record['document_id'],
+        article_record
+    )
+    record_v1 = database_service.read(article_record['document_id'])
+    dates_v1 = record_v1.get('created_date'), record_v1.get('updated_date')
+
     attachment_id = "filename"
     database_service.put_attachment(
         document_id=article_record['document_id'],
@@ -228,9 +254,11 @@ def test_put_attachment_to_document_register_change(mocked_register_change,
         }
     )
 
-    mocked_register_change.assert_called_with(document_record,
-                                              ChangeType.UPDATE,
-                                              attachment_id)
+    record_v2 = database_service.read(article_record['document_id'])
+    dates_v2 = record_v2.get('created_date'), record_v2.get('updated_date')
+    assert dates_v1[0] == dates_v2[0]
+    assert dates_v1[1] is None
+    assert dates_v2[1] > dates_v1[0]
 
 
 def test_put_attachment_to_document_not_found(setup,
@@ -250,55 +278,6 @@ def test_put_attachment_to_document_not_found(setup,
     )
 
 
-@patch.object(DatabaseService, '_register_change')
-def test_update_attachment_register_change_if_it_exists(mocked_register_change,
-                                                        setup,
-                                                        database_service,
-                                                        xml_test):
-    article_record = get_article_record({'Test': 'Test9'})
-    database_service.register(
-        article_record['document_id'],
-        article_record
-    )
-    attachment_id = "filename"
-    database_service.put_attachment(
-        document_id=article_record['document_id'],
-        file_id=attachment_id,
-        content=xml_test.encode('utf-8'),
-        file_properties={
-            'content_type': "text/xml",
-            'content_size': len(xml_test)
-        }
-    )
-    record = database_service.read(article_record['document_id'])
-    document_record = {
-        'document_id': record['document_id'],
-        'document_type': record['document_type'],
-        'created_date': record['created_date'],
-    }
-    database_service.put_attachment(
-        document_id=article_record['document_id'],
-        file_id=attachment_id,
-        content=xml_test.encode('utf-8'),
-        file_properties={
-            'content_type': "text/xml",
-            'content_size': len(xml_test)
-        }
-    )
-
-    record_check = dict(
-        database_service.db_manager.database[article_record['document_id']]
-    )
-    assert record_check is not None
-    assert database_service.db_manager.attachment_exists(
-        article_record['document_id'],
-        attachment_id
-    )
-    mocked_register_change.assert_called_with(document_record,
-                                              ChangeType.UPDATE,
-                                              attachment_id)
-
-
 def test_read_document_with_attachments(setup, database_service, xml_test):
     article_record = get_article_record({'Test': 'Test10'})
     file_id = "href_file"