def test_complex_query(self): """ Tests the factor_query method for an API that doens't accept phrases and for which the query exceeds the search term limit. """ terms = SearchTerm.objects.filter(pk__in=[1, 2, 3]) # 6 terms when parsed accounts = Account.objects.filter(pk__in=[1, 2]) locations = Location.objects.filter(pk=2) query = ReservoirQuery(searchterms=list(terms), accounts=list(accounts), locations=list(locations), trm_loc_logic='AND') specs = PipeSpecSheet(searchterms_limit=10, followees_limit=1, accepts_phrases=False, combine_trm_fol=True, combine_trm_opr=True, trm_trm_logic='AND', trm_fol_logic='AND', trm_loc_logic='AND', OR_operator='OR', location_format='radius', radius_limit_km=0.1) engineer = Engineer(query=query, specs=specs) queries = engineer.factor_query() self.assertEqual(len(queries), 4)
def test_invalid_location(self): """ Tests the format_locations method for an invalid location_format. """ query = ReservoirQuery(locations=self.locations) specs = PipeSpecSheet(location_format='bad_format') engineer = Engineer(query=query, specs=specs) with six.assertRaisesRegex(self, AttributeError, 'Invalid location format "bad_format"'): engineer._format_locations()
def test_no_limits_exceeded(self): """ Tests the _factor_components method for a query that doesn't exceed any API limits on number of arguments. """ specs = PipeSpecSheet(searchterms_limit=10, locations_limit=10, followees_limit=10) engineer = Engineer(query=self.query, specs=specs) queries = engineer._factor_components([self.query]) self.assertEqual(len(queries), 1)
def test_exceeds_searchterms_limit(self): """ Tests the _factor_components method for a query that exceeds the API limit on number of terms. """ specs = PipeSpecSheet(searchterms_limit=1, locations_limit=10, followees_limit=10) engineer = Engineer(query=self.query, specs=specs) queries = engineer._factor_components([self.query]) self.assertEqual(len(queries), 3)
def test_for_small_circle(self): """ Tests the factor_query method for a circular location smaller than the radius limit. """ locations = Location.objects.filter(pk=2) query = ReservoirQuery(locations=locations) specs = PipeSpecSheet(location_format='radius', radius_limit_km=1) engineer = Engineer(query=query, specs=specs) queries = engineer.factor_query() self.assertTrue(len(queries[0].locations) == 1)
def test_without_or_operator(self): """ Tests the get_num_searchterms method when the operators are counted as search terms, the default logic is "AND", and an "OR" operator cannot be used to join terms. """ terms = SearchTerm.objects.filter(pk=1) accounts = Account.objects.filter(pk__in=[1, 2]) query = ReservoirQuery(searchterms=terms, accounts=accounts) specs = PipeSpecSheet(combine_trm_fol=True, combine_trm_opr=True, trm_trm_logic='AND', OR_operator='OR') specs.combine_trm_opr = True specs.trm_trm_logic = 'AND' specs.OR_operator = None engineer = Engineer(query=query, specs=specs) # expect 5: 1 for SearchTerm, 2 for Accounts self.assertEqual(engineer.get_num_searchterms(query), 3)
def test_dup_terms(self): """ Tests the _handle_phrases method for terms with duplicate words. """ terms = [SearchTerm(term='police'), SearchTerm(term='police officer')] query = ReservoirQuery(searchterms=terms) specs = PipeSpecSheet(accepts_phrases=False) engineer = Engineer(query=query, specs=specs) engineer._handle_phrases() searchterms = engineer.get_searchterms() self.assertEqual(len(searchterms), 3)
def test_for_negation_not_allowed(self): """ Tests the _handle_negation method for an API that doesn't allow negation of search terms. """ terms = [SearchTerm(term='police'), SearchTerm(term='car', negate=True)] query = ReservoirQuery(searchterms=terms) engineer = Engineer(query=query, specs=PipeSpecSheet()) engineer._handle_negation() searchterms = engineer.get_searchterms() self.assertEqual(len(searchterms), 1)
def test_for_accepts_phrases(self): """ Tests the _handle_phrases method for an API that accepts phrases. """ terms = [SearchTerm(term='police officer')] query = ReservoirQuery(searchterms=terms) specs = PipeSpecSheet(accepts_phrases=True) engineer = Engineer(query=query, specs=specs) engineer._handle_phrases() searchterms = engineer.get_searchterms() self.assertEqual(len(searchterms), 1) self.assertEqual(searchterms[0].term, 'police officer')
def test_factor_queries_by_component(self): """ Tests the _factor_queries_by_component method for searchterms. """ specs = PipeSpecSheet(searchterms_limit=1) engineer = Engineer(query=self.query, specs=specs) queries1 = engineer._factor_queries_by_component([self.query], 'locations', 1) self.assertEqual(len(queries1), 4) queries2 = engineer._factor_queries_by_component([self.query], 'accounts', 1) self.assertEqual(len(queries2), 2)
def test_for_multiple_phrases(self): """ Tests the _handle_phrases method for an API that accepts phrases. """ # 'police', 'police oficeer', 'bank robbery' terms = SearchTerm.objects.filter(pk__in=[1, 2, 3]) query = ReservoirQuery(searchterms=terms) specs = PipeSpecSheet(accepts_phrases=False) engineer = Engineer(query=query, specs=specs) engineer._handle_phrases() searchterms = engineer.get_searchterms() # 'police', 'officer', policeoficeer', 'bank', 'robbery', bankrobbery' self.assertEqual(len(searchterms), 6)
def test_needs_splitting(self): """ Tests the _handle_combined_trm_fol_limit method for a query that includes accounts and search terms, but that doesn't exceed the API limit. """ specs = PipeSpecSheet(searchterms_limit=8, combine_trm_fol=True, trm_trm_logic='AND', combine_trm_opr=True, OR_operator='OR') engineer = Engineer(query=self.query, specs=specs) queries = engineer._handle_combined_trm_fol_limit([self.query]) self.assertEqual(len(queries), 2)
def test_format_for_box(self): """ Tests the format_locations method for a 'box' location_format. """ query = ReservoirQuery(locations=self.locations) specs = PipeSpecSheet(location_format='box') engineer = Engineer(query=query, specs=specs) engineer._format_locations() all_boxes = False for new_location in engineer.get_locations(): if new_location.shape == 'Rectangle': all_boxes = True else: all_boxes = False break self.assertTrue(all_boxes)
def test_format_for_radius(self): """ Tests the format_locations method for a 'radius' location_format. """ query = ReservoirQuery(locations=self.locations) specs = PipeSpecSheet(location_format='radius', radius_limit_km=100) engineer = Engineer(query=query, specs=specs) engineer._format_locations() all_circles = False for new_location in engineer.get_locations(): if new_location.shape == 'Circle': all_circles = True else: all_circles = False break self.assertTrue(all_circles)
def setUp(self): super(GetQueryParametersTestCase, self).setUp() self.engineer = Engineer(query=self.query, specs=PipeSpecSheet())