def setup(self):
        self.graph = create_app(testing=True)
        self.user_store = self.graph.user_store
        self.follower_relationship_store = self.graph.follower_relationship_store

        self.username1 = "glen.runciter"
        self.user1 = User(
            username=self.username1,
            email="*****@*****.**",
            first_name="Glen",
            last_name="Runciter",
            title="Big Boss",
            bio="Ubik-- get it today!",
        )

        self.username2 = "joe.chip"
        self.user2 = User(
            username=self.username2,
            email="*****@*****.**",
            first_name="Joe",
            last_name="Chip",
            title="Technician",
            bio="Ubik-- get it today!",
        )

        self.context = SessionContext(self.graph)
        self.context.recreate_all()
        self.context.open()

        with transaction():
            self.user1.create()
            self.user2.create()
예제 #2
0
    def test_create_duplicate(self):
        user1 = User(
            username=self.username,
            email=self.email,
            first_name=self.first_name,
            last_name=self.last_name,
            title=self.title,
            bio=self.bio,
        )
        user2 = User(
            username=self.username,
            email=self.email,
            first_name=self.first_name,
            last_name=self.last_name,
            title=self.title,
            bio=self.bio,
        )

        with transaction():
            self.user_store.create(user1)

        assert_that(
            calling(self.user_store.create).with_args(user2),
            raises(DuplicateModelError),
        )
예제 #3
0
    def setup(self):
        self.graph = create_app(testing=True)
        self.client = self.graph.flask.test_client()
        recreate_all(self.graph)

        self.username1 = "glen.runciter"
        self.user1 = User(
            username=self.username1,
            email="*****@*****.**",
            first_name="Glen",
            last_name="Runciter",
            title="Big Boss",
            bio="Ubik-- get it today!",
        )

        self.username2 = "joe.chip"
        self.user2 = User(
            username=self.username2,
            email="*****@*****.**",
            first_name="Joe",
            last_name="Chip",
            title="Technician",
            bio="Ubik-- get it today!",
        )

        with SessionContext(self.graph), transaction():
            self.user1.create()
            self.user2.create()

        self.user1_follow_user2 = FollowerRelationship(
            id=new_object_id(),
            user_id=self.user2.id,
            follower_id=self.user1.id,
        )

        self.user2_tweet_content1 = """
            Friends, this is clean-up time and we’re discounting all our silent,
            electric Ubiks by this much money. Yes, we’re throwing away the blue-book.
            And remember: every Ubik on our lot has been used only as directed.
        """
        self.user2_tweet_content2 = """
            The best way to ask for beer is to sing out Ubik.
            Made from select hops, choice water, slow-aged for perfect flavor,
            Ubik is the nation’s number-one choice in beer. Made only in Cleveland.
        """

        with SessionContext(self.graph), transaction():
            self.user1_follow_user2.create()

            self.user2_tweet1 = Tweet(
                id=new_object_id(),
                user_id=self.user2.id,
                tweet_content=self.user2_tweet_content1,
            ).create()
            self.user2_tweet2 = Tweet(
                id=new_object_id(),
                user_id=self.user2.id,
                tweet_content=self.user2_tweet_content2,
            ).create()
예제 #4
0
    def setup(self):
        self.graph = create_app(testing=True)
        self.tweet_store = self.graph.tweet_store
        self.user_store = self.graph.user_store

        self.username = "******"
        self.user = User(
            username=self.username,
            email="*****@*****.**",
            first_name="Joe",
            last_name="Chip",
            title="Technician",
            bio="Ubik-- get it today!",
        )

        self.context = SessionContext(self.graph)
        self.context.recreate_all()
        self.context.open()

        with transaction():
            self.user_store.create(self.user)

        self.tweet_content = """
            Friends, this is clean-up time and we’re discounting all our silent,
            electric Ubiks by this much money. Yes, we’re throwing away the blue-book.
            And remember: every Ubik on our lot has been used only as directed.
        """

        self.tweet = Tweet(
            user_id=self.user.id,
            tweet_content=self.tweet_content,
        )
예제 #5
0
    def setup(self):
        self.graph = create_app(testing=True)
        self.client = self.graph.flask.test_client()
        recreate_all(self.graph)

        self.username1 = "glen.runciter"
        self.email1 = "*****@*****.**"
        self.first_name1 = "Glen"
        self.last_name1 = "Runciter"
        self.title1 = "Big Boss"
        self.bio1 = "Ubik-- get it today!"

        self.user1 = User(
            id=new_object_id(),
            username=self.username1,
            email=self.email1,
            first_name=self.first_name1,
            last_name=self.last_name1,
            title=self.title1,
            bio=self.bio1,
        )
예제 #6
0
    def setup(self):
        self.graph = create_app(testing=True)
        self.client = self.graph.flask.test_client()
        recreate_all(self.graph)

        self.username1 = "glen.runciter"
        self.user1 = User(
            username=self.username1,
            email="*****@*****.**",
            first_name="Glen",
            last_name="Runciter",
            title="Big Boss",
            bio="Ubik-- get it today!",
        )

        self.username2 = "joe.chip"
        self.user2 = User(
            username=self.username2,
            email="*****@*****.**",
            first_name="Joe",
            last_name="Chip",
            title="Technician",
            bio="Ubik-- get it today!",
        )

        with SessionContext(self.graph), transaction():
            self.user1.create()
            self.user2.create()

        self.user1_follow_user2 = FollowerRelationship(
            id=new_object_id(),
            user_id=self.user1.id,
            follower_id=self.user2.id,
        )
        self.user2_follow_user1 = FollowerRelationship(
            id=new_object_id(),
            user_id=self.user2.id,
            follower_id=self.user1.id,
        )
예제 #7
0
    def test_create(self):
        new_user = User(
            username=self.username,
            email=self.email,
            first_name=self.first_name,
            last_name=self.last_name,
            title=self.title,
            bio=self.bio,
        )

        with transaction():
            self.user_store.create(new_user)

        retrieved_user = self.user_store.retrieve(new_user.id)
        assert_that(retrieved_user, is_(equal_to(new_user)))
예제 #8
0
class TestUserRelationRoutes:
    def setup(self):
        self.graph = create_app(testing=True)
        self.client = self.graph.flask.test_client()
        recreate_all(self.graph)

        self.username1 = "glen.runciter"
        self.user1 = User(
            username=self.username1,
            email="*****@*****.**",
            first_name="Glen",
            last_name="Runciter",
            title="Big Boss",
            bio="Ubik-- get it today!",
        )

        self.username2 = "joe.chip"
        self.user2 = User(
            username=self.username2,
            email="*****@*****.**",
            first_name="Joe",
            last_name="Chip",
            title="Technician",
            bio="Ubik-- get it today!",
        )

        with SessionContext(self.graph), transaction():
            self.user1.create()
            self.user2.create()

        self.user1_follow_user2 = FollowerRelationship(
            id=new_object_id(),
            user_id=self.user2.id,
            follower_id=self.user1.id,
        )

        self.user2_tweet_content1 = """
            Friends, this is clean-up time and we’re discounting all our silent,
            electric Ubiks by this much money. Yes, we’re throwing away the blue-book.
            And remember: every Ubik on our lot has been used only as directed.
        """
        self.user2_tweet_content2 = """
            The best way to ask for beer is to sing out Ubik.
            Made from select hops, choice water, slow-aged for perfect flavor,
            Ubik is the nation’s number-one choice in beer. Made only in Cleveland.
        """

        with SessionContext(self.graph), transaction():
            self.user1_follow_user2.create()

            self.user2_tweet1 = Tweet(
                id=new_object_id(),
                user_id=self.user2.id,
                tweet_content=self.user2_tweet_content1,
            ).create()
            self.user2_tweet2 = Tweet(
                id=new_object_id(),
                user_id=self.user2.id,
                tweet_content=self.user2_tweet_content2,
            ).create()

    def teardown(self):
        self.graph.postgres.dispose()

    def test_search_following_by_user(self):
        uri = "/api/v1/user/{}/following".format(self.user1.id)

        response = self.client.get(uri)

        assert_that(response.status_code, is_(equal_to(200)))
        data = loads(response.data)
        assert_that(len(data["items"]), is_(equal_to(1)))
        assert_that(
            data,
            has_entries(items=contains(
                has_entries(
                    id=str(self.user2.id),
                    username=self.user2.username,
                    email=self.user2.email,
                    firstName=self.user2.first_name,
                    lastName=self.user2.last_name,
                    title=self.user2.title,
                    bio=self.user2.bio,
                ), ), ))

        uri = "/api/v1/user/{}/following".format(self.user2.id)

        response = self.client.get(uri)

        assert_that(response.status_code, is_(equal_to(200)))
        data = loads(response.data)
        assert_that(len(data["items"]), is_(equal_to(0)))

    def test_search_followers_by_user(self):
        uri = "/api/v1/user/{}/followers".format(self.user2.id)

        response = self.client.get(uri)

        assert_that(response.status_code, is_(equal_to(200)))
        data = loads(response.data)
        assert_that(len(data["items"]), is_(equal_to(1)))
        assert_that(
            data,
            has_entries(items=contains(
                has_entries(
                    id=str(self.user1.id),
                    username=self.user1.username,
                    email=self.user1.email,
                    firstName=self.user1.first_name,
                    lastName=self.user1.last_name,
                    title=self.user1.title,
                    bio=self.user1.bio,
                ), ), ))

        uri = "/api/v1/user/{}/followers".format(self.user1.id)

        response = self.client.get(uri)

        assert_that(response.status_code, is_(equal_to(200)))
        data = loads(response.data)
        assert_that(len(data["items"]), is_(equal_to(0)))

    def test_search_tweets_by_user(self):
        uri = "/api/v1/user/{}/tweets".format(self.user2.id)

        response = self.client.get(uri)

        assert_that(response.status_code, is_(equal_to(200)))
        data = loads(response.data)
        assert_that(len(data["items"]), is_(equal_to(2)))
        assert_that(
            data,
            has_entries(items=contains(
                has_entries(
                    id=str(self.user2_tweet2.id),
                    userId=str(self.user2_tweet2.user_id),
                    tweetContent=self.user2_tweet2.tweet_content,
                ),
                has_entries(
                    id=str(self.user2_tweet1.id),
                    userId=str(self.user2_tweet1.user_id),
                    tweetContent=self.user2_tweet1.tweet_content,
                ),
            ), ))

        uri = "/api/v1/user/{}/tweets".format(self.user1.id)

        response = self.client.get(uri)

        assert_that(response.status_code, is_(equal_to(200)))
        data = loads(response.data)
        assert_that(len(data["items"]), is_(equal_to(0)))

    def test_search_feed_by_user(self):
        uri = "/api/v1/user/{}/feed".format(self.user1.id)

        response = self.client.get(uri)

        assert_that(response.status_code, is_(equal_to(200)))
        data = loads(response.data)
        assert_that(len(data["items"]), is_(equal_to(2)))
        assert_that(
            data,
            has_entries(items=contains(
                has_entries(
                    id=str(self.user2_tweet2.id),
                    userId=str(self.user2_tweet2.user_id),
                    tweetContent=self.user2_tweet2.tweet_content,
                ),
                has_entries(
                    id=str(self.user2_tweet1.id),
                    userId=str(self.user2_tweet1.user_id),
                    tweetContent=self.user2_tweet1.tweet_content,
                ),
            ), ))

        uri = "/api/v1/user/{}/feed".format(self.user2.id)

        response = self.client.get(uri)

        assert_that(response.status_code, is_(equal_to(200)))
        data = loads(response.data)
        assert_that(len(data["items"]), is_(equal_to(0)))
class TestFollowerRelationship:
    def setup(self):
        self.graph = create_app(testing=True)
        self.user_store = self.graph.user_store
        self.follower_relationship_store = self.graph.follower_relationship_store

        self.username1 = "glen.runciter"
        self.user1 = User(
            username=self.username1,
            email="*****@*****.**",
            first_name="Glen",
            last_name="Runciter",
            title="Big Boss",
            bio="Ubik-- get it today!",
        )

        self.username2 = "joe.chip"
        self.user2 = User(
            username=self.username2,
            email="*****@*****.**",
            first_name="Joe",
            last_name="Chip",
            title="Technician",
            bio="Ubik-- get it today!",
        )

        self.context = SessionContext(self.graph)
        self.context.recreate_all()
        self.context.open()

        with transaction():
            self.user1.create()
            self.user2.create()

    def teardown(self):
        self.context.close()
        self.graph.postgres.dispose()

    def test_create(self):
        new_follower_relationship = FollowerRelationship(
            user_id=self.user1.id,
            follower_id=self.user2.id,
        )

        with transaction():
            self.follower_relationship_store.create(new_follower_relationship)

        retrieved_follower_relationship = self.follower_relationship_store.retrieve(
            new_follower_relationship.id)
        assert_that(retrieved_follower_relationship,
                    is_(equal_to(new_follower_relationship)))

    def test_create_duplicate(self):
        follower_relationship1 = FollowerRelationship(
            user_id=self.user1.id,
            follower_id=self.user2.id,
        )
        follower_relationship2 = FollowerRelationship(
            user_id=self.user1.id,
            follower_id=self.user2.id,
        )

        with transaction():
            self.follower_relationship_store.create(follower_relationship1)

        assert_that(
            calling(self.follower_relationship_store.create).with_args(
                follower_relationship2),
            raises(DuplicateModelError),
        )
예제 #10
0
class TestTweetRoutes:
    def setup(self):
        self.graph = create_app(testing=True)
        self.client = self.graph.flask.test_client()
        recreate_all(self.graph)

        self.username1 = "glen.runciter"
        self.user1 = User(
            username=self.username1,
            email="*****@*****.**",
            first_name="Glen",
            last_name="Runciter",
            title="Big Boss",
            bio="Ubik-- get it today!",
        )
        self.user1_tweet_content1 = """
            Friends, this is clean-up time and we’re discounting all our silent,
            electric Ubiks by this much money. Yes, we’re throwing away the blue-book.
            And remember: every Ubik on our lot has been used only as directed.
        """
        self.user1_tweet_content2 = """
            The best way to ask for beer is to sing out Ubik.
            Made from select hops, choice water, slow-aged for perfect flavor,
            Ubik is the nation’s number-one choice in beer. Made only in Cleveland.
        """

        self.username2 = "joe.chip"
        self.user2 = User(
            username=self.username2,
            email="*****@*****.**",
            first_name="Joe",
            last_name="Chip",
            title="Technician",
            bio="Ubik-- get it today!",
        )
        self.user2_tweet_content1 = """
            Instant Ubik has all the fresh flavor of just-brewed drip coffee.
            Your husband will say, Christ, Sally, I used to think your coffee was only so-so.
            But now, wow! Safe when taken as directed.
        """

        with SessionContext(self.graph), transaction():
            self.user1.create()
            self.user2.create()

        self.user1_tweet1 = Tweet(
            id=new_object_id(),
            user_id=self.user1.id,
            tweet_content=self.user1_tweet_content1,
        )
        self.user1_tweet2 = Tweet(
            id=new_object_id(),
            user_id=self.user1.id,
            tweet_content=self.user1_tweet_content2,
        )
        self.user2_tweet1 = Tweet(
            id=new_object_id(),
            user_id=self.user2.id,
            tweet_content=self.user2_tweet_content1,
        )

    def teardown(self):
        self.graph.postgres.dispose()

    def test_search(self):
        with SessionContext(self.graph), transaction():
            self.user1_tweet1.create()
            self.user1_tweet2.create()
            self.user2_tweet1.create()

        uri = "/api/v1/tweet"

        response = self.client.get(uri)

        assert_that(response.status_code, is_(equal_to(200)))
        data = loads(response.data)
        assert_that(len(data["items"]), is_(equal_to(3)))
        assert_that(
            data,
            has_entries(items=contains(
                has_entries(
                    id=str(self.user2_tweet1.id),
                    userId=str(self.user2_tweet1.user_id),
                    tweetContent=self.user2_tweet1.tweet_content,
                ),
                has_entries(
                    id=str(self.user1_tweet2.id),
                    userId=str(self.user1_tweet2.user_id),
                    tweetContent=self.user1_tweet2.tweet_content,
                ),
                has_entries(
                    id=str(self.user1_tweet1.id),
                    userId=str(self.user1_tweet1.user_id),
                    tweetContent=self.user1_tweet1.tweet_content,
                ),
            ), ))

        response = self.client.get(
            uri,
            query_string=dict(user_id=str(self.user1.id), ),
        )

        assert_that(response.status_code, is_(equal_to(200)))
        data = loads(response.data)
        assert_that(len(data["items"]), is_(equal_to(2)))
        assert_that(
            data,
            has_entries(items=contains(
                has_entries(
                    id=str(self.user1_tweet2.id),
                    userId=str(self.user1_tweet2.user_id),
                    tweetContent=self.user1_tweet2.tweet_content,
                ),
                has_entries(
                    id=str(self.user1_tweet1.id),
                    userId=str(self.user1_tweet1.user_id),
                    tweetContent=self.user1_tweet1.tweet_content,
                ),
            ), ))

    def test_create(self):
        uri = "/api/v1/tweet"

        with patch.object(self.graph.tweet_store, "new_object_id") as mocked:
            mocked.return_value = self.user1_tweet1.id
            response = self.client.post(uri,
                                        data=dumps({
                                            "userId":
                                            str(self.user1_tweet1.user_id),
                                            "tweetContent":
                                            self.user1_tweet1.tweet_content,
                                        }))

        assert_that(response.status_code, is_(equal_to(201)))
        data = loads(response.data)
        assert_that(
            data,
            has_entries(
                id=str(self.user1_tweet1.id),
                userId=str(self.user1_tweet1.user_id),
                tweetContent=self.user1_tweet1.tweet_content,
            ))

    def test_retrieve(self):
        with SessionContext(self.graph), transaction():
            self.user1_tweet1.create()

        uri = "/api/v1/tweet/{}".format(self.user1_tweet1.id)

        response = self.client.get(uri)

        data = loads(response.data)
        assert_that(
            data,
            has_entries(
                id=str(self.user1_tweet1.id),
                userId=str(self.user1_tweet1.user_id),
                tweetContent=self.user1_tweet1.tweet_content,
            ))

    def test_delete(self):
        with SessionContext(self.graph), transaction():
            self.user1_tweet1.create()

        uri = "/api/v1/tweet/{}".format(self.user1_tweet1.id)

        response = self.client.delete(uri)
        assert_that(response.status_code, is_(equal_to(204)))
예제 #11
0
    def test_search(self):
        self.username2 = "joe.chip"
        self.email2 = "*****@*****.**"
        self.first_name2 = "Joe"
        self.last_name2 = "Chip"
        self.title2 = "Technician"
        self.bio2 = "Ubik-- get it today!"

        self.user2 = User(
            id=new_object_id(),
            username=self.username2,
            email=self.email2,
            first_name=self.first_name2,
            last_name=self.last_name2,
            title=self.title2,
            bio=self.bio2,
        )

        with SessionContext(self.graph), transaction():
            self.user1.create()
            self.user2.create()

        uri = "/api/v1/user"

        response = self.client.get(uri)

        assert_that(response.status_code, is_(equal_to(200)))
        data = loads(response.data)
        assert_that(len(data["items"]), is_(equal_to(2)))
        assert_that(data, has_entries(
            items=contains(
                has_entries(
                    id=str(self.user1.id),
                    username=self.user1.username,
                    email=self.user1.email,
                    firstName=self.user1.first_name,
                    lastName=self.user1.last_name,
                    title=self.user1.title,
                    bio=self.user1.bio,
                ),
                has_entries(
                    id=str(self.user2.id),
                    username=self.user2.username,
                    email=self.user2.email,
                    firstName=self.user2.first_name,
                    lastName=self.user2.last_name,
                    title=self.user2.title,
                    bio=self.user2.bio,
                ),
            ),
        ))

        response = self.client.get(
            uri,
            query_string=dict(
                username=self.user1.username,
            ),
        )

        assert_that(response.status_code, is_(equal_to(200)))
        data = loads(response.data)
        assert_that(len(data["items"]), is_(equal_to(1)))
        assert_that(data, has_entries(
            items=contains(
                has_entries(
                    id=str(self.user1.id),
                    username=self.user1.username,
                    email=self.user1.email,
                    firstName=self.user1.first_name,
                    lastName=self.user1.last_name,
                    title=self.user1.title,
                    bio=self.user1.bio,
                ),
            ),
        ))
예제 #12
0
class TestUserRoutes:

    def setup(self):
        self.graph = create_app(testing=True)
        self.client = self.graph.flask.test_client()
        recreate_all(self.graph)

        self.username1 = "glen.runciter"
        self.email1 = "*****@*****.**"
        self.first_name1 = "Glen"
        self.last_name1 = "Runciter"
        self.title1 = "Big Boss"
        self.bio1 = "Ubik-- get it today!"

        self.user1 = User(
            id=new_object_id(),
            username=self.username1,
            email=self.email1,
            first_name=self.first_name1,
            last_name=self.last_name1,
            title=self.title1,
            bio=self.bio1,
        )

    def teardown(self):
        self.graph.postgres.dispose()

    def test_search(self):
        self.username2 = "joe.chip"
        self.email2 = "*****@*****.**"
        self.first_name2 = "Joe"
        self.last_name2 = "Chip"
        self.title2 = "Technician"
        self.bio2 = "Ubik-- get it today!"

        self.user2 = User(
            id=new_object_id(),
            username=self.username2,
            email=self.email2,
            first_name=self.first_name2,
            last_name=self.last_name2,
            title=self.title2,
            bio=self.bio2,
        )

        with SessionContext(self.graph), transaction():
            self.user1.create()
            self.user2.create()

        uri = "/api/v1/user"

        response = self.client.get(uri)

        assert_that(response.status_code, is_(equal_to(200)))
        data = loads(response.data)
        assert_that(len(data["items"]), is_(equal_to(2)))
        assert_that(data, has_entries(
            items=contains(
                has_entries(
                    id=str(self.user1.id),
                    username=self.user1.username,
                    email=self.user1.email,
                    firstName=self.user1.first_name,
                    lastName=self.user1.last_name,
                    title=self.user1.title,
                    bio=self.user1.bio,
                ),
                has_entries(
                    id=str(self.user2.id),
                    username=self.user2.username,
                    email=self.user2.email,
                    firstName=self.user2.first_name,
                    lastName=self.user2.last_name,
                    title=self.user2.title,
                    bio=self.user2.bio,
                ),
            ),
        ))

        response = self.client.get(
            uri,
            query_string=dict(
                username=self.user1.username,
            ),
        )

        assert_that(response.status_code, is_(equal_to(200)))
        data = loads(response.data)
        assert_that(len(data["items"]), is_(equal_to(1)))
        assert_that(data, has_entries(
            items=contains(
                has_entries(
                    id=str(self.user1.id),
                    username=self.user1.username,
                    email=self.user1.email,
                    firstName=self.user1.first_name,
                    lastName=self.user1.last_name,
                    title=self.user1.title,
                    bio=self.user1.bio,
                ),
            ),
        ))

    def test_create(self):
        uri = "/api/v1/user"

        with patch.object(self.graph.user_store, "new_object_id") as mocked:
            mocked.return_value = self.user1.id
            response = self.client.post(uri, data=dumps({
                "username": self.user1.username,
                "email": self.user1.email,
                "firstName": self.user1.first_name,
                "lastName": self.user1.last_name,
                "title": self.user1.title,
                "bio": self.user1.bio,
            }))

        assert_that(response.status_code, is_(equal_to(201)))
        data = loads(response.data)
        assert_that(data, has_entries(
            id=str(self.user1.id),
            username=self.user1.username,
            email=self.user1.email,
            firstName=self.user1.first_name,
            lastName=self.user1.last_name,
            title=self.user1.title,
            bio=self.user1.bio,
        ))

    def test_update(self):
        with SessionContext(self.graph), transaction():
            self.user1.create()

        uri = "/api/v1/user/{}".format(self.user1.id)

        response = self.client.patch(uri, data=dumps({
            "firstName": self.user1.first_name,
            "lastName": self.user1.last_name,
            "title": self.user1.title,
            "bio": "A new bio!",
        }))

        assert_that(response.status_code, is_(equal_to(200)))
        data = loads(response.data)
        assert_that(data, has_entries(
            id=str(self.user1.id),
            username=self.user1.username,
            email=self.user1.email,
            firstName=self.user1.first_name,
            lastName=self.user1.last_name,
            title=self.user1.title,
            bio="A new bio!",
        ))

    def test_retrieve(self):
        with SessionContext(self.graph), transaction():
            self.user1.create()

        uri = "/api/v1/user/{}".format(self.user1.id)

        response = self.client.get(uri)

        data = loads(response.data)
        assert_that(data, has_entries(
            id=str(self.user1.id),
            username=self.user1.username,
            email=self.user1.email,
            firstName=self.user1.first_name,
            lastName=self.user1.last_name,
            title=self.user1.title,
            bio=self.user1.bio,
        ))

    def test_delete(self):
        with SessionContext(self.graph), transaction():
            self.user1.create()

        uri = "/api/v1/user/{}".format(self.user1.id)

        response = self.client.delete(uri)
        assert_that(response.status_code, is_(equal_to(204)))
예제 #13
0
class TestFollowerRelationshipRoutes:
    def setup(self):
        self.graph = create_app(testing=True)
        self.client = self.graph.flask.test_client()
        recreate_all(self.graph)

        self.username1 = "glen.runciter"
        self.user1 = User(
            username=self.username1,
            email="*****@*****.**",
            first_name="Glen",
            last_name="Runciter",
            title="Big Boss",
            bio="Ubik-- get it today!",
        )

        self.username2 = "joe.chip"
        self.user2 = User(
            username=self.username2,
            email="*****@*****.**",
            first_name="Joe",
            last_name="Chip",
            title="Technician",
            bio="Ubik-- get it today!",
        )

        with SessionContext(self.graph), transaction():
            self.user1.create()
            self.user2.create()

        self.user1_follow_user2 = FollowerRelationship(
            id=new_object_id(),
            user_id=self.user1.id,
            follower_id=self.user2.id,
        )
        self.user2_follow_user1 = FollowerRelationship(
            id=new_object_id(),
            user_id=self.user2.id,
            follower_id=self.user1.id,
        )

    def teardown(self):
        self.graph.postgres.dispose()

    def test_search(self):
        with SessionContext(self.graph), transaction():
            self.user1_follow_user2.create()
            self.user2_follow_user1.create()

        uri = "/api/v1/follower_relationship"

        response = self.client.get(uri)

        assert_that(response.status_code, is_(equal_to(200)))
        data = loads(response.data)
        assert_that(len(data["items"]), is_(equal_to(2)))
        assert_that(
            data,
            has_entries(items=contains(
                has_entries(
                    id=str(self.user2_follow_user1.id),
                    userId=str(self.user2_follow_user1.user_id),
                    followerId=str(self.user2_follow_user1.follower_id),
                ),
                has_entries(
                    id=str(self.user1_follow_user2.id),
                    userId=str(self.user1_follow_user2.user_id),
                    followerId=str(self.user1_follow_user2.follower_id),
                ),
            ), ))

        response = self.client.get(
            uri,
            query_string=dict(follower_id=str(self.user1.id), ),
        )

        assert_that(response.status_code, is_(equal_to(200)))
        data = loads(response.data)
        assert_that(len(data["items"]), is_(equal_to(1)))
        assert_that(
            data,
            has_entries(items=contains(
                has_entries(
                    id=str(self.user2_follow_user1.id),
                    userId=str(self.user2_follow_user1.user_id),
                    followerId=str(self.user2_follow_user1.follower_id),
                ), ), ))

    def test_create(self):
        uri = "/api/v1/follower_relationship"

        with patch.object(self.graph.follower_relationship_store,
                          "new_object_id") as mocked:
            mocked.return_value = self.user1_follow_user2.id
            response = self.client.post(
                uri,
                data=dumps({
                    "userId":
                    str(self.user1_follow_user2.user_id),
                    "followerId":
                    str(self.user1_follow_user2.follower_id),
                }))

        assert_that(response.status_code, is_(equal_to(201)))
        data = loads(response.data)
        assert_that(
            data,
            has_entries(
                id=str(self.user1_follow_user2.id),
                userId=str(self.user1_follow_user2.user_id),
                followerId=str(self.user1_follow_user2.follower_id),
            ))

    def test_retrieve(self):
        with SessionContext(self.graph), transaction():
            self.user1_follow_user2.create()

        uri = "/api/v1/follower_relationship/{}".format(
            self.user1_follow_user2.id)

        response = self.client.get(uri)

        data = loads(response.data)
        assert_that(
            data,
            has_entries(
                id=str(self.user1_follow_user2.id),
                userId=str(self.user1_follow_user2.user_id),
                followerId=str(self.user1_follow_user2.follower_id),
            ))

    def test_delete(self):
        with SessionContext(self.graph), transaction():
            self.user1_follow_user2.create()

        uri = "/api/v1/follower_relationship/{}".format(
            self.user1_follow_user2.id)

        response = self.client.delete(uri)
        assert_that(response.status_code, is_(equal_to(204)))