Beispiel #1
0
    def person_wrong_name( self ):
        self.initialize()

        person = Person()
        person.name = "Ela"
        person.surname = "Gabor"
        person.permalink = "ela-gabor"
        person.imdb_code = "3140529"
        person.verified_imdb_code = True
        person.type = Person.TYPE_PERSON
        person.save()

        fti1 = FilmToImport( user = self.user, title = '180/100',
                imdb_url = 'http://www.imdb.com/title/tt2089598/', imdb_id = '2089598', status = FilmToImport.ACCEPTED )
        fti1.save()

        run( False, False, False, False, False, True, "http" )

        self.assertEqual( Person.objects.filter( imdb_code__isnull=True ).count(), 0 )

        person = Person.objects.get( imdb_code="3140529" )
        self.assertEqual( person.name, "Ela" )
        self.assertEqual( person.surname, "Gabor" )
        self.assertEqual( person.permalink, "ela-gabor" )
        self.assertEqual( person.import_comment, "imdb name: Éva Gábor" )
Beispiel #2
0
class RequestTestCase(TestCase):
    fixtures = ['test_users.json']

    def setUp(self):
        self.user = User.objects.get(username='******')
        self.user.set_password('bob')
        self.user.save()

        self.film = Film(type=1,
                         permalink='przypadek',
                         imdb_code=111,
                         status=1,
                         version=1,
                         release_year=1999,
                         title='Przypadek',
                         popularity=1,
                         popularity_month=1)
        self.film.save()

        self.person = Person(type=2,
                             permalink='jerzy-stuhr',
                             imdb_code=112,
                             status=1,
                             version=1,
                             name='Jerzy',
                             surname='Stuhr')
        self.person.save()

    def test_index(self):
        response = self.client.get('/')
        self.failUnlessEqual(response.status_code, 200)

    def test_film(self):
        response = self.client.get('/film/przypadek/')
        self.failUnlessEqual(response.status_code, 200)

    def test_film_authorized(self):
        self.client.login(username='******', password='******')
        response = self.client.get('/film/przypadek/')
        self.assertTrue(response.context['request'].user.is_authenticated())
        self.failUnlessEqual(response.status_code, 200)

    def test_person(self):
        response = self.client.get(self.person.get_absolute_url())
        self.assertEquals(response.status_code, 200)

    def test_person_authorized(self):
        self.client.login(username='******', password='******')
        response = self.client.get(self.person.get_absolute_url())
        self.assertEquals(response.status_code, 200)

    def tearDown(self):
        Person.objects.all().delete()
        Film.objects.all().delete()
Beispiel #3
0
class RequestTestCase(TestCase):
    fixtures = ['test_users.json']
    def setUp(self):
        self.user = User.objects.get(username='******')
        self.user.set_password('bob')
        self.user.save()
        
        self.film = Film(type=1, permalink='przypadek', imdb_code=111, status=1, version=1, 
            release_year=1999, title='Przypadek', popularity=1, popularity_month=1)
        self.film.save()
        
        self.person = Person(type=2, permalink='jerzy-stuhr', imdb_code=112, status=1, version=1, 
            name='Jerzy', surname='Stuhr')
        self.person.save()

    
    def test_index(self):
        response = self.client.get('/')
        self.failUnlessEqual(response.status_code, 200)

    def test_film(self):
        response = self.client.get('/film/przypadek/')
        self.failUnlessEqual(response.status_code, 200)

    def test_film_authorized(self):
        self.client.login(username='******', password='******')
        response = self.client.get('/film/przypadek/')
        self.assertTrue(response.context['request'].user.is_authenticated())
        self.failUnlessEqual(response.status_code, 200)

    def test_person(self):
        response = self.client.get(self.person.get_absolute_url())
        self.assertEquals(response.status_code, 200)
    
    def test_person_authorized(self):
        self.client.login(username='******', password='******')
        response = self.client.get(self.person.get_absolute_url())
        self.assertEquals(response.status_code, 200)

    def tearDown(self):
        Person.objects.all().delete()
        Film.objects.all().delete()
Beispiel #4
0
    def duplicate_imdb_code(self):
        """
            ImdbFetcher is trying to save person with imdb_code,
            already existing in db
        """

        self.initialize()

        self.client.login(username=self.user.username, password='******')

        per = Person()
        per.name = "Jan"
        per.surname = "Kowalski"
        # Schwarzeneger's imdb code
        per.imdb_code = "0000216"
        per.type = Person.TYPE_PERSON
        per.save()
        per = Person.objects.get(imdb_code="0000216")
        self.assertEqual(per.name, "Jan")
        self.assertEqual(per.imdb_code, "0000216")
        
        film_to_import = FilmToImport(user = self.user,
                                  title = 'Terminator',
                                  imdb_url = 'http://www.imdb.com/title/tt0088247/',
					              imdb_id='0088247',
                                  status=FilmToImport.ACCEPTED)
        film_to_import.save()

        run(False, False, False, False, False, True, "http")

        person = Person.objects.get(imdb_code="0000216")
        # duplicate imdb_code - should be set to Schwarzenegger
        self.assertEqual(person.name, "Arnold")
        self.assertEqual(person.surname, "Schwarzenegger")
        self.assertEqual(person.verified_imdb_code, True)
        self.assertEqual(person.imdb_code, "0000216")

        # old imdb_code should be set to None
        per = Person.objects.get(name="Jan", surname="Kowalski")
        self.assertEqual(per.imdb_code, None)
Beispiel #5
0
    def duplicate_imdb_code(self):
        """
            ImdbFetcher is trying to save person with imdb_code,
            already existing in db
        """

        self.initialize()

        self.client.login(username=self.user.username, password='******')

        per = Person()
        per.name = "Jan"
        per.surname = "Kowalski"
        # Schwarzeneger's imdb code
        per.imdb_code = "0000216"
        per.type = Person.TYPE_PERSON
        per.save()
        per = Person.objects.get(imdb_code="0000216")
        self.assertEqual(per.name, "Jan")
        self.assertEqual(per.imdb_code, "0000216")
        
        film_to_import = FilmToImport(user = self.user,
                                  title = 'Terminator',
                                  imdb_url = 'http://www.imdb.com/title/tt0088247/',
					              imdb_id='0088247',
                                  status=FilmToImport.ACCEPTED)
        film_to_import.save()

        run(False, False, False, False, False, True, "http")

        person = Person.objects.get(imdb_code="0000216")
        # duplicate imdb_code - should be set to Schwarzenegger
        self.assertEqual(person.name, "Arnold")
        self.assertEqual(person.surname, "Schwarzenegger")
        self.assertEqual(person.verified_imdb_code, True)
        self.assertEqual(person.imdb_code, "0000216")

        # old imdb_code should be set to None
        per = Person.objects.get(name="Jan", surname="Kowalski")
        self.assertEqual(per.imdb_code, None)
Beispiel #6
0
    def unique_imdb_code(self):
        """
            There is a person in db but, he has different imdb_code
        """

        self.initialize()

        self.client.login(username=self.user.username, password='******')

        per = Person()
        per.name = "Jan"
        per.surname = "Kowalski"
        # Schwarzeneger's imdb code
        per.imdb_code = "0101010101010101"
        per.type = Person.TYPE_PERSON
        per.save()
        per = Person.objects.get(imdb_code="0101010101010101")
        self.assertEqual(per.name, "Jan")
        self.assertEqual(per.imdb_code, "0101010101010101")

        film_to_import = FilmToImport(user = self.user,
                                  title = 'Terminator',
                                  imdb_url = 'http://www.imdb.com/title/tt0088247/',
					              imdb_id='0088247',
                                  status=FilmToImport.ACCEPTED)
        film_to_import.save()

        run(False, False, False, False, False, True, "http")

        person = Person.objects.get(imdb_code="0000216")
        self.assertEqual(person.name, "Arnold")
        self.assertEqual(person.surname, "Schwarzenegger")
        self.assertEqual(person.verified_imdb_code, True)
        self.assertEqual(person.imdb_code, "0000216")

        # old imdb_code should be set to None
        per = Person.objects.get(name="Jan", surname="Kowalski")
        self.assertEqual(per.imdb_code, "0101010101010101")
Beispiel #7
0
    def person_short_imdb_code( self ):
        self.initialize()

        person = Person()
        person.name = "Harrison"
        person.surname = "Ford"
        person.permalink = "harrison-ford"
        person.imdb_code = "148"
        person.type = Person.TYPE_PERSON
        person.save()

        person = Person()
        person.name = "Harrison"
        person.surname = "Ford (II)"
        person.permalink = "harrison-ford-ii"
        person.imdb_code = "0001230"
        person.type = Person.TYPE_PERSON
        person.save()

        self.assertEqual( Person.objects.filter( imdb_code='148' ).count(), 1 )

        fti1 = FilmToImport( user = self.user, title = 'Love in High Gear',
                imdb_url = 'http://www.imdb.com/title/tt0022093/', imdb_id = '0022093', status = FilmToImport.ACCEPTED )
        fti1.save()

        fti2 = FilmToImport( user = self.user, title = 'Journey to shiloh',
                imdb_url = 'http://www.imdb.com/title/tt0063161/', imdb_id = '0063161', status = FilmToImport.ACCEPTED )
        fti2.save()

        fti3 = FilmToImport( user = self.user, title = 'Guys choice Awards 2011',
                imdb_url = 'http://www.imdb.com/title/tt2023505/', imdb_id = '2023505', status = FilmToImport.ACCEPTED )
        fti3.save()

        run( False, False, False, False, False, True, "http" )

        self.assertEqual( Person.objects.filter( name='Harrison', surname__contains='Ford' ).count(), 2 )
        self.assertEqual( Person.objects.filter( imdb_code='148' ).count(), 0 )
        self.assertEqual( Person.objects.get( imdb_code='0000148' ).permalink, 'harrison-ford' )
Beispiel #8
0
    def unique_imdb_code(self):
        """
            There is a person in db but, he has different imdb_code
        """

        self.initialize()

        self.client.login(username=self.user.username, password='******')

        per = Person()
        per.name = "Jan"
        per.surname = "Kowalski"
        # Schwarzeneger's imdb code
        per.imdb_code = "0101010101010101"
        per.type = Person.TYPE_PERSON
        per.save()
        per = Person.objects.get(imdb_code="0101010101010101")
        self.assertEqual(per.name, "Jan")
        self.assertEqual(per.imdb_code, "0101010101010101")

        film_to_import = FilmToImport(user = self.user,
                                  title = 'Terminator',
                                  imdb_url = 'http://www.imdb.com/title/tt0088247/',
					              imdb_id='0088247',
                                  status=FilmToImport.ACCEPTED)
        film_to_import.save()

        run(False, False, False, False, False, True, "http")

        person = Person.objects.get(imdb_code="0000216")
        self.assertEqual(person.name, "Arnold")
        self.assertEqual(person.surname, "Schwarzenegger")
        self.assertEqual(person.verified_imdb_code, True)
        self.assertEqual(person.imdb_code, "0000216")

        # old imdb_code should be set to None
        per = Person.objects.get(name="Jan", surname="Kowalski")
        self.assertEqual(per.imdb_code, "0101010101010101")
Beispiel #9
0
class BaseTest(TestCase):
    def setUp(self):
        User.objects.all().delete()
        Person.objects.all().delete()
        Country.objects.all().delete()
        AddedFilm.objects.all().delete()
        AddedCharacter.objects.all().delete()

        self.client = Client(follow=True)

        # test user with no permissions
        self.u1 = User.objects.create_user("user", "*****@*****.**", "user")

        # test user with moderation_permission
        self.u2 = User.objects.create_user("root", "*****@*****.**", "root")
        self.u2.user_permissions.add(
            Permission.objects.get(codename="can_accept_added_films"))
        self.u2.save()

        # some persons
        self.p1 = Person(name="Clint",
                         surname="Eastwood",
                         imdb_code=None,
                         type=Person.TYPE_PERSON)
        self.p1.save()

        self.p2 = Person(name="Sylwester",
                         surname="Stallone",
                         imdb_code=None,
                         type=Person.TYPE_PERSON)
        self.p2.save()

        self.p3 = Person(name="Jack",
                         surname="Mort",
                         imdb_code=None,
                         type=Person.TYPE_PERSON)
        self.p3.save()

        # countries
        self.c1 = Country.objects.create(country='USA')
        self.c2 = Country.objects.create(country='Poland')

    def tearDown(self):
        User.objects.all().delete()
        Person.objects.all().delete()
        Country.objects.all().delete()
        AddedFilm.objects.all().delete()
        AddedCharacter.objects.all().delete()
Beispiel #10
0
class BaseTest( TestCase ):
    
    def setUp( self ):
        User.objects.all().delete()
        Person.objects.all().delete()
        Country.objects.all().delete()
        AddedFilm.objects.all().delete()
        AddedCharacter.objects.all().delete()

        self.client = Client( follow=True )

        # test user with no permissions
        self.u1 = User.objects.create_user( "user", "*****@*****.**", "user" )
        
        # test user with moderation_permission
        self.u2 = User.objects.create_user( "root", "*****@*****.**", "root" )
        self.u2.user_permissions.add( Permission.objects.get( codename="can_accept_added_films" ) )
        self.u2.save()
        
        # some persons
        self.p1 = Person( name="Clint", surname="Eastwood", imdb_code=None, type=Person.TYPE_PERSON  )
        self.p1.save()

        self.p2 = Person( name="Sylwester", surname="Stallone", imdb_code=None, type=Person.TYPE_PERSON  )
        self.p2.save()

        self.p3 = Person( name="Jack", surname="Mort", imdb_code=None, type=Person.TYPE_PERSON  )
        self.p3.save()

        # countries
        self.c1 = Country.objects.create( country='USA' )
        self.c2 = Country.objects.create( country='Poland' )

    def tearDown( self ):
        User.objects.all().delete()
        Person.objects.all().delete()
        Country.objects.all().delete()
        AddedFilm.objects.all().delete()
        AddedCharacter.objects.all().delete()
Beispiel #11
0
    def initialize(self):
        self.clean_data()

        # set up users
        self.u1 = User.objects.create_user('michuk',
                                           '*****@*****.**',
                                           'secret')
        self.u1.save()
        self.u2 = User(username='******', email='*****@*****.**')
        self.u2.save()
        self.u3 = User(username='******', email='*****@*****.**')
        self.u3.save()
        self.u4 = User(username='******', email='*****@*****.**')
        self.u4.save()

        self.contest = Contest()
        self.contest.name = "Plebiscyt"
        self.contest.start_date = datetime.now()
        self.contest.end_date = datetime.now()
        self.contest.event_status = Contest.STATUS_OPEN
        self.contest.type = Object.TYPE_CONTEST
        self.contest.permalink = "plebiscyt"
        self.contest.save()

        film1 = Film()
        film1.title = "Battlefield Earth II"
        film1.type = Object.TYPE_FILM
        film1.permalink = "/battlefirld-earth-ii/"
        film1.release_year = 2010
        film1.save()

        person1 = Person()
        person1.name = "John"
        person1.surname = "Travolta"
        person1.type = Object.TYPE_PERSON
        person1.permalink = "/john-travolta/"
        person1.save()

        person2 = Person()
        person2.name = "Tom"
        person2.surname = "Cruise"
        person2.type = Object.TYPE_PERSON
        person2.permalink = "/tom-cruise/"
        person2.save()

        person3 = Person()
        person3.name = "Forrest"
        person3.surname = "Whitaker"
        person3.type = Object.TYPE_PERSON
        person3.permalink = "/forrest-whitaker/"
        person3.save()

        character1 = Character()
        character1.character = "Scientologist 1"
        character1.person = person1
        character1.film = film1
        character1.save()
        self.character1 = character1

        character2 = Character()
        character2.character = "Scientologist 2"
        character2.person = person2
        character2.film = film1
        character2.save()
        self.character2 = character2

        character3 = Character()
        character3.character = "A non-scientologist"
        character3.person = person3
        character3.film = film1
        character3.save()
        self.character3 = character3

        today = datetime.today()
        tomorrow = today + timedelta(1)

        self.game1 = Game()
        self.game1.contest = self.contest
        self.game1.type = Object.TYPE_GAME
        self.game1.permalink = "scientologist-1-vs-scientologist-2"
        self.game1.level = Game.LEVEL_32
        self.game1.character1 = character1
        self.game1.character2 = character2
        self.game1.start_date = today
        self.game1.end_date = today
        self.game1.save()

        self.game2 = Game()
        self.game2.contest = self.contest
        self.game2.type = Object.TYPE_GAME
        self.game1.permalink = "scientologist-1-vs-scientologist-2"
        self.game2.level = Game.LEVEL_32
        self.game2.character1 = character1
        self.game2.character2 = character2
        self.game2.start_date = tomorrow
        self.game2.end_date = tomorrow
        self.game2.save()
Beispiel #12
0
class QueriesTestCase(TestCase):
    def setUp(self):
        User.objects.all().delete()

        Object.objects.all().delete()
        Film.objects.all().delete()
        FilmLog.objects.all().delete()
        Rating.objects.all().delete()
        Recommendation.objects.all().delete()
        Person.objects.all().delete()

        AddedCharacter.objects.all().delete()
        AddedFilm.objects.all().delete()

        # set up users
        self.u1 = User(username="******", email="*****@*****.**")
        self.u1.save()

        self.u2 = User.objects.create_user("root", "*****@*****.**", "root")
        self.u2.user_permissions.add(Permission.objects.get(codename="can_accept_added_films"))
        self.u2.save()

        self.u3 = User(username="******", email="*****@*****.**")
        self.u3.save()

        self.u4 = User(username="******", email="*****@*****.**")
        self.u4.save()

        self.u5 = User(username="******", email="*****@*****.**")
        self.u5.save()

        self.u6 = User(username="******", email="*****@*****.**")
        self.u6.save()

        # some persons
        self.p1 = Person(name="Clint", surname="Eastwood", imdb_code=None, type=Person.TYPE_PERSON)
        self.p1.save()

        self.p2 = Person(name="Sylvester", surname="Stallone", imdb_code=None, type=Person.TYPE_PERSON)
        self.p2.save()

        self.p3 = Person(name="Jack", surname="Mort", imdb_code=None, type=Person.TYPE_PERSON)
        self.p3.save()

        # set up films
        self.f1 = Film(
            type=1,
            permalink="przypadek",
            imdb_code=111,
            status=1,
            version=1,
            release_year=1999,
            title="Przypadek",
            popularity=20,
            popularity_month=10,
        )
        self.f1.save()

        self.f2 = Film(
            type=1,
            permalink="wrestler",
            imdb_code=112,
            status=1,
            version=1,
            release_year=2008,
            title="Wrestler",
            popularity=10,
            popularity_month=1,
        )
        self.f2.save()

        self.f3 = Film(
            type=1,
            permalink="american-history-x",
            imdb_code=113,
            status=1,
            version=1,
            release_year=1998,
            title="American History X",
            popularity=1,
            popularity_month=1,
        )
        self.f3.save()

        self.f4 = Film(
            type=1,
            permalink="the-big-lebowski",
            imdb_code=114,
            status=1,
            version=1,
            release_year=1998,
            title="The Big Lebowski",
            popularity=1,
            popularity_month=1,
        )
        self.f4.save()

        self.f5 = Film(
            type=1,
            permalink="the-lord-of-the-rings-the-fellowship-of-the-ring",
            imdb_code=115,
            status=1,
            version=1,
            release_year=2001,
            title="The Lord of the Rings: The Fellowship of the Ring",
            popularity=1,
            popularity_month=1,
        )
        self.f5.save()

        self.f6 = Film(
            type=1,
            permalink="raiders-of-the-lost-ark",
            imdb_code=116,
            status=1,
            version=1,
            release_year=1981,
            title="Raiders of the Lost Ark",
            popularity=1,
            popularity_month=1,
        )
        self.f6.save()

        self.f7 = Film(
            type=1,
            permalink="the-alien",
            imdb_code=117,
            status=1,
            version=1,
            release_year=1979,
            title="The Alien",
            popularity=1,
            popularity_month=1,
        )
        self.f7.save()

        self.f8 = Film(
            type=1,
            permalink="terminator-2-judgment-day",
            imdb_code=118,
            status=1,
            version=1,
            release_year=1991,
            title="Terminator 2: Judgment Day",
            popularity=1,
            popularity_month=1,
        )
        self.f8.save()

        self.f9 = Film(
            type=1,
            permalink="przypadek",
            imdb_code=119,
            status=1,
            version=1,
            release_year=1999,
            title="Przypadek",
            popularity=20,
            popularity_month=10,
        )
        self.f9.save()

        self.f10 = Film(
            type=1,
            permalink="wrestler",
            imdb_code=120,
            status=1,
            version=1,
            release_year=2008,
            title="Wrestler",
            popularity=10,
            popularity_month=1,
        )
        self.f10.save()

        self.f11 = Film(
            type=1,
            permalink="american-history-x",
            imdb_code=121,
            status=1,
            version=1,
            release_year=1998,
            title="American History X",
            popularity=1,
            popularity_month=1,
        )
        self.f11.save()

        self.f12 = Film(
            type=1,
            permalink="the-big-lebowski",
            imdb_code=122,
            status=1,
            version=1,
            release_year=1998,
            title="The Big Lebowski",
            popularity=1,
            popularity_month=1,
        )
        self.f12.save()

        self.f13 = Film(
            type=1,
            permalink="the-lord-of-the-rings-the-fellowship-of-the-ring",
            imdb_code=123,
            status=1,
            version=1,
            release_year=2001,
            title="The Lord of the Rings: The Fellowship of the Ring",
            popularity=1,
            popularity_month=1,
        )
        self.f13.save()

        self.f14 = Film(
            type=1,
            permalink="raiders-of-the-lost-ark",
            imdb_code=124,
            status=1,
            version=1,
            release_year=1981,
            title="Raiders of the Lost Ark",
            popularity=1,
            popularity_month=1,
        )
        self.f14.save()

        self.f15 = Film(
            type=1,
            permalink="the-alien",
            imdb_code=125,
            status=1,
            version=1,
            release_year=1979,
            title="The Alien",
            popularity=1,
            popularity_month=1,
        )
        self.f15.save()

        self.f16 = Film(
            type=1,
            permalink="przypadek",
            imdb_code=126,
            status=1,
            version=1,
            release_year=1999,
            title="Przypadek",
            popularity=20,
            popularity_month=10,
        )
        self.f16.save()

        self.f17 = Film(
            type=1,
            permalink="wrestler",
            imdb_code=127,
            status=1,
            version=1,
            release_year=2008,
            title="Wrestler",
            popularity=10,
            popularity_month=1,
        )
        self.f17.save()

        self.f18 = Film(
            type=1,
            permalink="american-history-x",
            imdb_code=128,
            status=1,
            version=1,
            release_year=1998,
            title="American History X",
            popularity=1,
            popularity_month=1,
        )
        self.f18.save()

        self.f19 = Film(
            type=1,
            permalink="the-big-lebowski",
            imdb_code=129,
            status=1,
            version=1,
            release_year=1998,
            title="The Big Lebowski",
            popularity=1,
            popularity_month=1,
        )
        self.f19.save()

        self.f20 = Film(
            type=1,
            permalink="the-lord-of-the-rings-the-fellowship-of-the-ring",
            imdb_code=130,
            status=1,
            version=1,
            release_year=2001,
            title="The Lord of the Rings: The Fellowship of the Ring",
            popularity=1,
            popularity_month=1,
        )
        self.f20.save()

        self.f21 = Film(
            type=1,
            permalink="raiders-of-the-lost-ark",
            imdb_code=131,
            status=1,
            version=1,
            release_year=1981,
            title="Raiders of the Lost Ark",
            popularity=1,
            popularity_month=1,
        )
        self.f21.save()

        self.f22 = Film(
            type=1,
            permalink="the-alien",
            imdb_code=132,
            status=1,
            version=1,
            release_year=1979,
            title="The Alien",
            popularity=1,
            popularity_month=1,
        )
        self.f22.save()

        self.f23 = Film(
            type=1,
            permalink="terminator-2-judgment-day",
            imdb_code=133,
            status=1,
            version=1,
            release_year=1991,
            title="Terminator 2: Judgment Day",
            popularity=1,
            popularity_month=1,
        )
        self.f23.save()

        self.f24 = Film(
            type=1,
            permalink="przypadek",
            imdb_code=134,
            status=1,
            version=1,
            release_year=1999,
            title="Przypadek",
            popularity=20,
            popularity_month=10,
        )
        self.f24.save()

        self.f25 = Film(
            type=1,
            permalink="wrestler",
            imdb_code=135,
            status=1,
            version=1,
            release_year=2008,
            title="Wrestler",
            popularity=10,
            popularity_month=1,
        )
        self.f25.save()

        self.f26 = Film(
            type=1,
            permalink="american-history-x",
            imdb_code=136,
            status=1,
            version=1,
            release_year=1998,
            title="American History X",
            popularity=1,
            popularity_month=1,
        )
        self.f26.save()

        self.f27 = Film(
            type=1,
            permalink="the-big-lebowski",
            imdb_code=137,
            status=1,
            version=1,
            release_year=1998,
            title="The Big Lebowski",
            popularity=1,
            popularity_month=1,
        )
        self.f27.save()

        self.f28 = Film(
            type=1,
            permalink="the-lord-of-the-rings-the-fellowship-of-the-ring",
            imdb_code=138,
            status=1,
            version=1,
            release_year=2001,
            title="The Lord of the Rings: The Fellowship of the Ring",
            popularity=1,
            popularity_month=1,
        )
        self.f28.save()

        self.f29 = Film(
            type=1,
            permalink="raiders-of-the-lost-ark",
            imdb_code=139,
            status=1,
            version=1,
            release_year=1981,
            title="Raiders of the Lost Ark",
            popularity=1,
            popularity_month=1,
        )
        self.f29.save()

        self.f30 = Film(
            type=1,
            permalink="the-alien",
            imdb_code=140,
            status=1,
            version=1,
            release_year=1979,
            title="The Alien",
            popularity=1,
            popularity_month=1,
        )
        self.f30.save()

        self.f31 = AddedFilm(
            title="Added film part 1", localized_title="Added film PL", release_year=2010, user=self.u1
        )
        self.f31.save()

        self.f31.directors.add(self.p1)
        self.f31.save()

        AddedCharacter.objects.create(added_film=self.f31, person=self.p3, character="Jack Mort")

        self.f31.accept(self.u2)

        self.f32 = AddedFilm(
            title="Added film part 2", localized_title="Added film PL", release_year=2010, user=self.u1
        )
        self.f32.save()

        self.f32.directors.add(self.p2)
        self.f32.save()

        AddedCharacter.objects.create(added_film=self.f32, person=self.p3, character="Jack Mort")

        self.f32.accept(self.u2)

        # tagging
        self.f1.save_tags("comedy drama horror animation mystery")
        self.f2.save_tags("comedy sci-fi thriller series")
        self.f3.save_tags("comedy")
        self.f4.save_tags("sci-fi")
        self.f5.save_tags("horror")
        self.f6.save_tags("thriller")
        self.f7.save_tags("drama")
        self.f8.save_tags("series")
        self.f9.save_tags("sci-fi")
        self.f10.save_tags("fantasy")
        self.f11.save_tags("mystery")
        self.f12.save_tags("action")
        self.f13.save_tags("adventure")
        self.f14.save_tags("animation")
        self.f15.save_tags("family")
        self.f16.save_tags("thriller")
        self.f17.save_tags("drama")
        self.f18.save_tags("series")
        self.f19.save_tags("sci-fi")
        self.f20.save_tags("fantasy")
        self.f21.save_tags("comedy")
        self.f22.save_tags("animation")
        self.f23.save_tags("comedy")
        self.f24.save_tags("sci-fi")
        self.f25.save_tags("horror")
        self.f26.save_tags("thriller")
        self.f27.save_tags("drama")
        self.f28.save_tags("series")
        self.f29.save_tags("sci-fi")
        self.f30.save_tags("fantasy")

        # First user ratings
        save_rating(self.f1, self.u1, 1)
        save_rating(self.f2, self.u1, 2)
        save_rating(self.f3, self.u1, 3)
        save_rating(self.f4, self.u1, 4)
        save_rating(self.f5, self.u1, 5)
        save_rating(self.f6, self.u1, 6)
        save_rating(self.f7, self.u1, 7)
        save_rating(self.f8, self.u1, 8)
        save_rating(self.f9, self.u1, 9)
        save_rating(self.f10, self.u1, 10)
        save_rating(self.f11, self.u1, 1)
        save_rating(self.f12, self.u1, 2)
        save_rating(self.f13, self.u1, 3)
        save_rating(self.f14, self.u1, 4)
        save_rating(self.f15, self.u1, 5)
        save_rating(self.f16, self.u1, 6)
        save_rating(self.f17, self.u1, 7)
        save_rating(self.f18, self.u1, 8)
        save_rating(self.f19, self.u1, 9)
        save_rating(self.f20, self.u1, 10)
        save_rating(self.f21, self.u1, 1)
        save_rating(self.f22, self.u1, 2)
        save_rating(self.f23, self.u1, 3)
        save_rating(self.f24, self.u1, 4)
        save_rating(self.f25, self.u1, 5)
        save_rating(self.f26, self.u1, 6)
        save_rating(self.f27, self.u1, 7)
        save_rating(self.f28, self.u1, 8)
        save_rating(self.f29, self.u1, 9)
        save_rating(self.f30, self.u1, 10)

        # Second user ratings - more than minimal common films requirement (15)
        save_rating(self.f1, self.u2, 1)
        save_rating(self.f2, self.u2, 2)
        save_rating(self.f3, self.u2, 3)
        save_rating(self.f4, self.u2, 4)
        save_rating(self.f5, self.u2, 5)
        save_rating(self.f6, self.u2, 6)
        save_rating(self.f7, self.u2, 7)
        save_rating(self.f8, self.u2, 8)
        save_rating(self.f9, self.u2, 9)
        save_rating(self.f10, self.u2, 10)
        save_rating(self.f11, self.u2, 1)
        # save_rating(self.f12, self.u2, 2)
        save_rating(self.f13, self.u2, 3)
        # save_rating(self.f14, self.u2, 4)
        save_rating(self.f15, self.u2, 5)
        # save_rating(self.f16, self.u2, 6)
        save_rating(self.f17, self.u2, 7)
        # save_rating(self.f18, self.u2, 8)
        save_rating(self.f19, self.u2, 9)
        # save_rating(self.f20, self.u2, 10)
        save_rating(self.f21, self.u2, 1)
        # save_rating(self.f22, self.u2, 2)
        save_rating(self.f23, self.u2, 3)
        # save_rating(self.f24, self.u2, 4)
        save_rating(self.f25, self.u2, 5)
        # save_rating(self.f26, self.u2, 6)
        save_rating(self.f27, self.u2, 7)
        # save_rating(self.f28, self.u2, 8)
        save_rating(self.f29, self.u2, 9)
        # save_rating(self.f30, self.u2, 10)

        # Third user ratings - equal with minimal common films requirement (15)
        # save_rating(self.f1, self.u3, 1)
        # save_rating(self.f2, self.u3, 2)
        # save_rating(self.f3, self.u3, 3)
        # save_rating(self.f4, self.u3, 4)
        # save_rating(self.f5, self.u3, 5)
        # save_rating(self.f6, self.u3, 6)
        # save_rating(self.f7, self.u3, 7)
        # save_rating(self.f8, self.u3, 8)
        # save_rating(self.f9, self.u3, 9)
        # save_rating(self.f10, self.u3, 10)
        # save_rating(self.f11, self.u3, 1)
        # save_rating(self.f12, self.u3, 2)
        # save_rating(self.f13, self.u3, 3)
        # save_rating(self.f14, self.u3, 4)
        # save_rating(self.f15, self.u3, 5)
        save_rating(self.f16, self.u3, 6)
        save_rating(self.f17, self.u3, 7)
        save_rating(self.f18, self.u3, 8)
        save_rating(self.f19, self.u3, 9)
        save_rating(self.f20, self.u3, 10)
        save_rating(self.f21, self.u3, 1)
        save_rating(self.f22, self.u3, 2)
        save_rating(self.f23, self.u3, 3)
        save_rating(self.f24, self.u3, 4)
        save_rating(self.f25, self.u3, 5)
        save_rating(self.f26, self.u3, 6)
        save_rating(self.f27, self.u3, 7)
        save_rating(self.f28, self.u3, 8)
        save_rating(self.f29, self.u3, 9)
        save_rating(self.f30, self.u3, 10)

        # Fourth user ratings - less than minimal films requirement
        # save_rating(self.f1, self.u4, 1)
        # save_rating(self.f2, self.u4, 2)
        # save_rating(self.f3, self.u4, 3)
        # save_rating(self.f4, self.u4, 4)
        # save_rating(self.f5, self.u4, 5)
        # save_rating(self.f6, self.u4, 6)
        # save_rating(self.f7, self.u4, 7)
        # save_rating(self.f8, self.u4, 8)
        # save_rating(self.f9, self.u4, 9)
        # save_rating(self.f10, self.u4, 10)
        # save_rating(self.f11, self.u4, 1)
        # save_rating(self.f12, self.u4, 2)
        # save_rating(self.f13, self.u4, 3)
        # save_rating(self.f14, self.u4, 4)
        # save_rating(self.f15, self.u4, 5)
        # save_rating(self.f16, self.u4, 6)
        # save_rating(self.f17, self.u4, 7)
        # save_rating(self.f18, self.u4, 8)
        # save_rating(self.f19, self.u4, 9)
        # save_rating(self.f20, self.u4, 10)
        save_rating(self.f21, self.u4, 1)
        save_rating(self.f22, self.u4, 2)
        save_rating(self.f23, self.u4, 3)
        save_rating(self.f24, self.u4, 4)
        save_rating(self.f25, self.u4, 5)
        save_rating(self.f26, self.u4, 6)
        save_rating(self.f27, self.u4, 7)
        save_rating(self.f28, self.u4, 8)
        save_rating(self.f29, self.u4, 9)
        save_rating(self.f30, self.u4, 10)

        # Fifth user ratings - the same films rated (1st and 5th user) but different
        # ratings
        # save_rating(self.f1, self.u5, 10)
        # save_rating(self.f2, self.u5, 9)
        # save_rating(self.f3, self.u5, 8)
        # save_rating(self.f4, self.u5, 7)
        # save_rating(self.f5, self.u5, 6)
        # save_rating(self.f6, self.u5, 5)
        # save_rating(self.f7, self.u5, 4)
        # save_rating(self.f8, self.u5, 3)
        # save_rating(self.f9, self.u5, 2)
        # save_rating(self.f10, self.u5, 1)
        # save_rating(self.f11, self.u5, 10)
        # save_rating(self.f12, self.u5, 9)
        # save_rating(self.f13, self.u5, 8)
        # save_rating(self.f14, self.u5, 7)
        # save_rating(self.f15, self.u5, 6)
        # save_rating(self.f16, self.u5, 5)
        # save_rating(self.f17, self.u5, 4)
        # save_rating(self.f18, self.u5, 3)
        # save_rating(self.f19, self.u5, 2)
        # save_rating(self.f20, self.u5, 1)
        # save_rating(self.f21, self.u5, 10)
        # save_rating(self.f22, self.u5, 9)
        # save_rating(self.f23, self.u5, 8)
        # save_rating(self.f24, self.u5, 7)
        # save_rating(self.f25, self.u5, 6)
        # save_rating(self.f26, self.u5, 5)
        # save_rating(self.f27, self.u5, 4)
        # save_rating(self.f28, self.u5, 3)
        # save_rating(self.f29, self.u5, 2)
        # save_rating(self.f30, self.u5, 1)

        # Sixth user ratings - the same films rated (1st and 5th user) but different
        # ratings
        save_rating(self.f1, self.u6, 10)
        save_rating(self.f2, self.u6, 10)
        save_rating(self.f3, self.u6, 10)
        save_rating(self.f4, self.u6, 10)
        save_rating(self.f5, self.u6, 10)
        save_rating(self.f6, self.u6, 10)
        save_rating(self.f7, self.u6, 10)
        save_rating(self.f8, self.u6, 10)
        save_rating(self.f9, self.u6, 10)
        save_rating(self.f10, self.u6, 10)
        save_rating(self.f11, self.u6, 10)
        save_rating(self.f12, self.u6, 10)
        save_rating(self.f13, self.u6, 10)
        save_rating(self.f14, self.u6, 10)
        save_rating(self.f15, self.u6, 10)
        save_rating(self.f16, self.u6, 10)
        save_rating(self.f17, self.u6, 10)
        save_rating(self.f18, self.u6, 10)
        save_rating(self.f19, self.u6, 10)
        save_rating(self.f20, self.u6, 10)
        save_rating(self.f21, self.u6, 10)
        save_rating(self.f22, self.u6, 10)
        save_rating(self.f23, self.u6, 10)
        save_rating(self.f24, self.u6, 10)
        save_rating(self.f25, self.u6, 10)
        save_rating(self.f26, self.u6, 10)
        save_rating(self.f27, self.u6, 10)
        save_rating(self.f28, self.u6, 10)
        save_rating(self.f29, self.u6, 10)
        save_rating(self.f30, self.u6, 10)

    @unittest.skipIf(not is_postgres, "sqlite is not supported yet")
    def test_get_recently_popular_films_query(self):
        print "Test get recently popular films query"
        """
           Test for query which gets the most popular films for the rating machine.
           First it should return all films, then we rate one and in the second call
           it should only retrieve the other film.
        """

        recom_helper = RecomHelper()
        film_helper = FilmHelper()

        u1_result = recom_helper.get_recently_popular_films_query(self.u1.id)
        self.assertEquals(len(u1_result), 2)

        u2_result = recom_helper.get_recently_popular_films_query(self.u2.id)
        self.assertEquals(len(u2_result), 12)

        film_with_director_and_actor = Film.objects.filter(title__startswith="Added film part 1")
        related_directors = film_helper.get_film_directors(film_with_director_and_actor[0])
        related_characters = film_helper.get_film_actors(film_with_director_and_actor[0])
        related_actors = []
        for character in related_characters:
            actor = character.person
            related_actors.append(actor)

        POST = {}
        # TODO check why it starts breaking (form invalid) when form_id is set
        # POST['form_id'] = '1'
        POST["rating"] = "6"
        POST["object_id"] = str(self.f12.parent.id)
        POST["form_type"] = str(Rating.TYPE_FILM)
        POST["cancel_rating"] = "0"
        rating_form = RatingForm(POST)
        if rating_form.is_valid():
            rating_helper.add_edit_rating(rating_form, self.u2)
        else:
            print "RATING FORM INVALID!"

        result = recom_helper.get_recently_popular_films_query(self.u2.id)
        self.assertEquals(len(result), 11)

        # test filtering
        result = recom_helper.get_recently_popular_films_query(self.u5.id)
        self.assertEquals(len(result), 32)

        result = recom_helper.get_recently_popular_films_query(self.u5.id, year_from="1990")
        self.assertEquals(len(result), 24)

        result = recom_helper.get_recently_popular_films_query(self.u5.id, year_to="2000")
        self.assertEquals(len(result), 22)

        result = recom_helper.get_recently_popular_films_query(self.u5.id, year_from="1990", year_to="2000")
        self.assertEquals(len(result), 14)

        result = recom_helper.get_recently_popular_films_query(self.u5.id, popularity="10")
        self.assertEquals(len(result), 8)

        result = recom_helper.get_recently_popular_films_query(
            self.u5.id, year_from="1990", year_to="2000", popularity="10"
        )
        self.assertEquals(len(result), 4)

        rated_films = [self.f1.id, self.f3.id, self.f5.id, self.f7.id, self.f9.id]
        result = recom_helper.get_recently_popular_films_query(self.u5.id, rated_films=rated_films)
        self.assertEquals(len(result), 27)

        result = recom_helper.get_recently_popular_films_query(
            self.u5.id, year_from="1990", year_to="2000", rated_films=rated_films, popularity="10"
        )
        self.assertEquals(len(result), 2)

        result = recom_helper.get_recently_popular_films_query(self.u5.id, related_director=related_directors)
        self.assertEquals(len(result), 1)

        result = recom_helper.get_recently_popular_films_query(self.u5.id, related_actor=related_actors)
        self.assertEquals(len(result), 2)

        result = recom_helper.get_recently_popular_films_query(
            self.u5.id, related_director=related_directors, related_actor=related_actors
        )
        self.assertEquals(len(result), 1)

        result = recom_helper.get_recently_popular_films_query(
            self.u5.id,
            year_from="1990",
            year_to="2011",
            related_director=related_directors,
            related_actor=related_actors,
        )
        self.assertEquals(len(result), 1)

    @unittest.skipIf(not is_postgres, "postgres dependend")
    def test_get_best_psi_films_queryset_alg2(self):
        """
           Test for query that retrieves top recommendations.
           First it should return all films, then we rate one and in the second call
           it should only retrieve the other film.
        """

        recom_helper = RecomHelper()
        film_helper = FilmHelper()

        # no recommendations yet, the result should be 0
        result = recom_helper.get_best_psi_films_queryset_alg2(self.u4.id)
        self.assertEquals(len(result), 0)

        # simulate generating recommendations (for all films) and test again, it should retrieve all films
        r1 = Recommendation()
        r1.guess_rating_alg2 = 7
        r1.film = self.f19
        r1.user = self.u4
        r1.save()
        r2 = Recommendation()
        r2.guess_rating_alg2 = 8
        r2.film = self.f20
        r2.user = self.u4
        r2.save()
        result = recom_helper.get_best_psi_films_queryset_alg2(self.u4.id)
        self.assertEquals(len(result), 2)

        # rate one film, it should be removed from the list
        POST = {}
        # TODO check why it starts breaking (form invalid) when form_id is set
        # POST['form_id'] = '1'
        POST["rating"] = "6"
        POST["object_id"] = str(self.f20.parent.id)
        POST["form_type"] = str(Rating.TYPE_FILM)
        POST["cancel_rating"] = "0"
        rating_form = RatingForm(POST)
        if rating_form.is_valid():
            rating_helper.add_edit_rating(rating_form, self.u4)
        else:
            print "RATING FORM INVALID!"

        result = recom_helper.get_recently_popular_films_query(self.u4.id)
        self.assertEquals(len(result), 21)

    def test_get_best_tci_users(self):
        """
           Test for query that retrieves top users (based on common taste).
        """
        do_create_comparators()
        recom_helper = RecomHelper()
        result = recom_helper.get_best_tci_users(self.u1)
        self.assertEquals(len(result), 3)

    def test_enrich_query_with_tags(self):
        print "Test enrich query with tags"
        recom_helper = RecomHelper()
        film_helper = FilmHelper()

        comedies = recom_helper.get_films_for_tag("comedy")
        scifis = recom_helper.get_films_for_tag("sci-fi")
        horrors = recom_helper.get_films_for_tag("horror")
        thrillers = recom_helper.get_films_for_tag("thriller")
        dramas = recom_helper.get_films_for_tag("drama")
        fantasies = recom_helper.get_films_for_tag("fantasy")
        mysteries = recom_helper.get_films_for_tag("mystery")
        animations = recom_helper.get_films_for_tag("animation")
        series = recom_helper.get_films_for_tag("series")

        self.assertEquals(len(comedies), 5)
        self.assertEquals(len(scifis), 6)
        self.assertEquals(len(horrors), 3)
        self.assertEquals(len(thrillers), 4)
        self.assertEquals(len(dramas), 4)
        self.assertEquals(len(fantasies), 3)
        self.assertEquals(len(mysteries), 2)
        self.assertEquals(len(animations), 3)
        self.assertEquals(len(series), 4)

    def test_get_all_ratings(self):
        recom_helper = RecomHelper()
        film_helper = FilmHelper()

        film_with_director_and_actor = Film.objects.filter(title__startswith="Added film part 2")
        related_directors = film_helper.get_film_directors(film_with_director_and_actor[0])
        related_characters = film_helper.get_film_actors(film_with_director_and_actor[0])
        related_actors = []
        for character in related_characters:
            actor = character.person
            related_actors.append(actor)

        non_rated_films = Film.objects.filter(rating__isnull=True, title__startswith="Added")

        POST = {}
        # POST['form_id'] = '1'
        POST["rating"] = "6"
        POST["object_id"] = str(non_rated_films[1].parent.id)
        POST["form_type"] = str(Rating.TYPE_FILM)
        POST["cancel_rating"] = "0"
        rating_form = RatingForm(POST)
        if rating_form.is_valid():
            rating_helper.add_edit_rating(rating_form, self.u1)
        else:
            print "RATING FORM INVALID!"

        all_ratings = recom_helper.get_all_ratings(self.u1.id)
        self.assertEquals(len(all_ratings), 31)

        director_ratings = recom_helper.get_all_ratings(self.u1.id, related_director=related_directors)
        self.assertEquals(len(director_ratings), 1)

        actor_ratings = recom_helper.get_all_ratings(self.u1.id, related_actor=related_actors)
        self.assertEquals(len(actor_ratings), 1)

        from_ratings = recom_helper.get_all_ratings(self.u1.id, year_from=1990)
        self.assertEquals(len(from_ratings), 23)

        to_ratings = recom_helper.get_all_ratings(self.u1.id, year_to=2000)
        self.assertEquals(len(to_ratings), 22)

        popularity_ratings = recom_helper.get_all_ratings(self.u1.id, popularity=5)
        self.assertEquals(len(popularity_ratings), 8)

        tag_ratings = recom_helper.get_all_ratings(self.u1.id, tags="comedy")
        self.assertEquals(len(tag_ratings), 5)

    def test_cache_count_ratings(self):
        recom_helper = RecomHelper()
        ratings_count = recom_helper.count_ratings(self.u3.id)
        self.assertEquals(ratings_count, 15)

        save_rating(self.f1, self.u3, 1)
        ratings_count = recom_helper.count_ratings(self.u3.id)
        self.assertEquals(ratings_count, 16)

    def tearDown(self):
        User.objects.all().delete()

        Object.objects.all().delete()
        Film.objects.all().delete()
        FilmLog.objects.all().delete()
        Rating.objects.all().delete()
        Recommendation.objects.all().delete()
        Person.objects.all().delete()

        AddedCharacter.objects.all().delete()
        AddedFilm.objects.all().delete()
Beispiel #13
0
    def initialize(self):
        self.clean_data()

        # set up users
        self.u1= User.objects.create_user('michuk', '*****@*****.**', 'secret')
        self.u1.save()
        self.u2 = User(username='******', email='*****@*****.**')
        self.u2.save()
        self.u3 = User(username='******', email='*****@*****.**')
        self.u3.save()
        self.u4 = User(username='******', email='*****@*****.**')
        self.u4.save()

        self.contest = Contest()
        self.contest.name = "Plebiscyt"
        self.contest.start_date = datetime.now()
        self.contest.end_date = datetime.now()
        self.contest.event_status = Contest.STATUS_OPEN
        self.contest.type = Object.TYPE_CONTEST
        self.contest.permalink = "plebiscyt"
        self.contest.save()

        film1 = Film()
        film1.title = "Battlefield Earth II"
        film1.type = Object.TYPE_FILM
        film1.permalink = "/battlefirld-earth-ii/"
        film1.release_year = 2010
        film1.save()

        person1 = Person()
        person1.name = "John"
        person1.surname = "Travolta"
        person1.type = Object.TYPE_PERSON
        person1.permalink = "/john-travolta/"
        person1.save()

        person2 = Person()
        person2.name = "Tom"
        person2.surname = "Cruise"
        person2.type = Object.TYPE_PERSON
        person2.permalink = "/tom-cruise/"
        person2.save()

        person3 = Person()
        person3.name = "Forrest"
        person3.surname = "Whitaker"
        person3.type = Object.TYPE_PERSON
        person3.permalink = "/forrest-whitaker/"
        person3.save()

        character1 = Character()
        character1.character = "Scientologist 1"
        character1.person = person1
        character1.film = film1
        character1.save()
        self.character1 = character1

        character2 = Character()
        character2.character = "Scientologist 2"
        character2.person = person2
        character2.film = film1
        character2.save()
        self.character2 = character2

        character3 = Character()
        character3.character = "A non-scientologist"
        character3.person = person3
        character3.film = film1
        character3.save()
        self.character3 = character3

        today = datetime.today()
        tomorrow = today + timedelta(1)

        self.game1 = Game()
        self.game1.contest = self.contest
        self.game1.type = Object.TYPE_GAME
        self.game1.permalink = "scientologist-1-vs-scientologist-2"
        self.game1.level = Game.LEVEL_32
        self.game1.character1 = character1
        self.game1.character2 = character2
        self.game1.start_date = today
        self.game1.end_date = today
        self.game1.save()

        self.game2 = Game()
        self.game2.contest = self.contest
        self.game2.type = Object.TYPE_GAME
        self.game1.permalink = "scientologist-1-vs-scientologist-2"
        self.game2.level = Game.LEVEL_32
        self.game2.character1 = character1
        self.game2.character2 = character2
        self.game2.start_date = tomorrow
        self.game2.end_date = tomorrow
        self.game2.save()
Beispiel #14
0
class QueriesTestCase(TestCase):
    def setUp(self):
        User.objects.all().delete()

        Object.objects.all().delete()
        Film.objects.all().delete()
        FilmLog.objects.all().delete()
        Rating.objects.all().delete()
        Recommendation.objects.all().delete()
        Person.objects.all().delete()

        AddedCharacter.objects.all().delete()
        AddedFilm.objects.all().delete()

        # set up users
        self.u1 = User(username='******', email='*****@*****.**')
        self.u1.save()

        self.u2 = User.objects.create_user("root", "*****@*****.**", "root")
        self.u2.user_permissions.add(
            Permission.objects.get(codename="can_accept_added_films"))
        self.u2.save()

        self.u3 = User(username='******', email='*****@*****.**')
        self.u3.save()

        self.u4 = User(username='******', email='*****@*****.**')
        self.u4.save()

        self.u5 = User(username='******', email='*****@*****.**')
        self.u5.save()

        self.u6 = User(username='******', email='*****@*****.**')
        self.u6.save()

        # some persons
        self.p1 = Person(name="Clint",
                         surname="Eastwood",
                         imdb_code=None,
                         type=Person.TYPE_PERSON)
        self.p1.save()

        self.p2 = Person(name="Sylvester",
                         surname="Stallone",
                         imdb_code=None,
                         type=Person.TYPE_PERSON)
        self.p2.save()

        self.p3 = Person(name="Jack",
                         surname="Mort",
                         imdb_code=None,
                         type=Person.TYPE_PERSON)
        self.p3.save()

        # set up films
        self.f1 = Film(type=1,
                       permalink='przypadek',
                       imdb_code=111,
                       status=1,
                       version=1,
                       release_year=1999,
                       title='Przypadek',
                       popularity=20,
                       popularity_month=10)
        self.f1.save()

        self.f2 = Film(type=1,
                       permalink='wrestler',
                       imdb_code=112,
                       status=1,
                       version=1,
                       release_year=2008,
                       title='Wrestler',
                       popularity=10,
                       popularity_month=1)
        self.f2.save()

        self.f3 = Film(type=1,
                       permalink='american-history-x',
                       imdb_code=113,
                       status=1,
                       version=1,
                       release_year=1998,
                       title='American History X',
                       popularity=1,
                       popularity_month=1)
        self.f3.save()

        self.f4 = Film(type=1,
                       permalink='the-big-lebowski',
                       imdb_code=114,
                       status=1,
                       version=1,
                       release_year=1998,
                       title='The Big Lebowski',
                       popularity=1,
                       popularity_month=1)
        self.f4.save()

        self.f5 = Film(
            type=1,
            permalink='the-lord-of-the-rings-the-fellowship-of-the-ring',
            imdb_code=115,
            status=1,
            version=1,
            release_year=2001,
            title='The Lord of the Rings: The Fellowship of the Ring',
            popularity=1,
            popularity_month=1)
        self.f5.save()

        self.f6 = Film(type=1,
                       permalink='raiders-of-the-lost-ark',
                       imdb_code=116,
                       status=1,
                       version=1,
                       release_year=1981,
                       title='Raiders of the Lost Ark',
                       popularity=1,
                       popularity_month=1)
        self.f6.save()

        self.f7 = Film(type=1,
                       permalink='the-alien',
                       imdb_code=117,
                       status=1,
                       version=1,
                       release_year=1979,
                       title='The Alien',
                       popularity=1,
                       popularity_month=1)
        self.f7.save()

        self.f8 = Film(type=1,
                       permalink='terminator-2-judgment-day',
                       imdb_code=118,
                       status=1,
                       version=1,
                       release_year=1991,
                       title='Terminator 2: Judgment Day',
                       popularity=1,
                       popularity_month=1)
        self.f8.save()

        self.f9 = Film(type=1,
                       permalink='przypadek',
                       imdb_code=119,
                       status=1,
                       version=1,
                       release_year=1999,
                       title='Przypadek',
                       popularity=20,
                       popularity_month=10)
        self.f9.save()

        self.f10 = Film(type=1,
                        permalink='wrestler',
                        imdb_code=120,
                        status=1,
                        version=1,
                        release_year=2008,
                        title='Wrestler',
                        popularity=10,
                        popularity_month=1)
        self.f10.save()

        self.f11 = Film(type=1,
                        permalink='american-history-x',
                        imdb_code=121,
                        status=1,
                        version=1,
                        release_year=1998,
                        title='American History X',
                        popularity=1,
                        popularity_month=1)
        self.f11.save()

        self.f12 = Film(type=1,
                        permalink='the-big-lebowski',
                        imdb_code=122,
                        status=1,
                        version=1,
                        release_year=1998,
                        title='The Big Lebowski',
                        popularity=1,
                        popularity_month=1)
        self.f12.save()

        self.f13 = Film(
            type=1,
            permalink='the-lord-of-the-rings-the-fellowship-of-the-ring',
            imdb_code=123,
            status=1,
            version=1,
            release_year=2001,
            title='The Lord of the Rings: The Fellowship of the Ring',
            popularity=1,
            popularity_month=1)
        self.f13.save()

        self.f14 = Film(type=1,
                        permalink='raiders-of-the-lost-ark',
                        imdb_code=124,
                        status=1,
                        version=1,
                        release_year=1981,
                        title='Raiders of the Lost Ark',
                        popularity=1,
                        popularity_month=1)
        self.f14.save()

        self.f15 = Film(type=1,
                        permalink='the-alien',
                        imdb_code=125,
                        status=1,
                        version=1,
                        release_year=1979,
                        title='The Alien',
                        popularity=1,
                        popularity_month=1)
        self.f15.save()

        self.f16 = Film(type=1,
                        permalink='przypadek',
                        imdb_code=126,
                        status=1,
                        version=1,
                        release_year=1999,
                        title='Przypadek',
                        popularity=20,
                        popularity_month=10)
        self.f16.save()

        self.f17 = Film(type=1,
                        permalink='wrestler',
                        imdb_code=127,
                        status=1,
                        version=1,
                        release_year=2008,
                        title='Wrestler',
                        popularity=10,
                        popularity_month=1)
        self.f17.save()

        self.f18 = Film(type=1,
                        permalink='american-history-x',
                        imdb_code=128,
                        status=1,
                        version=1,
                        release_year=1998,
                        title='American History X',
                        popularity=1,
                        popularity_month=1)
        self.f18.save()

        self.f19 = Film(type=1,
                        permalink='the-big-lebowski',
                        imdb_code=129,
                        status=1,
                        version=1,
                        release_year=1998,
                        title='The Big Lebowski',
                        popularity=1,
                        popularity_month=1)
        self.f19.save()

        self.f20 = Film(
            type=1,
            permalink='the-lord-of-the-rings-the-fellowship-of-the-ring',
            imdb_code=130,
            status=1,
            version=1,
            release_year=2001,
            title='The Lord of the Rings: The Fellowship of the Ring',
            popularity=1,
            popularity_month=1)
        self.f20.save()

        self.f21 = Film(type=1,
                        permalink='raiders-of-the-lost-ark',
                        imdb_code=131,
                        status=1,
                        version=1,
                        release_year=1981,
                        title='Raiders of the Lost Ark',
                        popularity=1,
                        popularity_month=1)
        self.f21.save()

        self.f22 = Film(type=1,
                        permalink='the-alien',
                        imdb_code=132,
                        status=1,
                        version=1,
                        release_year=1979,
                        title='The Alien',
                        popularity=1,
                        popularity_month=1)
        self.f22.save()

        self.f23 = Film(type=1,
                        permalink='terminator-2-judgment-day',
                        imdb_code=133,
                        status=1,
                        version=1,
                        release_year=1991,
                        title='Terminator 2: Judgment Day',
                        popularity=1,
                        popularity_month=1)
        self.f23.save()

        self.f24 = Film(type=1,
                        permalink='przypadek',
                        imdb_code=134,
                        status=1,
                        version=1,
                        release_year=1999,
                        title='Przypadek',
                        popularity=20,
                        popularity_month=10)
        self.f24.save()

        self.f25 = Film(type=1,
                        permalink='wrestler',
                        imdb_code=135,
                        status=1,
                        version=1,
                        release_year=2008,
                        title='Wrestler',
                        popularity=10,
                        popularity_month=1)
        self.f25.save()

        self.f26 = Film(type=1,
                        permalink='american-history-x',
                        imdb_code=136,
                        status=1,
                        version=1,
                        release_year=1998,
                        title='American History X',
                        popularity=1,
                        popularity_month=1)
        self.f26.save()

        self.f27 = Film(type=1,
                        permalink='the-big-lebowski',
                        imdb_code=137,
                        status=1,
                        version=1,
                        release_year=1998,
                        title='The Big Lebowski',
                        popularity=1,
                        popularity_month=1)
        self.f27.save()

        self.f28 = Film(
            type=1,
            permalink='the-lord-of-the-rings-the-fellowship-of-the-ring',
            imdb_code=138,
            status=1,
            version=1,
            release_year=2001,
            title='The Lord of the Rings: The Fellowship of the Ring',
            popularity=1,
            popularity_month=1)
        self.f28.save()

        self.f29 = Film(type=1,
                        permalink='raiders-of-the-lost-ark',
                        imdb_code=139,
                        status=1,
                        version=1,
                        release_year=1981,
                        title='Raiders of the Lost Ark',
                        popularity=1,
                        popularity_month=1)
        self.f29.save()

        self.f30 = Film(type=1,
                        permalink='the-alien',
                        imdb_code=140,
                        status=1,
                        version=1,
                        release_year=1979,
                        title='The Alien',
                        popularity=1,
                        popularity_month=1)
        self.f30.save()

        self.f31 = AddedFilm(title='Added film part 1',
                             localized_title='Added film PL',
                             release_year=2010,
                             user=self.u1)
        self.f31.save()

        self.f31.directors.add(self.p1)
        self.f31.save()

        AddedCharacter.objects.create(added_film=self.f31,
                                      person=self.p3,
                                      character="Jack Mort")

        self.f31.accept(self.u2)

        self.f32 = AddedFilm(title='Added film part 2',
                             localized_title='Added film PL',
                             release_year=2010,
                             user=self.u1)
        self.f32.save()

        self.f32.directors.add(self.p2)
        self.f32.save()

        AddedCharacter.objects.create(added_film=self.f32,
                                      person=self.p3,
                                      character="Jack Mort")

        self.f32.accept(self.u2)

        #tagging
        self.f1.save_tags('comedy drama horror animation mystery')
        self.f2.save_tags('comedy sci-fi thriller series')
        self.f3.save_tags('comedy')
        self.f4.save_tags('sci-fi')
        self.f5.save_tags('horror')
        self.f6.save_tags('thriller')
        self.f7.save_tags('drama')
        self.f8.save_tags('series')
        self.f9.save_tags('sci-fi')
        self.f10.save_tags('fantasy')
        self.f11.save_tags('mystery')
        self.f12.save_tags('action')
        self.f13.save_tags('adventure')
        self.f14.save_tags('animation')
        self.f15.save_tags('family')
        self.f16.save_tags('thriller')
        self.f17.save_tags('drama')
        self.f18.save_tags('series')
        self.f19.save_tags('sci-fi')
        self.f20.save_tags('fantasy')
        self.f21.save_tags('comedy')
        self.f22.save_tags('animation')
        self.f23.save_tags('comedy')
        self.f24.save_tags('sci-fi')
        self.f25.save_tags('horror')
        self.f26.save_tags('thriller')
        self.f27.save_tags('drama')
        self.f28.save_tags('series')
        self.f29.save_tags('sci-fi')
        self.f30.save_tags('fantasy')

        #First user ratings
        save_rating(self.f1, self.u1, 1)
        save_rating(self.f2, self.u1, 2)
        save_rating(self.f3, self.u1, 3)
        save_rating(self.f4, self.u1, 4)
        save_rating(self.f5, self.u1, 5)
        save_rating(self.f6, self.u1, 6)
        save_rating(self.f7, self.u1, 7)
        save_rating(self.f8, self.u1, 8)
        save_rating(self.f9, self.u1, 9)
        save_rating(self.f10, self.u1, 10)
        save_rating(self.f11, self.u1, 1)
        save_rating(self.f12, self.u1, 2)
        save_rating(self.f13, self.u1, 3)
        save_rating(self.f14, self.u1, 4)
        save_rating(self.f15, self.u1, 5)
        save_rating(self.f16, self.u1, 6)
        save_rating(self.f17, self.u1, 7)
        save_rating(self.f18, self.u1, 8)
        save_rating(self.f19, self.u1, 9)
        save_rating(self.f20, self.u1, 10)
        save_rating(self.f21, self.u1, 1)
        save_rating(self.f22, self.u1, 2)
        save_rating(self.f23, self.u1, 3)
        save_rating(self.f24, self.u1, 4)
        save_rating(self.f25, self.u1, 5)
        save_rating(self.f26, self.u1, 6)
        save_rating(self.f27, self.u1, 7)
        save_rating(self.f28, self.u1, 8)
        save_rating(self.f29, self.u1, 9)
        save_rating(self.f30, self.u1, 10)

        #Second user ratings - more than minimal common films requirement (15)
        save_rating(self.f1, self.u2, 1)
        save_rating(self.f2, self.u2, 2)
        save_rating(self.f3, self.u2, 3)
        save_rating(self.f4, self.u2, 4)
        save_rating(self.f5, self.u2, 5)
        save_rating(self.f6, self.u2, 6)
        save_rating(self.f7, self.u2, 7)
        save_rating(self.f8, self.u2, 8)
        save_rating(self.f9, self.u2, 9)
        save_rating(self.f10, self.u2, 10)
        save_rating(self.f11, self.u2, 1)
        #save_rating(self.f12, self.u2, 2)
        save_rating(self.f13, self.u2, 3)
        #save_rating(self.f14, self.u2, 4)
        save_rating(self.f15, self.u2, 5)
        #save_rating(self.f16, self.u2, 6)
        save_rating(self.f17, self.u2, 7)
        #save_rating(self.f18, self.u2, 8)
        save_rating(self.f19, self.u2, 9)
        #save_rating(self.f20, self.u2, 10)
        save_rating(self.f21, self.u2, 1)
        #save_rating(self.f22, self.u2, 2)
        save_rating(self.f23, self.u2, 3)
        #save_rating(self.f24, self.u2, 4)
        save_rating(self.f25, self.u2, 5)
        #save_rating(self.f26, self.u2, 6)
        save_rating(self.f27, self.u2, 7)
        #save_rating(self.f28, self.u2, 8)
        save_rating(self.f29, self.u2, 9)
        #save_rating(self.f30, self.u2, 10)

        # Third user ratings - equal with minimal common films requirement (15)
        #save_rating(self.f1, self.u3, 1)
        #save_rating(self.f2, self.u3, 2)
        #save_rating(self.f3, self.u3, 3)
        #save_rating(self.f4, self.u3, 4)
        #save_rating(self.f5, self.u3, 5)
        #save_rating(self.f6, self.u3, 6)
        #save_rating(self.f7, self.u3, 7)
        #save_rating(self.f8, self.u3, 8)
        #save_rating(self.f9, self.u3, 9)
        #save_rating(self.f10, self.u3, 10)
        #save_rating(self.f11, self.u3, 1)
        #save_rating(self.f12, self.u3, 2)
        #save_rating(self.f13, self.u3, 3)
        #save_rating(self.f14, self.u3, 4)
        #save_rating(self.f15, self.u3, 5)
        save_rating(self.f16, self.u3, 6)
        save_rating(self.f17, self.u3, 7)
        save_rating(self.f18, self.u3, 8)
        save_rating(self.f19, self.u3, 9)
        save_rating(self.f20, self.u3, 10)
        save_rating(self.f21, self.u3, 1)
        save_rating(self.f22, self.u3, 2)
        save_rating(self.f23, self.u3, 3)
        save_rating(self.f24, self.u3, 4)
        save_rating(self.f25, self.u3, 5)
        save_rating(self.f26, self.u3, 6)
        save_rating(self.f27, self.u3, 7)
        save_rating(self.f28, self.u3, 8)
        save_rating(self.f29, self.u3, 9)
        save_rating(self.f30, self.u3, 10)

        # Fourth user ratings - less than minimal films requirement
        #save_rating(self.f1, self.u4, 1)
        #save_rating(self.f2, self.u4, 2)
        #save_rating(self.f3, self.u4, 3)
        #save_rating(self.f4, self.u4, 4)
        #save_rating(self.f5, self.u4, 5)
        #save_rating(self.f6, self.u4, 6)
        #save_rating(self.f7, self.u4, 7)
        #save_rating(self.f8, self.u4, 8)
        #save_rating(self.f9, self.u4, 9)
        #save_rating(self.f10, self.u4, 10)
        #save_rating(self.f11, self.u4, 1)
        #save_rating(self.f12, self.u4, 2)
        #save_rating(self.f13, self.u4, 3)
        #save_rating(self.f14, self.u4, 4)
        #save_rating(self.f15, self.u4, 5)
        #save_rating(self.f16, self.u4, 6)
        #save_rating(self.f17, self.u4, 7)
        #save_rating(self.f18, self.u4, 8)
        #save_rating(self.f19, self.u4, 9)
        #save_rating(self.f20, self.u4, 10)
        save_rating(self.f21, self.u4, 1)
        save_rating(self.f22, self.u4, 2)
        save_rating(self.f23, self.u4, 3)
        save_rating(self.f24, self.u4, 4)
        save_rating(self.f25, self.u4, 5)
        save_rating(self.f26, self.u4, 6)
        save_rating(self.f27, self.u4, 7)
        save_rating(self.f28, self.u4, 8)
        save_rating(self.f29, self.u4, 9)
        save_rating(self.f30, self.u4, 10)

        # Fifth user ratings - the same films rated (1st and 5th user) but different
        # ratings
        #save_rating(self.f1, self.u5, 10)
        #save_rating(self.f2, self.u5, 9)
        #save_rating(self.f3, self.u5, 8)
        #save_rating(self.f4, self.u5, 7)
        #save_rating(self.f5, self.u5, 6)
        #save_rating(self.f6, self.u5, 5)
        #save_rating(self.f7, self.u5, 4)
        #save_rating(self.f8, self.u5, 3)
        #save_rating(self.f9, self.u5, 2)
        #save_rating(self.f10, self.u5, 1)
        #save_rating(self.f11, self.u5, 10)
        #save_rating(self.f12, self.u5, 9)
        #save_rating(self.f13, self.u5, 8)
        #save_rating(self.f14, self.u5, 7)
        #save_rating(self.f15, self.u5, 6)
        #save_rating(self.f16, self.u5, 5)
        #save_rating(self.f17, self.u5, 4)
        #save_rating(self.f18, self.u5, 3)
        #save_rating(self.f19, self.u5, 2)
        #save_rating(self.f20, self.u5, 1)
        #save_rating(self.f21, self.u5, 10)
        #save_rating(self.f22, self.u5, 9)
        #save_rating(self.f23, self.u5, 8)
        #save_rating(self.f24, self.u5, 7)
        #save_rating(self.f25, self.u5, 6)
        #save_rating(self.f26, self.u5, 5)
        #save_rating(self.f27, self.u5, 4)
        #save_rating(self.f28, self.u5, 3)
        #save_rating(self.f29, self.u5, 2)
        #save_rating(self.f30, self.u5, 1)

        # Sixth user ratings - the same films rated (1st and 5th user) but different
        # ratings
        save_rating(self.f1, self.u6, 10)
        save_rating(self.f2, self.u6, 10)
        save_rating(self.f3, self.u6, 10)
        save_rating(self.f4, self.u6, 10)
        save_rating(self.f5, self.u6, 10)
        save_rating(self.f6, self.u6, 10)
        save_rating(self.f7, self.u6, 10)
        save_rating(self.f8, self.u6, 10)
        save_rating(self.f9, self.u6, 10)
        save_rating(self.f10, self.u6, 10)
        save_rating(self.f11, self.u6, 10)
        save_rating(self.f12, self.u6, 10)
        save_rating(self.f13, self.u6, 10)
        save_rating(self.f14, self.u6, 10)
        save_rating(self.f15, self.u6, 10)
        save_rating(self.f16, self.u6, 10)
        save_rating(self.f17, self.u6, 10)
        save_rating(self.f18, self.u6, 10)
        save_rating(self.f19, self.u6, 10)
        save_rating(self.f20, self.u6, 10)
        save_rating(self.f21, self.u6, 10)
        save_rating(self.f22, self.u6, 10)
        save_rating(self.f23, self.u6, 10)
        save_rating(self.f24, self.u6, 10)
        save_rating(self.f25, self.u6, 10)
        save_rating(self.f26, self.u6, 10)
        save_rating(self.f27, self.u6, 10)
        save_rating(self.f28, self.u6, 10)
        save_rating(self.f29, self.u6, 10)
        save_rating(self.f30, self.u6, 10)

    @unittest.skipIf(not is_postgres, "sqlite is not supported yet")
    def test_get_recently_popular_films_query(self):
        print "Test get recently popular films query"
        """
           Test for query which gets the most popular films for the rating machine.
           First it should return all films, then we rate one and in the second call
           it should only retrieve the other film.
        """

        recom_helper = RecomHelper()
        film_helper = FilmHelper()

        u1_result = recom_helper.get_recently_popular_films_query(self.u1.id)
        self.assertEquals(len(u1_result), 2)

        u2_result = recom_helper.get_recently_popular_films_query(self.u2.id)
        self.assertEquals(len(u2_result), 12)

        film_with_director_and_actor = Film.objects.filter(
            title__startswith="Added film part 1")
        related_directors = film_helper.get_film_directors(
            film_with_director_and_actor[0])
        related_characters = film_helper.get_film_actors(
            film_with_director_and_actor[0])
        related_actors = []
        for character in related_characters:
            actor = character.person
            related_actors.append(actor)

        POST = {}
        # TODO check why it starts breaking (form invalid) when form_id is set
        #POST['form_id'] = '1'
        POST['rating'] = '6'
        POST['object_id'] = str(self.f12.parent.id)
        POST['form_type'] = str(Rating.TYPE_FILM)
        POST['cancel_rating'] = '0'
        rating_form = RatingForm(POST)
        if rating_form.is_valid():
            rating_helper.add_edit_rating(rating_form, self.u2)
        else:
            print "RATING FORM INVALID!"

        result = recom_helper.get_recently_popular_films_query(self.u2.id)
        self.assertEquals(len(result), 11)

        # test filtering
        result = recom_helper.get_recently_popular_films_query(self.u5.id)
        self.assertEquals(len(result), 32)

        result = recom_helper.get_recently_popular_films_query(
            self.u5.id, year_from='1990')
        self.assertEquals(len(result), 24)

        result = recom_helper.get_recently_popular_films_query(self.u5.id,
                                                               year_to='2000')
        self.assertEquals(len(result), 22)

        result = recom_helper.get_recently_popular_films_query(
            self.u5.id, year_from='1990', year_to='2000')
        self.assertEquals(len(result), 14)

        result = recom_helper.get_recently_popular_films_query(self.u5.id,
                                                               popularity='10')
        self.assertEquals(len(result), 8)

        result = recom_helper.get_recently_popular_films_query(
            self.u5.id, year_from='1990', year_to='2000', popularity='10')
        self.assertEquals(len(result), 4)

        rated_films = [
            self.f1.id, self.f3.id, self.f5.id, self.f7.id, self.f9.id
        ]
        result = recom_helper.get_recently_popular_films_query(
            self.u5.id, rated_films=rated_films)
        self.assertEquals(len(result), 27)

        result = recom_helper.get_recently_popular_films_query(
            self.u5.id,
            year_from='1990',
            year_to='2000',
            rated_films=rated_films,
            popularity='10')
        self.assertEquals(len(result), 2)

        result = recom_helper.get_recently_popular_films_query(
            self.u5.id, related_director=related_directors)
        self.assertEquals(len(result), 1)

        result = recom_helper.get_recently_popular_films_query(
            self.u5.id, related_actor=related_actors)
        self.assertEquals(len(result), 2)

        result = recom_helper.get_recently_popular_films_query(
            self.u5.id,
            related_director=related_directors,
            related_actor=related_actors)
        self.assertEquals(len(result), 1)

        result = recom_helper.get_recently_popular_films_query(
            self.u5.id,
            year_from='1990',
            year_to='2011',
            related_director=related_directors,
            related_actor=related_actors)
        self.assertEquals(len(result), 1)

#    @unittest.skipIf(not is_postgres, "postgres dependend")
#    def test_get_best_psi_films_queryset_alg2(self):
#        """
#           Test for query that retrieves top recommendations.
#           First it should return all films, then we rate one and in the second call
#           it should only retrieve the other film.
#        """
#
#        recom_helper = RecomHelper()
#        film_helper = FilmHelper()
#
#        # no recommendations yet, the result should be 0
#        result = recom_helper.get_best_psi_films_queryset_alg2(self.u4.id)
#        self.assertEquals(len(result), 0)
#
#        # simulate generating recommendations (for all films) and test again, it should retrieve all films
#        r1 = Recommendation()
#        r1.guess_rating_alg2 = 7
#        r1.film = self.f19
#        r1.user = self.u4
#        r1.save()
#        r2 = Recommendation()
#        r2.guess_rating_alg2 = 8
#        r2.film = self.f20
#        r2.user = self.u4
#        r2.save()
#        result = recom_helper.get_best_psi_films_queryset_alg2(self.u4.id)
#        self.assertEquals(len(result), 2)
#
#        # rate one film, it should be removed from the list
#        POST = {}
#       # TODO check why it starts breaking (form invalid) when form_id is set
#       #POST['form_id'] = '1'
#       POST['rating'] = '6'
#       POST['object_id'] = str(self.f20.parent.id)
#       POST['form_type'] = str(Rating.TYPE_FILM)
#       POST['cancel_rating'] = '0'
#       rating_form = RatingForm(POST)
#       if rating_form.is_valid():
#           rating_helper.add_edit_rating(rating_form, self.u4)
#       else:
#           print "RATING FORM INVALID!"
#
#        result = recom_helper.get_recently_popular_films_query(self.u4.id)
#        self.assertEquals(len(result), 21)

    @unittest.skip("temporary disabled")
    def test_get_best_tci_users(self):
        """
           Test for query that retrieves top users (based on common taste).
        """
        do_create_comparators()
        recom_helper = RecomHelper()
        result = recom_helper.get_best_tci_users(self.u1)
        print "result -->", result
        self.assertEquals(len(result), 4)

    def test_enrich_query_with_tags(self):
        print "Test enrich query with tags"
        recom_helper = RecomHelper()
        film_helper = FilmHelper()

        comedies = recom_helper.get_films_for_tag('comedy')
        scifis = recom_helper.get_films_for_tag('sci-fi')
        horrors = recom_helper.get_films_for_tag('horror')
        thrillers = recom_helper.get_films_for_tag('thriller')
        dramas = recom_helper.get_films_for_tag('drama')
        fantasies = recom_helper.get_films_for_tag('fantasy')
        mysteries = recom_helper.get_films_for_tag('mystery')
        animations = recom_helper.get_films_for_tag('animation')
        series = recom_helper.get_films_for_tag('series')

        self.assertEquals(len(comedies), 5)
        self.assertEquals(len(scifis), 6)
        self.assertEquals(len(horrors), 3)
        self.assertEquals(len(thrillers), 4)
        self.assertEquals(len(dramas), 4)
        self.assertEquals(len(fantasies), 3)
        self.assertEquals(len(mysteries), 2)
        self.assertEquals(len(animations), 3)
        self.assertEquals(len(series), 4)

    def test_get_all_ratings(self):
        recom_helper = RecomHelper()
        film_helper = FilmHelper()

        film_with_director_and_actor = Film.objects.filter(
            title__startswith="Added film part 2")
        related_directors = film_helper.get_film_directors(
            film_with_director_and_actor[0])
        related_characters = film_helper.get_film_actors(
            film_with_director_and_actor[0])
        related_actors = []
        for character in related_characters:
            actor = character.person
            related_actors.append(actor)

        non_rated_films = Film.objects.filter(rating__isnull=True,
                                              title__startswith="Added")

        POST = {}
        #POST['form_id'] = '1'
        POST['rating'] = '6'
        POST['object_id'] = str(non_rated_films[1].parent.id)
        POST['form_type'] = str(Rating.TYPE_FILM)
        POST['cancel_rating'] = '0'
        rating_form = RatingForm(POST)
        if rating_form.is_valid():
            rating_helper.add_edit_rating(rating_form, self.u1)
        else:
            print "RATING FORM INVALID!"

        all_ratings = recom_helper.get_all_ratings(self.u1.id)
        self.assertEquals(len(all_ratings), 31)

        director_ratings = recom_helper.get_all_ratings(
            self.u1.id, related_director=related_directors)
        self.assertEquals(len(director_ratings), 1)

        actor_ratings = recom_helper.get_all_ratings(
            self.u1.id, related_actor=related_actors)
        self.assertEquals(len(actor_ratings), 1)

        from_ratings = recom_helper.get_all_ratings(self.u1.id, year_from=1990)
        self.assertEquals(len(from_ratings), 23)

        to_ratings = recom_helper.get_all_ratings(self.u1.id, year_to=2000)
        self.assertEquals(len(to_ratings), 22)

        popularity_ratings = recom_helper.get_all_ratings(self.u1.id,
                                                          popularity=5)
        self.assertEquals(len(popularity_ratings), 8)

        tag_ratings = recom_helper.get_all_ratings(self.u1.id, tags='comedy')
        self.assertEquals(len(tag_ratings), 5)

    def test_cache_count_ratings(self):
        recom_helper = RecomHelper()
        ratings_count = recom_helper.count_ratings(self.u3.id)
        self.assertEquals(ratings_count, 15)

        save_rating(self.f1, self.u3, 1)
        ratings_count = recom_helper.count_ratings(self.u3.id)
        self.assertEquals(ratings_count, 16)

    def tearDown(self):
        User.objects.all().delete()

        Object.objects.all().delete()
        Film.objects.all().delete()
        FilmLog.objects.all().delete()
        Rating.objects.all().delete()
        Recommendation.objects.all().delete()
        Person.objects.all().delete()

        AddedCharacter.objects.all().delete()
        AddedFilm.objects.all().delete()
Beispiel #15
0
    def test_persons( self ):

        fti1 = FilmToImport( user=self.user, title='Nybyggarna',
                imdb_url='http://www.imdb.com/title/tt0069035/', imdb_id='0069035', status=FilmToImport.ACCEPTED )
        fti1.save()

        fti2 = FilmToImport( user=self.user, title='Utvandrarna',
                imdb_url='http://www.imdb.com/title/tt0067919/', imdb_id='0067919', status=FilmToImport.ACCEPTED )
        fti2.save()

        fti3 = FilmToImport( user=self.user, title='Sandade Sandadi',
                imdb_url='http://www.imdb.com/title/tt0338408/', imdb_id='0338408', status=FilmToImport.ACCEPTED )
        fti3.save()

        fti4 = FilmToImport( user = self.user, title = 'Journey to shiloh',
                imdb_url = 'http://www.imdb.com/title/tt0063161/', imdb_id = '0063161', status = FilmToImport.ACCEPTED )
        fti4.save()

        fti5 = FilmToImport( user = self.user, title = 'Guys choice Awards 2011',
                imdb_url = 'http://www.imdb.com/title/tt2023505/', imdb_id = '2023505', status = FilmToImport.ACCEPTED )
        fti5.save()

        self.assertTrue( compare( "Penélope Cruz", "Penélope Cruz", 2 ) )
 
        fti6 = FilmToImport( user=self.user, title='Volver',
                imdb_url='http://www.imdb.com/title/tt0441909/', imdb_id='0441909', status=FilmToImport.ACCEPTED )
        fti6.save()

        fti7 = FilmToImport( user=self.user, title='Gothika',
                imdb_url='http://www.imdb.com/title/tt0348836/', imdb_id='0348836', status=FilmToImport.ACCEPTED )
        fti7.save()
        
        run( False, False, False, False, False, True, "http" )

        self.assertEqual( Film.objects.count(), 7 )
        self.assertEqual( Person.objects.filter( name='Harrison', surname='Ford', imdb_code='0000148', verified_imdb_code=True ).count(), 1 )
        self.assertEqual( Person.objects.filter( name='Yvonne', surname='Oppstedt', imdb_code='0649235', verified_imdb_code=True ).count(), 1 )
        self.assertEqual( Person.objects.filter( name='Sonali', surname='Joshi', imdb_code='1359890', verified_imdb_code=True ).count(), 1 )
        self.assertEqual( Film.objects.filter( character__person__imdb_code='0649235' ).distinct().count(), 2 )
        self.assertEqual( Film.objects.filter( character__person__imdb_code='1359890' ).distinct().count(), 1 )

        # 1. two persons merged
        f1 = Film.objects.get( imdb_code='0338408' )
    
        yvonne = Person.objects.get( imdb_code='0649235' )
        sonali = Person.objects.get( imdb_code='1359890' )

        for ch in Character.objects.filter( person=sonali, film=f1 ):
            ch.person = yvonne
            ch.save()

        self.assertEqual( Film.objects.filter( character__person=yvonne ).distinct().count(), 3 )
        self.assertEqual( Film.objects.filter( character__person=sonali ).distinct().count(), 0 )

        sonali.delete()

        yvonne.imdb_code = '1359890'
        yvonne.verified_imdb_code = False
        yvonne.import_comment = 'imdb name: Sonali Joshi'
        yvonne.save()
        
        # 2. one person with two movies
        f2 = Film.objects.get( imdb_code='2023505' )

        harrison = Person.objects.get( imdb_code='0000148' )

        bad_harrison = Person()
        bad_harrison.name = "Harrison"
        bad_harrison.surname = "Ford"
        bad_harrison.permalink = "harrison-ford-1"
        bad_harrison.type = Person.TYPE_PERSON
        bad_harrison.save()

        for ch in Character.objects.filter( person=harrison, film=f2 ):
            ch.person = bad_harrison
            ch.save()

        harrison.verified_imdb_code = False
        harrison.imdb_code = '111111'
        harrison.save()

        bad_harrison.verified_imdb_code = True
        bad_harrison.imdb_code = '0000148'
        bad_harrison.save()

        self.assertEqual( Film.objects.filter( character__person=harrison ).distinct().count(), 1 )
        self.assertEqual( Film.objects.filter( character__person=bad_harrison ).distinct().count(), 1 )
        self.assertEqual( Person.objects.filter( verified_imdb_code=False ).count(), 2 )
 
        # 3. short imdb code duplicated
        self.assertEqual( Person.objects.filter( imdb_code='0880521', name='Liv', surname='Ullmann' ).count(), 1 )

        liv = Person.objects.get( imdb_code='0880521' )
        liv.permalink = 'liv-ullmann-1'
        liv.save()

        short_liv = Person()
        short_liv.imdb_code = '880521'
        short_liv.name = 'Liv' 
        short_liv.surname = 'Ullmann'
        short_liv.permalink = 'liv-ullmann'
        short_liv.type = Person.TYPE_PERSON
        short_liv.save()
 
        # 4. not verified imdb_code
        self.assertEqual( Person.objects.filter( imdb_code='0005493', name='Justin', surname='Timberlake', verified_imdb_code=True ).count(), 1 )
        
        justin = Person.objects.get( imdb_code='0005493' )
        justin.verified_imdb_code = False
        justin.imdb_code = None
        justin.save()

        self.assertEqual( Person.objects.filter( imdb_code='0873296', name='Jan', surname='Troell', verified_imdb_code=True ).count(), 1 )
        
        jan = Person.objects.get( imdb_code='0873296' )
        jan.verified_imdb_code = False
        jan.save()

        # 5. html entities in name
        self.assertEqual( Person.objects.filter( name='Penélope', surname='Cruz', imdb_code='0004851', verified_imdb_code=True ).count(), 1 )
        
        penelope = Person.objects.get( imdb_code='0004851' )
        gothika = Film.objects.get( imdb_code='0348836' )

        penelope.verified_imdb_code = False
        penelope.imdb_code = '4851'
        penelope.save()

        bad_penelope = Person()
        bad_penelope.name = "Penélope"
        bad_penelope.surname = "Cruz"
        bad_penelope.imdb_code = '0004851'
        bad_penelope.permalink = "penxe9lope-cruz"
        bad_penelope.type = Person.TYPE_PERSON
        bad_penelope.save()

        for ch in Character.objects.filter( person=penelope, film=gothika ):
            ch.person = bad_penelope
            ch.save()

        call_command( 'fix_imported_persons' )

        #for p in Person.objects.all():
        #    print p.verified_imdb_code, p, p.imdb_code, p.import_comment

        self.assertEqual( Person.objects.filter( verified_imdb_code=False ).count(), 1 )
        self.assertEqual( Film.objects.filter( character__person=harrison ).distinct().count(), 2 )
        self.assertEqual( Person.objects.get( pk=yvonne.pk ).import_comment, 'imdb name: Sonali Joshi, not matched movies: 0338408' )
        self.assertEqual( Person.objects.filter( imdb_code='880521' ).count(), 0 )
        self.assertEqual( Person.objects.filter( imdb_code='0880521', name='Liv', surname='Ullmann' ).count(), 1 )
        
        justin = Person.objects.get( pk=justin.pk )
        self.assertEqual( justin.imdb_code, '0005493' )
        self.assertTrue( justin.verified_imdb_code )

        jan = Person.objects.get( pk=jan.pk )
        self.assertEqual( jan.imdb_code, '0873296' )
        self.assertTrue( jan.verified_imdb_code )
        
        self.assertEqual( Person.objects.filter( name='Penélope', surname='Cruz', imdb_code='0004851', verified_imdb_code=True ).count(), 1 )
        acted = Film.objects.filter( character__person=penelope ).distinct()
        self.assertTrue( len( acted ), 2 )