コード例 #1
0
ファイル: test_views.py プロジェクト: tko-asn/keshikii
    def test_retrieve_success(self):
        """
        ユーザーモデルの個別取得APIへのGETリクエスト(正常系)
        """

        # 個別取得用URLを作成
        RETRIEVE_URL = self.TARGET_URL.format(str(self.user1.username))

        # リクエストを実行
        response = self.client.get(
            RETRIEVE_URL,
            format='json',
        )

        # JSONデータの読み込み
        content = json.loads(response.content)

        # ステータスコードをテスト
        self.assertEqual(response.status_code, 200)

        # テスト対象のデータを取得
        target_user = get_user_model().objects.get(
            username=self.user1.username)

        # シリアライザのインスタンスを作成
        serializer = CustomUserSerializer(target_user)

        # データ内容をテスト
        self.assertEqual(content, serializer.data)
コード例 #2
0
    def test_output_data(self):
        """出力データの内容検証"""

        # ユーザーを作成
        followed_user = get_user_model().objects.create_user(
            username='******',
            password='******',
        )
        following_user = get_user_model().objects.create_user(
            username='******',
            password='******',
        )

        # テスト対象のデータを作成
        following = Following.objects.create(
            followed_by=following_user,
            followed_user=followed_user,
        )
        followed_by_serializer = CustomUserSerializer(following_user)

        # シリアライザを作成
        serializer = FollowingSerializer(instance=following)

        # シリアライザの出力内容を検証
        expected_data = {
            'id': str(following.id),
            'followed_by': followed_by_serializer.data,
            'user_extra_field': {
                'username': followed_user.username,
                'icon_url': followed_user.icon_url,
            },
        }
        self.assertDictEqual(serializer.data, expected_data)
コード例 #3
0
    def test_input_invalid_if_username_is_null(self):
        """
        入力データのバリデーション
        (NG:ユーザー名の値がNoneの場合)
        """

        # シリアライザに渡すデータ
        input_data = {
            'username': None,
        }

        # シリアライザを作成
        serializer = CustomUserSerializer(data=input_data)

        # バリデーション結果をテスト
        self.assertFalse(serializer.is_valid())
        self.assertCountEqual(serializer.errors.keys(), ['username'])
        self.assertCountEqual(
            [x.code for x in serializer.errors['username']],
            ['null'],
        )
コード例 #4
0
    def test_input_valid(self):
        """入力データのバリデーション(OK)"""

        # お気に入りの投稿用の画像ファイル
        picture = SimpleUploadedFile(
            name='test.jpg',
            content=open(PICTURE_PATH, 'rb').read(),
            content_type='image/jpeg',
        )

        # お気に入りの投稿に登録する投稿を作成
        favorite_post = Post.objects.create(
            title='favorite_post',
            picture=picture,
        )

        # ユーザーアイコン用画像ファイル
        icon = SimpleUploadedFile(
            name='test_icon.jpg',
            content=open(PICTURE_PATH, 'rb').read(),
            content_type='image/jpeg',
        )

        # シリアライザに渡すデータ
        input_data = {
            'username': '******',
            'self_introduction': 'test_introduction',
            'favorite_posts': [favorite_post.id],
            'icon': icon,
        }

        # シリアライザを作成
        serializer = CustomUserSerializer(data=input_data)

        # バリデーション結果をテスト
        self.assertTrue(serializer.is_valid())
コード例 #5
0
    def test_output_data(self):
        """出力データの内容検証"""

        # お気に入りの投稿用の画像ファイル
        picture = SimpleUploadedFile(
            name='test.jpg',
            content=open(PICTURE_PATH, 'rb').read(),
            content_type='image/jpeg',
        )

        # お気に入りの投稿に登録する投稿を作成
        favorite_post = Post.objects.create(
            title='favorite_post',
            picture=picture,
        )

        # テスト対象のデータを作成
        user = get_user_model().objects.create_user(
            username='******',
            password='******',
        )
        user.favorite_posts.add(favorite_post)
        get_user_model().objects.filter(id=user.id).update(
            self_introduction='output_introduction', )
        registered_date = dateformat.format(
            timezone.localtime(user.registered_date),
            'Y年m月d日 H:i:s',
        )

        # シリアライザを作成
        serializer = CustomUserSerializer(instance=user)

        # シリアライザの出力内容を検証
        expected_data = {
            'id': str(user.id),
            'username': user.username,
            'self_introduction': user.self_introduction,
            'favorite_posts': [favorite_post.id],
            'icon_url': user.icon_url,
            'icon_filename': user.icon_filename,
            'registered_date': str(registered_date),
        }
        self.assertDictEqual(serializer.data, expected_data)
コード例 #6
0
    def test_output_data(self):
        """出力データの内容検証"""

        # テスト対象のデータを作成
        post = Post.objects.create(
            title='output_title',
            text='output_text',
            picture=self.picture,
            author=self.test_user,
            status='public',
            zip_code='1600022',
            prefecture='東京都',
            location='新宿区新宿3丁目38-1',
        )
        post.category.add(self.category)
        posted_date = dateformat.format(
            timezone.localtime(post.posted_date),
            'Y年m月d日 H:i',
        )
        author_serializer = CustomUserSerializer(post.author)

        # シリアライザを作成
        serializer = PostSerializer(instance=post)

        # シリアライザの出力内容を検証
        expected_data = {
            'id': str(post.id),
            'title': post.title,
            'text': post.text,
            'posted_date': str(posted_date),
            'picture_url': post.picture_url,
            'picture_filename': post.picture_filename,
            'author': author_serializer.data,
            'status': post.status,
            'category': [self.category.name],
            'zip_code': post.zip_code,
            'prefecture': post.prefecture,
            'location': post.location,
        }
        self.assertDictEqual(serializer.data, expected_data)