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_questionnaire_state(): model = QuestionnaireState("someuser", "somedata", "ce_sid", 1) new_model = create_model(model) assert new_model.__dict__ == model.__dict__ assert new_model.created_at >= NOW assert new_model.updated_at >= NOW
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_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_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)
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"])
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(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_put(self): model = QuestionnaireState("someuser", "data", "ce_sid", 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"]) self.assertEqual(model.collection_exercise_sid, put_data["collection_exercise_sid"]) self.assertEqual(model.submitted_at, put_data["submitted_at"])
def save( self, data: str, collection_exercise_sid: str, submitted_at: Optional[datetime] = None, expires_at: Optional[datetime] = None, ) -> None: compressed_data = snappy.compress(data) encrypted_data = self.encrypter.encrypt_data(compressed_data) questionnaire_state = QuestionnaireState( self._user_id, encrypted_data, collection_exercise_sid, QuestionnaireStore.LATEST_VERSION, submitted_at, expires_at, ) current_app.eq["storage"].put(questionnaire_state) # type: ignore
def _put_item(self, version, overwrite=True): model = QuestionnaireState("someuser", "data", "ce_sid", version) self.ddb.put(model, overwrite)
def test_delete(self): self._put_item(1) self._assert_item(1) model = QuestionnaireState("someuser", "data", "ce_sid", 1) self.ddb.delete(model) self._assert_item(None)
def test_set_date(): new_mixin = DateTimeSchemaMixin() questionnaire_store = new_mixin.set_date( QuestionnaireState("someuser", "somedata", "ce_sid", 1)) assert questionnaire_store.updated_at >= NOW
def test_questionnaire_state(self): new_model = self._test_model( QuestionnaireState("someuser", "somedata", 1)) self.assertGreaterEqual(new_model.created_at, NOW) self.assertGreaterEqual(new_model.updated_at, NOW)
def test_put_exclude_indexes(self, mock_entity): model = QuestionnaireState("someuser", "data", "ce_sid", 1) self.ds.put(model) put_call_args = mock_entity.call_args.kwargs self.assertIn("exclude_from_indexes", put_call_args) self.assertEqual(len(put_call_args["exclude_from_indexes"]), 5)