Best Python code snippet using localstack_python
anysearch.py
Source:anysearch.py
1"""2Compatibility library for smooth support of Elasticsearch and3OpenSearch packages (including *search-dsl).4The concept and some parts of the code have been snatched from the famous `six`5package.6"""7import logging8import os9import subprocess10import sys11import types12from importlib.util import spec_from_loader13from typing import Set14__title__ = "anysearch"15__version__ = "0.2.1"16__author__ = "Artur Barseghyan <artur.barseghyan@gmail.com>"17__copyright__ = "2022 Artur Barseghyan"18__license__ = "MIT"19LOGGER = logging.getLogger(__name__)20def get_installed_packages() -> Set[str]:21 """Get installed packages.22 :return: Set of installed packages.23 """24 try:25 reqs = subprocess.check_output([sys.executable, "-m", "pip", "freeze"])26 installed_packages = set(27 [r.decode().split("==")[0] for r in reqs.split()]28 )29 return installed_packages30 except subprocess.CalledProcessError:31 LOGGER.exception("Please install pip3 before running this script.")32 return set()33def check_if_package_is_installed(34 package_name, installed_packages: Set[str] = None35) -> bool:36 """Check if a package is installed.37 :param package_name: Package name.38 :param installed_packages: Set of installed packages.39 :return: True if package is installed, False otherwise.40 """41 if not installed_packages:42 installed_packages = get_installed_packages()43 return package_name in installed_packages44ELASTICSEARCH = "Elasticsearch"45OPENSEARCH = "OpenSearch"46def detect_search_backend():47 """Detect the search backend."""48 env_var = os.environ.get("ANYSEARCH_PREFERRED_BACKEND")49 if env_var == ELASTICSEARCH:50 return ELASTICSEARCH51 elif env_var == OPENSEARCH:52 return OPENSEARCH53 else:54 if check_if_package_is_installed("opensearch-dsl"):55 return OPENSEARCH56 elif check_if_package_is_installed("elasticsearch-dsl"):57 return ELASTICSEARCH58 raise Exception(59 "You should either set `ANYSEARCH_BACKEND` env var to `elasticsearch` "60 "or `opensearch` or install a combination of (1) `elasticsearch`, "61 "`elasticsearch-dsl` or (2) "62 "`opensearch-py`, `opensearch-dsl`."63 )64SEARCH_BACKEND = detect_search_backend()65IS_ELASTICSEARCH = SEARCH_BACKEND == ELASTICSEARCH66IS_OPENSEARCH = SEARCH_BACKEND == OPENSEARCH67def _import_module(name):68 """Import module, returning the module after the last dot."""69 __import__(name)70 return sys.modules[name]71class _LazyDescr(object):72 def __init__(self, name):73 self.name = name74 def __get__(self, obj, tp):75 result = self._resolve()76 setattr(obj, self.name, result) # Invokes __set__.77 try:78 # This is a bit ugly, but it avoids running this again by79 # removing this descriptor.80 delattr(obj.__class__, self.name)81 except AttributeError:82 pass83 return result84class MovedModule(_LazyDescr):85 def __init__(self, name, old, new=None):86 super(MovedModule, self).__init__(name)87 if IS_OPENSEARCH:88 if new is None:89 new = name90 self.mod = new91 else:92 self.mod = old93 def _resolve(self):94 return _import_module(self.mod)95 def __getattr__(self, attr):96 _module = self._resolve()97 value = getattr(_module, attr)98 setattr(self, attr, value)99 return value100class _LazyModule(types.ModuleType):101 def __init__(self, name):102 super(_LazyModule, self).__init__(name)103 self.__doc__ = self.__class__.__doc__104 def __dir__(self):105 attrs = ["__doc__", "__name__"]106 attrs += [__attr.name for __attr in self._moved_attributes]107 return attrs108 # Subclasses should override this109 _moved_attributes = []110class MovedAttribute(_LazyDescr):111 def __init__(self, name, old_mod, new_mod, old_attr=None, new_attr=None):112 super(MovedAttribute, self).__init__(name)113 if IS_OPENSEARCH:114 if new_mod is None:115 new_mod = name116 self.mod = new_mod117 if new_attr is None:118 if old_attr is None:119 new_attr = name120 else:121 new_attr = old_attr122 self.attr = new_attr123 else:124 self.mod = old_mod125 if old_attr is None:126 old_attr = name127 self.attr = old_attr128 def _resolve(self):129 module = _import_module(self.mod)130 return getattr(module, self.attr)131class _AnySearchMetaPathImporter(object):132 """133 A meta path importer to import anysearch.search, anysearch.search_dsl,134 and its submodules.135 This class implements a PEP302 finder and loader. It should be compatible136 with Python 2.5 and all existing versions of Python3.137 """138 def __init__(self, module_name):139 self.name = module_name140 self.known_modules = {}141 def _add_module(self, mod, *fullnames):142 for fullname in fullnames:143 self.known_modules[self.name + "." + fullname] = mod144 def _get_module(self, fullname):145 return self.known_modules[self.name + "." + fullname]146 def find_module(self, fullname, path=None):147 if fullname in self.known_modules:148 return self149 return None150 def find_spec(self, fullname, path, target=None):151 if fullname in self.known_modules:152 return spec_from_loader(fullname, self)153 return None154 def __get_module(self, fullname):155 try:156 return self.known_modules[fullname]157 except KeyError:158 raise ImportError("This loader does not know module " + fullname)159 def load_module(self, fullname):160 try:161 # in case of a reload162 return sys.modules[fullname]163 except KeyError:164 pass165 mod = self.__get_module(fullname)166 if isinstance(mod, MovedModule):167 mod = mod._resolve()168 else:169 mod.__loader__ = self170 sys.modules[fullname] = mod171 return mod172 def is_package(self, fullname):173 """174 Return true, if the named module is a package.175 We need this method to get correct spec objects with176 Python 3.4 (see PEP451)177 """178 return hasattr(self.__get_module(fullname), "__path__")179 def get_code(self, fullname):180 """Return None181 Required, if is_package is implemented"""182 self.__get_module(fullname) # eventually raises ImportError183 return None184 get_source = get_code # same as get_code185 def create_module(self, spec):186 return self.load_module(spec.name)187 def exec_module(self, module):188 pass189_importer = _AnySearchMetaPathImporter(__name__)190# **************************************************191# ****************** Search ************************192# **************************************************193class _SearchMovedItems(_LazyModule):194 """Lazy loading of search objects"""195 __path__ = [] # mark as package196_search_moved_attributes = [197 # elasticsearch/opensearch198 # **********************************************199 # **************** Moved modules ***************200 # **********************************************201 # .202 MovedModule("client", "elasticsearch", "opensearchpy"),203 MovedModule("compat", "elasticsearch", "opensearchpy"),204 MovedModule("exceptions", "elasticsearch", "opensearchpy"),205 MovedModule("serializer", "elasticsearch", "opensearchpy"),206 MovedModule("transport", "elasticsearch", "opensearchpy"),207 MovedModule("helpers", "elasticsearch", "opensearchpy"),208 # As attributes of the module.209 MovedAttribute("client", "elasticsearch", "opensearchpy"),210 MovedAttribute("compat", "elasticsearch", "opensearchpy"),211 MovedAttribute("exceptions", "elasticsearch", "opensearchpy"),212 MovedAttribute("serializer", "elasticsearch", "opensearchpy"),213 MovedAttribute("transport", "elasticsearch", "opensearchpy"),214 MovedAttribute("helpers", "elasticsearch", "opensearchpy"),215 MovedAttribute("bulk", "elasticsearch.helpers", "opensearchpy.helpers"),216 MovedAttribute("parallel_bulk", "elasticsearch.helpers", "opensearchpy.helpers"),217 # **********************************************218 # ************* Moved attributes ***************219 # **********************************************220 # .221 MovedAttribute("__version__", "elasticsearch", "opensearchpy"),222 MovedAttribute(223 "AnySearch",224 "elasticsearch",225 "opensearchpy",226 "Elasticsearch",227 "OpenSearch",228 ),229 MovedAttribute("Transport", "elasticsearch", "opensearchpy"),230 MovedAttribute("ConnectionPool", "elasticsearch", "opensearchpy"),231 MovedAttribute("ConnectionSelector", "elasticsearch", "opensearchpy"),232 MovedAttribute("RoundRobinSelector", "elasticsearch", "opensearchpy"),233 MovedAttribute("JSONSerializer", "elasticsearch", "opensearchpy"),234 MovedAttribute("Connection", "elasticsearch", "opensearchpy"),235 MovedAttribute("RequestsHttpConnection", "elasticsearch", "opensearchpy"),236 MovedAttribute("Urllib3HttpConnection", "elasticsearch", "opensearchpy"),237 MovedAttribute("ImproperlyConfigured", "elasticsearch", "opensearchpy"),238 MovedAttribute(239 "AnySearchException",240 "elasticsearch",241 "opensearchpy",242 "ElasticsearchException",243 "OpenSearchException",244 ),245 MovedAttribute("SerializationError", "elasticsearch", "opensearchpy"),246 MovedAttribute("TransportError", "elasticsearch", "opensearchpy"),247 MovedAttribute("NotFoundError", "elasticsearch", "opensearchpy"),248 MovedAttribute("ConflictError", "elasticsearch", "opensearchpy"),249 MovedAttribute("RequestError", "elasticsearch", "opensearchpy"),250 MovedAttribute("ConnectionError", "elasticsearch", "opensearchpy"),251 MovedAttribute("SSLError", "elasticsearch", "opensearchpy"),252 MovedAttribute("ConnectionTimeout", "elasticsearch", "opensearchpy"),253 MovedAttribute("AuthenticationException", "elasticsearch", "opensearchpy"),254 MovedAttribute("AuthorizationException", "elasticsearch", "opensearchpy"),255 MovedAttribute(256 "AnySearchDeprecationWarning",257 "elasticsearch",258 "opensearchpy",259 "ElasticsearchDeprecationWarning",260 "OpenSearchDeprecationWarning",261 ),262]263for _search_attr in _search_moved_attributes:264 setattr(_SearchMovedItems, _search_attr.name, _search_attr)265 if isinstance(_search_attr, MovedModule):266 _importer._add_module(_search_attr, "search." + _search_attr.name)267try:268 del _search_attr269except:270 pass271_SearchMovedItems._moved_attributes = _search_moved_attributes272search = _SearchMovedItems(__name__ + ".search")273_importer._add_module(search, "search")274# **************************************************275# **************************************************276# ****************** Search-DSL ********************277# **************************************************278# **************************************************279class _SearchDSLMovedItems(_LazyModule):280 """Lazy loading of search_dsl objects"""281 __path__ = [] # mark as package282_search_dsl_moved_attributes = [283 # elasticsearch_dsl/opensearch_dsl284 # **********************************************285 # **************** Moved modules ***************286 # **********************************************287 MovedModule("aggs", "elasticsearch_dsl", "opensearch_dsl"),288 MovedModule("analysis", "elasticsearch_dsl", "opensearch_dsl"),289 MovedModule("connections", "elasticsearch_dsl", "opensearch_dsl"),290 MovedModule("document", "elasticsearch_dsl", "opensearch_dsl"),291 MovedModule("exceptions", "elasticsearch_dsl", "opensearch_dsl"),292 MovedModule("faceted_search", "elasticsearch_dsl", "opensearch_dsl"),293 MovedModule("field", "elasticsearch_dsl", "opensearch_dsl"),294 MovedModule("function", "elasticsearch_dsl", "opensearch_dsl"),295 MovedModule("index", "elasticsearch_dsl", "opensearch_dsl"),296 MovedModule("mapping", "elasticsearch_dsl", "opensearch_dsl"),297 MovedModule("query", "elasticsearch_dsl", "opensearch_dsl"),298 MovedModule("search", "elasticsearch_dsl", "opensearch_dsl"),299 MovedModule("serializer", "elasticsearch_dsl", "opensearch_dsl"),300 MovedModule("update_by_query", "elasticsearch_dsl", "opensearch_dsl"),301 MovedModule("utils", "elasticsearch_dsl", "opensearch_dsl"),302 MovedModule("wrappers", "elasticsearch_dsl", "opensearch_dsl"),303 # Again, but now as attributes of the module.304 MovedAttribute("aggs", "elasticsearch_dsl", "opensearch_dsl"),305 MovedAttribute("analysis", "elasticsearch_dsl", "opensearch_dsl"),306 MovedAttribute("connections", "elasticsearch_dsl", "opensearch_dsl"),307 MovedAttribute("document", "elasticsearch_dsl", "opensearch_dsl"),308 MovedAttribute("exceptions", "elasticsearch_dsl", "opensearch_dsl"),309 MovedAttribute("faceted_search", "elasticsearch_dsl", "opensearch_dsl"),310 MovedAttribute("field", "elasticsearch_dsl", "opensearch_dsl"),311 MovedAttribute("function", "elasticsearch_dsl", "opensearch_dsl"),312 MovedAttribute("index", "elasticsearch_dsl", "opensearch_dsl"),313 MovedAttribute("mapping", "elasticsearch_dsl", "opensearch_dsl"),314 MovedAttribute("query", "elasticsearch_dsl", "opensearch_dsl"),315 MovedAttribute("search", "elasticsearch_dsl", "opensearch_dsl"),316 MovedAttribute("serializer", "elasticsearch_dsl", "opensearch_dsl"),317 MovedAttribute("update_by_query", "elasticsearch_dsl", "opensearch_dsl"),318 MovedAttribute("utils", "elasticsearch_dsl", "opensearch_dsl"),319 MovedAttribute("wrappers", "elasticsearch_dsl", "opensearch_dsl"),320 # **********************************************321 # ************* Moved attributes ***************322 # **********************************************323 # .324 MovedAttribute("__version__", "elasticsearch_dsl", "opensearch_dsl"),325 MovedAttribute("connections", "elasticsearch_dsl", "opensearch_dsl"),326 # .aggs327 MovedAttribute("A", "elasticsearch_dsl", "opensearch_dsl"),328 # .analysis329 MovedAttribute("analyzer", "elasticsearch_dsl", "opensearch_dsl"),330 MovedAttribute("char_filter", "elasticsearch_dsl", "opensearch_dsl"),331 MovedAttribute("normalizer", "elasticsearch_dsl", "opensearch_dsl"),332 MovedAttribute("token_filter", "elasticsearch_dsl", "opensearch_dsl"),333 MovedAttribute("tokenizer", "elasticsearch_dsl", "opensearch_dsl"),334 # .document335 MovedAttribute("Document", "elasticsearch_dsl", "opensearch_dsl"),336 MovedAttribute("InnerDoc", "elasticsearch_dsl", "opensearch_dsl"),337 MovedAttribute("MetaField", "elasticsearch_dsl", "opensearch_dsl"),338 # .exceptions339 MovedAttribute(340 "AnySearchDslException",341 "elasticsearch_dsl",342 "opensearch_dsl",343 "ElasticsearchDslException",344 "OpenSearchDslException",345 ),346 MovedAttribute("IllegalOperation", "elasticsearch_dsl", "opensearch_dsl"),347 MovedAttribute("UnknownDslObject", "elasticsearch_dsl", "opensearch_dsl"),348 MovedAttribute(349 "ValidationException", "elasticsearch_dsl", "opensearch_dsl"350 ),351 # .faceted_search352 MovedAttribute("DateHistogramFacet", "elasticsearch_dsl", "opensearch_dsl"),353 MovedAttribute("Facet", "elasticsearch_dsl", "opensearch_dsl"),354 MovedAttribute("FacetedResponse", "elasticsearch_dsl", "opensearch_dsl"),355 MovedAttribute("FacetedSearch", "elasticsearch_dsl", "opensearch_dsl"),356 MovedAttribute("HistogramFacet", "elasticsearch_dsl", "opensearch_dsl"),357 MovedAttribute("NestedFacet", "elasticsearch_dsl", "opensearch_dsl"),358 MovedAttribute("RangeFacet", "elasticsearch_dsl", "opensearch_dsl"),359 MovedAttribute("TermsFacet", "elasticsearch_dsl", "opensearch_dsl"),360 # .field361 MovedAttribute("Binary", "elasticsearch_dsl", "opensearch_dsl"),362 MovedAttribute("Boolean", "elasticsearch_dsl", "opensearch_dsl"),363 MovedAttribute("Byte", "elasticsearch_dsl", "opensearch_dsl"),364 MovedAttribute("Completion", "elasticsearch_dsl", "opensearch_dsl"),365 MovedAttribute("CustomField", "elasticsearch_dsl", "opensearch_dsl"),366 MovedAttribute("Date", "elasticsearch_dsl", "opensearch_dsl"),367 MovedAttribute("DateRange", "elasticsearch_dsl", "opensearch_dsl"),368 MovedAttribute("DenseVector", "elasticsearch_dsl", "opensearch_dsl"),369 MovedAttribute("Double", "elasticsearch_dsl", "opensearch_dsl"),370 MovedAttribute("DoubleRange", "elasticsearch_dsl", "opensearch_dsl"),371 MovedAttribute("Field", "elasticsearch_dsl", "opensearch_dsl"),372 MovedAttribute("Float", "elasticsearch_dsl", "opensearch_dsl"),373 MovedAttribute("FloatRange", "elasticsearch_dsl", "opensearch_dsl"),374 MovedAttribute("GeoPoint", "elasticsearch_dsl", "opensearch_dsl"),375 MovedAttribute("GeoShape", "elasticsearch_dsl", "opensearch_dsl"),376 MovedAttribute("HalfFloat", "elasticsearch_dsl", "opensearch_dsl"),377 MovedAttribute("Integer", "elasticsearch_dsl", "opensearch_dsl"),378 MovedAttribute("IntegerRange", "elasticsearch_dsl", "opensearch_dsl"),379 MovedAttribute("Ip", "elasticsearch_dsl", "opensearch_dsl"),380 MovedAttribute("IpRange", "elasticsearch_dsl", "opensearch_dsl"),381 MovedAttribute("Join", "elasticsearch_dsl", "opensearch_dsl"),382 MovedAttribute("Keyword", "elasticsearch_dsl", "opensearch_dsl"),383 MovedAttribute("Long", "elasticsearch_dsl", "opensearch_dsl"),384 MovedAttribute("LongRange", "elasticsearch_dsl", "opensearch_dsl"),385 MovedAttribute("Murmur3", "elasticsearch_dsl", "opensearch_dsl"),386 MovedAttribute("Nested", "elasticsearch_dsl", "opensearch_dsl"),387 MovedAttribute("Object", "elasticsearch_dsl", "opensearch_dsl"),388 MovedAttribute("Percolator", "elasticsearch_dsl", "opensearch_dsl"),389 MovedAttribute("RangeField", "elasticsearch_dsl", "opensearch_dsl"),390 MovedAttribute("RankFeature", "elasticsearch_dsl", "opensearch_dsl"),391 MovedAttribute("RankFeatures", "elasticsearch_dsl", "opensearch_dsl"),392 MovedAttribute("ScaledFloat", "elasticsearch_dsl", "opensearch_dsl"),393 MovedAttribute("SearchAsYouType", "elasticsearch_dsl", "opensearch_dsl"),394 MovedAttribute("Short", "elasticsearch_dsl", "opensearch_dsl"),395 MovedAttribute("SparseVector", "elasticsearch_dsl", "opensearch_dsl"),396 MovedAttribute("Text", "elasticsearch_dsl", "opensearch_dsl"),397 MovedAttribute("TokenCount", "elasticsearch_dsl", "opensearch_dsl"),398 MovedAttribute("construct_field", "elasticsearch_dsl", "opensearch_dsl"),399 # .function400 MovedAttribute("SF", "elasticsearch_dsl", "opensearch_dsl"),401 # .index402 MovedAttribute("Index", "elasticsearch_dsl", "opensearch_dsl"),403 MovedAttribute("IndexTemplate", "elasticsearch_dsl", "opensearch_dsl"),404 # .mapping405 MovedAttribute("Mapping", "elasticsearch_dsl", "opensearch_dsl"),406 # .query407 MovedAttribute("Q", "elasticsearch_dsl.query", "opensearch_dsl.query"),408 # .search409 MovedAttribute("MultiSearch", "elasticsearch_dsl", "opensearch_dsl"),410 MovedAttribute("Search", "elasticsearch_dsl", "opensearch_dsl"),411 # .update_by_query412 MovedAttribute("UpdateByQuery", "elasticsearch_dsl", "opensearch_dsl"),413 # .utils414 MovedAttribute(415 "AttrDict", "elasticsearch_dsl.utils", "opensearch_dsl.utils"416 ),417 MovedAttribute(418 "AttrList", "elasticsearch_dsl.utils", "opensearch_dsl.utils"419 ),420 MovedAttribute("DslBase", "elasticsearch_dsl", "opensearch_dsl"),421 # .wrappers422 MovedAttribute("Range", "elasticsearch_dsl", "opensearch_dsl"),423 # **********************************************424 # ********** Additional moved attributes *******425 # **********************************************426 MovedAttribute(427 "AggsProxy", "elasticsearch_dsl.search", "opensearch_dsl.search"428 ),429 MovedAttribute(430 "MoreLikeThis", "elasticsearch_dsl.query", "opensearch_dsl.query"431 ),432]433for _search_dsl_attr in _search_dsl_moved_attributes:434 setattr(_SearchDSLMovedItems, _search_dsl_attr.name, _search_dsl_attr)435 if isinstance(_search_dsl_attr, MovedModule):436 _importer._add_module(437 _search_dsl_attr, "search_dsl." + _search_dsl_attr.name438 )439try:440 del _search_dsl_attr441except:442 pass443_SearchDSLMovedItems._moved_attributes = _search_dsl_moved_attributes444search_dsl = _SearchDSLMovedItems(__name__ + ".search_dsl")445_importer._add_module(search_dsl, "search_dsl")446# # **************************************************447# # **************************************************448# # ************** Django-Search-DSL *****************449# # **************************************************450# # **************************************************451#452#453# class _DjangoSearchDSLMovedItems(_LazyModule):454#455# """Lazy loading of django_search_dsl objects"""456#457# __path__ = []458#459#460# _django_search_dsl_moved_attributes = [461# # **********************************************462# # **************** Moved modules ***************463# # **********************************************464# # django_elasticsearch_dsl/django_opensearch_dsl465# MovedModule("apps", "django_elasticsearch_dsl", "django_opensearch_dsl"),466# MovedModule(467# "documents", "django_elasticsearch_dsl", "django_opensearch_dsl"468# ),469# MovedModule(470# "exceptions", "django_elasticsearch_dsl", "django_opensearch_dsl"471# ),472# MovedModule("fields", "django_elasticsearch_dsl", "django_opensearch_dsl"),473# # MovedModule("indices", "django_elasticsearch_dsl", "django_opensearch_dsl"),474# # MovedModule("models", "django_elasticsearch_dsl", "django_opensearch_dsl"),475# MovedModule(476# "registries", "django_elasticsearch_dsl", "django_opensearch_dsl"477# ),478# MovedModule("search", "django_elasticsearch_dsl", "django_opensearch_dsl"),479# MovedModule("signals", "django_elasticsearch_dsl", "django_opensearch_dsl"),480# # Again, but now as attributes of the module.481# MovedAttribute("apps", "django_elasticsearch_dsl", "django_opensearch_dsl"),482# MovedAttribute(483# "documents", "django_elasticsearch_dsl", "django_opensearch_dsl"484# ),485# MovedAttribute(486# "exceptions", "django_elasticsearch_dsl", "django_opensearch_dsl"487# ),488# MovedAttribute(489# "fields", "django_elasticsearch_dsl", "django_opensearch_dsl"490# ),491# # MovedAttribute("indices", "django_elasticsearch_dsl", "django_opensearch_dsl"),492# # MovedAttribute("models", "django_elasticsearch_dsl", "django_opensearch_dsl"),493# MovedAttribute(494# "registries", "django_elasticsearch_dsl", "django_opensearch_dsl"495# ),496# MovedAttribute(497# "search", "django_elasticsearch_dsl", "django_opensearch_dsl"498# ),499# MovedAttribute(500# "signals", "django_elasticsearch_dsl", "django_opensearch_dsl"501# ),502# # **********************************************503# # *************** Moved attributes *************504# # **********************************************505# MovedAttribute(506# "Document", "django_elasticsearch_dsl", "django_opensearch_dsl"507# ),508# # MovedAttribute("Index", "django_elasticsearch_dsl", "django_opensearch_dsl"),509# # **********************************************510# # ********* Additional moved attributes ********511# # **********************************************512# MovedAttribute(513# "registry",514# "django_elasticsearch_dsl.registries",515# "django_opensearch_dsl.registries",516# ),517# MovedAttribute(518# "DjangoAnySearchConfig",519# "django_elasticsearch_dsl.apps",520# "django_opensearch_dsl.apps",521# "DEDConfig",522# "DODConfig",523# ),524# MovedAttribute(525# "RealTimeSignalProcessor",526# "django_elasticsearch_dsl.signals",527# "django_opensearch_dsl.signals",528# ),529# MovedAttribute(530# "BaseSignalProcessor",531# "django_elasticsearch_dsl.signals",532# "django_opensearch_dsl.signals",533# ),534# ]535#536# for _django_search_dsl_attr in _django_search_dsl_moved_attributes:537# setattr(538# _DjangoSearchDSLMovedItems,539# _django_search_dsl_attr.name,540# _django_search_dsl_attr,541# )542# if isinstance(_django_search_dsl_attr, MovedModule):543# _importer._add_module(544# _django_search_dsl_attr,545# "django_search_dsl." + _django_search_dsl_attr.name,546# )547#548# try:549# del _django_search_dsl_attr550# except:551# pass552#553#554# _DjangoSearchDSLMovedItems._moved_attributes = (555# _django_search_dsl_moved_attributes556# )557#558# django_search_dsl = _DjangoSearchDSLMovedItems(__name__ + ".django_search_dsl")559# _importer._add_module(django_search_dsl, "django_search_dsl")560# **************************************************561# **************************************************562# **************************************************563# Index = getattr(moves, "Index", None)564#565# if not Index:566# from copy import deepcopy567#568# from opensearch_dsl import Index as DSLIndex569#570# from django_opensearch_dsl.apps import DEDConfig571# from django_opensearch_dsl.registries import registry572#573#574# class Index(DSLIndex):575# def __init__(self, *args, **kwargs):576# super(Index, self).__init__(*args, **kwargs)577# default_index_settings = deepcopy(578# DEDConfig.default_index_settings())579# self.settings(**default_index_settings)580#581# def document(self, document):582# """583# Extend to register the document in the global document registry584# """585# document = super(Index, self).document(document)586# registry.register_document(document)587# return document588#589# doc_type = document590#591# def __str__(self):592# return self._name593# Complete the moves implementation.594# This code is at the end of this module to speed up module loading.595# Turn this module into a package.596__path__ = [] # required for PEP 302 and PEP 451597__package__ = __name__ # see PEP 366 @ReservedAssignment598if globals().get("__spec__") is not None:599 __spec__.submodule_search_locations = [] # PEP 451 @UndefinedVariable600# Remove other six meta path importers, since they cause problems. This can601# happen if six is removed from sys.modules and then reloaded. (Setuptools does602# this for some reason.)603if sys.meta_path:604 for i, importer in enumerate(sys.meta_path):605 # Here's some real nastiness: Another "instance" of the six module might606 # be floating around. Therefore, we can't use isinstance() to check for607 # the six meta path importer, since the other six instance will have608 # inserted an importer with different class.609 if (610 type(importer).__name__ == "_AnySearchMetaPathImporter"611 and importer.name == __name__612 ):613 del sys.meta_path[i]614 break615 del i, importer616# Finally, add the importer to the meta path import hook....
test_anysearch.py
Source:test_anysearch.py
1import logging2import unittest3from importlib import import_module4from unittest import mock5from anysearch import (6 ELASTICSEARCH,7 OPENSEARCH,8 check_if_package_is_installed,9 detect_search_backend,10 get_installed_packages,11)12__title__ = "test_anysearch"13__author__ = "Artur Barseghyan <artur.barseghyan@gmail.com>"14__copyright__ = "2022 Artur Barseghyan"15__license__ = "MIT"16LOGGER = logging.getLogger(__name__)17def MovedModule(*args):18 """Moved module."""19 return args20def MovedAttribute(*args):21 """Moved attribute."""22 return args23SEARCH_MOVED_MODULES = []24SEARCH_MOVED_ATTRIBUTES = [25 # .26 # MovedAttribute("__version__", "elasticsearch", "opensearchpy"),27 MovedAttribute(28 "AnySearch",29 "elasticsearch",30 "opensearchpy",31 "Elasticsearch",32 "OpenSearch",33 ),34 MovedAttribute("Transport", "elasticsearch", "opensearchpy"),35 MovedAttribute("ConnectionPool", "elasticsearch", "opensearchpy"),36 MovedAttribute("ConnectionSelector", "elasticsearch", "opensearchpy"),37 MovedAttribute("RoundRobinSelector", "elasticsearch", "opensearchpy"),38 MovedAttribute("JSONSerializer", "elasticsearch", "opensearchpy"),39 MovedAttribute("Connection", "elasticsearch", "opensearchpy"),40 MovedAttribute("RequestsHttpConnection", "elasticsearch", "opensearchpy"),41 MovedAttribute("Urllib3HttpConnection", "elasticsearch", "opensearchpy"),42 MovedAttribute("ImproperlyConfigured", "elasticsearch", "opensearchpy"),43 MovedAttribute(44 "AnySearchException",45 "elasticsearch",46 "opensearchpy",47 "ElasticsearchException",48 "OpenSearchException",49 ),50 MovedAttribute("SerializationError", "elasticsearch", "opensearchpy"),51 MovedAttribute("TransportError", "elasticsearch", "opensearchpy"),52 MovedAttribute("NotFoundError", "elasticsearch", "opensearchpy"),53 MovedAttribute("ConflictError", "elasticsearch", "opensearchpy"),54 MovedAttribute("RequestError", "elasticsearch", "opensearchpy"),55 MovedAttribute("ConnectionError", "elasticsearch", "opensearchpy"),56 MovedAttribute("SSLError", "elasticsearch", "opensearchpy"),57 MovedAttribute("ConnectionTimeout", "elasticsearch", "opensearchpy"),58 MovedAttribute("AuthenticationException", "elasticsearch", "opensearchpy"),59 MovedAttribute("AuthorizationException", "elasticsearch", "opensearchpy"),60 # MovedAttribute(61 # "AnySearchDeprecationWarning",62 # "elasticsearch",63 # "opensearchpy",64 # "ElasticsearchDeprecationWarning",65 # "OpenSearchDeprecationWarning",66 # ),67]68SEARCH_DSL_MOVED_MODULES = [69 MovedModule("aggs", "elasticsearch_dsl", "opensearch_dsl"),70 MovedModule("analysis", "elasticsearch_dsl", "opensearch_dsl"),71 MovedModule("connections", "elasticsearch_dsl", "opensearch_dsl"),72 MovedModule("document", "elasticsearch_dsl", "opensearch_dsl"),73 MovedModule("exceptions", "elasticsearch_dsl", "opensearch_dsl"),74 MovedModule("faceted_search", "elasticsearch_dsl", "opensearch_dsl"),75 MovedModule("field", "elasticsearch_dsl", "opensearch_dsl"),76 MovedModule("function", "elasticsearch_dsl", "opensearch_dsl"),77 MovedModule("index", "elasticsearch_dsl", "opensearch_dsl"),78 MovedModule("mapping", "elasticsearch_dsl", "opensearch_dsl"),79 MovedModule("query", "elasticsearch_dsl", "opensearch_dsl"),80 MovedModule("search", "elasticsearch_dsl", "opensearch_dsl"),81 MovedModule("serializer", "elasticsearch_dsl", "opensearch_dsl"),82 MovedModule("update_by_query", "elasticsearch_dsl", "opensearch_dsl"),83 MovedModule("utils", "elasticsearch_dsl", "opensearch_dsl"),84 MovedModule("wrappers", "elasticsearch_dsl", "opensearch_dsl"),85]86SEARCH_DSL_MOVED_ATTRIBUTES_TYPE_SHORTCUTS = [87 # .analysis88 MovedAttribute("analyzer", "elasticsearch_dsl", "opensearch_dsl"),89 MovedAttribute("char_filter", "elasticsearch_dsl", "opensearch_dsl"),90 MovedAttribute("normalizer", "elasticsearch_dsl", "opensearch_dsl"),91 MovedAttribute("token_filter", "elasticsearch_dsl", "opensearch_dsl"),92 MovedAttribute("tokenizer", "elasticsearch_dsl", "opensearch_dsl"),93]94SEARCH_DSL_MOVED_ATTRIBUTES = [95 # .96 # MovedAttribute("__version__", "elasticsearch_dsl", "opensearch_dsl"),97 # MovedAttribute("connections", "elasticsearch_dsl", "opensearch_dsl"), # Probably remove98 # .aggs99 MovedAttribute("A", "elasticsearch_dsl", "opensearch_dsl"),100 # .analysis101 # MovedAttribute("analyzer", "elasticsearch_dsl", "opensearch_dsl"),102 # MovedAttribute("char_filter", "elasticsearch_dsl", "opensearch_dsl"),103 # MovedAttribute("normalizer", "elasticsearch_dsl", "opensearch_dsl"),104 # MovedAttribute("token_filter", "elasticsearch_dsl", "opensearch_dsl"),105 # MovedAttribute("tokenizer", "elasticsearch_dsl", "opensearch_dsl"),106 # .document107 MovedAttribute("Document", "elasticsearch_dsl", "opensearch_dsl"),108 MovedAttribute("InnerDoc", "elasticsearch_dsl", "opensearch_dsl"),109 MovedAttribute("MetaField", "elasticsearch_dsl", "opensearch_dsl"),110 # .exceptions111 # MovedAttribute(112 # "ElasticsearchDslException", "elasticsearch_dsl", "opensearch_dsl"113 # ),114 MovedAttribute("IllegalOperation", "elasticsearch_dsl", "opensearch_dsl"),115 MovedAttribute("UnknownDslObject", "elasticsearch_dsl", "opensearch_dsl"),116 MovedAttribute(117 "ValidationException", "elasticsearch_dsl", "opensearch_dsl"118 ),119 # .faceted_search120 MovedAttribute("DateHistogramFacet", "elasticsearch_dsl", "opensearch_dsl"),121 MovedAttribute("Facet", "elasticsearch_dsl", "opensearch_dsl"),122 MovedAttribute("FacetedResponse", "elasticsearch_dsl", "opensearch_dsl"),123 MovedAttribute("FacetedSearch", "elasticsearch_dsl", "opensearch_dsl"),124 MovedAttribute("HistogramFacet", "elasticsearch_dsl", "opensearch_dsl"),125 MovedAttribute("NestedFacet", "elasticsearch_dsl", "opensearch_dsl"),126 MovedAttribute("RangeFacet", "elasticsearch_dsl", "opensearch_dsl"),127 MovedAttribute("TermsFacet", "elasticsearch_dsl", "opensearch_dsl"),128 # .field129 MovedAttribute("Binary", "elasticsearch_dsl", "opensearch_dsl"),130 MovedAttribute("Boolean", "elasticsearch_dsl", "opensearch_dsl"),131 MovedAttribute("Byte", "elasticsearch_dsl", "opensearch_dsl"),132 MovedAttribute("Completion", "elasticsearch_dsl", "opensearch_dsl"),133 MovedAttribute("CustomField", "elasticsearch_dsl", "opensearch_dsl"),134 MovedAttribute("Date", "elasticsearch_dsl", "opensearch_dsl"),135 MovedAttribute("DateRange", "elasticsearch_dsl", "opensearch_dsl"),136 MovedAttribute("DenseVector", "elasticsearch_dsl", "opensearch_dsl"),137 MovedAttribute("Double", "elasticsearch_dsl", "opensearch_dsl"),138 MovedAttribute("DoubleRange", "elasticsearch_dsl", "opensearch_dsl"),139 MovedAttribute("Field", "elasticsearch_dsl", "opensearch_dsl"),140 MovedAttribute("Float", "elasticsearch_dsl", "opensearch_dsl"),141 MovedAttribute("FloatRange", "elasticsearch_dsl", "opensearch_dsl"),142 MovedAttribute("GeoPoint", "elasticsearch_dsl", "opensearch_dsl"),143 MovedAttribute("GeoShape", "elasticsearch_dsl", "opensearch_dsl"),144 MovedAttribute("HalfFloat", "elasticsearch_dsl", "opensearch_dsl"),145 MovedAttribute("Integer", "elasticsearch_dsl", "opensearch_dsl"),146 MovedAttribute("IntegerRange", "elasticsearch_dsl", "opensearch_dsl"),147 MovedAttribute("Ip", "elasticsearch_dsl", "opensearch_dsl"),148 MovedAttribute("IpRange", "elasticsearch_dsl", "opensearch_dsl"),149 MovedAttribute("Join", "elasticsearch_dsl", "opensearch_dsl"),150 MovedAttribute("Keyword", "elasticsearch_dsl", "opensearch_dsl"),151 MovedAttribute("Long", "elasticsearch_dsl", "opensearch_dsl"),152 MovedAttribute("LongRange", "elasticsearch_dsl", "opensearch_dsl"),153 MovedAttribute("Murmur3", "elasticsearch_dsl", "opensearch_dsl"),154 MovedAttribute("Nested", "elasticsearch_dsl", "opensearch_dsl"),155 MovedAttribute("Object", "elasticsearch_dsl", "opensearch_dsl"),156 MovedAttribute("Percolator", "elasticsearch_dsl", "opensearch_dsl"),157 MovedAttribute("RangeField", "elasticsearch_dsl", "opensearch_dsl"),158 MovedAttribute("RankFeature", "elasticsearch_dsl", "opensearch_dsl"),159 MovedAttribute("RankFeatures", "elasticsearch_dsl", "opensearch_dsl"),160 MovedAttribute("ScaledFloat", "elasticsearch_dsl", "opensearch_dsl"),161 MovedAttribute("SearchAsYouType", "elasticsearch_dsl", "opensearch_dsl"),162 MovedAttribute("Short", "elasticsearch_dsl", "opensearch_dsl"),163 MovedAttribute("SparseVector", "elasticsearch_dsl", "opensearch_dsl"),164 MovedAttribute("SparseVector", "elasticsearch_dsl", "opensearch_dsl"),165 MovedAttribute("construct_field", "elasticsearch_dsl", "opensearch_dsl"),166 # .function167 MovedAttribute("SF", "elasticsearch_dsl", "opensearch_dsl"),168 # .index169 MovedAttribute("Index", "elasticsearch_dsl", "opensearch_dsl"),170 MovedAttribute("IndexTemplate", "elasticsearch_dsl", "opensearch_dsl"),171 # .mapping172 MovedAttribute("Mapping", "elasticsearch_dsl", "opensearch_dsl"),173 # .query174 MovedAttribute("Q", "elasticsearch_dsl.query", "opensearch_dsl.query"),175 # .search176 MovedAttribute("MultiSearch", "elasticsearch_dsl", "opensearch_dsl"),177 MovedAttribute("Search", "elasticsearch_dsl", "opensearch_dsl"),178 # .update_by_query179 MovedAttribute("UpdateByQuery", "elasticsearch_dsl", "opensearch_dsl"),180 # .utils181 MovedAttribute(182 "AttrDict", "elasticsearch_dsl.utils", "opensearch_dsl.utils"183 ),184 MovedAttribute(185 "AttrList", "elasticsearch_dsl.utils", "opensearch_dsl.utils"186 ),187 MovedAttribute("DslBase", "elasticsearch_dsl", "opensearch_dsl"),188 # .wrappers189 MovedAttribute("Range", "elasticsearch_dsl", "opensearch_dsl"),190]191# DJANGO_SEARCH_DSL_MOVED_MODULES = [192# # **********************************************193# # **************** Moved modules ***************194# # **********************************************195# # django_elasticsearch_dsl/django_opensearch_dsl196# MovedModule("apps", "django_elasticsearch_dsl", "django_opensearch_dsl"),197# MovedModule(198# "documents", "django_elasticsearch_dsl", "django_opensearch_dsl"199# ),200# MovedModule(201# "exceptions", "django_elasticsearch_dsl", "django_opensearch_dsl"202# ),203# MovedModule("fields", "django_elasticsearch_dsl", "django_opensearch_dsl"),204# # MovedModule("indices", "django_elasticsearch_dsl", "django_opensearch_dsl"),205# # MovedModule("models", "django_elasticsearch_dsl", "django_opensearch_dsl"),206# MovedModule(207# "registries", "django_elasticsearch_dsl", "django_opensearch_dsl"208# ),209# MovedModule("search", "django_elasticsearch_dsl", "django_opensearch_dsl"),210# MovedModule("signals", "django_elasticsearch_dsl", "django_opensearch_dsl"),211# ]212#213# DJANGO_SEARCH_DSL_MOVED_ATTRIBUTES = [214# # **********************************************215# # *************** Moved attributes *************216# # **********************************************217# MovedAttribute(218# "Document", "django_elasticsearch_dsl", "django_opensearch_dsl"219# ),220# # MovedAttribute("Index", "django_elasticsearch_dsl", "django_opensearch_dsl"),221# ]222class AnySearchBaseTestCase(unittest.TestCase):223 def _assert_expected_module_path(self, attr, package, name):224 """Check that the expected path is found."""225 self.assertEqual(attr.__name__, f"{package}.{name}")226 def _assert_expected_attribute_path(self, attr, package, name):227 """Check that the expected path is found."""228 self.assertEqual(attr.__name__, name)229 self.assertTrue(attr.__module__.startswith(package))230 def _assert_expected_attribute_path_type_shortcut(231 self, attr, package, name232 ):233 """Check that the expected path is found."""234 self.assertTrue(attr.__module__.startswith(package))235 def _check_expected_module_path(self, attr, package, name):236 """Check that the expected path is found."""237 if attr.__name__ != f"{package}.{name}":238 LOGGER.exception(f"Fail: {attr.__name__} != {package}.{name}")239 else:240 LOGGER.exception(f"Pass: {attr.__name__} == {package}.{name}")241 def _check_expected_attribute_path(self, attr, package, name):242 """Check that the expected path is found."""243 if attr.__name__ != name:244 LOGGER.exception(f"Fail: {attr.__name__} != {name}")245 else:246 LOGGER.exception(f"Pass: {attr.__name__} == {name}")247 def _test_module_moved_attributes(248 self, module, name, package, orig_name=None249 ):250 with self.subTest(f"name: {name}, package: {package}"):251 if not orig_name:252 orig_name = name253 module_path = f"anysearch.{module}"254 _module = import_module(module_path)255 attr = getattr(_module, name)256 self._assert_expected_attribute_path(attr, package, orig_name)257 # self._check_expected_attribute_path(attr, package, name)258 def _test_module_moved_attributes_type_shortcuts(259 self, module, name, package260 ):261 with self.subTest(f"name: {name}, package: {package}"):262 module_path = f"anysearch.{module}"263 _module = import_module(module_path)264 attr = getattr(_module, name)265 self._assert_expected_attribute_path_type_shortcut(266 attr, package, name267 )268 # self._check_expected_attribute_path(attr, package, name)269 def _test_module_moved_modules(self, module, name, package):270 with self.subTest(f"name: {name}, package: {package}"):271 module_path = f"anysearch.{module}"272 _module = import_module(module_path)273 attr = getattr(_module, name)274 self._assert_expected_module_path(attr, package, name)275 # self._check_expected_module_path(attr, package, name)276class SearchTestCase(AnySearchBaseTestCase):277 """Test search."""278 def _test_moved_attributes(self, name, package, orig_name=None):279 self._test_module_moved_attributes("search", name, package, orig_name)280 # **************************************************281 # ******************** opensearch ******************282 # **************************************************283 @mock.patch.dict("os.environ", {"ANYSEARCH_PREFERRED_BACKEND": OPENSEARCH})284 @unittest.skipIf(285 detect_search_backend() != OPENSEARCH,286 "Skipped, because opensearch is not installed.",287 )288 def test_opensearch_moved_attributes(self):289 """Test OpenSearch."""290 for name, _, package, *options in SEARCH_MOVED_ATTRIBUTES:291 orig_name = options[1] if options else name292 self._test_moved_attributes(name, package, orig_name)293 # **************************************************294 # ******************* elasticsearch ****************295 # **************************************************296 @mock.patch.dict(297 "os.environ", {"ANYSEARCH_PREFERRED_BACKEND": ELASTICSEARCH}298 )299 @unittest.skipIf(300 detect_search_backend() != ELASTICSEARCH,301 "Skipped, because elasticsearch is not installed.",302 )303 def test_elasticsearch_moved_attributes(self):304 """Test Elasticsearch."""305 for name, package, _, *options in SEARCH_MOVED_ATTRIBUTES:306 orig_name = options[0] if options else name307 self._test_moved_attributes(name, package, orig_name)308class SearchDSLTestCase(AnySearchBaseTestCase):309 """Test search DSL."""310 def _test_moved_attributes(self, name, package):311 self._test_module_moved_attributes("search_dsl", name, package)312 def _test_moved_attributes_type_shortcuts(self, name, package):313 self._test_module_moved_attributes_type_shortcuts(314 "search_dsl", name, package315 )316 def _test_moved_modules(self, name, package):317 self._test_module_moved_modules("search_dsl", name, package)318 # **************************************************319 # ****************** opensearch-dsl ****************320 # **************************************************321 @mock.patch.dict("os.environ", {"ANYSEARCH_PREFERRED_BACKEND": OPENSEARCH})322 @unittest.skipIf(323 detect_search_backend() != OPENSEARCH,324 "Skipped, because opensearch-dsl is not installed.",325 )326 def test_opensearch_dsl_moved_attributes(self):327 """Test OpenSearch-DSL."""328 for name, _, package in SEARCH_DSL_MOVED_ATTRIBUTES:329 self._test_moved_attributes(name, package)330 @mock.patch.dict("os.environ", {"ANYSEARCH_PREFERRED_BACKEND": OPENSEARCH})331 @unittest.skipIf(332 detect_search_backend() != OPENSEARCH,333 "Skipped, because opensearch-dsl is not installed.",334 )335 def test_opensearch_dsl_moved_attributes_type_shortcuts(self):336 """Test OpenSearch-DSL."""337 for name, _, package in SEARCH_DSL_MOVED_ATTRIBUTES_TYPE_SHORTCUTS:338 self._test_moved_attributes_type_shortcuts(name, package)339 @mock.patch.dict("os.environ", {"ANYSEARCH_PREFERRED_BACKEND": OPENSEARCH})340 @unittest.skipIf(341 detect_search_backend() != OPENSEARCH,342 "Skipped, because opensearch-dsl is not installed.",343 )344 def test_opensearch_dsl_moved_modules(self):345 """Test OpenSearch-DSL."""346 for name, _, package in SEARCH_DSL_MOVED_MODULES:347 self._test_moved_modules(name, package)348 # **************************************************349 # **************** elasticsearch-dsl ***************350 # **************************************************351 @mock.patch.dict(352 "os.environ", {"ANYSEARCH_PREFERRED_BACKEND": ELASTICSEARCH}353 )354 @unittest.skipIf(355 detect_search_backend() != ELASTICSEARCH,356 "Skipped, because elasticsearch-dsl is not installed.",357 )358 def test_elasticsearch_dsl_moved_attributes(self):359 """Test Elasticsearch-DSL."""360 for name, package, _ in SEARCH_DSL_MOVED_ATTRIBUTES:361 self._test_moved_attributes(name, package)362 @mock.patch.dict(363 "os.environ", {"ANYSEARCH_PREFERRED_BACKEND": ELASTICSEARCH}364 )365 @unittest.skipIf(366 detect_search_backend() != ELASTICSEARCH,367 "Skipped, because elasticsearch-dsl is not installed.",368 )369 def test_elasticsearch_dsl_moved_attributes_type_shortcuts(self):370 """Test OpenSearch-DSL."""371 for name, package, _ in SEARCH_DSL_MOVED_ATTRIBUTES_TYPE_SHORTCUTS:372 self._test_moved_attributes_type_shortcuts(name, package)373 @mock.patch.dict(374 "os.environ", {"ANYSEARCH_PREFERRED_BACKEND": ELASTICSEARCH}375 )376 @unittest.skipIf(377 detect_search_backend() != ELASTICSEARCH,378 "Skipped, because elasticsearch-dsl is not installed.",379 )380 def test_elasticsearch_dsl_moved_modules(self):381 """Test Elasticsearch-DSL."""382 for name, package, _ in SEARCH_DSL_MOVED_MODULES:383 self._test_moved_modules(name, package)384# class DjangoSearchDSLTestCase(AnySearchBaseTestCase):385# """Test Django search DSL."""386#387# def _test_moved_attributes(self, name, package):388# self._test_module_moved_attributes("django_search_dsl", name, package)389#390# def _test_moved_attributes_type_shortcuts(self, name, package):391# self._test_module_moved_attributes_type_shortcuts(392# "django_search_dsl", name, package393# )394#395# def _test_moved_modules(self, name, package):396# self._test_module_moved_modules("django_search_dsl", name, package)397#398# # **************************************************399# # **************** django-opensearch-dsl ***********400# # **************************************************401#402# @mock.patch.dict("os.environ", {"ANYSEARCH_PREFERRED_BACKEND": OPENSEARCH})403# @unittest.skipIf(404# detect_search_backend() != OPENSEARCH,405# "Skipped, because opensearch-dsl is not installed.",406# )407# def test_django_opensearch_dsl_moved_attributes(self):408# """Test Django-OpenSearch-DSL."""409# for name, _, package in DJANGO_SEARCH_DSL_MOVED_ATTRIBUTES:410# self._test_moved_attributes(name, package)411#412# @mock.patch.dict("os.environ", {"ANYSEARCH_PREFERRED_BACKEND": OPENSEARCH})413# @unittest.skipIf(414# detect_search_backend() != OPENSEARCH,415# "Skipped, because opensearch-dsl is not installed.",416# )417# def test_django_opensearch_dsl_moved_modules(self):418# """Test Django-OpenSearch-DSL."""419# for name, _, package in DJANGO_SEARCH_DSL_MOVED_MODULES:420# self._test_moved_modules(name, package)421#422# # **************************************************423# # ************** django-elasticsearch-dsl **********424# # **************************************************425#426# @mock.patch.dict(427# "os.environ", {"ANYSEARCH_PREFERRED_BACKEND": ELASTICSEARCH}428# )429# @unittest.skipIf(430# detect_search_backend() != ELASTICSEARCH,431# "Skipped, because opensearch-dsl is not installed.",432# )433# def test_django_elasticsearch_dsl_moved_attributes(self):434# """Test Django-OpenSearch-DSL."""435# name = "Document"436# package = "django_elasticsearch_dsl"437# module = "django_search_dsl"438# with self.subTest(f"name: {name}, package: {package}"):439# module_path = f"anysearch.{module}"440# _module = import_module(module_path)441# attr = getattr(_module, name)442# self._assert_expected_attribute_path(attr, package, "DocType")443#444# @mock.patch.dict(445# "os.environ", {"ANYSEARCH_PREFERRED_BACKEND": ELASTICSEARCH}446# )447# @unittest.skipIf(448# detect_search_backend() != ELASTICSEARCH,449# "Skipped, because opensearch-dsl is not installed.",450# )451# def test_django_elasticsearch_dsl_moved_modules(self):452# """Test Django-OpenSearch-DSL."""453# for name, package, _ in DJANGO_SEARCH_DSL_MOVED_MODULES:454# self._test_moved_modules(name, package)455class AnySearchTestCase(unittest.TestCase):456 """Test AnySearch helpers."""457 def test_detect_search_backend(self):458 with mock.patch.dict(459 "os.environ", {"ANYSEARCH_PREFERRED_BACKEND": ELASTICSEARCH}460 ):461 self.assertEqual(detect_search_backend(), ELASTICSEARCH)462 with mock.patch.dict(463 "os.environ", {"ANYSEARCH_PREFERRED_BACKEND": OPENSEARCH}464 ):465 self.assertEqual(detect_search_backend(), OPENSEARCH)466 def test_get_installed_packages(self):467 """Test get_installed_packages."""468 installed_packages = get_installed_packages()469 self.assertIn("pytest", installed_packages)470 def test_check_if_package_is_installed(self):471 """Test get_installed_packages."""...
ops.py
Source:ops.py
1#!/usr/bin/env python32import json3import random4import string5import aws_cdk as cdk6from aws_cdk import (7 Stack,8 aws_ec2,9 aws_s3 as s3,10 aws_opensearchservice,11 aws_secretsmanager12)13from constructs import Construct14random.seed(47)15class OpenSearchStack(Stack):16 def __init__(self, scope: Construct, construct_id: str, vpc, **kwargs) -> None:17 super().__init__(scope, construct_id, **kwargs)18 OPENSEARCH_DOMAIN_NAME = cdk.CfnParameter(self, 'OpenSearchDomainName',19 type='String',20 description='Amazon OpenSearch Service domain name',21 default='opensearch-{}'.format(''.join(random.sample((string.ascii_letters), k=5))),22 allowed_pattern='[a-z]+[A-Za-z0-9\-]+'23 )24 EC2_KEY_PAIR_NAME = cdk.CfnParameter(self, 'EC2KeyPairName',25 type='String',26 description='Amazon EC2 Instance KeyPair name'27 )28 #XXX: https://docs.aws.amazon.com/cdk/api/latest/python/aws_cdk.aws_ec2/InstanceClass.html29 #XXX: https://docs.aws.amazon.com/cdk/api/latest/python/aws_cdk.aws_ec2/InstanceSize.html#aws_cdk.aws_ec2.InstanceSize30 ec2_instance_type = aws_ec2.InstanceType.of(aws_ec2.InstanceClass.BURSTABLE3, aws_ec2.InstanceSize.MEDIUM)31 sg_bastion_host = aws_ec2.SecurityGroup(self, "BastionHostSG",32 vpc=vpc,33 allow_all_outbound=True,34 description='security group for an bastion host',35 security_group_name='bastion-host-sg'36 )37 cdk.Tags.of(sg_bastion_host).add('Name', 'bastion-host-sg')38 #TODO: SHOULD restrict IP range allowed to ssh acces39 sg_bastion_host.add_ingress_rule(peer=aws_ec2.Peer.ipv4("0.0.0.0/0"), connection=aws_ec2.Port.tcp(22), description='SSH access')40 bastion_host = aws_ec2.Instance(self, "BastionHost",41 vpc=vpc,42 instance_type=ec2_instance_type,43 machine_image=aws_ec2.MachineImage.latest_amazon_linux(),44 vpc_subnets=aws_ec2.SubnetSelection(subnet_type=aws_ec2.SubnetType.PUBLIC),45 security_group=sg_bastion_host,46 key_name=EC2_KEY_PAIR_NAME.value_as_string47 )48 sg_use_opensearch = aws_ec2.SecurityGroup(self, "OpenSearchClientSG",49 vpc=vpc,50 allow_all_outbound=True,51 description='security group for an opensearch client',52 security_group_name='use-opensearch-cluster-sg'53 )54 cdk.Tags.of(sg_use_opensearch).add('Name', 'use-opensearch-cluster-sg')55 self.ops_client_sg_id = sg_use_opensearch.security_group_id56 sg_opensearch_cluster = aws_ec2.SecurityGroup(self, "OpenSearchSG",57 vpc=vpc,58 allow_all_outbound=True,59 description='security group for an opensearch cluster',60 security_group_name='opensearch-cluster-sg'61 )62 cdk.Tags.of(sg_opensearch_cluster).add('Name', 'opensearch-cluster-sg')63 sg_opensearch_cluster.add_ingress_rule(peer=sg_opensearch_cluster, connection=aws_ec2.Port.all_tcp(), description='opensearch-cluster-sg')64 sg_opensearch_cluster.add_ingress_rule(peer=sg_use_opensearch, connection=aws_ec2.Port.tcp(443), description='use-opensearch-cluster-sg')65 sg_opensearch_cluster.add_ingress_rule(peer=sg_use_opensearch, connection=aws_ec2.Port.tcp_range(9200, 9300), description='use-opensearch-cluster-sg')66 sg_opensearch_cluster.add_ingress_rule(peer=sg_bastion_host, connection=aws_ec2.Port.tcp(443), description='bastion-host-sg')67 sg_opensearch_cluster.add_ingress_rule(peer=sg_bastion_host, connection=aws_ec2.Port.tcp_range(9200, 9300), description='bastion-host-sg')68 master_user_secret = aws_secretsmanager.Secret(self, "OpenSearchMasterUserSecret",69 generate_secret_string=aws_secretsmanager.SecretStringGenerator(70 secret_string_template=json.dumps({"username": "admin"}),71 generate_string_key="password",72 # Master password must be at least 8 characters long and contain at least one uppercase letter,73 # one lowercase letter, one number, and one special character.74 password_length=875 )76 )77 #XXX: aws cdk elastsearch example - https://github.com/aws/aws-cdk/issues/287378 # You should camelCase the property names instead of PascalCase79 opensearch_domain = aws_opensearchservice.Domain(self, "OpenSearch",80 domain_name=OPENSEARCH_DOMAIN_NAME.value_as_string,81 #XXX: Supported versions of OpenSearch and Elasticsearch82 # https://docs.aws.amazon.com/opensearch-service/latest/developerguide/what-is.html#choosing-version83 version=aws_opensearchservice.EngineVersion.OPENSEARCH_1_3,84 #XXX: You cannot use graviton instances with non-graviton instances.85 # Use graviton instances as data nodes or use non-graviton instances as master nodes.86 capacity={87 "master_nodes": 3,88 "master_node_instance_type": "r6g.large.search",89 "data_nodes": 3,90 "data_node_instance_type": "r6g.large.search"91 },92 ebs={93 "volume_size": 10,94 "volume_type": aws_ec2.EbsDeviceVolumeType.GP395 },96 #XXX: az_count must be equal to vpc subnets count.97 zone_awareness={98 "availability_zone_count": 399 },100 logging={101 "slow_search_log_enabled": True,102 "app_log_enabled": True,103 "slow_index_log_enabled": True104 },105 fine_grained_access_control=aws_opensearchservice.AdvancedSecurityOptions(106 master_user_name=master_user_secret.secret_value_from_json("username").to_string(),107 master_user_password=master_user_secret.secret_value_from_json("password")108 ),109 # Enforce HTTPS is required when fine-grained access control is enabled.110 enforce_https=True,111 # Node-to-node encryption is required when fine-grained access control is enabled112 node_to_node_encryption=True,113 # Encryption-at-rest is required when fine-grained access control is enabled.114 encryption_at_rest={115 "enabled": True116 },117 use_unsigned_basic_auth=True,118 security_groups=[sg_opensearch_cluster],119 #XXX: For domains running OpenSearch or Elasticsearch 5.3 and later, OpenSearch Service takes hourly automated snapshots120 # Only applies for Elasticsearch versions below 5.3121 # automated_snapshot_start_hour=17, # 2 AM (GTM+9)122 vpc=vpc,123 #XXX: az_count must be equal to vpc subnets count.124 vpc_subnets=[aws_ec2.SubnetSelection(one_per_az=True, subnet_type=aws_ec2.SubnetType.PRIVATE_WITH_EGRESS)],125 removal_policy=cdk.RemovalPolicy.DESTROY # default: cdk.RemovalPolicy.RETAIN126 )127 cdk.Tags.of(opensearch_domain).add('Name', f'{OPENSEARCH_DOMAIN_NAME.value_as_string}')128 self.ops_domain_arn = opensearch_domain.domain_arn129 cdk.CfnOutput(self, 'BastionHostId', value=bastion_host.instance_id, export_name='BastionHostId')130 cdk.CfnOutput(self, 'OpenSearchDomainEndpoint', value=opensearch_domain.domain_endpoint, export_name='OpenSearchDomainEndpoint')131 cdk.CfnOutput(self, 'OpenSearchDashboardsURL', value=f"{opensearch_domain.domain_endpoint}/_dashboards/", export_name='OpenSearchDashboardsURL')...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!