def test_save_synonyms(self): # Test null response self.index.save_synonyms([]).wait() # Test object id validation with self.assertRaises(MissingObjectIdException) as _: self.index.save_synonyms([F.synonym(object_id=False)]) # Test object id validation with self.assertRaises(MissingObjectIdException) as _: self.index.save_synonym(F.synonym(object_id=False))
def test_cross_app_copy_index(self): rule = F.rule(object_id='one') synonym = F.synonym(object_id='one') responses = [ self.index.save_object({'objectID': 'one'}), self.index.save_rule(rule), self.index.save_synonym(synonym), self.index.set_settings({'searchableAttributes': ['objectID']}) ] MultipleResponse(responses).wait() AccountClient.copy_index(self.index, self.index2).wait() # Assert objects got copied res = self.index2.search('') self.assertEqual(len(res['hits']), 1) self.assertEqual(res['hits'][0], {'objectID': 'one'}) # Assert settings got copied settings = self.index2.get_settings() self.assertEqual(settings['searchableAttributes'], ['objectID']) # Assert synonyms got copied self.assertEqual(self.index2.get_rule('one'), rule) # Assert synonyms got copied self.assertEqual(self.index2.get_synonym('one'), synonym) # Assert that copying again fails because index already exists with self.assertRaises(AlgoliaException) as _: AccountClient.copy_index(self.index, self.index2)
def test_cross_app_copy_index(self): rule = F.rule(object_id="one") synonym = F.synonym(object_id="one") responses = [ self.index.save_object({"objectID": "one"}), self.index.save_rule(rule), self.index.save_synonym(synonym), self.index.set_settings({"searchableAttributes": ["objectID"]}), ] MultipleResponse(responses).wait() AccountClient.copy_index(self.index, self.index2).wait() # Assert objects got copied res = self.index2.search("") self.assertEqual(len(res["hits"]), 1) self.assertEqual(res["hits"][0], {"objectID": "one"}) # Assert settings got copied settings = self.index2.get_settings() self.assertEqual(settings["searchableAttributes"], ["objectID"]) # Assert rules got copied self.assertEqual(rule_without_metadata(self.index2.get_rule("one")), rule) # Assert synonyms got copied list_synonyms1 = [synonym for synonym in self.index.browse_synonyms()] list_synonyms2 = [synonym for synonym in self.index2.browse_synonyms()] self.assertEqual(list_synonyms1, list_synonyms2) # Assert synomys are the same self.assertEqual(self.index2.get_synonym("one"), synonym) # Assert that copying again fails because index already exists with self.assertRaises(AlgoliaException) as _: AccountClient.copy_index(self.index, self.index2)
def test_synonyms(self): responses = MultipleResponse() responses.push(self.index.save_objects([ {"console": "Sony PlayStation <PLAYSTATIONVERSION>"}, {"console": "Nintendo Switch"}, {"console": "Nintendo Wii U"}, {"console": "Nintendo Game Boy Advance"}, {"console": "Microsoft Xbox"}, {"console": "Microsoft Xbox 360"}, {"console": "Microsoft Xbox One"} ], {'autoGenerateObjectIDIfNotExist': True})) responses.push(self.index.save_synonym(F.synonym({ 'synonyms': [ "gba", "gameboy advance", "game boy advance" ] }, 'gba'))) synonym1 = { 'objectID': 'wii_to_wii_u', 'type': 'onewaysynonym', 'input': 'wii', 'synonyms': ['wii U'] } synonym2 = { 'objectID': 'playstation_version_placeholder', 'type': 'placeholder', 'placeholder': '<PLAYSTATIONVERSION>', 'replacements': [ "1", "One", "2", "3", "4", "4 Pro", ] } synonym3 = { 'objectID': 'ps4', 'type': 'altcorrection1', 'word': 'ps4', 'corrections': ['playstation4'] } synonym4 = { 'objectID': 'psone', 'type': 'altcorrection2', 'word': 'psone', 'corrections': ['playstationone'] } responses.push(self.index.save_synonyms([ synonym1, synonym2, synonym3, synonym4 ])) responses.wait() self.assertEqual(self.index.get_synonym(synonym1['objectID']), synonym1) self.assertEqual(self.index.get_synonym(synonym2['objectID']), synonym2) self.assertEqual(self.index.get_synonym(synonym3['objectID']), synonym3) self.assertEqual(self.index.get_synonym(synonym4['objectID']), synonym4) self.assertEqual(self.index.search_synonyms('')['nbHits'], 5) # Browse all records with browse_objects results = [] for obj in self.index.browse_synonyms(): results.append(obj) synonyms = [ synonym1, synonym2, synonym3, synonym4 ] for synonym in synonyms: self.assertIn(synonym, results) self.index.delete_synonym('gba').wait() # Try to get the synonym with getSynonym with objectID `gba and c # heck that the synonym does not exist anymore with self.assertRaises(RequestException) as _: self.index.get_synonym('gba') # Clear all the synonyms using clear_synonyms self.index.clear_synonyms().wait() # Perform a synonym search using searchSynonyms with an empty query # and check that the number of returned synonyms is equal to 0 self.assertEqual(self.index.search_synonyms('')['nbHits'], 0)
def test_synonyms(self): responses = MultipleResponse() responses.push( self.index.save_objects( [ { "console": "Sony PlayStation <PLAYSTATIONVERSION>" }, { "console": "Nintendo Switch" }, { "console": "Nintendo Wii U" }, { "console": "Nintendo Game Boy Advance" }, { "console": "Microsoft Xbox" }, { "console": "Microsoft Xbox 360" }, { "console": "Microsoft Xbox One" }, ], {"autoGenerateObjectIDIfNotExist": True}, )) responses.push( self.index.save_synonym( F.synonym( { "synonyms": ["gba", "gameboy advance", "game boy advance"] }, "gba"))) synonym1 = { "objectID": "wii_to_wii_u", "type": "onewaysynonym", "input": "wii", "synonyms": ["wii U"], } synonym2 = { "objectID": "playstation_version_placeholder", "type": "placeholder", "placeholder": "<PLAYSTATIONVERSION>", "replacements": ["1", "One", "2", "3", "4", "4 Pro"], } synonym3 = { "objectID": "ps4", "type": "altcorrection1", "word": "ps4", "corrections": ["playstation4"], } synonym4 = { "objectID": "psone", "type": "altcorrection2", "word": "psone", "corrections": ["playstationone"], } responses.push( self.index.save_synonyms([synonym1, synonym2, synonym3, synonym4])) responses.wait() self.assertEqual(self.index.get_synonym(synonym1["objectID"]), synonym1) self.assertEqual(self.index.get_synonym(synonym2["objectID"]), synonym2) self.assertEqual(self.index.get_synonym(synonym3["objectID"]), synonym3) self.assertEqual(self.index.get_synonym(synonym4["objectID"]), synonym4) self.assertEqual(self.index.search_synonyms("")["nbHits"], 5) # Browse all records with browse_objects results = [] for obj in self.index.browse_synonyms(): results.append(obj) synonyms = [synonym1, synonym2, synonym3, synonym4] for synonym in synonyms: self.assertIn(synonym, results) self.index.delete_synonym("gba").wait() # Try to get the synonym with getSynonym with objectID `gba and c # heck that the synonym does not exist anymore with self.assertRaises(RequestException) as _: self.index.get_synonym("gba") # Clear all the synonyms using clear_synonyms self.index.clear_synonyms().wait() # Perform a synonym search using searchSynonyms with an empty query # and check that the number of returned synonyms is equal to 0 self.assertEqual(self.index.search_synonyms("")["nbHits"], 0)