def testSearchProfRating(self): self.cq = CourseQuery() self.cq.connect() prof_rating = 4.5 cursor = self.cq.search_for_course_cursor(min_prof_rating = prof_rating) for json in cursor: self.assertGreaterEqual(json['prof_rating'], min_prof_rating) self.cq.disconnect()
def testSearchCreditHour(self): self.cq = CourseQuery() self.cq.connect() credit_hour = 4 cursor = self.cq.search_for_course_cursor(credit_hours = credit_hour) for json in cursor: self.assertIsNotNone(set([credit_hour, str(credit_hour)]).intersection(set(json['credit_hours']))) self.cq.disconnect()
def testSearchGPA(self): self.cq = CourseQuery() self.cq.connect() min_gpa = 3 cursor = self.cq.search_for_course_cursor(min_gpa = min_gpa) for json in cursor: self.assertGreaterEqual(json['gpa'], min_gpa) self.cq.disconnect()
def testSearchCreditHours(self): self.cq = CourseQuery() self.cq.connect() credit_hours = [4, 6, '4', '6'] credit_hours_set = set(credit_hours) cursor = self.cq.search_for_course_cursor(credit_hours = credit_hours) for json in cursor: self.assertIsNotNone(credit_hours_set.intersection(set(json['credit_hours']))) self.cq.disconnect()
def __init__(self, query={}, table_size=25): ''' Constructor ''' self.course_query = CourseQuery() self.cursor = None self.query = None self.set_query(query) self.set_table_size(table_size)
def post(self): myClass = json.loads(request.data) print(myClass) #initiate object to get details of specific classs query = CourseQuery() query.connect() info = query.get_course_JSON(myClass['sub'], myClass['subId']) del info['_id'] info['class_title'] = info['title'] del info['title'] query.disconnect() subjectIds = ast.literal_eval(json.dumps(info)) print(subjectIds) #create object to query the times of the specific course section = SectionQuery() sectionList = [] section.connect() for crn in info['crns']: cursor = section.get_section_cursor_crn(crn) courseSect = cursor[0] del courseSect['_id'] courseSect['class_start'] = courseSect['start'] del courseSect['start'] # courseSection = ast.literal_eval(json.dumps(courseSect)) sectionList.append(courseSect) section.disconnect() print(sectionList) return jsonify({'success':True, 'classInfo': info, 'times':sectionList})
def get(self): query = CourseQuery() query.connect() codes = query.get_subject_codes() query.disconnect() subjectCodes = ast.literal_eval(json.dumps(codes)) return jsonify({'success':True, 'subjectCodes': subjectCodes})
def post(self): subject = json.loads(request.data) query = CourseQuery() query.connect() ids = query.get_course_ids_for_subject(subject['sub']) query.disconnect() subjectIds = ast.literal_eval(json.dumps(ids)) return jsonify({'success':True, 'subjectIds': list(set(ids))})
def testCourse(self): self.cq = CourseQuery() self.cq.connect() subject_code = "CS" id_num = "125" cursor1 = self.cq.get_cursor({'code':subject_code, 'course_id':id_num}) cursor2 = self.cq.get_course_cursor(subject_code, id_num) self.assertEqual(cursor1.count(), 1) self.assertEqual(cursor1.count(), cursor2.count()) self.assertEqual(cursor1[0], cursor2[0]) cursor1 = self.cq.get_cursor({'code':subject_code}) cursor2 = self.cq.get_courses_cursor_subject(subject_code) self.assertEqual(cursor1.count(), cursor2.count()) self.assertEqual(cursor1[0], cursor2[0]) self.assertEqual(cursor1[cursor1.count()-1], cursor2[cursor2.count()-1]) self.cq.disconnect()
def post(self): myFilter = json.loads(request.data)['filter'] print(myFilter) query = CourseQuery() query.connect() filterQuery = query.search_for_course_cursor(min_gpa = myFilter['gpa'], credit_hours = myFilter['credit_hours'], min_prof_rating = myFilter['prof_rating']) query.disconnect() filteredClasses = [] for course in filterQuery: del course['_id'] filteredClasses.append(course) return jsonify({'success':True, 'filteredClasses':filteredClasses})
def test2(self): cursor1 = self.ctq.get_table_page_cursor(1) cq = CourseQuery() cq.connect() cursor2 = cq.get_courses_cursor_subject(subject_code) cq.disconnect() list1 = [] for x in cursor1: list1.append(x) self.assertEqual(len(list1), 20) list2 = [] for x in cursor2: list2.append(x) for a,b in zip(list1,list2): self.assertEqual(a,b)
def testSearchTime(self): self.cq = CourseQuery() self.sq = SectionQuery() self.cq.connect() self.sq.connect() start_num = 1000 end_num = 1400 list = self.cq.search_for_course_with_time_cursor(subject_code = 'CS', start_time = start_num, end_time = end_num) for json in list: boolean = False start_end_list for crn in json['crns']: section_cursor = self.sq.get_section_cursor_crn(crn) if section_cursor.count() > 0: section_json = section_cursor[0] if 'start_num' in section_json and 'end_num' in section_json: start_end_list.append((section_json['start_num'], section_json['end_num'])) for pair in start_end_list: if start_num < pair[0] and pair[1] < end_num: boolean = True break self.assertTrue(boolean) self.cq.disconnect() self.sq.disconnect()
class Test(unittest.TestCase): def testProf(self): self.pq = ProfQuery() self.pq.connect() self.assertEqual(self.pq.get_prof_cursor_first_initial('L', 'Angrave').count(), 1) self.assertEqual(self.pq.get_prof_cursor('Lawrence', 'Angrave')[0], self.pq.get_prof_cursor_first_initial('L', 'Angrave')[0]) self.assertEqual(self.pq.get_prof_cursor('Lawrence', 'Angrave').count(), self.pq.get_prof_cursor_first_initial('L', 'Angrave').count()) self.pq.disconnect() def testCourse(self): self.cq = CourseQuery() self.cq.connect() subject_code = "CS" id_num = "125" cursor1 = self.cq.get_cursor({'code':subject_code, 'course_id':id_num}) cursor2 = self.cq.get_course_cursor(subject_code, id_num) self.assertEqual(cursor1.count(), 1) self.assertEqual(cursor1.count(), cursor2.count()) self.assertEqual(cursor1[0], cursor2[0]) cursor1 = self.cq.get_cursor({'code':subject_code}) cursor2 = self.cq.get_courses_cursor_subject(subject_code) self.assertEqual(cursor1.count(), cursor2.count()) self.assertEqual(cursor1[0], cursor2[0]) self.assertEqual(cursor1[cursor1.count()-1], cursor2[cursor2.count()-1]) self.cq.disconnect() def testSection(self): self.sq = SectionQuery() self.sq.connect() crn = '31359' cursor1 = self.sq.get_section_cursor_crn(crn) cursor2 = self.sq.get_cursor({'crn': crn}) self.assertEqual(cursor1.count(), 1) self.assertEqual(cursor1.count(), cursor2.count()) self.assertEqual(cursor1[0], cursor2[0]) self.sq.disconnect() def testSearchGPA(self): self.cq = CourseQuery() self.cq.connect() min_gpa = 3 cursor = self.cq.search_for_course_cursor(min_gpa = min_gpa) for json in cursor: self.assertGreaterEqual(json['gpa'], min_gpa) self.cq.disconnect() def testSearchProfRating(self): self.cq = CourseQuery() self.cq.connect() prof_rating = 4.5 cursor = self.cq.search_for_course_cursor(min_prof_rating = prof_rating) for json in cursor: self.assertGreaterEqual(json['prof_rating'], min_prof_rating) self.cq.disconnect() def testSearchCreditHour(self): self.cq = CourseQuery() self.cq.connect() credit_hour = 4 cursor = self.cq.search_for_course_cursor(credit_hours = credit_hour) for json in cursor: self.assertIsNotNone(set([credit_hour, str(credit_hour)]).intersection(set(json['credit_hours']))) self.cq.disconnect() def testSearchCreditHours(self): self.cq = CourseQuery() self.cq.connect() credit_hours = [4, 6, '4', '6'] credit_hours_set = set(credit_hours) cursor = self.cq.search_for_course_cursor(credit_hours = credit_hours) for json in cursor: self.assertIsNotNone(credit_hours_set.intersection(set(json['credit_hours']))) self.cq.disconnect() def testSearchTime(self): self.cq = CourseQuery() self.sq = SectionQuery() self.cq.connect() self.sq.connect() start_num = 1000 end_num = 1400 list = self.cq.search_for_course_with_time_cursor(subject_code = 'CS', start_time = start_num, end_time = end_num) for json in list: boolean = False start_end_list for crn in json['crns']: section_cursor = self.sq.get_section_cursor_crn(crn) if section_cursor.count() > 0: section_json = section_cursor[0] if 'start_num' in section_json and 'end_num' in section_json: start_end_list.append((section_json['start_num'], section_json['end_num'])) for pair in start_end_list: if start_num < pair[0] and pair[1] < end_num: boolean = True break self.assertTrue(boolean) self.cq.disconnect() self.sq.disconnect()
class CourseTableQuery(object): ''' classdocs ''' #=========================================================================== # __init__ #=========================================================================== def __init__(self, query={}, table_size=25): ''' Constructor ''' self.course_query = CourseQuery() self.cursor = None self.query = None self.set_query(query) self.set_table_size(table_size) def __del__(self): pass #=========================================================================== # connect #=========================================================================== def connect(self): self.course_query.connect() #=========================================================================== # disconnect #=========================================================================== def disconnect(self): self.course_query.disconnect() #=========================================================================== # set_cursor # Not advised, but can be used in place of set_query #=========================================================================== def set_cursor(self, cursor): self.cursor = cursor self.query = None #=========================================================================== # set_query #=========================================================================== def set_query(self, query): self.query = query #=========================================================================== # set_table_size #=========================================================================== def set_table_size(self, table_size): self.table_size = table_size #=========================================================================== # get_table_page_cursor #=========================================================================== def get_table_page_cursor(self, page_num): temp_cursor = None if self.query: temp_cursor = self.course_query.get_cursor(query) elif self.cursor: temp_cursor = self.cursor.clone() else: temp_cursor = self.course_query.get_cursor({}) start = self.table_size * (page_num - 1) end = self.table_size * page_num return temp_cursor[start:end] #=========================================================================== # get_table_page_JSON_list #=========================================================================== def get_table_page_JSON_list(self, page_num): cursor = self.get_table_page_cursor(page_num) JSON_list = [] for json in cursor: JSON_list.append(json) return JSON_list
''' Created on Apr 6, 2014 @author: Sam Gegg ''' from mongoquery import MongoQuery, SectionQuery, CourseQuery, ProfQuery from coursetablequery import CourseTableQuery from pprint import PrettyPrinter if __name__ == '__main__': pp = PrettyPrinter() client_courses = CourseQuery() client_courses.connect() pp.pprint(client_courses.get_course_JSON('CS', '125')) #client_courses.set_database_name('professors') #client_courses.set_collection('professors') #print(client_courses.get_cursor({})[1]) client_courses.disconnect() section_query = SectionQuery() section_query.connect() print(section_query.get_cursor({})[0]) print(section_query.client[section_query.db_name][section_query.collection_name].distinct('days_of_week')) print('HERE') section_query.disconnect() prof_query = ProfQuery()