def _assert_create(self, response): # status code の確認 self.assertEqual(response.status_code, 200) # response に含まれるデータの正誤 obj = json.loads(response.data) model = PEDoc.get_by_key_name(obj['_docId']) self.assertEqual(obj['_docId'], model.key().name()) self.assertEqual(obj['_createdAt'], model.createdAt.isoformat()) self.assertEqual(obj['_updatedAt'], model.updatedAt.isoformat()) self.assertEqual(obj['docType'], 'myDoc') self.assertEqual(obj['boolean'], [True, False]) self.assertEqual(obj['double'], 20.0) self.assertEqual(obj['string'], 'Hoge') # response に含まれないデータの正誤 self.assertEqual(model.createdBy.email(), gae_test_base.LOGGED_IN_USER) self.assertEqual(model.updatedBy.email(), gae_test_base.LOGGED_IN_USER) self.assertEqual(model.schemaVersion, 1) self.assertEqual(model.version, 1)
def test_post_a_new_entity_by_json(self): # entity 数を POST の前後で確認して保存の有無 self.assertEqual(PEDoc.all().count(), 0) response = self.CLIENT.post('/_api/myDoc?_doc=%s' % self.DAT) self.assertEqual(PEDoc.all().count(), 1) self._assert_create(response) # with _docId. response = self.CLIENT.post('/_api/myDoc?_doc=%s' % self.DAT_WITH_DOC_ID) self._assert_create(response) # response に含まれるデータにおいて、_docId のみ確認 doc_id = json.loads(response.data)['_docId'] self.assertEqual(doc_id, '1234567890abcdefghijklmnopqrstuv') # update syntax response = \ self.CLIENT.post('/_api/myDoc/vutsrqponmlkjihgfedcba0987654321' + '?_doc=%s' % self.DAT) self._assert_create(response) # response に含まれるデータにおいて、_docId のみ確認 doc_id_up = json.loads(response.data)['_docId'] self.assertEqual(doc_id_up, 'vutsrqponmlkjihgfedcba0987654321')
def get(self, doc_type): models = PEDoc.gql('WHERE docType = :1', doc_type) try: result = {doc_type: [PEDoc.convert_dict(i) for i in models]} except AttributeError: return Response(status=404) return render_escaped_json_response(result)
def get(self, doc_type, doc_id): model = PEDoc.get_by_key_name(doc_id) try: result = PEDoc.convert_dict(model) except AttributeError: return Response(status=404) return render_escaped_json_response(result)
def _assert_update( self, first_response, second_response, key, value, ): # status code の確認 self.assertEqual(second_response.status_code, 200) # データに変更が無いもの obj = json.loads(first_response.data) updated_obj = json.loads(second_response.data) updated_model = PEDoc.get_by_key_name(updated_obj['_docId']) self.assertEqual(updated_obj['_createdAt'], obj['_createdAt']) self.assertEqual(updated_obj['_docId'], obj['_docId']) self.assertEqual(updated_obj['docType'], 'myDoc') self.assertEqual(updated_model.createdBy.email(), gae_test_base.LOGGED_IN_USER) self.assertEqual(updated_model.updatedBy.email(), gae_test_base.LOGGED_IN_USER) self.assertEqual(updated_model.schemaVersion, 1) self.assertEqual(updated_model.version, 1) # データに変更が有るもの self.assertTrue(updated_obj['_updatedAt'] > obj['_updatedAt']) self.assertEqual(updated_obj[key], value)
def put(self, doc_type): try: (doc_values, doc_id) = self._get_doc_values_and_id(self.request) except NotFound: return Response(status=404) if doc_id and PEDoc.get_by_key_name(doc_id) is None: return Response(status=404) return self._after(doc_id, doc_type, doc_values)
def put(self, doc_type, doc_id): if PEDoc.get_by_key_name(doc_id) is None: return Response(status=404) try: doc_values = self._get_doc_values(self.request) except NotFound: return Response(status=404) return self._after(doc_id, doc_type, doc_values)
def _after( self, doc_id, doc_type, doc_values, ): if doc_id and PEDoc.get_by_key_name(doc_id): try: result = PEDoc.update_and_get_by_dict(doc_id, doc_type, doc_values) except ConflictError: return Response(status=409) except InternalServerError: return Response(status=500) else: try: result = PEDoc.create_and_get_by_dict(doc_id, doc_type, doc_values) except InternalServerError: return Response(status=500) return render_escaped_json_response(result)
def delete(self, doc_type, doc_id): updated_at = self.request.args.get('_checkUpdatesAfter') if updated_at is None: updated_at = self.request.args.get('_updatedAt') try: result = PEDoc.delete_in_txn(doc_id, updated_at) except NotFound: return Response(status=404) except ConflictError: return Response(status=409) except InternalServerError: return Response(status=500) return Response(status=200)
def _assert_delete(self, response, docId): # status code & データの削除を確認 self.assertEqual(response.status_code, 200) self.assertEqual(PEDoc.get_by_key_name(docId), None)