0

アルバムと写真の 2 つのモデルと、album_reorder写真 ID のリストを受け取り、それに応じて重みを変更する関数があります。これは正常に動作しますが、次のような関数のテストを作成しようとしています:

Class ReposTests(unittest.TestCase):
    def test_album_reorder__fail__not_owned(self):

        # Create a test user, album and pictures.
        user = self.create_test_user()
        # create_default_album() generates an Album along with 6 default Picture records.    
        create_default_album(user)
        album = get_first_album(user.profile)
        pictures = Picture.objects.filter(album=album)

        # Get all picture IDs into a list and record each pictures existing weight.
        picture_ids = []
        picture_weights = {}
        for picture in pictures:
            picture_ids.append(picture.id)
            print "%s:%s:%s" % (album.id, picture.id, picture.weight)
            picture_weights[picture.id] = picture.weight

        # Create another test user, album and pictures.
        user2 = self.create_test_user()
        create_default_album(user2)
        album2 = get_first_album(user2.profile)
        pictures2 = Picture.objects.filter(album=album2)

        # Add a Picture ID from a different user to test the restriction that an album cannot be re-ordered if any pictures do not belong to the specified user.
        picture_ids.append(pictures2[0].id)
        print "%s:%s:%s" % (album2.id, pictures2[0].id, pictures2[0].weight)
        picture_weights[pictures[2].id] = pictures2[0].weight

        # Shuffle all picture IDs
        random = Random()
        while pictures[0].id == picture_ids[0]:
            random.shuffle(picture_ids)

        # Reorder picture IDs according to shuffled ID list, however this should fail due to the ownership check.
        album_reorder(user, picture_ids)
        print picture_ids
        print picture_weights

        # Check each
        for picture_id in picture_ids:
            self.assertEqual(picture_weights[picture_id], Picture.objects.get(id=picture_id).weight)

picture_weights現在、問題はdictの構築方法にあるようです。次の 2 行が出力されるため、次のようになります。

# Loop output: (album.id, picture.id, picture.weight)
# 1346:5699:0
# 1346:5700:1
# 1346:5701:2
# 1346:5702:3
# 1346:5703:4
# 1346:5704:5
# 1347:5705:0

print picture_ids
# Outputs: [5700L, 5703L, 5702L, 5699L, 5704L, 5705L, 5701L]

print picture_weights
# Outputs: {5699L: 0L, 5700L: 1L, 5701L: 0L, 5702L: 3L, 5703L: 4L, 5704L: 5L}

picture_idsは予想どおり 7 要素の長さですが、aspicture_weightsは 6 要素のみであり、重みはループからの出力と一致しません。2 番目のユーザー画像5705は から欠落していますがpicture_weights、そのうちの 1 つはpicture_weightsの不適切な重みが割り当てられてい0Lます。

私は辞書の経験はまったくありませんが、PHP連想配列の知識を適用しようとしているだけなので、知識にギャップがあると想定しています。アドバイスをありがとう!

4

1 に答える 1

3

リストと辞書は次の行で分岐しています。

    # Add a Picture ID from a different user to test the restriction that an album cannot be re-ordered if any pictures do not belong to the specified user.
    picture_ids.append(pictures2[0].id)
    print "%s:%s:%s" % (album2.id, pictures2[0].id, pictures2[0].weight)
    picture_weights[pictures[2].id] = pictures2[0].weight

をどのように使用したかに注意してくださいpictures[2].id。これは5701L、その特定のキーの重みが辞書で変更される理由です)。あなたはおそらくその行が次のようになることを意図していました:

    picture_weights[pictures2[0].id] = pictures2[0].weight
于 2012-10-22T04:22:24.743 に答える