def read(self, request, text): index_path = os.path.join(settings.BASEDIR, 'wstore') index_path = os.path.join(index_path, 'search') index_path = os.path.join(index_path, 'indexes') search_engine = SearchEngine(index_path) filter_ = request.GET.get('filter', None) action = request.GET.get('action', None) start = request.GET.get('start', None) limit = request.GET.get('limit', None) sort = request.GET.get('sort', None) # Check the filter value if filter_ and filter_ != 'published' and filter_ != 'provided' and filter_ != 'purchased': return build_response(request, 400, 'Invalid filter') count = False pagination = None # Check if the action is count if action != None: if action == 'count': count = True else: return build_response(request, 400, 'Invalid action') else: # Check pagination params (Only when action is none) if start != None and limit != None: pagination = { 'start': int(start), 'limit': int(limit) } elif (start != None and limit == None) or (start == None and limit != None): return build_response(request, 400, 'Missing pagination param') # Check sorting values if sort != None: if sort != 'date' and sort != 'popularity' and sort != 'name': return build_response(request, 400, 'Invalid sorting') if not filter_: response = search_engine.full_text_search(request.user, text, count=count, pagination=pagination, sort=sort) elif filter_ == 'provided': state = request.GET.get('state', 'all') # Check the state value if state != 'all' and state != 'uploaded'\ and state != 'published' and state != 'deleted': return build_response(request, 400, 'Invalid state') response = search_engine.full_text_search(request.user, text, state=state, count=count, pagination=pagination, sort=sort) elif filter_ == 'purchased': response = search_engine.full_text_search(request.user, text, state='purchased', count=count, pagination=pagination, sort=sort) return HttpResponse(json.dumps(response), status=200, mimetype='application/json')
def test_search_for_purchased_offerings(self): user = User.objects.get(username='******') user_profile = UserProfile.objects.get(user=user) org = Organization.objects.get(name='test_organization') user_profile.current_organization = org user_profile.organizations.append({ 'organization': org.pk, 'roles': ['provider', 'customer'] }) user_profile.save() org.offerings_purchased = ["61000a0a8905ac2115f022f0"] org.save() purchase = Purchase.objects.all()[0] purchase.organization_owned = True purchase.owner_organization = org purchase.save() se = SearchEngine(settings.BASEDIR + '/wstore/test/test_index') result = se.full_text_search(user, 'purchased', state='purchased') self.assertEqual(len(result), 1) self.assertEqual(result[0]['name'], 'purchased_offering') self.assertEqual(result[0]['version'], '1.0') self.assertEqual(result[0]['state'], 'purchased')
def test_search_not_existing_index(self): user = User.objects.get(username='******') user_profile = UserProfile.objects.get(user=user) org = Organization.objects.get(name='test_organization') user_profile.current_organization = org user_profile.organizations.append({ 'organization': org.pk, 'roles': ['provider', 'customer'] }) user_profile.save() se = SearchEngine(settings.BASEDIR + '/wstore/test/no_index') error = False msg = None try: se.full_text_search(user, 'index offering') except Exception, e: error = True msg = e.message
def test_search_no_results(self): user = User.objects.get(username='******') user_profile = UserProfile.objects.get(user=user) org = Organization.objects.get(name='test_organization') user_profile.current_organization = org user_profile.organizations.append({ 'organization': org.pk, 'roles': ['provider', 'customer'] }) user_profile.save() se = SearchEngine(settings.BASEDIR + '/wstore/test/test_index') result = se.full_text_search(user, 'no result') self.assertEqual(len(result), 0)
def test_basic_search(self): user = User.objects.get(username='******') user_profile = UserProfile.objects.get(user=user) org = Organization.objects.get(name='test_organization') user_profile.current_organization = org user_profile.organizations.append({ 'organization': org.pk, 'roles': ['provider', 'customer'] }) user_profile.save() se = SearchEngine(settings.BASEDIR + '/wstore/test/test_index') result = se.full_text_search(user, 'first') self.assertEqual(len(result), 1) self.assertEqual(result[0]['name'], 'test_offering1') self.assertEqual(result[0]['version'], '1.0')
def test_search_offerings(self, expected_result, side_effect=None, state=None, count=False, pagination=None, sort=None, err_type=None, err_msg=None): # Create user data user = User.objects.get(username='******') if side_effect: side_effect(self) # Create the search engine se = SearchEngine(settings.BASEDIR + '/wstore/test/test_index') # Call full text search error = None try: result = se.full_text_search(user, 'offering', state=state, count=count, pagination=pagination, sort=sort) except Exception as e: error = e # Check results if not err_type: self.assertEquals(error, None) if count: self.assertEquals(result, expected_result) else: # If a sorting has been defined the result must be strict self.assertEquals(len(result), len(expected_result)) i = 0 for res in result: if sort: self.assertEquals(res['name'], expected_result[i]) else: self.assertTrue(res['name'] in expected_result) i += 1 else: self.assertTrue(isinstance(error, err_type)) self.assertEquals(unicode(e), err_msg)
def read(self, request, text): index_path = os.path.join(settings.BASEDIR, 'wstore') index_path = os.path.join(index_path, 'search') index_path = os.path.join(index_path, 'indexes') search_engine = SearchEngine(index_path) filter_ = request.GET.get('filter', None) action = request.GET.get('action', None) start = request.GET.get('start', None) limit = request.GET.get('limit', None) sort = request.GET.get('sort', None) state = request.GET.get('state', None) if state: if state == 'ALL': state = ['uploaded', 'published', 'deleted'] else: state = state.split(',') # Check the filter value if filter_ and filter_ != 'published' and filter_ != 'provided' and filter_ != 'purchased': return build_response(request, 400, 'Invalid filter') if state and filter_ != 'provided': return build_response(request, 400, 'Invalid filters') if filter_ == 'provider' and not state: return build_response(request, 400, 'Invalid filters') count = False pagination = None # Check if the action is count if action != None: if action == 'count': count = True else: return build_response(request, 400, 'Invalid action') else: # Check pagination params (Only when action is none) if start != None and limit != None: pagination = { 'start': int(start), 'limit': int(limit) } elif (start != None and limit == None) or (start == None and limit != None): return build_response(request, 400, 'Missing pagination param') # Check sorting values if sort != None: if sort != 'date' and sort != 'popularity' and sort != 'name': return build_response(request, 400, 'Invalid sorting') if not filter_ or filter_ == 'published': response = search_engine.full_text_search(request.user, text, count=count, pagination=pagination, sort=sort) elif filter_ == 'provided': response = search_engine.full_text_search(request.user, text, state=state, count=count, pagination=pagination, sort=sort) elif filter_ == 'purchased': response = search_engine.full_text_search(request.user, text, state=['purchased'], count=count, pagination=pagination, sort=sort) return HttpResponse(json.dumps(response), status=200, mimetype='application/json')