Beispiel #1
0
    def test_dao_save_all_saves_multiple_dtos(self):
        # All other behavior of save_all() tested via save() since they share
        # implementation and save() provides a simpler interface.
        first_dto = models.ContentChunkDTO({
            'content_type': self.content_type,
            'contents': self.contents,
            'id': self.id,
            'resource_id': self.resource_id,
            'supports_custom_tags': self.supports_custom_tags,
            'type_id': self.type_id,
        })
        second_dto = models.ContentChunkDTO({
            'content_type':
            'second_' + self.content_type,
            'contents':
            'second_' + self.contents,
            'id':
            self.id + 1,
            'resource_id':
            'resource:second_id',
            'supports_custom_tags':
            self.supports_custom_tags,
            'type_id':
            'second_' + self.type_id,
        })
        keys = models.ContentChunkDAO.save_all([first_dto, second_dto])
        saved_dtos = [
            models.ContentChunkDAO._make_dto(entity) for entity in db.get(keys)
        ]

        self.assertEqual(2, len(keys))
        self.assertEqual(2, len(saved_dtos))
        self.assert_fuzzy_equal(first_dto, saved_dtos[0])
        self.assert_fuzzy_equal(second_dto, saved_dtos[1])
    def test_put_returns_200_and_updates_existing_entity(self):
        models.ContentChunkDAO.save(
            models.ContentChunkDTO({
                'content_type': 'old_' + self.content_type,
                'contents': 'old_' + self.contents,
                'resource_id': self.document_id,
                'type_id': self.type_id,
            }))
        params = self._make_params({
            'contents': self.contents,
            'document_id': self.document_id,
            'type_id': self.type_id,
            'xsrf_token': self.xsrf_token,
        })
        old_dto = models.ContentChunkDAO.get_by_uid(self.uid)[0]

        self.assertEqual('old_' + self.contents, old_dto.contents)
        self.assertEqual('old_' + self.content_type, old_dto.content_type)

        response = self.testapp.put(core_tags._GOOGLE_DRIVE_TAG_PATH,
                                    params=params)
        matches = models.ContentChunkDAO.get_by_uid(self.uid)
        created = matches[0]

        self.assert_200_response(response)
        self.assertEqual(1, len(matches))
        self.assertEqual(self.contents, created.contents)
        self.assertEqual(self.document_id, created.resource_id)
        self.assertEqual(self.type_id, created.type_id)
Beispiel #3
0
    def test_dao_save_updates_existing_object_and_does_not_populate_cache(
            self):
        self.assertIsNone(models.MemcacheManager.get(self.memcache_key))

        dto = models.ContentChunkDTO({
            'content_type': self.content_type,
            'contents': self.contents,
            'id': self.id,
            'resource_id': self.resource_id,
            'supports_custom_tags': self.supports_custom_tags,
            'type_id': self.type_id,
        })
        key = models.ContentChunkDAO.save(dto)
        saved_dto = models.ContentChunkDAO._make_dto(db.get(key))

        self.assert_fuzzy_equal(dto, saved_dto)
        self.assertIsNone(models.MemcacheManager.get(self.memcache_key))

        dto.content_type = 'new_content_type'
        dto.contents = 'new_contents'
        dto.supports_custom_tags = True
        dto.uid = 'new_system_id:new_resource:id'
        models.ContentChunkDAO.save(dto)
        saved_dto = models.ContentChunkDAO._make_dto(db.get(key))

        self.assert_fuzzy_equal(dto, saved_dto)
        self.assertIsNone(models.MemcacheManager.get(self.memcache_key))
Beispiel #4
0
    def _save_content(self, meta, content, content_chunk_id):
        # load both
        dto = drive_models.DriveSyncDAO.load(meta.file_id)
        content_chunk = models.ContentChunkDAO.get(content_chunk_id)

        # update sync entity
        dto.dict['title'] = meta.title
        dto.version = meta.version
        dto.sync_succeeded()

        # instantiate new content chunk if necessary
        if not content_chunk:
            content_chunk = models.ContentChunkDTO({
                'type_id': meta.type,
                'resource_id': meta.file_id,
            })

        # populate content
        {
            drive_api_client.SHEET_TYPE: self._save_sheet_content,
            drive_api_client.DOC_TYPE: self._save_doc_content,
        }[dto.type](content_chunk, content)

        # Save both
        models.ContentChunkDAO.save(content_chunk)
        drive_models.DriveSyncDAO.save(dto)
Beispiel #5
0
    def setup_schedule_for_file(
            self, file_id, availability='private', synced=False):
        # pylint: disable=protected-access
        client_mock = mocks._APIClientWrapperMock()
        meta = client_mock.get_file_meta(
            file_id)
        # pylint: enable=protected-access
        dto = drive_models.DriveSyncDAO.load_or_new(file_id)
        dto.dict.update({
            'id': meta.key,
            'title': meta.title,
            'type': meta.type,
            'sync_interval': 'hour',
            'availability': availability,
        })

        if synced:
            content_chunk = models.ContentChunkDTO({
                'type_id': meta.type,
                'resource_id': meta.file_id,
            })

            if meta.type == 'sheet':
                content_chunk.contents = json.dumps(client_mock.get_sheet_data(
                    file_id).to_json())
                content_chunk.content_type = 'application/json'
            else:
                content_chunk.contents = client_mock.get_doc_as_html(file_id)
                content_chunk.content_type = 'text/html'

            models.ContentChunkDAO.save(content_chunk)
            dto.sync_succeeded()

        drive_models.DriveSyncDAO.save(dto)
Beispiel #6
0
 def setUp(self):
     super(GoogleDriveTagRendererTest, self).setUp()
     self.contents = 'contents_value'
     self.resource_id = 'resource_id_value'
     self.type_id = 'type_id_value'
     dto = models.ContentChunkDTO({
         'content_type': 'text/html',
         'contents': self.contents,
         'resource_id': self.resource_id,
         'type_id': self.type_id,
     })
     self.uid = models.ContentChunkDAO.make_uid(self.type_id,
                                                self.resource_id)
     models.ContentChunkDAO.save(dto)
     self.dto = models.ContentChunkDAO.get_by_uid(self.uid)
Beispiel #7
0
    def test_dao_save_creates_new_object_and_populates_cache(self):
        self.assertIsNone(models.MemcacheManager.get(self.memcache_key))

        key = models.ContentChunkDAO.save(
            models.ContentChunkDTO({
                'content_type': self.content_type,
                'contents': self.contents,
                'id': self.id,
                'resource_id': self.resource_id,
                'supports_custom_tags': self.supports_custom_tags,
                'type_id': self.type_id,
            }))
        expected_dto = models.ContentChunkDAO._make_dto(db.get(key))

        self.assert_fuzzy_equal(expected_dto,
                                models.MemcacheManager.get(self.memcache_key))
    def test_get_returns_200_with_first_chunk_if_multiple_matches(self):
        models.ContentChunkDAO.save(
            models.ContentChunkDTO({
                'content_type': 'text/html',
                'contents': 'other contents',
                'resource_id': self.resource_id,
                'type_id': self.type_id,
            }))
        response = self.testapp.get(core_tags._GOOGLE_DRIVE_TAG_RENDERER_PATH,
                                    params={
                                        'resource_id': self.resource_id,
                                        'type_id': self.type_id,
                                    })

        self.assertEqual(2, len(models.ContentChunkDAO.get_by_uid(self.uid)))
        self.assert_200_response(self.contents, response)
    def test_dao_get_returns_cached_entity(self):
        key = models.ContentChunkDAO.save(models.ContentChunkDTO({
            'content_type': self.content_type,
            'contents': self.contents,
            'resource_id': self.resource_id,
            'supports_custom_tags': self.supports_custom_tags,
            'type_id': self.type_id,
        }))
        entity = db.get(key)
        entity.contents = 'patched'
        patched_dto = models.ContentChunkDAO._make_dto(entity)
        models.MemcacheManager.set(self.memcache_key, patched_dto)
        from_datastore = models.ContentChunkEntity.get_by_id(self.id)
        from_cache = models.MemcacheManager.get(self.memcache_key)

        self.assert_fuzzy_equal(patched_dto, from_cache)
        self.assertNotEqual(patched_dto.contents, from_datastore.contents)
Beispiel #10
0
    def test_dao_get_returns_datastore_entity_and_populates_cache(self):
        self.assertIsNone(models.MemcacheManager.get(self.memcache_key))

        key = models.ContentChunkDAO.save(
            models.ContentChunkDTO({
                'content_type': self.content_type,
                'contents': self.contents,
                'resource_id': self.resource_id,
                'supports_custom_tags': self.supports_custom_tags,
                'type_id': self.type_id,
            }))
        expected_dto = models.ContentChunkDAO._make_dto(db.get(key))
        from_datastore = models.ContentChunkEntity.get_by_id(self.id)
        from_cache = models.MemcacheManager.get(self.memcache_key)

        self.assert_fuzzy_equal(
            expected_dto, models.ContentChunkDAO._make_dto(from_datastore))
        self.assert_fuzzy_equal(expected_dto, from_cache)
    def test_dao_delete_deletes_entity_and_cached_dto(self):
        key = models.ContentChunkDAO.save(models.ContentChunkDTO({
            'content_type': self.content_type,
            'contents': self.contents,
            'id': self.id,
            'resource_id': self.resource_id,
            'supports_custom_tags': self.supports_custom_tags,
            'type_id': self.type_id,
        }))
        entity = db.get(key)
        dto = models.ContentChunkDAO.get(key.id())

        self.assertIsNotNone(entity)
        self.assertIsNotNone(dto)

        models.ContentChunkDAO.delete(key.id())
        entity = db.get(key)
        dto = models.ContentChunkDAO.get(key.id())

        self.assertIsNone(entity)
        self.assertIsNone(dto)
Beispiel #12
0
    def _save_content_chunk(self, contents, type_id, resource_id):
        key = None
        uid = models.ContentChunkDAO.make_uid(type_id, resource_id)
        matches = models.ContentChunkDAO.get_by_uid(uid)

        if not matches:
            key = models.ContentChunkDAO.save(
                models.ContentChunkDTO({
                    'content_type': 'text/html',
                    'contents': contents,
                    'resource_id': resource_id,
                    'type_id': type_id,
                }))
        else:
            # There is a data race in the DAO -- it's possible to create two
            # entries at the same time with the same UID. If that happened,
            # use the first one saved.
            dto = matches[0]
            dto.contents = contents
            dto.content_type = 'text/html'
            key = models.ContentChunkDAO.save(dto)

        return key