def testQuerySession(self):
     """ TEST: Return sessions for a given query"""
     self.initDatabase()
     form = SessionQueryForms()
     form.filters = [
         SessionQueryForm(field='NAME', operator='EQ', value='Google App Engine')
     ]
     response = self.api.querySessions(form)
     r_sessions = response.items
     assert len(r_sessions) == 1, 'returned an invalid number of sessions'
     assert r_sessions[0].name == 'Google App Engine', 'returned an invalid session'
    def testTask3QueryProblem(self):
        """ TEST: Solve task 3 "the query related problem"  """
        # init and verify database fixture
        self.initDatabase()
        workshopSessions = Session.query(Session.typeOfSession == 'workshop').fetch()
        assert len(workshopSessions) == 2, "This shouldn't fail. Maybe someone messed with database fixture"
        # manually get the solution so we can compare it against the response
        validSessions = []
        for session in Session.query(Session.startTime < datetime.datetime.strptime('19:00', '%H:%M').time()):
            if session not in workshopSessions:
                validSessions.append(session)
        assert len(validSessions) > 0, "This shouldn't fail. Maybe someone messed with database fixture"

        # create a query using `sessionQueryForms()` and add 2 inequalities
        form = SessionQueryForms()
        form.filters = [
            SessionQueryForm(field='TYPE_OF_SESSION', operator='NE', value='workshop'),
            SessionQueryForm(field='START_TIME', operator='LT', value='19:00')
        ]
        response = self.api.querySessions(form)
        # evaluate the response
        sessions = response.items
        assert len(sessions) == len(validSessions), 'Returned an invalid number of sessions'
        validKeys = [v.key.urlsafe() for v in validSessions]
        for s in sessions:
            assert s.websafeKey in validKeys, 'Returned an invalid session'

        # ----- Attempt a 9 inequality query -----------
        # Create a unique session
        uniqueSession = {'name': 'BONUS ROUND', 'speaker': Speaker(name='BONUS ROUND'), 'typeOfSession': 'BONUS ROUND',
                         'date': datetime.datetime.strptime('2015-12-12', '%Y-%m-%d'),
                         'startTime': datetime.time(hour=3), 'duration': 200}
        # verify this session is unique
        for key, value in uniqueSession.iteritems():
            count = Session.query(Session._properties[key] == value).count()
            assert count == 0, 'Ahhh failed to setup bonus round, maybe someone messed with the database fixture'

        # add unique session to database
        conf = Conference.query().get()
        c_id = Session.allocate_ids(size=1, parent=conf.key)[0]
        uniqueSession['key'] = ndb.Key(Session, c_id, parent=conf.key)
        Session(**uniqueSession).put()

        # create a form with multiple inequalities
        inequalities = [
            {'field': 'START_TIME', 'operator': 'GTEQ', 'value': '02:30'},
            {'field': 'DATE', 'operator': 'LTEQ', 'value': '2015-12-12'},
            {'field': 'DURATION', 'operator': 'GT', 'value': '30'}
        ]
        form = SessionQueryForms()
        for inequality in inequalities:
            form.filters.append(SessionQueryForm(**inequality))

        # grab all sessions (EXCLUDING unique session)
        sessions = Session.query(Session.key != uniqueSession['key']).fetch()
        assert len(sessions) == 6
        # add additional inequalities using the session names
        for session in sessions:
            form.filters.append(SessionQueryForm(
                field='NAME',
                operator='NE',
                value=session.name
            ))
        # 9 inequality filters
        assert len(form.filters) == 9, 'Ahhh failed to setup bonus round, expected 9 inequality filters'
        # From the way the test was setup, `START_TIME` will be the only
        # property filtered by datastore. Everything else will be filtered using python.

        # make the query using the 9 inequality filters to retrieve the `uniqueSession`
        response = self.api.querySessions(form)
        assert len(response.items) == 1
        assert response.items[0].name == 'BONUS ROUND'