2012-10-22 280 views
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字典的構造方式。由於以下兩行輸出:

# 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層的元件長,正如所料,在那裏如picture_weights只有6的元件,並且權重不匹配的那些從環路輸出。 picture_weights中缺少第二個用戶圖片5705,但picture_weights中的一個已被指定不正確的權重0L

我沒有dict的經驗,但是我只是試圖運用我的PHP關聯數組的知識,所以我假設我在那裏有一個缺口。感謝您的任何建議!

+2

爲什麼你行'picture_weights [圖片[2] .ID = pictures2 [0] .weight',而不是'picture_weights [pictures2 [0] .ID ] =圖片2 [0] .weight'? –

回答

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 
+0

已修復!對不起*腮紅* – DanH

+0

不用擔心,它發生在我們所有人身上。 –