def post(self): name = self.get_body_argument('name', None, True) hiragana = self.get_body_argument('hiragana', None, True) english_id = self.get_body_argument('english_id', None, True) pronunciation = self.get_body_argument('pronunciation', '', True) other_names = self.get_body_argument('other_names', '', True) num_titles = self.get_body_argument('num_titles', 0, True) star_entity = Star( name=name, hiragana=hiragana, english_id=english_id, pronunciation=pronunciation, other_names=other_names, num_titles=int(num_titles), ) service = StarService() self.set_header('Content-Type', 'application/json') try: star = yield service.create_with_entity(star_entity) self.write({"status": "ok", "uuid": star.uuid}) except EntityAlreadyExistsError: self.write({ "status": "failed", "errorMessage": "Star name {} exist.".format(name) })
def get(self, id): service = StarService() self.set_header('Content-Type', 'application/json') try: star = yield service.get(id) self.write({'status': 'ok', 'star': star.to_dict()}) except ResourceNotFoundError: self.write({"status": "failed", "errorMessage": "Not found."})
def get(self): star_entity = Star( name='fg', hiragana='fgdsb', english_id='fgdsb', pronunciation='fg', other_names='fgdsb', num_titles=0, ) service = StarService() try: star = yield service.create_with_entity(star_entity) self.write('Added {}'.format(star.uuid)) except EntityAlreadyExistsError: self.write('Star name {} exist.'.format(star_entity.name))
def test_get_by_name_not_found(self, mock_get): mock_get.return_value = gen.maybe_future(None) fake_name = 'abc' star = yield StarService().get_by_name(fake_name) mock_get.assert_called_once_with(fake_name) self.assertIsNone(star)
def get(self): service = StarService() self.set_header('Content-Type', 'application/json') if not self.get_argument("name", None, True): stars = yield service.get_all() self.write({ "status": "ok", "stars": [star.to_dict() for star in stars] }) else: star_name = self.get_argument("name", None, True) star = yield service.get_by_name(urllib.unquote(star_name)) if not star: self.write({"status": "failed", "errorMessage": "Not found."}) else: self.write({'status': 'ok', 'star': star.to_dict()})
def test_get_by_name(self, mock_get): fake_star = mock.Mock() mock_get.return_value = gen.maybe_future(fake_star) fake_name = 'abc' star = yield StarService().get_by_name(fake_name) mock_get.assert_called_once_with(fake_name) self.assertEquals(star, fake_star)
def test_user_dislike_star(http_client, base_url): user_entity = User(user_name='fg_dislike_star', password='******', email='fgdsb@fgdsb') user = yield UserService().create_with_entity(user_entity) star_entity = Star(name='fgdislike', hiragana='fgdsb', english_id='fgdsb') star = yield StarService().create_with_entity(star_entity) mock_service = gen_mock_service() expect(mock_service).like_star.with_args(user.uuid, star.uuid, False).and_return_future(None) response = yield http_client.fetch(base_url + '/api/users/' + user.uuid + '/like_stars/' + star.uuid + '?like=n') assert response.body == '{"status": "ok"}' assert response.code == httplib.OK
def test_get_star(http_client, base_url): star_entity = Star( name='super-fg', hiragana='fgdsb', english_id='fgdsb', pronunciation='fg', other_names='fgdsb', num_titles=0, ) star = yield StarService().create_with_entity(star_entity) mock_service = gen_mock_service() expect(mock_service).get.and_return_future(star) response = yield http_client.fetch(base_url + '/api/stars/' + star.uuid) result_object = json.loads(response.body) assert result_object['status'] == 'ok' assert result_object['star'] == star.to_dict() assert response.code == httplib.OK
def test_get_star_by_name(http_client, base_url): star_entity = Star( name='super-star-fg', hiragana='fgdsb', english_id='fgdsb', pronunciation='fg', other_names='fgdsb', num_titles=0, ) star = yield StarService().create_with_entity(star_entity) mock_service = gen_mock_service() expect(mock_service).get_by_name.with_args( star.name).and_return_future(star) response = yield http_client.fetch(base_url + '/api/stars?name=' + star.name) assert json.loads(response.body) == { "status": "ok", "star": star.to_dict() } assert response.code == httplib.OK
def gen_mock_service(): class_name = 'bootcamp.handlers.add_star.StarService' mock_service = StarService() service_class = patch_class(class_name) allow_constructor(service_class).and_return(mock_service) return mock_service