def test_interests(self): if self.skip_tests: return interests = ["interest_number_{0}".format(i) for i in range(1, 9)] [InterestService.create(interest_name=i) for i in interests] interests = InterestService.get_all_interests().to_primitive( )["interests"] self.assertEqual(len(interests), 8) # Create new interest. new_interest = InterestService.create( interest_name="test_new_interest") interests = InterestService.get_all_interests().to_primitive( )["interests"] self.assertEqual(len(interests), 9) # Update interest new_interest.name = "new_interest_name" updated_interest = InterestService.update(new_interest.id, new_interest) self.assertEqual(new_interest.name, updated_interest.name) # Associate users with interest. iids, uids = TestInterestService.relationship_user( interests, self.test_user.id, [1, 4, 5]) self.assertEqual(uids, iids) # Try again. iids, uids = TestInterestService.relationship_user( interests, self.test_user.id, [1, 2]) self.assertEqual(uids, iids) # Validate unexistent interest. with self.assertRaises(NotFound): InterestService.create_or_update_user_interests( self.test_user.id, [0]) # Associate projects with interest. iids, pids = TestInterestService.relationship_project( interests, self.test_project.id, [1, 2]) self.assertEqual(pids, iids) # Validate unexistent interest. with self.assertRaises(NotFound): InterestService.create_or_update_project_interests( self.test_project.id, [0]) # Delete one by one. for interest in interests: InterestService.delete(interest["id"]) interests = InterestService.get_all_interests().to_primitive( )["interests"] self.assertEqual(len(interests), 0)
def relationship_user(interests, user_id, ids): interest_ids = [interests[c]["id"] for c in ids] user_interests = InterestService.create_or_update_user_interests( user_id, interest_ids) user_interests_ids = [ i["id"] for i in user_interests.to_primitive()["interests"] ] return interest_ids, user_interests_ids
def post(self): """ Creates a relationship between user and interests --- tags: - interests produces: - application/json parameters: - in: header name: Authorization description: Base64 encoded session token required: true type: string default: Token sessionTokenHere== - in: body name: body required: true description: JSON object for creating/updating user and interests relationships schema: properties: interests: type: array items: type: integer responses: 200: description: New user interest relationship created 400: description: Invalid Request 401: description: Unauthorized - Invalid credentials 500: description: Internal Server Error """ try: data = request.get_json() user_interests = InterestService.create_or_update_user_interests( tm.authenticated_user_id, data["interests"]) return user_interests.to_primitive(), 200 except ValueError as e: return {"Error": str(e)}, 400 except NotFound: return {"Error": "Interest not Found"}, 404 except Exception as e: error_msg = f"User relationship POST - unhandled error: {str(e)}" current_app.logger.critical(error_msg) return {"Error": error_msg}, 500