def test_get_data_id_null_version(self):
     version = Version(id="254", version=None, browser="2")
     browser = Browser(id="2", slug="the_browser")
     collection = Collection()
     collection.add(version)
     collection.add(browser)
     self.assertEqual(
         ('versions', 'the_browser', ''), version.get_data_id())
示例#2
0
 def test_get_data_id_null_version(self):
     version = Version(id='254', version=None, browser='2')
     browser = Browser(id='2', slug='the_browser')
     collection = Collection()
     collection.add(version)
     collection.add(browser)
     self.assertEqual(('versions', 'the_browser', ''),
                      version.get_data_id())
示例#3
0
 def test_get_data_id_with_empty_subpath(self):
     maturity = Maturity(id='2')
     spec = Specification(id='22', maturity='2', mdn_key='SPEC')
     section = Section(id='_sec', specification='22', subpath={})
     collection = Collection()
     collection.add(maturity)
     collection.add(spec)
     collection.add(section)
     self.assertEqual(('sections', 'SPEC', ''), section.get_data_id())
示例#4
0
    def test_set_collection(self):
        browser = Browser(slug='firefox', versions=['1.0', '2.0'])
        self.assertIsNone(browser._collection)
        self.assertIsNone(browser.versions.links[0].collection)

        collection = Collection()
        browser.set_collection(collection)
        self.assertEqual(collection, browser._collection)
        self.assertEqual(collection, browser.versions.links[0].collection)
 def test_get_data_id_without_subpath(self):
     maturity = Maturity(id="2")
     spec = Specification(id="22", maturity="2", mdn_key="SPEC")
     feature = Feature(id="222")
     section = Section(id="_sec", specification="22", features=["222"])
     collection = Collection()
     collection.add(maturity)
     collection.add(spec)
     collection.add(feature)
     collection.add(section)
     self.assertEqual(('sections', 'SPEC', ''), section.get_data_id())
示例#6
0
 def test_get_data_id_with_number(self):
     maturity = Maturity(id='2')
     spec = Specification(id='22', maturity='2', mdn_key='SPEC')
     section = Section(id='_sec',
                       specification='22',
                       number={'en': '1.2.3'},
                       subpath={'en': '#123'})
     collection = Collection()
     collection.add(maturity)
     collection.add(spec)
     collection.add(section)
     self.assertEqual(('sections', 'SPEC', '#123'), section.get_data_id())
示例#7
0
 def test_load_collection_with_id(self):
     firefox = Browser(slug='firefox', id=6)
     self.col.add(firefox)
     key = ('browsers', 'firefox')
     self.assertEqual({key: firefox},
                      self.col.get_resources_by_data_id('browsers'))
     copy_col = Collection()
     copy_col.load_collection(self.col)
     new_resources = copy_col.get_resources_by_data_id('browsers')
     self.assertEqual([key], list(new_resources.keys()))
     new_firefox = new_resources[key]
     self.assertEqual(firefox.to_json_api(), new_firefox.to_json_api())
     self.assertEqual(new_firefox.id.id, 6)
示例#8
0
    def test_override_ids_resource_without_id(self):
        firefox = Browser(slug='firefox')
        chrome = Browser(slug='chrome')
        self.col.add(firefox)
        self.col.add(chrome)
        self.assertEqual(None, self.col.get('browsers', '1'))

        other_col = Collection()
        other_firefox = Browser(id='1', slug='firefox')
        other_col.add(other_firefox)
        self.col.override_ids_to_match(other_col)
        self.assertEqual(firefox, self.col.get('browsers', '1'))
        self.assertIsNone(chrome.id)
 def test_get_data_id_with_number(self):
     maturity = Maturity(id="2")
     spec = Specification(id="22", maturity="2", mdn_key="SPEC")
     feature = Feature(id="222")
     section = Section(
         id="_sec", specification="22", features=["222"],
         number={"en": "1.2.3"}, subpath={"en": "#123"})
     collection = Collection()
     collection.add(maturity)
     collection.add(spec)
     collection.add(feature)
     collection.add(section)
     self.assertEqual(('sections', 'SPEC', '#123'), section.get_data_id())
示例#10
0
    def test_override_ids_resource_with_falsy_id(self):
        firefox = Browser(id="", slug='firefox')
        chrome = Browser(id=0, slug='chrome')
        self.col.add(firefox)
        self.col.add(chrome)
        self.assertEqual(None, self.col.get('browsers', '1'))

        other_col = Collection()
        other_chrome = Browser(id="1", slug="chrome")
        other_firefox = Browser(id="2", slug="firefox")
        other_col.add(other_chrome)
        other_col.add(other_firefox)
        self.col.override_ids_to_match(other_col)
        self.assertEqual(chrome, self.col.get('browsers', '1'))
        self.assertEqual(firefox, self.col.get('browsers', '2'))
示例#11
0
 def setUp(self):
     self.client = Client('http://example.com')
     self.client.request = mock.Mock()
     self.client.request.side_effect = Exception('should not be called')
     self.orig_col = Collection(self.client)
     self.new_col = Collection()
示例#12
0
    def test_override_ids_to_match(self):
        # Setup collection resources
        browser = Browser(id='_chrome', slug='chrome', versions=['_version'])
        version = Version(id='_version', version='1.0', browser='_chrome')
        feature = Feature(id='_feature', slug='the-feature')
        support = Support(id='_support',
                          feature='_feature',
                          version='_version')
        maturity = Maturity(id='_maturity', slug='REC')
        specification = Specification(id='_spec',
                                      slug='css1',
                                      mdn_key='CSS1',
                                      maturity='_maturity')
        section = Section(id='_section',
                          number={'en': '5.6.1'},
                          specification='_spec')
        self.col.add(browser)
        self.col.add(version)
        self.col.add(feature)
        self.col.add(support)
        self.col.add(maturity)
        self.col.add(specification)
        self.col.add(section)

        # JSON API representation changes
        expected = {
            'versions': {
                'version': '1.0',
                'links': {
                    'browser': '_chrome',
                },
            }
        }
        self.assertEqual(expected, version.to_json_api())

        # Setup other collection with different IDs
        sync_browser = Browser(id='1', slug='chrome')
        sync_version = Version(id='2', version='1.0', browser='1')
        sync_feature = Feature(id='3', slug='the-feature')
        sync_support = Support(id='4', feature='3', version='2')
        sync_maturity = Maturity(id='5', slug='REC')
        sync_specification = Specification(id='6',
                                           slug='css1',
                                           mdn_key='CSS1',
                                           maturity='5')
        sync_section = Section(id='7',
                               number={'en': '5.6.1'},
                               specification='6')
        sync_collection = Collection()
        sync_collection.add(sync_browser)
        sync_collection.add(sync_version)
        sync_collection.add(sync_feature)
        sync_collection.add(sync_support)
        sync_collection.add(sync_maturity)
        sync_collection.add(sync_specification)
        sync_collection.add(sync_section)

        # Lookup is by local ID
        self.assertEqual(browser, self.col.get('browsers', '_chrome'))
        self.assertEqual(None, self.col.get('browsers', '1'))

        # Synchronize IDs to the other collection
        self.col.override_ids_to_match(sync_collection)

        # Other IDs are now the same as original IDs
        self.assertEqual('1', browser.id.id)
        self.assertEqual('2', version.id.id)
        self.assertEqual('3', feature.id.id)
        self.assertEqual('4', support.id.id)
        self.assertEqual('5', maturity.id.id)
        self.assertEqual('6', specification.id.id)
        self.assertEqual('7', section.id.id)

        # Linked IDs are changed as well
        self.assertEqual('1', version.browser.id)
        self.assertEqual(['2'], browser.versions.ids)
        self.assertEqual('2', support.version.id)
        self.assertEqual('3', support.feature.id)
        self.assertEqual('5', specification.maturity.id)
        self.assertEqual('6', section.specification.id)

        # JSON API representation changes
        expected = {
            'versions': {
                'version': '1.0',
                'links': {
                    'browser': '1',
                },
            }
        }
        self.assertEqual(expected, version.to_json_api())

        # Lookup is by original ID
        self.assertEqual(None, self.col.get('browsers', '_chrome'))
        self.assertEqual(browser, self.col.get('browsers', '1'))
示例#13
0
 def setUp(self):
     self.col = Collection()
示例#14
0
 def test_null_link_with_collection(self):
     collection = Collection()
     link = Link(collection, 'browsers')
     self.assertIsNone(link.id)
示例#15
0
 def test_link_with_collection(self):
     collection = Collection()
     link = Link(collection, 'browsers', '1')
     self.assertEqual(link.id, '1')
     collection._override_ids = {'browsers': {'1': '_browser1'}}
     self.assertEqual(link.id, '_browser1')
示例#16
0
    def _process_data(self):
        """Load the linked data and compare to current data."""
        assert not hasattr(self, 'changes'), '_process_data called twice.'
        assert hasattr(self,
                       'errors'), ('_process_data not called by is_valid().')
        r_by_t = Collection.resource_by_type

        # Create and load collection of new data
        new_collection = Collection()
        for rtype, items in self.data.items():
            resource_cls = r_by_t.get(rtype)
            if resource_cls:
                for seq, json_api_item in enumerate(items):
                    item = json_api_item.copy()
                    links = item.pop('links', {})
                    item.update(links)
                    resource = self.load_resource(resource_cls, item)
                    resource._seq = seq
                    new_collection.add(resource)

        # Create native representation of current feature data
        current_collection = Collection(DjangoResourceClient())
        feature_serializer = ViewFeatureSerializer(context=self.context)
        current_feature = feature_serializer.to_representation(self.feature)
        current_extra = current_feature.pop('_view_extra')
        del current_extra['meta']

        # Load feature into new and current collection
        current_feature_resource = self.load_resource(r_by_t['features'],
                                                      current_feature)
        current_collection.add(current_feature_resource)
        current_feature.update(self.feature._in_extra)
        current_feature['id'] = str(current_feature['id'])
        resource_feature = self.load_resource(r_by_t['features'],
                                              current_feature)
        resource_feature._seq = None
        new_collection.add(resource_feature)

        # Populate collection of current data
        for rtype, items in current_extra.items():
            resource_cls = r_by_t[rtype]
            for item in items:
                resource = self.load_resource(resource_cls, item)
                current_collection.add(resource)

        # Add existing items not explicit in PUT content
        # This avoids 'delete' changes
        new_items = new_collection.get_all_by_data_id()
        for data_id, item in current_collection.get_all_by_data_id().items():
            if data_id not in new_items:
                rtype = item._resource_type
                resource = r_by_t[rtype]()
                json_api_rep = item.to_json_api()
                json_api_rep[rtype]['id'] = item.id.id
                resource.from_json_api(json_api_rep)
                resource._seq = None
                new_collection.add(resource)

        # Add existing items used in new collection to current collection
        # This avoids incorrect 'new' changes
        existing_items = current_collection.get_all_by_data_id()
        for data_id, item in new_collection.get_all_by_data_id().items():
            if item.id:
                item_id = item.id.id
                int_id = None
                existing_item = existing_items.get(data_id)
                try:
                    int_id = int(item_id)
                except ValueError:
                    pass
                if int_id and (existing_item is None):
                    rtype = item._resource_type
                    resource_cls = r_by_t[rtype]
                    model_cls, serializer_cls = view_cls_by_name[rtype]
                    obj = model_cls.objects.get(id=int_id)
                    serializer = serializer_cls()
                    data = serializer.to_representation(obj)
                    resource = self.load_resource(resource_cls, data)
                    current_collection.add(resource)

        # Load the diff
        self.changeset = CollectionChangeset(current_collection,
                                             new_collection)
        assert not self.changeset.changes.get('deleted'), (
            'Existing items were not added, so deletions found:\n%s' %
            self.changes['deleted'])