Pytest -m connection failed (I've try to find the solution on other topics but No solution found)

(MacOS with Anaconda → activate mflix env → set “.ini” form dotini_unix because I use Mac)

My “.ini” file as…

Ticket: Connection

Rename this file to .ini after filling in your MFLIX_DB_URI and your SECRET_KEY

Do not surround the URI with quotes

[PROD]
SECRET_KEY = super_secret_key_you_should_change
MFLIX_DB_URI = mongodb+srv://m220student:m220password@mflix.9cmxdc7.mongodb.net
MFLIX_NS = sample_mflix

[TEST]
SECRET_KEY = super_secret_testing_key
MFLIX_DB_URI = your_testing_db_uri (can be the same as Atlas, or a local MongoDB database)
MFLIX_NS = sample_mflix
And When I run “pytest -m connection” command → It shows error as…

(mflix) dboth@Samittis-MBP mflix-python % pytest -m connection
=========================================================================================== test session starts ============================================================================================
platform darwin – Python 3.9.13, pytest-3.3.0, py-1.8.0, pluggy-0.6.0
rootdir: /Users/dboth/Documents/MongoDB_prac/Py/mflix-python, inifile: pytest.ini
plugins: flask-0.11.0
collected 43 items

tests/test_db_connection.py FFFF [100%]

================================================================================================= FAILURES =================================================================================================
_____________________________________________________________________________________________ test_atlas_setup _____________________________________________________________________________________________

client = <FlaskClient <Flask ‘mflix.factory’>>
config = <Config {‘DEBUG’: False, ‘TESTING’: False, ‘PROPAGATE_EXCEPTIONS’: True, ‘PRESERVE_CONTEXT_ON_EXCEPTION’: None, ‘SECRE…IX_NS’: ‘sample_mflix’, ‘MFLIX_DB_URI’: ‘your_testing_db_uri (can be the same as Atlas, or a local MongoDB database)’}>

@pytest.mark.connection
@pytest.mark.usefixtures('config')
def test_atlas_setup(client, config):
  result = get_coll_names(config)

tests/test_db_connection.py:24:


tests/test_db_connection.py:18: in get_coll_names
return db.list_collection_names()
…/…/…/…/opt/anaconda3/envs/mflix/lib/python3.9/site-packages/pymongo/database.py:953: in list_collection_names
return [result[“name”] for result in self.list_collections(session=session, **kwargs)]
…/…/…/…/opt/anaconda3/envs/mflix/lib/python3.9/site-packages/pymongo/database.py:905: in list_collections
return self.__client._retryable_read(_cmd, read_pref, session)
…/…/…/…/opt/anaconda3/envs/mflix/lib/python3.9/site-packages/pymongo/_csot.py:105: in csot_wrapper
return func(self, *args, **kwargs)
…/…/…/…/opt/anaconda3/envs/mflix/lib/python3.9/site-packages/pymongo/mongo_client.py:1413: in _retryable_read
server = self._select_server(read_pref, session, address=address)
…/…/…/…/opt/anaconda3/envs/mflix/lib/python3.9/site-packages/pymongo/mongo_client.py:1229: in _select_server
server = topology.select_server(server_selector)
…/…/…/…/opt/anaconda3/envs/mflix/lib/python3.9/site-packages/pymongo/topology.py:272: in select_server
server = self._select_server(selector, server_selection_timeout, address)
…/…/…/…/opt/anaconda3/envs/mflix/lib/python3.9/site-packages/pymongo/topology.py:261: in _select_server
servers = self.select_servers(selector, server_selection_timeout, address)
…/…/…/…/opt/anaconda3/envs/mflix/lib/python3.9/site-packages/pymongo/topology.py:223: in select_servers
server_descriptions = self._select_servers_loop(selector, server_timeout, address)


self = <Topology <TopologyDescription id: 6311f7b1487ac5c1e1814905, topology_type: Unknown, servers: [<ServerDescription (’ o…nect(‘your_testing_db_uri (can be the same as atlas:27017: [Errno 8] nodename nor servname provided, or not known’)>]>>
selector = Primary(), timeout = 30, address = None

def _select_servers_loop(self, selector, timeout, address):
    """select_servers() guts. Hold the lock when calling this."""
    now = time.monotonic()
    end_time = now + timeout
    server_descriptions = self._description.apply_selector(
        selector, address, custom_selector=self._settings.server_selector
    )

    while not server_descriptions:
        # No suitable servers.
        if timeout == 0 or now > end_time:
          raise ServerSelectionTimeoutError(
                "%s, Timeout: %ss, Topology Description: %r"
                % (self._error_message(selector), timeout, self.description)
            )

E pymongo.errors.ServerSelectionTimeoutError: or a local mongodb database):27017: [Errno 8] nodename nor servname provided, or not known,your_testing_db_uri (can be the same as atlas:27017: [Errno 8] nodename nor servname provided, or not known, Timeout: 30s, Topology Description: <TopologyDescription id: 6311f7b1487ac5c1e1814905, topology_type: Unknown, servers: [<ServerDescription (’ or a local mongodb database)’, 27017) server_type: Unknown, rtt: None, error=AutoReconnect(’ or a local mongodb database):27017: [Errno 8] nodename nor servname provided, or not known’)>, <ServerDescription (‘your_testing_db_uri (can be the same as atlas’, 27017) server_type: Unknown, rtt: None, error=AutoReconnect(‘your_testing_db_uri (can be the same as atlas:27017: [Errno 8] nodename nor servname provided, or not known’)>]>

…/…/…/…/opt/anaconda3/envs/mflix/lib/python3.9/site-packages/pymongo/topology.py:238: ServerSelectionTimeoutError
____________________________________________________________________________________________ test_basic_movies _____________________________________________________________________________________________

client = <FlaskClient <Flask ‘mflix.factory’>>

@pytest.mark.connection
def test_basic_movies(client):
  (actual, num_found) = get_movies({}, 0, 20)

tests/test_db_connection.py:31:


mflix/db.py:224: in get_movies
total_num_movies = db.movies.count_documents(query)
…/…/…/…/opt/anaconda3/envs/mflix/lib/python3.9/site-packages/pymongo/collection.py:1835: in count_documents
return self._retryable_non_cursor_read(_cmd, session)
…/…/…/…/opt/anaconda3/envs/mflix/lib/python3.9/site-packages/pymongo/collection.py:1840: in _retryable_non_cursor_read
with client._tmp_session(session) as s:
…/…/…/…/opt/anaconda3/envs/mflix/lib/python3.9/contextlib.py:119: in enter
return next(self.gen)
…/…/…/…/opt/anaconda3/envs/mflix/lib/python3.9/site-packages/pymongo/mongo_client.py:1729: in _tmp_session
s = self._ensure_session(session)
…/…/…/…/opt/anaconda3/envs/mflix/lib/python3.9/site-packages/pymongo/mongo_client.py:1712: in _ensure_session
return self.__start_session(True, causal_consistency=False)
…/…/…/…/opt/anaconda3/envs/mflix/lib/python3.9/site-packages/pymongo/mongo_client.py:1657: in __start_session
self._topology._check_implicit_session_support()
…/…/…/…/opt/anaconda3/envs/mflix/lib/python3.9/site-packages/pymongo/topology.py:538: in _check_implicit_session_support
self._check_session_support()
…/…/…/…/opt/anaconda3/envs/mflix/lib/python3.9/site-packages/pymongo/topology.py:554: in _check_session_support
self._select_servers_loop(


self = <Topology <TopologyDescription id: 6311f7cf487ac5c1e1814906, topology_type: Unknown, servers: [<ServerDescription (’ o…nect(‘your_testing_db_uri (can be the same as atlas:27017: [Errno 8] nodename nor servname provided, or not known’)>]>>
selector = <function readable_server_selector at 0x7f77d4720ca0>, timeout = 30, address = None

def _select_servers_loop(self, selector, timeout, address):
    """select_servers() guts. Hold the lock when calling this."""
    now = time.monotonic()
    end_time = now + timeout
    server_descriptions = self._description.apply_selector(
        selector, address, custom_selector=self._settings.server_selector
    )

    while not server_descriptions:
        # No suitable servers.
        if timeout == 0 or now > end_time:
          raise ServerSelectionTimeoutError(
                "%s, Timeout: %ss, Topology Description: %r"
                % (self._error_message(selector), timeout, self.description)
            )

E pymongo.errors.ServerSelectionTimeoutError: or a local mongodb database):27017: [Errno 8] nodename nor servname provided, or not known,your_testing_db_uri (can be the same as atlas:27017: [Errno 8] nodename nor servname provided, or not known, Timeout: 30s, Topology Description: <TopologyDescription id: 6311f7cf487ac5c1e1814906, topology_type: Unknown, servers: [<ServerDescription (’ or a local mongodb database)’, 27017) server_type: Unknown, rtt: None, error=AutoReconnect(’ or a local mongodb database):27017: [Errno 8] nodename nor servname provided, or not known’)>, <ServerDescription (‘your_testing_db_uri (can be the same as atlas’, 27017) server_type: Unknown, rtt: None, error=AutoReconnect(‘your_testing_db_uri (can be the same as atlas:27017: [Errno 8] nodename nor servname provided, or not known’)>]>

…/…/…/…/opt/anaconda3/envs/mflix/lib/python3.9/site-packages/pymongo/topology.py:238: ServerSelectionTimeoutError
_________________________________________________________________________________________ test_search_by_movie_id __________________________________________________________________________________________

client = <FlaskClient <Flask ‘mflix.factory’>>

@pytest.mark.connection
def test_search_by_movie_id(client):
    actual = get_movie("573a13acf29313caabd29647")
  assert actual['title'] == 'King Kong'

E KeyError: ‘title’

tests/test_db_connection.py:39: KeyError
_________________________________________________________________________________________ test_simple_text_search __________________________________________________________________________________________

client = <FlaskClient <Flask ‘mflix.factory’>>

@pytest.mark.connection
def test_simple_text_search(client):
  (actual, _) = get_movies({"$text": {"$search": "King Kong"}}, 0, 20)

tests/test_db_connection.py:44:


mflix/db.py:224: in get_movies
total_num_movies = db.movies.count_documents(query)
…/…/…/…/opt/anaconda3/envs/mflix/lib/python3.9/site-packages/pymongo/collection.py:1835: in count_documents
return self._retryable_non_cursor_read(_cmd, session)
…/…/…/…/opt/anaconda3/envs/mflix/lib/python3.9/site-packages/pymongo/collection.py:1840: in _retryable_non_cursor_read
with client._tmp_session(session) as s:
…/…/…/…/opt/anaconda3/envs/mflix/lib/python3.9/contextlib.py:119: in enter
return next(self.gen)
…/…/…/…/opt/anaconda3/envs/mflix/lib/python3.9/site-packages/pymongo/mongo_client.py:1729: in _tmp_session
s = self._ensure_session(session)
…/…/…/…/opt/anaconda3/envs/mflix/lib/python3.9/site-packages/pymongo/mongo_client.py:1712: in _ensure_session
return self.__start_session(True, causal_consistency=False)
…/…/…/…/opt/anaconda3/envs/mflix/lib/python3.9/site-packages/pymongo/mongo_client.py:1657: in __start_session
self._topology._check_implicit_session_support()
…/…/…/…/opt/anaconda3/envs/mflix/lib/python3.9/site-packages/pymongo/topology.py:538: in _check_implicit_session_support
self._check_session_support()
…/…/…/…/opt/anaconda3/envs/mflix/lib/python3.9/site-packages/pymongo/topology.py:554: in _check_session_support
self._select_servers_loop(


self = <Topology <TopologyDescription id: 6311f80c487ac5c1e1814908, topology_type: Unknown, servers: [<ServerDescription (’ o…nect(‘your_testing_db_uri (can be the same as atlas:27017: [Errno 8] nodename nor servname provided, or not known’)>]>>
selector = <function readable_server_selector at 0x7f77d4720ca0>, timeout = 30, address = None

def _select_servers_loop(self, selector, timeout, address):
    """select_servers() guts. Hold the lock when calling this."""
    now = time.monotonic()
    end_time = now + timeout
    server_descriptions = self._description.apply_selector(
        selector, address, custom_selector=self._settings.server_selector
    )

    while not server_descriptions:
        # No suitable servers.
        if timeout == 0 or now > end_time:
          raise ServerSelectionTimeoutError(
                "%s, Timeout: %ss, Topology Description: %r"
                % (self._error_message(selector), timeout, self.description)
            )

E pymongo.errors.ServerSelectionTimeoutError: or a local mongodb database):27017: [Errno 8] nodename nor servname provided, or not known,your_testing_db_uri (can be the same as atlas:27017: [Errno 8] nodename nor servname provided, or not known, Timeout: 30s, Topology Description: <TopologyDescription id: 6311f80c487ac5c1e1814908, topology_type: Unknown, servers: [<ServerDescription (’ or a local mongodb database)’, 27017) server_type: Unknown, rtt: None, error=AutoReconnect(’ or a local mongodb database):27017: [Errno 8] nodename nor servname provided, or not known’)>, <ServerDescription (‘your_testing_db_uri (can be the same as atlas’, 27017) server_type: Unknown, rtt: None, error=AutoReconnect(‘your_testing_db_uri (can be the same as atlas:27017: [Errno 8] nodename nor servname provided, or not known’)>]>

…/…/…/…/opt/anaconda3/envs/mflix/lib/python3.9/site-packages/pymongo/topology.py:238: ServerSelectionTimeoutError
=========================================================================================== 39 tests deselected ============================================================================================
================================================================================ 4 failed, 39 deselected in 121.90 seconds ================================================================================

please try to read things carefully. this will save you a great deal of time instead of writing this kind of long “what is the error” posts.

ini file expects you to either replace this URI with a test database or use the same URI in the “PROD” section. this single line you ignored has caused many pages of errors you copied.

2 Likes

Oh! Thank you. It’s my fault.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.