class TestItem(): def setUp(self): self.d = MockDao() self.d.setResponses([deepcopy(ITEM_DATA)]) def test_mock_dao(self): assert_equals(self.d.get("123"), ITEM_DATA) def ITEM_DATA_init(self): i = models.Item(self.d) assert_equals(len(i.id), 32) # made a uuid, yay def ITEM_DATA_save(self): i = models.Item(self.d, id="123") # load all the values from the item_DATA into the test item. for key in ITEM_DATA: setattr(i, key, ITEM_DATA[key]) i.save() assert_equals(i.aliases, ALIAS_DATA) seed = deepcopy(ITEM_DATA) seed["_id"] = "123" # the fake dao puts the doc-to-save in the self.input var. assert_equals(self.input, seed)
class TestCollection(): def setUp(self): self.d = MockDao() def test_mock_dao(self): self.d.setResponses([ deepcopy(COLLECTION_DATA)]) assert_equals(self.d.get("SomeCollectionId"), COLLECTION_DATA) def COLLECTION_DATA_init(self): c = models.Collection(self.d) assert_equals(len(c.id), 32) # made a uuid, yay def COLLECTION_DATA_add_items(self): c = models.Collection(self.d, seed=deepcopy(COLLECTION_DATA)) c.add_items(["newtiid1", "newtiid2"]) assert_equals(c.item_ids(), [u'origtiid1', u'origtiid2', 'newtiid1', 'newtiid2']) def COLLECTION_DATA_remove_item(self): c = models.Collection(self.d, seed=deepcopy(COLLECTION_DATA)) c.remove_item("origtiid1") assert_equals(c.item_ids(), ["origtiid2"]) def COLLECTION_DATA_save(self): # this fake save method puts the doc-to-save in the self.input variable def fake_save(data, id): self.input = data self.d.update_item = fake_save c = models.Collection(self.d) # load all the values from the item_DATA into the test item. for key in COLLECTION_DATA: setattr(c, key, COLLECTION_DATA[key]) c.save() seed = deepcopy(COLLECTION_DATA) # the dao changes the contents to give the id variable the leading underscore expected by couch seed["_id"] = seed["id"] del(seed["id"]) # check to see if the fake save method did in fact "save" the collection as expected assert_equals(self.input, seed) def test_collection_delete(self): self.d.delete = lambda id: True c = models.Collection(self.d, id="SomeCollectionId") response = c.delete() assert_equals(response, True)
class TestCollectionFactory(): def setUp(self): self.d = MockDao() def COLLECTION_DATA_load(self): self.d.get = lambda id: deepcopy(COLLECTION_DATA) c = models.Collection(self.d, id="SomeCollectionId") c.load() assert_equals(c.collection_name, "My Collection") assert_equals(c.item_ids(), [u'origtiid1', u'origtiid2']) @raises(LookupError) def test_load_with_nonexistant_collection_fails(self): self.d.setResponses([None]) factory = models.CollectionFactory.make(self.d, "AnUnknownCollectionId") def test_creates_identifier(self): coll = models.CollectionFactory.make(self.d) assert_equals(len(coll.id), 6)
class TestItemFactory(): def setUp(self): self.d = MockDao() def test_make_new(self): '''create an item from scratch.''' item = models.ItemFactory.make(self.d, default_settings.PROVIDERS) assert_equals(len(item.id), 32) assert item.created < time.time assert_equals(item.aliases.__class__.__name__, "Aliases") assert_equals(item.metrics["wikipedia:mentions"]['values'], {}) def test_get(self): self.d.setResponses([deepcopy(ITEM_DATA)]) item = models.ItemFactory.get( self.d, "123", provider.ProviderFactory.get_provider, default_settings.PROVIDERS) assert_equals(item.id, ITEM_DATA['_id']) assert_equals(item.aliases.__class__.__name__, "Aliases") assert_equals(item.as_dict()["aliases"], ITEM_DATA["aliases"]) def test_get_creates_metrics(self): item_data = deepcopy(ITEM_DATA) item_data2 = deepcopy(ITEM_DATA) del item_data2['metrics'] self.d.setResponses([item_data, item_data2]) item = models.ItemFactory.get( self.d, "123", provider.ProviderFactory.get_provider, default_settings.PROVIDERS) assert_equals(item.metrics["wikipedia:mentions"]['values'][KEY1], VAL1) # works even if there are no metrics in the db: item2 = models.ItemFactory.get( self.d, "123", provider.ProviderFactory.get_provider, default_settings.PROVIDERS) print item2.metrics assert_equals(item2.metrics["dryad:package_views"]['values'], {}) def test_adds_static_meta(self): self.d.setResponses([deepcopy(ITEM_DATA)]) item = models.ItemFactory.get( self.d, "123", provider.ProviderFactory.get_provider, default_settings.PROVIDERS) expected = {'category': 'NA', 'provider_url': 'http://www.wikipedia.org/', 'display_name': 'mentions', 'description': 'Wikipedia is the free encyclopedia that anyone can edit.', 'provider': 'Wikipedia', 'other_terms_of_use': 'NA', 'can_embed': 'NA', 'can_use_commercially': 'NA', 'can_aggregate': 'NA', 'icon': 'http://wikipedia.org/favicon.ico'} assert_equals(item.metrics["wikipedia:mentions"]["static_meta"], expected) def test_adds_provenance_url(self): self.d.setResponses([deepcopy(ITEM_DATA)]) item = models.ItemFactory.get( self.d, "123", provider.ProviderFactory.get_provider, default_settings.PROVIDERS) print item.metrics assert_equals(item.metrics["wikipedia:mentions"]["provenance_url"], "http://en.wikipedia.org/wiki/Special:Search?search='10.1371/journal.pmed.0020124'&go=Go")