Beispiel #1
0
    def test_invalid_story_wrong_type_story(self):


        roll = ['bike']
        with self.assertRaises(WrongFormatStoryError) as cm:
            cs.check_storyV2(122, roll)
        err = cm.exception
        self.assertEqual(str(err), "The story must be a string.")
Beispiel #2
0
    def test_invalid_story_wrong_type_diceset(self):


        storySet = "a b c"
        roll = "a"

        with self.assertRaises(WrongFormatDiceError) as cm:
            cs.check_storyV2(storySet,roll)
        err = cm.exception
        self.assertEqual(str(err), "The dice set must be a list.")
Beispiel #3
0
    def test_invalid_story_short_story(self):


        storySet = ""
        roll = ['bike', 'tulip', 'happy', 'cat', 'ladder', 'rain']

        with self.assertRaises(TooSmallStoryError) as cm:
            cs.check_storyV2(storySet,roll)
        err = cm.exception
        self.assertEqual(str(err), "The number of words of the story must greater or equal of the number of resulted faces.")
Beispiel #4
0
    def test_invalid_story(self):
        storySet = ""
        for elem in range(0,30):
            string = "a"
            storySet = storySet + string + " "
        roll = ['bike', 'tulip', 'happy', 'cat', 'ladder', 'rain']


        with self.assertRaises(InvalidStory) as cm:
            cs.check_storyV2(storySet, roll)
        err = cm.exception
        self.assertEqual(str(err), 'Invalid story')
Beispiel #5
0
    def test_invalid_story_long_story(self):


        storySet = ""
        for elem in range(0,2000):
            storySet = storySet + "a "

        roll = ['bike', 'tulip', 'happy', 'cat', 'ladder', 'rain']

        with self.assertRaises(TooLongStoryError) as cm:
            cs.check_storyV2(storySet,roll)
        err = cm.exception
        self.assertEqual(str(err), "The story is too long. The length is > 1000 characters.")
Beispiel #6
0
    def test_invalid_story_wrong_format_single_dice_error(self):


        die0 = 'bike moonandstars bag bird crying angry'
        die1 = 'tulip mouth caravan clock whale drink'
        die2 = 'happy coffee plate bus letter paws'
        die3 = 'cat pencil baloon bananas phone icecream'
        die4 = 'ladder car fire bang hat hamburger'
        die5 = 'rain heart glasses poo ball sun'
        storySet = die0 + " " + die1 + " " + die2 + " " + die3 + " " + die4 + " " + die5
        roll = ['bike','tulip','happy','cat','ladder',1]


        with self.assertRaises(WrongFormatSingleDiceError) as cm:
            cs.check_storyV2( storySet, roll)
        err = cm.exception
        self.assertEqual(str(err), "Every dice of the dice set must be a die.")
Beispiel #7
0
 def test_valid_story(self):
     die0 = 'bike moonandstars bag bird crying angry'
     die1 = 'tulip mouth caravan clock whale drink'
     die2 = 'happy coffee plate bus letter paws'
     die3 = 'cat pencil baloon bananas phone icecream'
     die4 = 'ladder car fire bang hat hamburger'
     die5 = 'rain heart glasses poo ball sun'
     storySet = die0 + " " + die1 + " " + die2 + " " + die3 + " " + die4 + " " + die5
     roll = ['bike','tulip','happy','cat','ladder','rain']
     self.assertEqual(cs.check_storyV2(storySet,roll),None)
Beispiel #8
0
def get_stories():
    if 'POST' == request.method:

        # Verify that the user exists

        code = -1
        message = ""

        arr = []

        allstories = db.session.query(Story).all()

        ris = send_request_reactions_service(arr=arr, allstories=allstories)

        if ris == -2:
            message = "One or more of the stories written by the user does not exists in the reaction database"
            result = jsonify({"result": -10, "message": message})
            return result
        elif ris == -1:
            message = "Timeout: the reactions service is not responding"
            result = jsonify({"result": -11, "message": message})
            return result
        else:

            userid = request.args.get('userid')

            if userid == None:
                message = "The userid is None"
                result = jsonify({
                    "result": -9,
                    "message": message,
                    "stories": arr
                })
                return result

            ris = send_request_user_service(userid)

            if ris == -2:
                message = "Timeout: the user service is not responding"
                result = jsonify({"result": -7, "message": message})
                return result

            elif ris == -1:

                # if it does not exist I set the params accordingly

                code = 0
                message = "The user does not exists"
                result = jsonify({
                    "result": code,
                    "message": message,
                    "stories": arr
                })
                return result
            else:

                # otherwise I create the story and I verify that it is valid

                # Create a new story
                new_story = Story()
                new_story.author_id = userid
                new_story.likes = 0
                new_story.dislikes = 0

                if request.json == None:
                    message = "Wrong parameters"
                    result = jsonify({
                        "result": -8,
                        "message": message,
                        "stories": arr
                    })
                    return result

                created_story = request.json['created_story']

                if len(created_story) != 2:
                    message = "Wrong parameters"
                    result = jsonify({
                        "result": -8,
                        "message": message,
                        "stories": arr
                    })
                    return result

                try:
                    text = created_story['text']
                    roll = created_story['roll']
                except KeyError:
                    message = "Wrong parameters"
                    result = jsonify({
                        "result": -8,
                        "message": message,
                        "stories": arr
                    })
                    return result

                if text == None or roll == None:
                    message = "Wrong parameters"
                    result = jsonify({
                        "result": -8,
                        "message": message,
                        "stories": arr
                    })
                    return result
                """

                if (type(roll) is str):
                    roll = roll.replace("[", "")
                    roll = roll.replace("]", "")
                    roll = roll.replace("'", "")
                    roll = roll.replace(" ", "")
                    aux = roll.split(",")
                    roll = aux

                """

                if not isinstance(roll, list):
                    message = "There was an error. Try again."
                    result = jsonify({
                        "result": -2,
                        "message": message,
                        "stories": arr
                    })
                    return result

                dicenumber = len(roll)

                try:
                    check_storyV2(text, roll)
                    new_story.text = text
                    new_story.roll = {'dice': roll}
                    new_story.dicenumber = dicenumber
                    db.session.add(new_story)
                    db.session.commit()
                    code = 1
                    message = "Story created"
                    arr.append(serializeble_story(new_story))
                except WrongFormatStoryError:
                    # print('ERROR 1', file=sys.stderr)
                    code = -1
                    message = "There was an error. Try again."

                except WrongFormatSingleDiceError:
                    # print('ERROR 5', file=sys.stderr)
                    code = -3
                    message = "There was an error. Try again."

                except TooLongStoryError:
                    # print('ERROR 3', file=sys.stderr)
                    code = -4
                    message = "The story is too long. The length is > 1000 characters."

                except TooSmallStoryError:
                    # print('ERROR 4', file=sys.stderr)
                    code = -5
                    message = "The number of words of the story must greater or equal of the number of resulted faces."

                except InvalidStory:
                    # print('ERROR 6', file=sys.stderr)
                    code = -6
                    message = "Invalid story. Try again!"

            result = jsonify({
                "result": code,
                "message": message,
                "stories": arr
            })
            return result

    elif 'GET' == request.method:

        allstories = db.session.query(Story).all()

        if len(allstories) > 0:

            arr = []
            ris = send_request_reactions_service(arr, allstories)

            if ris == -2:
                message = "One or more of the stories written by the user does not exists in the reaction database"
                result = jsonify({"result": -1, "message": message})
                return result
            elif ris == -1:
                message = "Timeout: the reactions service is not responding"
                result = jsonify({"result": -2, "message": message})
                return result
            else:
                message = "Stories"
                return jsonify({
                    "result": 1,
                    "stories": arr,
                    'message': message
                })
        else:
            message = "No stories"
            return jsonify({"result": 0, 'message': message})