~launchpad-pqm/launchpad/devel

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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# Copyright 2010 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

"""Test Processor and ProcessorFamily features."""

from zope.component import getUtility

from canonical.launchpad.webapp.interfaces import (
    DEFAULT_FLAVOR,
    IStoreSelector,
    MAIN_STORE,
    )
from canonical.launchpad.testing.pages import LaunchpadWebServiceCaller
from canonical.testing.layers import (
    DatabaseFunctionalLayer,
    LaunchpadZopelessLayer,
    )

from lp.soyuz.interfaces.processor import (
    IProcessor,
    IProcessorFamily,
    IProcessorFamilySet,
    IProcessorSet,
    ProcessorNotFound,
    )
from lp.testing import (
    ExpectedException,
    logout,
    TestCaseWithFactory,
    )


class ProcessorFamilyTests(TestCaseWithFactory):
    """Test ProcessorFamily."""

    layer = LaunchpadZopelessLayer

    def test_create(self):
        """Test adding a new ProcessorFamily."""
        family = getUtility(IProcessorFamilySet).new("avr", "Atmel AVR",
            "The Modified Harvard architecture 8-bit RISC processors.")
        self.assertProvides(family, IProcessorFamily)

    def test_add_processor(self):
        """Test adding a new Processor to a ProcessorFamily."""
        family = getUtility(IProcessorFamilySet).new("avr", "Atmel AVR",
            "The Modified Harvard architecture 8-bit RISC processors.")
        proc = family.addProcessor(
            "avr2001", "The 2001 AVR", "Fast as light.")
        self.assertProvides(proc, IProcessor)
        self.assertEquals(family, proc.family)

    def test_get_restricted(self):
        """Test retrieving all restricted processors."""
        family_set = getUtility(IProcessorFamilySet)
        normal_family = getUtility(IProcessorFamilySet).new(
            "avr", "Atmel AVR",
            "The Modified Harvard architecture 8-bit RISC processors.")
        restricted_family = getUtility(IProcessorFamilySet).new(
            "5051", "5051", "Another small processor family",
            restricted=True)
        self.assertFalse(normal_family in family_set.getRestricted())
        self.assertTrue(restricted_family in family_set.getRestricted())


class ProcessorSetTests(TestCaseWithFactory):
    layer = DatabaseFunctionalLayer

    def test_getByName(self):
        processor_set = getUtility(IProcessorSet)
        q1 = self.factory.makeProcessorFamily(name='q1')
        self.assertEquals(q1.processors[0], processor_set.getByName('q1'))

    def test_getByName_not_found(self):
        processor_set = getUtility(IProcessorSet)
        with ExpectedException(ProcessorNotFound, 'No such processor.*'):
            processor_set.getByName('q1')

    def test_getAll(self):
        processor_set = getUtility(IProcessorSet)
        # Make it easy to filter out sample data
        store = getUtility(IStoreSelector).get(MAIN_STORE, DEFAULT_FLAVOR)
        store.execute("UPDATE Processor SET name = 'sample_data_' || name")
        self.factory.makeProcessorFamily(name='q1')
        self.factory.makeProcessorFamily(name='i686')
        self.factory.makeProcessorFamily(name='g4')
        self.assertEquals(
            ['g4', 'i686', 'q1'],
            sorted(
            processor.name for processor in processor_set.getAll()
            if not processor.name.startswith('sample_data_')))


class ProcessorSetWebServiceTests(TestCaseWithFactory):
    layer = DatabaseFunctionalLayer

    def setUp(self):
        super(ProcessorSetWebServiceTests, self).setUp()
        self.webservice = LaunchpadWebServiceCaller()

    def test_getByName(self):
        self.factory.makeProcessorFamily(name='transmeta')
        logout()

        processor = self.webservice.named_get(
            '/+processors', 'getByName', name='transmeta',
            api_version='devel',
            ).jsonBody()
        self.assertEquals('transmeta', processor['name'])

    def test_default_collection(self):
        # Make it easy to filter out sample data
        store = getUtility(IStoreSelector).get(MAIN_STORE, DEFAULT_FLAVOR)
        store.execute("UPDATE Processor SET name = 'sample_data_' || name")
        self.factory.makeProcessorFamily(name='q1')
        self.factory.makeProcessorFamily(name='i686')
        self.factory.makeProcessorFamily(name='g4')

        logout()

        collection = self.webservice.get(
            '/+processors?ws.size=10', api_version='devel').jsonBody()
        self.assertEquals(
            ['g4', 'i686', 'q1'],
            sorted(
            processor['name'] for processor in collection['entries']
            if not processor['name'].startswith('sample_data_')))