def test_actor_init(self): o = Actor('nico') o.add_film('spyderman') self.assertEqual(o.name, 'nico') self.assertIsInstance(o, Actor) self.assertIsInstance(o.films, list) self.assertEqual(len(o.films), 1)
def test_add_film(self): actor = Actor('Nicolas Zanforlini') actor.add_film('Lord of war') self.assertEqual(len(actor.films), 1) self.assertEqual(actor.films[0], 'Lord of war') actor.add_film('Wow') self.assertEqual(len(actor.films), 2) self.assertEqual(actor.films[0], 'Lord of war') self.assertEqual(actor.films[1], 'Wow')
def test_load_actor(self): client = MongoClient('localhost', 27017) db = client.unittest_pymongo self.assertEqual(db, client.unittest_pymongo) o = Actor('nicolas') o.add_film('wow') di = {o.name: o.films} self.assertIsInstance(di, dict) self.assertIsNotNone(di)
def test_load(self): actor = Actor('Nicolas Zanforlini') actor.add_film('Lord of war') actor.add_film('Wow') actor.load(db) actor_document = db.actors.find_one({'name': 'Nicolas Zanforlini'}) self.assertIsNotNone(actor_document) self.assertEqual(actor_document.get('name'), 'Nicolas Zanforlini') self.assertEqual(actor_document.get('films'), ['Lord of war', 'Wow']) db.actors.delete_many({'name': 'Nicolas Zanforlini'})
def clean_actor(actor): actor_nettoye = actor.replace('\n', '').replace('.', '').strip() return actor_nettoye def clean_title(title): title_nettoye = title.replace('\xa0', '') return title_nettoye dico_actor = {} for film in db.films.find(): for actor in film.get('actors'): act = clean_actor(actor) if act not in dico_actor: dico_actor[act] = [] tit = film.get('title') titre = clean_title(tit) dico_actor[act].append(titre) print(dico_actor) for act, tit in dico_actor.items(): o = Actor(act) for t in tit: o.add_film(t) o.load(db) client.close()
def test_add_film(self): o = Actor('test') o.add_film('blabla') self.assertEqual(o.films[0], 'blabla')