def test_delete(self):
        model = QuestionnaireState(USER_ID, STATE_DATA, VERSION)

        with mock.patch('app.storage.dynamo_api.delete_item') as delete_item:
            data_access.delete(model)

        self.assertEqual(delete_item.call_args[0][1], {'user_id': USER_ID})
    def test_delete(self):
        model = QuestionnaireState('someuser', 'data', 1)
        s3_api.delete(model)

        self.mock_client.delete_object.assert_called_once_with(
            Bucket=self._app.config['EQ_QUESTIONNAIRE_STATE_TABLE_NAME'],
            Key=model.user_id)
示例#3
0
    def save(self, data):
        compressed_data = snappy.compress(data)
        encrypted_data = self.encrypter.encrypt_data(compressed_data)
        questionnaire_state = QuestionnaireState(
            self._user_id, encrypted_data, QuestionnaireStore.LATEST_VERSION)

        current_app.eq["storage"].put(questionnaire_state)
    def test_retry(self):
        model = QuestionnaireState("someuser", "data", 1)

        self.mock_client.put = mock.Mock(side_effect=[
            exceptions.InternalServerError("error"), mock.DEFAULT
        ])
        self.ds.put(model, True)
        assert self.mock_client.put.call_count > 1
    def test_put_without_overwrite(self):
        model = QuestionnaireState("someuser", "data", 1)

        with self.assertRaises(NotImplementedError) as exception:
            self.ds.put(model, False)

        self.assertEqual(exception.exception.args[0],
                         "Unique key checking not supported")
    def test_put(self):
        model = QuestionnaireState(USER_ID, STATE_DATA, VERSION)

        with mock.patch('app.storage.dynamodb_api.put') as put:
            data_access.put(model)

        self.assertEqual(put.call_args[0][0], model)
        self.assertTrue(put.call_args[0][1])
示例#7
0
    def test_delete(self):
        model = QuestionnaireState('someuser', 'data', 1)
        gc_datastore_api.delete(model)

        self.assertEqual(self.mock_client.key.call_args[0][1], model.user_id)

        m_key = self.mock_client.key.return_value

        self.mock_client.delete.assert_called_once_with(m_key)
    def test_put_rds_no_overwrite(self):
        model = QuestionnaireState(USER_ID, STATE_DATA, VERSION)
        setattr(model, '_use_dynamo', False)

        with mock.patch('app.data_model.models.db.session.add', side_effect=IntegrityError('', '', '')) as add, \
            self.assertRaises(ItemAlreadyExistsError):
            data_access.put(model, overwrite=False)

        self.assertTrue(add.called)
    def test_delete(self):
        model = QuestionnaireState("someuser", "data", 1)
        self.ds.delete(model)

        self.assertEqual(self.mock_client.key.call_args[0][1], model.user_id)

        m_key = self.mock_client.key.return_value

        self.mock_client.delete.assert_called_once_with(m_key)
示例#10
0
def _save_state_data(user_id,
                     data,
                     state_version=QuestionnaireStore.LATEST_VERSION):
    encryption = StorageEncryption(user_id, "mock", "mock")

    state_data = encryption.encrypt_data(data)

    questionnaire_state = QuestionnaireState(user_id, state_data,
                                             state_version)
    current_app.eq["storage"].put(questionnaire_state)
    def test_put(self):
        model = QuestionnaireState("someuser", "data", 1)

        self.ds.put(model, True)

        put_data = self.mock_client.put.call_args[0][0]

        self.assertEqual(model.user_id, put_data["user_id"])
        self.assertEqual(model.state_data, put_data["state_data"])
        self.assertEqual(model.version, put_data["version"])
示例#12
0
    def test_put(self):
        model = QuestionnaireState('someuser', 'data', 1)

        gc_datastore_api.put(model, False)

        put_data = self.mock_client.put.call_args[0][0]

        self.assertEqual(model.user_id, put_data['user_id'])
        self.assertEqual(model.state_data, put_data['state_data'])
        self.assertEqual(str(model.version), put_data['version'])
    def test_get_by_key(self):
        model = QuestionnaireState('someuser', 'data', 1)
        model_data, _ = QuestionnaireStateSchema(strict=True).dump(model)

        self.mock_redis.get.return_value = json.dumps(model_data)

        returned_model = redis_api.get_by_key(QuestionnaireState, 'someuser')

        self.assertEqual(model.user_id, returned_model.user_id)
        self.assertEqual(model.state_data, returned_model.state_data)
        self.assertEqual(model.version, returned_model.version)
    def test_delete(self):
        m_blob = mock.Mock()
        self.mock_bucket.blob.return_value = m_blob

        model = QuestionnaireState('someuser', 'data', 1)
        gcs_api.delete(model)

        key = '{}/someuser'.format(
            self._app.config['EQ_QUESTIONNAIRE_STATE_TABLE_NAME'])
        self.mock_bucket.blob.assert_called_once_with(key)
        m_blob.delete.assert_called_once()
    def test_delete_rds(self):
        model = QuestionnaireState(USER_ID, STATE_DATA, VERSION)
        setattr(model, '_use_dynamo', False)

        with mock.patch('app.data_model.models.db.session.delete') as delete:
            data_access.delete(model)

        self.assertTrue(delete.called)

        rds_model = delete.call_args[0][0]
        self.assertEqual(rds_model.user_id, USER_ID)
    def test_put_propogate_dynamo_exception(self):
        model = QuestionnaireState(USER_ID, STATE_DATA, VERSION)

        side_effect = ClientError({'Error': {
            'Code': 'RandomError'
        }}, 'PutItem')
        with mock.patch('app.storage.dynamo_api.put_item', side_effect=side_effect) as put_item, \
            self.assertRaises(ClientError):
            data_access.put(model, overwrite=False)

        self.assertFalse(put_item.call_args[1]['overwrite'])
    def test_put_no_overwrite(self):
        model = QuestionnaireState(USER_ID, STATE_DATA, VERSION)

        side_effect = ClientError(
            {'Error': {
                'Code': 'ConditionalCheckFailedException'
            }}, 'PutItem')
        with mock.patch('app.storage.dynamo_api.put_item', side_effect=side_effect) as put_item, \
            self.assertRaises(ItemAlreadyExistsError):
            data_access.put(model, overwrite=False)

        self.assertFalse(put_item.call_args[1]['overwrite'])
    def test_put(self):
        m_blob = mock.Mock()
        self.mock_bucket.blob.return_value = m_blob

        model = QuestionnaireState('someuser', 'data', 1)

        gcs_api.put(model, False)

        put_data = json.loads(m_blob.upload_from_string.call_args[0][0])

        self.assertEqual(model.user_id, put_data['user_id'])
        self.assertEqual(model.state_data, put_data['state_data'])
        self.assertEqual(str(model.version), put_data['version'])
    def test_get_by_key(self):
        m_blob = mock.Mock()
        self.mock_bucket.blob.return_value = m_blob

        model = QuestionnaireState('someuser', 'data', 1)
        model_data, _ = QuestionnaireStateSchema(strict=True).dump(model)
        m_blob.download_as_string.return_value = json.dumps(model_data)

        returned_model = gcs_api.get_by_key(QuestionnaireState, 'someuser')

        self.assertEqual(model.user_id, returned_model.user_id)
        self.assertEqual(model.state_data, returned_model.state_data)
        self.assertEqual(model.version, returned_model.version)
    def test_get_by_key(self):
        model = QuestionnaireState("someuser", "data", 1)
        model_data = QuestionnaireStateSchema().dump(model)

        m_entity = google_datastore.Entity()
        m_entity.update(model_data)
        self.mock_client.get.return_value = m_entity

        returned_model = self.ds.get_by_key(QuestionnaireState, "someuser")

        self.assertEqual(model.user_id, returned_model.user_id)
        self.assertEqual(model.state_data, returned_model.state_data)
        self.assertEqual(model.version, returned_model.version)
    def test_get_by_key(self):
        dynamo_item = QuestionnaireState(USER_ID, STATE_DATA, VERSION)

        with mock.patch('app.storage.dynamodb_api.get_by_key',
                        return_value=dynamo_item) as get_by_key:
            model = data_access.get_by_key(QuestionnaireState, USER_ID)

        self.assertEqual(get_by_key.call_args[0][0], QuestionnaireState)
        self.assertEqual(get_by_key.call_args[0][1], USER_ID)

        self.assertEqual(model.user_id, USER_ID)
        self.assertEqual(model.state_data, STATE_DATA)
        self.assertEqual(model.version, VERSION)
    def test_put_overwrite(self):
        model = QuestionnaireState('someuser', 'data', 1)

        redis_api.put(model, False)

        key, data = self.mock_redis.setnx.call_args[0]
        put_data = json.loads(data)

        self.assertEqual(key, model.user_id)

        self.assertEqual(model.user_id, put_data['user_id'])
        self.assertEqual(model.state_data, put_data['state_data'])
        self.assertEqual(str(model.version), put_data['version'])
    def test_put_rds(self):
        model = QuestionnaireState(USER_ID, STATE_DATA, VERSION)
        setattr(model, '_use_dynamo', False)

        with mock.patch('app.data_model.models.db.session.merge') as merge:
            data_access.put(model)

        self.assertTrue(merge.called)

        rds_model = merge.call_args[0][0]
        self.assertEqual(rds_model.user_id, USER_ID)
        self.assertEqual(rds_model.state, STATE_DATA)
        self.assertEqual(rds_model.version, VERSION)
    def test_put(self):
        model = QuestionnaireState(USER_ID, STATE_DATA, VERSION)

        with mock.patch('app.storage.dynamo_api.put_item') as put_item:
            data_access.put(model)

        self.assertEqual(put_item.call_args[0][1], 'user_id')

        dynamo_item = put_item.call_args[0][2]
        self.assertEqual(dynamo_item['user_id'], USER_ID)
        self.assertEqual(dynamo_item['state_data'], STATE_DATA)
        self.assertEqual(dynamo_item['version'], VERSION)

        self.assertTrue(put_item.call_args[1]['overwrite'])
    def add_or_update(self, data, version):
        compressed_data = snappy.compress(data)
        encrypted_data = self.encrypter.encrypt_data(compressed_data)
        questionnaire_state = self._find_questionnaire_state()
        if questionnaire_state:
            logger.debug('updating questionnaire data', user_id=self._user_id)
            questionnaire_state.state_data = encrypted_data
            questionnaire_state.version = version
        else:
            logger.debug('creating questionnaire data', user_id=self._user_id)
            questionnaire_state = QuestionnaireState(self._user_id,
                                                     encrypted_data, version)

        data_access.put(questionnaire_state)
    def test_put(self):
        model = QuestionnaireState('someuser', 'data', 1)

        s3_api.put(model, True)

        self.assertEqual(self.mock_client.put_object.call_args[1]['Key'],
                         model.user_id)

        data = self.mock_client.put_object.call_args[1]['Body']
        put_data = json.loads(data)

        self.assertEqual(model.user_id, put_data['user_id'])
        self.assertEqual(model.state_data, put_data['state_data'])
        self.assertEqual(str(model.version), put_data['version'])
示例#27
0
    def test_get_by_key(self):
        model = QuestionnaireState('someuser', 'data', 1)
        model_data, _ = QuestionnaireStateSchema(strict=True).dump(model)

        m_entity = datastore.Entity()
        m_entity.update(model_data)
        self.mock_client.get.return_value = m_entity

        returned_model = gc_datastore_api.get_by_key(QuestionnaireState,
                                                     'someuser')

        self.assertEqual(model.user_id, returned_model.user_id)
        self.assertEqual(model.state_data, returned_model.state_data)
        self.assertEqual(model.version, returned_model.version)
    def add_or_update(self, data, version):
        encrypted_data = self.encrypter.encrypt_data(data)
        encrypted_data_json = json.dumps({'data': encrypted_data})
        questionnaire_state = self._find_questionnaire_state()
        if questionnaire_state:
            logger.debug('updating questionnaire data', user_id=self._user_id)
            questionnaire_state.state_data = encrypted_data_json
            questionnaire_state.version = version
        else:
            logger.debug('creating questionnaire data', user_id=self._user_id)
            questionnaire_state = QuestionnaireState(self._user_id,
                                                     encrypted_data_json,
                                                     version)

        data_access.put(questionnaire_state)
    def test_get_by_key(self):
        model = QuestionnaireState('someuser', 'data', 1)
        model_data, _ = QuestionnaireStateSchema(strict=True).dump(model)

        self.mock_client.get_object.return_value = {
            'Body': io.StringIO(json.dumps(model_data))
        }

        returned_model = s3_api.get_by_key(QuestionnaireState, 'someuser')

        self.mock_client.get_object.assert_called_once_with(
            Bucket=self._app.config['EQ_QUESTIONNAIRE_STATE_TABLE_NAME'],
            Key=model.user_id)

        self.assertEqual(model.user_id, returned_model.user_id)
        self.assertEqual(model.state_data, returned_model.state_data)
        self.assertEqual(model.version, returned_model.version)
示例#30
0
    def test_put(self):
        model = QuestionnaireState('someuser', 'data', 1)

        row = self.mock_instance.table.return_value.row
        set_cell = row.return_value.set_cell

        bigtable_api.put(model, False)

        cell_calls = set_cell.call_args_list

        self.assertIn(mock.call('cf1', 'user_id', model.user_id), cell_calls)
        self.assertIn(mock.call('cf1', 'state_data', model.state_data),
                      cell_calls)
        self.assertIn(mock.call('cf1', 'version', str(model.version)),
                      cell_calls)

        row.return_value.commit.assert_called_once()