def test_new_versions_to_existing_browser(self): """When order matters, new items update the parent item.""" browser = Browser(id='_b', slug='browser', versions=['u']) v_1 = Version(id='1.0', version='1.0', browser='_b') self.orig_col.add(browser) self.orig_col.add(v_1) browser_new = Browser(id='_b', slug='browser', versions=['u', '1.0', '2.0']) v_unknown = Version(id='u', version=None, browser='_b') v_1_same = Version(id='1.0', version='1.0', browser='_b') v_2 = Version(id='2.0', version='2.0', browser='_b') self.new_col.add(browser_new) self.new_col.add(v_unknown) self.new_col.add(v_1_same) self.new_col.add(v_2) cc = CollectionChangeset(self.orig_col, self.new_col) expected_order = OrderedDict([ (('versions', 'browser', ''), v_unknown), (('versions', 'browser', '2.0'), v_2), ]) expected = { 'new': expected_order, 'same': OrderedDict([ (('versions', 'browser', '1.0'), v_1_same), ]), 'changed': OrderedDict([ (('browsers', 'browser'), browser_new), ]), 'deleted': OrderedDict(), } self.assertEqual(expected_order.keys(), cc.changes['new'].keys()) self.assertEqual(expected, cc.changes)
def test_from_json_api(self): rep = { 'browsers': { 'id': '1', 'slug': 'chrome', 'name': { 'en': 'Chrome' }, 'note': None, 'links': { 'history': ['1'], 'history_current': '1', 'versions': ['1'], } }, 'links': { 'browsers.history': { 'href': ('http://example.com/api/v1/historical_browsers/' '{browsers.history}'), 'type': 'historical_browsers' }, 'browsers.history_current': { 'href': ('http://localhost:8000/api/v1/historical_browsers/' '{browsers.history_current}'), 'type': 'historical_browsers' }, 'browsers.versions': { 'href': ('http://localhost:8000/api/v1/versions/' '{browsers.versions}'), 'type': 'versions' } } } browser = Browser() self.assertIsNone(browser.id) self.assertIsNone(browser.slug) self.assertIsNone(browser.name) self.assertIsNone(browser.note) self.assertIsNone(browser.history) self.assertIsNone(browser.history_current) self.assertIsNone(browser.versions) browser.from_json_api(rep) self.assertIsInstance(browser.id, Link) self.assertEqual('1', browser.id.id) self.assertEqual('chrome', browser.slug) self.assertEqual({'en': 'Chrome'}, browser.name) self.assertIsNone(browser.note) self.assertIsInstance(browser.history, LinkList) self.assertEqual(['1'], browser.history.ids) self.assertIsInstance(browser.history_current, Link) self.assertEqual('1', browser.history_current.id) self.assertIsInstance(browser.versions, LinkList) self.assertEqual(['1'], browser.versions.ids)
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_to_json_api_simple(self): browser = Browser() browser.name = {'en': 'Browser'} expected = { "browsers": { "name": {'en': 'Browser'} } } actual = browser.to_json_api() self.assertEqual(expected, actual)
def test_from_json_api(self): rep = { "browsers": { "id": "1", "slug": "chrome", "name": {"en": "Chrome"}, "note": None, "links": { "history": ["1"], "history_current": "1", "versions": ["1"], } }, "links": { "browsers.history": { "href": ( "http://example.com/api/v1/historical_browsers/" "{browsers.history}"), "type": "historical_browsers" }, "browsers.history_current": { "href": ( "http://localhost:8000/api/v1/historical_browsers/" "{browsers.history_current}"), "type": "historical_browsers" }, "browsers.versions": { "href": ( "http://localhost:8000/api/v1/versions/" "{browsers.versions}"), "type": "versions" } } } browser = Browser() self.assertIsNone(browser.id) self.assertIsNone(browser.slug) self.assertIsNone(browser.name) self.assertIsNone(browser.note) self.assertIsNone(browser.history) self.assertIsNone(browser.history_current) self.assertIsNone(browser.versions) browser.from_json_api(rep) self.assertIsInstance(browser.id, Link) self.assertEqual("1", browser.id.id) self.assertEqual("chrome", browser.slug) self.assertEqual({'en': 'Chrome'}, browser.name) self.assertIsNone(browser.note) self.assertIsInstance(browser.history, LinkList) self.assertEqual(["1"], browser.history.ids) self.assertIsInstance(browser.history_current, Link) self.assertEqual("1", browser.history_current.id) self.assertIsInstance(browser.versions, LinkList) self.assertEqual(["1"], browser.versions.ids)
def test_from_json_api(self): rep = { 'browsers': { 'id': '1', 'slug': 'chrome', 'name': {'en': 'Chrome'}, 'note': None, 'links': { 'history': ['1'], 'history_current': '1', 'versions': ['1'], } }, 'links': { 'browsers.history': { 'href': ( 'http://example.com/api/v1/historical_browsers/' '{browsers.history}'), 'type': 'historical_browsers' }, 'browsers.history_current': { 'href': ( 'http://localhost:8000/api/v1/historical_browsers/' '{browsers.history_current}'), 'type': 'historical_browsers' }, 'browsers.versions': { 'href': ( 'http://localhost:8000/api/v1/versions/' '{browsers.versions}'), 'type': 'versions' } } } browser = Browser() self.assertIsNone(browser.id) self.assertIsNone(browser.slug) self.assertIsNone(browser.name) self.assertIsNone(browser.note) self.assertIsNone(browser.history) self.assertIsNone(browser.history_current) self.assertIsNone(browser.versions) browser.from_json_api(rep) self.assertIsInstance(browser.id, Link) self.assertEqual('1', browser.id.id) self.assertEqual('chrome', browser.slug) self.assertEqual({'en': 'Chrome'}, browser.name) self.assertIsNone(browser.note) self.assertIsInstance(browser.history, LinkList) self.assertEqual(['1'], browser.history.ids) self.assertIsInstance(browser.history_current, Link) self.assertEqual('1', browser.history_current.id) self.assertIsInstance(browser.versions, LinkList) self.assertEqual(['1'], browser.versions.ids)
def test_to_json_api_complex(self): browser = Browser( id="1", slug="chrome", name={"en": "Chrome"}, history=["1"], history_current="1", versions=["1"]) expected = { "browsers": { "slug": "chrome", "name": {"en": "Chrome"}, } } actual = browser.to_json_api(with_sorted=False) self.assertEqual(expected, actual)
def test_to_json_api_complex(self): browser = Browser( id='1', slug='chrome', name={'en': 'Chrome'}, history=['1'], history_current='1', versions=['1']) expected = { 'browsers': { 'slug': 'chrome', 'name': {'en': 'Chrome'}, } } actual = browser.to_json_api(with_sorted=False) self.assertEqual(expected, actual)
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)
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_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)
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'))
def setup_new(self): browser = Browser(id='_chrome', slug='chrome') version = Version(version='2.0', browser='_chrome') self.new_col.add(browser) self.new_col.add(version) resources = (browser, version) return resources, CollectionChangeset(self.orig_col, self.new_col)
def test_new_browser_with_ordered_versions(self): """When order matters, creation order is sort order.""" browser = Browser(id='_b', slug='browser', versions=['u', '1.0', '2.0']) v_unknown = Version(id='u', version=None, browser='_b') v_1 = Version(id='1.0', version='1.0', browser='_b') v_2 = Version(id='2.0', version='2.0', browser='_b') self.new_col.add(browser) self.new_col.add(v_1) self.new_col.add(v_unknown) self.new_col.add(v_2) cc = CollectionChangeset(self.orig_col, self.new_col) expected_order = OrderedDict([ (('browsers', 'browser'), browser), (('versions', 'browser', ''), v_unknown), (('versions', 'browser', '1.0'), v_1), (('versions', 'browser', '2.0'), v_2), ]) expected = { 'new': expected_order, 'same': OrderedDict(), 'changed': OrderedDict(), 'deleted': OrderedDict(), } self.assertEqual(expected_order.keys(), cc.changes['new'].keys()) self.assertEqual(expected, cc.changes)
def test_get_all_by_data_id(self): browser = Browser(id='1', slug='chrome') self.col.add(browser) version = Version(id='2', version='1.0', browser='1') self.col.add(version) feature = Feature(id='3', slug='the-feature') self.col.add(feature) support = Support(id='4', feature='3', version='2') self.col.add(support) maturity = Maturity(id='5', slug='REC') self.col.add(maturity) specification = Specification(id='6', slug='css1', mdn_key='CSS1', maturity='5') self.col.add(specification) section = Section(id='7', subpath={'en': '#561'}, specification='6') self.col.add(section) index = self.col.get_all_by_data_id() expected = { ('browsers', 'chrome'): browser, ('versions', 'chrome', '1.0'): version, ('features', 'the-feature'): feature, ('supports', 'the-feature', 'chrome', '1.0'): support, ('maturities', 'REC'): maturity, ('specifications', 'CSS1'): specification, ('sections', 'CSS1', '#561'): section, } self.assertEqual(expected, index)
def test_missing_attribute(self): raised = False browser = Browser() try: browser.missing_attribute except AttributeError: raised = True self.assertTrue(raised)
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())
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())
def test_to_json_api_complex(self): browser = Browser(id='1', slug='chrome', name={'en': 'Chrome'}, history=['1'], history_current='1', versions=['1']) expected = { 'browsers': { 'slug': 'chrome', 'name': { 'en': 'Chrome' }, } } actual = browser.to_json_api(with_sorted=False) self.assertEqual(expected, actual)
def setup_deleted(self, skip_deletes=False): browser = Browser(id='_chrome', slug='chrome') version = Version(id='_chrome_2', version='2.0', browser='_chrome') self.orig_col.add(browser) self.orig_col.add(version) resources = (browser, version) return resources, CollectionChangeset(self.orig_col, self.new_col, skip_deletes)
def setup_matched(self): browser = Browser(id='1', slug='chrome') version = Version( id='1', version='2.0', browser='1', note={'en': 'Second Version'}) self.orig_col.add(browser) self.orig_col.add(version) browser_same = Browser(id='_chrome', slug='chrome') version_diff = Version( id='_version', version='2.0', browser='_chrome', note=OrderedDict(( ('en', 'Second Version'), ('es', 'Segunda Versión')))) self.new_col.add(version_diff) self.new_col.add(browser_same) resources = (version, browser_same, version_diff) return resources, CollectionChangeset(self.orig_col, self.new_col)
def test_get_resources_by_data_id(self): browser = Browser(id='1', slug='firefox') version = Version(version='1.0', browser='1') self.col.add(browser) self.col.add(version) versions = self.col.get_resources_by_data_id('versions') self.assertEqual({('versions', 'firefox', '1.0'): version}, versions) browsers = self.col.get_resources_by_data_id('browsers') self.assertEqual({('browsers', 'firefox'): browser}, browsers)
def test_filter_by_property(self): browser = Browser(id='_firefox', slug='firefox') version1 = Version(version='1.0', browser='_firefox') version2 = Version(version='2.0', browser='_firefox') self.col.add(browser) self.col.add(version1) self.col.add(version2) versions = self.col.filter('versions', version='1.0') self.assertEqual([version1], versions)
def test_filter_link_is_none(self): browser = Browser(id='_firefox', slug='firefox') version1 = Version(version=None, browser='_firefox') version2 = Version(version='1.0') self.col.add(browser) self.col.add(version1) self.col.add(version2) versions = self.col.filter('versions', browser=None) self.assertEqual([version2], versions)
def test_filter_link(self): browser = Browser(id='_firefox', slug='firefox') version1 = Version(version=None, browser='_firefox') version2 = Version(version='1.0', browser='_firefox') self.col.add(browser) self.col.add(version1) self.col.add(version2) versions = self.col.filter('versions', browser='_firefox') self.assertEqual(2, len(versions)) self.assertTrue(version1 in versions) self.assertTrue(version2 in versions)
def test_remove(self): firefox = Browser(id='_firefox', slug='firefox') chrome = Browser(id='_chrome', slug='chrome') self.col.add(firefox) self.col.add(chrome) self.assertEqual([chrome], self.col.filter('browsers', slug='chrome')) self.assertEqual([firefox, chrome], self.col.get_resources('browsers')) self.assertEqual(chrome, self.col.get('browsers', '_chrome')) expected = { ('browsers', 'firefox'): firefox, ('browsers', 'chrome'): chrome, } self.assertEqual(expected, self.col.get_resources_by_data_id('browsers')) self.col.remove(chrome) self.assertEqual([], self.col.filter('browsers', slug='chrome')) self.assertEqual([firefox], self.col.get_resources('browsers')) self.assertEqual(None, self.col.get('browsers', '_chrome')) expected = {('browsers', 'firefox'): firefox} self.assertEqual(expected, self.col.get_resources_by_data_id('browsers'))
def test_get_data_id(self): browser = Browser(slug='firefox') self.assertEqual(('browsers', 'firefox'), browser.get_data_id())
def test_load_at_init(self): browser = Browser(slug='browser') self.assertEqual('browser', browser.slug)
def test_remove_resource_without_id(self): firefox = Browser(slug='firefox') self.col.add(firefox) self.assertEqual([firefox], self.col.get_resources('browsers')) self.col.remove(firefox) self.assertEqual([], self.col.get_resources('browsers'))
def test_load_by_attribute(self): browser = Browser() browser.id = "6" self.assertIsInstance(browser.id, Link) self.assertEqual("6", browser.id.id)
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'))
def test_get_resources(self): browser = Browser(slug='firefox') self.col.add(browser) browsers = self.col.get_resources('browsers') self.assertEqual([browser], browsers)
def test_load_by_attribute(self): browser = Browser() browser.id = '6' self.assertIsInstance(browser.id, Link) self.assertEqual('6', browser.id.id)