• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

zopefoundation / zope.size / 16248883852

14 Feb 2025 09:00AM UTC coverage: 100.0%. Remained the same
16248883852

push

github

icemac
Back to development: 5.2

8 of 8 branches covered (100.0%)

Branch coverage included in aggregate %.

102 of 102 relevant lines covered (100.0%)

1.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/zope/size/tests.py
1
##############################################################################
2
#
3
# Copyright (c) 2001, 2002, 2011 Zope Foundation and Contributors.
4
# All Rights Reserved.
5
#
6
# This software is subject to the provisions of the Zope Public License,
7
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
8
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
9
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
11
# FOR A PARTICULAR PURPOSE.
12
#
13
##############################################################################
14
"""Test ISized Adapter
15
"""
16

17
import unittest
1✔
18

19
import zope.component
1✔
20
import zope.configuration.xmlconfig
1✔
21

22
import zope.size
1✔
23
from zope.size.interfaces import ISized
1✔
24

25

26
class ZCMLTest(unittest.TestCase):
1✔
27

28
    def test_configure_zcml_should_be_loadable(self):
1✔
29
        try:
1✔
30
            zope.configuration.xmlconfig.XMLConfig(
1✔
31
                'configure.zcml', zope.size)()
32
        except Exception as e:  # pragma: no cover
33
            self.fail(e)
34

35
    def test_configure_should_register_n_components(self):
1✔
36
        gsm = zope.component.getGlobalSiteManager()
1✔
37
        u_count = len(list(gsm.registeredUtilities()))
1✔
38
        a_count = len(list(gsm.registeredAdapters()))
1✔
39
        s_count = len(list(gsm.registeredSubscriptionAdapters()))
1✔
40
        h_count = len(list(gsm.registeredHandlers()))
1✔
41
        zope.configuration.xmlconfig.XMLConfig(
1✔
42
            'configure.zcml', zope.size)()
43
        self.assertEqual(u_count + 8, len(list(gsm.registeredUtilities())))
1✔
44
        self.assertEqual(a_count + 1, len(list(gsm.registeredAdapters())))
1✔
45
        self.assertEqual(
1✔
46
            s_count, len(list(gsm.registeredSubscriptionAdapters())))
47
        self.assertEqual(h_count, len(list(gsm.registeredHandlers())))
1✔
48

49

50
class DummyObject:
1✔
51

52
    def __init__(self, size):
1✔
53
        self._size = size
1✔
54

55
    def getSize(self):
1✔
56
        return self._size
1✔
57

58

59
class Test(unittest.TestCase):
1✔
60

61
    def testImplementsISized(self):
1✔
62
        from zope.size import DefaultSized
1✔
63
        sized = DefaultSized(object())
1✔
64
        self.assertTrue(ISized.providedBy(sized))
1✔
65

66
    def testSizeWithBytes(self):
1✔
67
        from zope.size import DefaultSized
1✔
68
        obj = DummyObject(1023)
1✔
69
        sized = DefaultSized(obj)
1✔
70
        self.assertEqual(sized.sizeForSorting(), ('byte', 1023))
1✔
71
        self.assertEqual(sized.sizeForDisplay(), '1 KB')
1✔
72

73
    def testSizeWithNone(self):
1✔
74
        from zope.size import DefaultSized
1✔
75
        obj = DummyObject(None)
1✔
76
        sized = DefaultSized(obj)
1✔
77
        self.assertEqual(sized.sizeForSorting(), (None, None))
1✔
78
        self.assertEqual(sized.sizeForDisplay(), 'not-available')
1✔
79

80
    def testSizeNotAvailable(self):
1✔
81
        from zope.size import DefaultSized
1✔
82
        sized = DefaultSized(object())
1✔
83
        self.assertEqual(sized.sizeForSorting(), (None, None))
1✔
84
        self.assertEqual(sized.sizeForDisplay(), 'not-available')
1✔
85

86
    def testVariousSizes(self):
1✔
87
        from zope.size import DefaultSized
1✔
88

89
        sized = DefaultSized(DummyObject(0))
1✔
90
        self.assertEqual(sized.sizeForSorting(), ('byte', 0))
1✔
91
        self.assertEqual(sized.sizeForDisplay(), '0 KB')
1✔
92

93
        sized = DefaultSized(DummyObject(1))
1✔
94
        self.assertEqual(sized.sizeForSorting(), ('byte', 1))
1✔
95
        self.assertEqual(sized.sizeForDisplay(), '1 KB')
1✔
96

97
        sized = DefaultSized(DummyObject(2048))
1✔
98
        self.assertEqual(sized.sizeForSorting(), ('byte', 2048))
1✔
99
        self.assertEqual(sized.sizeForDisplay(), '${size} KB')
1✔
100
        self.assertEqual(sized.sizeForDisplay().mapping, {'size': '2'})
1✔
101

102
        sized = DefaultSized(DummyObject(2000000))
1✔
103
        self.assertEqual(sized.sizeForSorting(), ('byte', 2000000))
1✔
104
        self.assertEqual(sized.sizeForDisplay(), '${size} MB')
1✔
105
        self.assertEqual(sized.sizeForDisplay().mapping, {'size': '1.91'})
1✔
106

107
    def test_byteDisplay(self):
1✔
108
        from zope.size import byteDisplay
1✔
109
        self.assertEqual(byteDisplay(0), '0 KB')
1✔
110
        self.assertEqual(byteDisplay(1), '1 KB')
1✔
111
        self.assertEqual(byteDisplay(2048), '${size} KB')
1✔
112
        self.assertEqual(byteDisplay(2048).mapping, {'size': '2'})
1✔
113
        self.assertEqual(byteDisplay(2000000), '${size} MB')
1✔
114
        self.assertEqual(byteDisplay(2000000).mapping, {'size': '1.91'})
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc