File: torrent.py

package info (click to toggle)
transmissionrpc 0.11-1%2Bdeb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 572 kB
  • ctags: 312
  • sloc: python: 2,330; makefile: 15
file content (80 lines) | stat: -rwxr-xr-x 3,276 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# -*- coding: utf-8 -*-
# 2008-12, Erik Svensson <erik.public@gmail.com>
# Licensed under the MIT license.

import time, datetime
import unittest
import transmissionrpc
import transmissionrpc.constants
import transmissionrpc.utils

class torrent(unittest.TestCase):
    def assertPropertyException(self, exception, object, property):
        try:
            getattr(object, property)
        except exception:
            pass
        else:
            self.fail()
    
    def testConstruction(self):
        self.failUnlessRaises(ValueError, transmissionrpc.Torrent, None, {})
        torrent = transmissionrpc.Torrent(None, {'id': 42})
    
    def testAttributes(self):
        torrent = transmissionrpc.Torrent(None, {'id': 42})
        self.assertTrue(hasattr(torrent, 'id'))
        self.assertEqual(torrent.id, 42)
        self.assertPropertyException(KeyError, torrent, 'status')
        self.assertPropertyException(KeyError, torrent, 'progress')
        self.assertPropertyException(KeyError, torrent, 'ratio')
        self.assertPropertyException(KeyError, torrent, 'eta')
        self.assertPropertyException(KeyError, torrent, 'date_active')
        self.assertPropertyException(KeyError, torrent, 'date_added')
        self.assertPropertyException(KeyError, torrent, 'date_started')
        self.assertPropertyException(KeyError, torrent, 'date_done')
        
        self.failUnlessRaises(KeyError, torrent.format_eta)
        self.assertEqual(torrent.files(), {})
        
        data = {
            'id': 1,
            'status': 4,
            'sizeWhenDone': 1000,
            'leftUntilDone': 500,
            'uploadedEver': 1000,
            'downloadedEver': 2000,
            'uploadRatio': 0.5,
            'eta': 3600,
            'activityDate': time.mktime((2008,12,11,11,15,30,0,0,-1)),
            'addedDate': time.mktime((2008,12,11,8,5,10,0,0,-1)),
            'startDate': time.mktime((2008,12,11,9,10,5,0,0,-1)),
            'doneDate': time.mktime((2008,12,11,10,0,15,0,0,-1)),
        }
        
        torrent = transmissionrpc.Torrent(None, data)
        self.assertEqual(torrent.id, 1)
        self.assertEqual(torrent.leftUntilDone, 500)
        self.assertEqual(torrent.status, 'downloading')
        self.assertEqual(torrent.progress, 50.0)
        self.assertEqual(torrent.ratio, 0.5)
        self.assertEqual(torrent.eta, datetime.timedelta(seconds=3600))
        self.assertEqual(torrent.date_active, datetime.datetime(2008,12,11,11,15,30))
        self.assertEqual(torrent.date_added, datetime.datetime(2008,12,11,8,5,10))
        self.assertEqual(torrent.date_started, datetime.datetime(2008,12,11,9,10,5))
        self.assertEqual(torrent.date_done, datetime.datetime(2008,12,11,10,0,15))
        
        self.assertEqual(torrent.format_eta(), transmissionrpc.utils.format_timedelta(torrent.eta))

        torrent = transmissionrpc.Torrent(None, {'id': 42, 'eta': -1})
        self.assertPropertyException(ValueError, torrent, 'eta')

    def testUnicode(self):
        torrent = transmissionrpc.Torrent(None, {'id': 42, 'name': 'あみ'})
        self.assertEqual(torrent.id, 42)
        repr(torrent)
        str(torrent)

def suite():
    suite = unittest.TestLoader().loadTestsFromTestCase(torrent)
    return suite