コード例 #1
0
    def test1(self):
        global _app
        tested_app = create_app(debug=True)
        _app = tested_app
        with tested_app.test_client() as client:
            with client.session_transaction() as sess:
                db.drop_all()
                db.create_all()

        with tested_app.test_client() as client:
            # Create user
            resp = client.post('/signup',
                       data=dict(firstname="admin_firstname",
                        lastname="admin_lastname",
                        email="*****@*****.**",
                        dateofbirth=1994,
                        password="******"),
                       follow_redirects=True)
            assert b'Index Page' in resp.data

            # login
            reply = login(client, '*****@*****.**', 'admin')
            assert b'Hi admin_firstname!' in reply.data

            # post a new story
            roll = json.dumps(["bike", "tulip", "happy", "cat", "ladder", "rain"])
            reply = client.post('/stories', data=dict(text="bike tulip happy cat ladder rain", roll=roll), follow_redirects=True)
            assert b'bike tulip happy cat ladder rain' in reply.data

            reply = client.get('/stories')
            assert b'bike tulip happy cat ladder rain' in reply.data

            # add reaction to a story
            reply = client.get('/stories/reaction/1/1')
            print(reply.data)
            assert b'Reaction created' in reply.data

            # add same reaction to a story - delete that reaction
            reply = client.get('/stories/reaction/1/1')
            assert b'Reaction removed!' in reply.data

            # add different reaction to a story
            reply = client.get('/stories/reaction/1/2')
            assert b'Reaction created' in reply.data

            # change the reaction to a story
            reply = client.get('/stories/reaction/1/1')
            assert b'Reaction changed!' in reply.data

            # add reaction to non-existing story
            reply = client.get('/stories/reaction/3/1')
            assert b'Story not exists!' in reply.data
コード例 #2
0
def database(app):
    '''
    Provides a reference to the temporary database in the app context. Use
    this instance instead of importing db from monolith.db.
    '''
    with app.app_context():
        db.create_all()

        _init_database(db)
        yield db

        db.drop_all()
        db.session.commit()
コード例 #3
0
    def test_json_wall(self):
        app = test_app.test_client()

        with app.session_transaction() as sess:
            db.drop_all()
            db.create_all()

        q = db.session.query(User).filter(User.email == '*****@*****.**')
        user = q.first()
        if user is None:
            example = User()
            example.firstname = 'userwall'
            example.lastname = 'theWall'
            example.email = '*****@*****.**'
            example.dateofbirth = datetime.datetime(2020, 10, 5)
            example.is_admin = True
            example.set_password('daddysflownacrosstheocean')
            db.session.add(example)
            db.session.commit()
            q = db.session.query(User).filter(User.email == '*****@*****.**')
            user = q.first()

        q = db.session.query(Story).filter(Story.author_id == user.id)
        story = q.first()
        if story is None:

            example = Story()
            example.text = 'We dont need no education We dont need no...All in all you re just another brick in the wall'
            example.likes = 42
            example.dislikes = 1
            example.dicenumber = 6
            example.author_id = user.id
            db.session.add(example)

            example = Story()
            example.text = 'Leaving just a memory...Snapshot in the family album...Daddy what else did you leave for me?'
            example.likes = 42
            example.dislikes = 0
            example.dicenumber = 4
            example.author_id = user.id
            db.session.add(example)

            db.session.commit()
            q = db.session.query(Story).filter(Story.author_id == user.id)

        stories = []
        thewalltest = Wall(user)
        for s in q:
            s: Story
            thewalltest.add_story(s)
            stories.append({
                'story_id': s.id,
                'text': s.text,
                'likes': s.likes,
                'dislikes': s.dislikes
            })

        reply = app.get('/thewall/' + str(user.id))
        body = json.loads(str(reply.data, 'utf8'))

        self.assertEqual(
            body,
            {
                "id": user.id,
                "firstname": user.firstname,
                "lastname": user.lastname,
                "email": user.email,
                "stories": stories  # thewalltest.stories
            })

        wall_repl = Wall()
        wall_repl.acquire_from_json(reply)
        self.assertEqual(thewalltest.id, wall_repl.id, "Json acquire fail")
コード例 #4
0
 def tearDown(self):
     with self.context:
         db.drop_all()
コード例 #5
0
 def tearDown(self) -> None:
     print("TEAR DOWN")
     db.session.remove()
     db.drop_all()
コード例 #6
0
    def test1(self):
        global _app
        tested_app = create_app(debug=True)
        _app = tested_app
        with tested_app.test_client() as client:
            with client.session_transaction() as sess:
                db.drop_all()
                db.create_all()

                # create user
                user_a = User()
                user_a.email = '*****@*****.**'
                user_a.set_password('test')
                db.session.add(user_a)
                db.session.commit()

                user_b = User()
                user_b.email = '*****@*****.**'
                user_b.set_password('test')
                db.session.add(user_b)
                db.session.commit()

                # create story
                story = Story()
                story.text = 'Text a'
                story.likes = 0
                story.dislikes = 0
                story.author_id = user_a.get_id()
                story.roll = {
                    'dice':
                    ['bike', 'tulip', 'happy', 'cat', 'ladder', 'rain']
                }
                db.session.add(story)
                db.session.commit()

                # add like
                like = Reaction()
                like.marked = 0
                like.story_id = story.id
                like.user_id = user_b.id
                like.type = 1
                db.session.add(story)
                db.session.commit()

                # create 1000 user and like the story
                users = []
                for i in range(10):
                    user = User()
                    user.email = 'user' + str(i) + '@test.com'
                    user.set_password('test')
                    db.session.add(user)
                    users.append(user)
                    db.session.add(story)

                db.session.commit()
                for u in users:
                    add_reaction(u.id, 1, 1)
                #
                # reaction = Reaction.query.count()
                # print(str(reaction)))
                res = update_reactions.apply_async(args=[1], time_limit=3)
                res.get()
                q = Story.query.filter_by(author_id=1).first()
                self.assertEqual(int(q.likes), 10)
コード例 #7
0
 def tearDown(self) -> None:
     db.session.remove()
     db.drop_all()