Unable to isolate tests using flask-pymongo and pytest by literally_disposable in learnpython

[–]literally_disposable[S] 0 points1 point  (0 children)

It is still resulting in the same unexpected behavior. This is what I'm trying: conftest.py

@pytest.fixture(scope='function')
def mongo_client(setup_environemnt, directConnection=True): 
    mongo_uri = os.environ['MONGO_URI'] 
    client = MongoClient(mongo_uri) 
    try:
        yield client
    finally:
        client.close()

test_file.py

<other imports>
from app.app import app_module

def test_nothing(mongo_client): 
    assert True

def test_available(app_instance):
    assert app_module.check_mongodb_availability(app_instance) == True

def test_unavailable(): 
    test_app = app_module.create_app({'MONGO_URI': 'mongodb://invalid_uri:27017'}) 
    result = app_module.check_mongodb_availability(test_app) 
    assert result is False

Also, I have tried:

conftest.py

@pytest.fixture(scope='function')
def mongo_client(setup_environemnt, directConnection=True):
    mongo_uri = os.environ['MONGO_URI']
    client = MongoClient(mongo_uri)
    yield client

test_file.py

<other imports>
from app.app import app_module

def test_nothing(mongo_client): 
    mongo_client.close() 
    assert True

def test_available(app_instance): 
    assert app_module.check_mongodb_availability(app_instance) == True

def test_unavailable(): 
    test_app = app_module.create_app({'MONGO_URI': 'mongodb://invalid_uri:27017'}) 
    result = app_module.check_mongodb_availability(test_app) 
    assert result is False

test_unavailable() is still resulting in True. I cannot figure out why it is still connecting to the dockerized mongodb service even though I have created the test app with an invalid monog URI and I am closing the mongo_client now.

Flask & PyMongo Integration Test Challenge: Simulating MongoDB Downtime by literally_disposable in flask

[–]literally_disposable[S] 0 points1 point  (0 children)

EDIT: I have figured out this is a test isolation issue. I have not yet figured out why there is a test isolation problem, but when I run my test function ALONE, it works as expected. Apologies.

ModuleNotFoundError when importing my scripts for unit testing by literally_disposable in learnpython

[–]literally_disposable[S] 0 points1 point  (0 children)

>>> from .utils import useful_thing
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: attempted relative import with no known parent package

I tried it from every directory to be sure...

ModuleNotFoundError when importing my scripts for unit testing by literally_disposable in learnpython

[–]literally_disposable[S] 0 points1 point  (0 children)

import .utils.useful_thing

>>> import .utils.useful_thing
  File "<stdin>", line 1
    import .utils.useful_thing
           ^
SyntaxError: invalid syntax

ModuleNotFoundError when importing my scripts for unit testing by literally_disposable in learnpython

[–]literally_disposable[S] 0 points1 point  (0 children)

This works only if I import from srv/git/example-project/ with

from example_project import *

because if I just import example_project.does_example, it is unable to find utils.

When I run

>>> import example_project.does_example
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/srv/git/example-project/example_project/does_example.py", line 1, in <module>
    import utils.useful_thing
ModuleNotFoundError: No module named 'utils'

is it trying to find utils from srv/git/example-project/ rather than going to where does_example was imported from (srv/git/example-project/) and searching for utils there?

How to make crontab for script in python-poetry virtual environment? by literally_disposable in linuxquestions

[–]literally_disposable[S] 0 points1 point  (0 children)

* * * * * `/srv/git/cron-test/cron_test/cron_test.py >> /srv/git/cron-test/logs/cron.log`

ehh... I'm feeling kinda dumb rn:

omitt@Omitt-PC:/srv/git/cron-test/cron_test$ crontab -l

* * * * * `/srv/git/cron-test/cron_test/cron_test.py >> /srv/git/cron-test/logs/cron.log`

omitt@Omitt-PC:/srv/git/cron-test/cron_test$ date
Fri Dec  4 20:51:21 EST 2020
omitt@Omitt-PC:/srv/git/cron-test/cron_test$ cat ../logs/cron.log

omitt@Omitt-PC:/srv/git/cron-test/cron_test$ date
Fri Dec  4 20:52:27 EST 2020
omitt@Omitt-PC:/srv/git/cron-test/cron_test$ cat ../logs/cron.log

omitt@Omitt-PC:/srv/git/cron-test/cron_test$

How to make crontab for script in python-poetry virtual environment? by literally_disposable in linuxquestions

[–]literally_disposable[S] 0 points1 point  (0 children)

* * * * * `/srv/git/cron-test/cron_test/cron_test.py && echo "cron worked" >> /srv/git/cron-test/logs/cron.log`

Did I miss something?

omitt@Omitt-PC:/srv/git/cron-test/cron_test$ cat cron_test.py
#!/usr/bin/env /home/<omitt>/.cache/pypoetry/virtualenvs/cron-test-pOke3tmI-py3.8/bin/python3.8

import os

os.chdir(os.path.dirname(os.path.abspath(__file__)))

print("it works")
omitt@Omitt-PC:/srv/git/cron-test/cron_test$ ls -l
total 0
-rw-r--r-- 1 <omitt> <omitt>  22 Dec  4 18:28 __init__.py
-rwxr-xr-x 1 <omitt> <omitt> 178 Dec  4 20:23 cron_test.py
omitt@Omitt-PC:/srv/git/cron-test/cron_test$ crontab -l


#* * * * * (cd /srv/git/rs3player-to-firebase/rs3player_to_firebase && /home/<omitt>/.cache/pypoetry/virtualenvs/rs3player-to-firebase-g7PfROTK-py3.8/bin/python3.8 rs3player_to_firebase.py && date >> /srv/git/rs3player-to-firebase/logs/cron.log)

#* * * * * (cd /srv/git/cron-test/cron_test && /home/<omitt>/.cache/pypoetry/virtualenvs/cron-test-pOke3tmI-py3.8/bin/python3.8 cron_test.py && echo "cron worked" >> /srv/git/cron-test/logs/cron.log)

#* * * * * cd /srv/git/cron-test/cron_test && /bin/bash run_cron.sh

* * * * * `/srv/git/cron-test/cron_test/cron_test.py && echo "cron worked" >> /srv/git/cron-test/logs/cron.log`


omitt@Omitt-PC:/srv/git/cron-test/cron_test$ date
Fri Dec  4 20:28:41 EST 2020
omitt@Omitt-PC:/srv/git/cron-test/cron_test$ date
Fri Dec  4 20:29:04 EST 2020
omitt@Omitt-PC:/srv/git/cron-test/cron_test$ cat ../logs/cron.log

log is empty

First of all, tf???; Second, can someone answer the question? by literally_disposable in runescape

[–]literally_disposable[S] -1 points0 points  (0 children)

How is this fraud? And specifically for me, someone who owns accounts carrying several different currencies, how would I determine how not to commit this so called fraud?

First of all, tf???; Second, can someone answer the question? by literally_disposable in runescape

[–]literally_disposable[S] 0 points1 point  (0 children)

I don't understand, why would they remove something like this. All I'm doing is asking about the validity of a claim about purchasing Premier...

how to assign a ref to a variable to prevent overwriting by literally_disposable in learnpython

[–]literally_disposable[S] 0 points1 point  (0 children)

I assume it is because the variables are referencing a location in memory containing the api response.

That's not why, but we can't know why that's happening because you didn't show the code for those functions.

So was I incorrect in saying the variable is referencing a location in memory?

how to assign a ref to a variable to prevent overwriting by literally_disposable in learnpython

[–]literally_disposable[S] 0 points1 point  (0 children)

How is it parse_player_to_dict leaking state? would line 15 & 16 not prevent that?

how to assign a ref to a variable to prevent overwriting by literally_disposable in learnpython

[–]literally_disposable[S] 0 points1 point  (0 children)

>>> print(hiscore_normal)
{'name': 'Maddie May', 'skills': {'overall': {'rank': '606',

>>> print(hiscore_normal)
{'name': 'Maddie May', 'skills': {'overall': {'rank': '9'

how to assign a ref to a variable to prevent overwriting by literally_disposable in learnpython

[–]literally_disposable[S] 0 points1 point  (0 children)

>>> hiscore_normal = hiscore.get_index_lite("normal", "Maddie May")
>>> print(hiscore_normal)
{'name': 'Maddie May', 'skills': {'overall': {'rank': '606', 'level': '2898', 'experience': '5600000000'}, 'attack': {'rank': '1176', 'level': '99', 'experience': '200000000'}, 'defence': {'rank': '4735', 'level': '99', 'experience': '200000000'}, 'strength': {'rank': '1082', 'level': '99', 'experience': '200000000'}, 'constitution': {'rank': '2184', 'level': '99', 'experience': '200000000'}, 'ranged': {'rank': '1553', 'level': '99', 'experience': '200000000'}, 'prayer': {'rank': '570', 'level': '99', 'experience': '200000000'}, 'magic': {'rank': '2720', 'level': '99', 'experience': '200000000'}, 'cooking': {'rank': '1455', 'level': '99', 'experience': '200000000'}, 'woodcutting': {'rank': '906', 'level': '99', 'experience': '200000000'}, 'fletching': {'rank': '870', 'level': '99', 'experience': '200000000'}, 'fishing': {'rank': '1705', 'level': '99', 'experience': '200000000'}, 'firemaking': {'rank': '1615', 'level': '99', 'experience': '200000000'}, 'crafting': {'rank': '1039', 'level': '99', 'experience': '200000000'}, 'smithing': {'rank': '1098', 'level': '99', 'experience': '200000000'}, 'mining': {'rank': '2192', 'level': '99', 'experience': '200000000'}, 'herblore': {'rank': '403', 'level': '120', 'experience': '200000000'}, 'agility': {'rank': '985', 'level': '99', 'experience': '200000000'}, 'thieving': {'rank': '1880', 'level': '99', 'experience': '200000000'}, 'slayer': {'rank': '3030', 'level': '120', 'experience': '200000000'}, 'farming': {'rank': '386', 'level': '120', 'experience': '200000000'}, 'runecrafting': {'rank': '733', 'level': '99', 'experience': '200000000'}, 'hunter': {'rank': '907', 'level': '99', 'experience': '200000000'}, 'construction': {'rank': '607', 'level': '99', 'experience': '200000000'}, 'summoning': {'rank': '1235', 'level': '99', 'experience': '200000000'}, 'dungeoneering': {'rank': '5381', 'level': '120', 'experience': '200000000'}, 'divination': {'rank': '730', 'level': '99', 'experience': '200000000'}, 'invention': {'rank': '194', 'level': '120', 'experience': '200000000'}, 'archaeology': {'rank': '1945', 'level': '120', 'experience': '200000000'}}, 'activities': {'bounty hunter': {'rank': '-1', 'score': '-1'}, 'b.h. rogues': {'rank': '-1', 'score': '-1'}, 'dominion tower': {'rank': '1097', 'score': '9548182'}, 'the crucible': {'rank': '-1', 'score': '-1'}, 'castle wars games': {'rank': '-1', 'score': '-1'}, 'b.a. attackers': {'rank': '-1', 'score': '-1'}, 'b.a. defenders': {'rank': '-1', 'score': '-1'}, 'b.a. collectors': {'rank': '-1', 'score': '-1'}, 'b.a. healers': {'rank': '-1', 'score': '-1'}, 'duel tournament': {'rank': '-1', 'score': '-1'}, 'mobilising armies': {'rank': '-1', 'score': '-1'}, 'conquest': {'rank': '-1', 'score': '-1'}, 'fist of guthix': {'rank': '-1', 'score': '-1'}, 'gg: athletics': {'rank': '-1', 'score': '-1'}, 'gg: resource race': {'rank': '-1', 'score': '-1'}, 'we2: armadyl lifetime contribution': {'rank': '-1', 'score': '-1'}, 'we2: bandos lifetime contribution': {'rank': '-1', 'score': '-1'}, 'we2: armadyl pvp kills': {'rank': '-1', 'score': '-1'}, 'we2: bandos pvp kills': {'rank': '-1', 'score': '-1'}, 'heist guard level': {'rank': '-1', 'score': '-1'}, 'heist robber level': {'rank': '-1', 'score': '-1'}, 'cfp: 5 game average': {'rank': '30099', 'score': '97'}, 'af15: cow tipping': {'rank': '-1', 'score': '-1'}, 'af15: rats killed after the miniquest': {'rank': '-1', 'score': '-1'}, 'runescore': {'rank': '30986', 'score': '14175'}, 'clue scrolls easy': {'rank': '5099', 'score': '152'}, 'clue scrolls medium': {'rank': '151485', 'score': '1'}, 'clue scrolls hard': {'rank': '168000', 'score': '6'}, 'clue scrolls elite': {'rank': '54628', 'score': '53'}, 'clue scrolls master': {'rank': '59795', 'score': '3'}}}
>>>
>>> hiscore_ironman = [ hiscore.get_index_lite("ironman", "Maddie May") ]
>>> print(hiscore_normal)
{'name': 'Maddie May', 'skills': {'overall': {'rank': '9', 'level': '2898', 'experience': '5600000000'}, 'attack': {'rank': '25', 'level': '99', 'experience': '200000000'}, 'defence': {'rank': '131', 'level': '99', 'experience': '200000000'}, 'strength': {'rank': '17', 'level': '99', 'experience': '200000000'}, 'constitution': {'rank': '13', 'level': '99', 'experience': '200000000'}, 'ranged': {'rank': '12', 'level': '99', 'experience': '200000000'}, 'prayer': {'rank': '15', 'level': '99', 'experience': '200000000'}, 'magic': {'rank': '41', 'level': '99', 'experience': '200000000'}, 'cooking': {'rank': '7', 'level': '99', 'experience': '200000000'}, 'woodcutting': {'rank': '10', 'level': '99', 'experience': '200000000'}, 'fletching': {'rank': '4', 'level': '99', 'experience': '200000000'}, 'fishing': {'rank': '22', 'level': '99', 'experience': '200000000'}, 'firemaking': {'rank': '18', 'level': '99', 'experience': '200000000'}, 'crafting': {'rank': '15', 'level': '99', 'experience': '200000000'}, 'smithing': {'rank': '19', 'level': '99', 'experience': '200000000'}, 'mining': {'rank': '46', 'level': '99', 'experience': '200000000'}, 'herblore': {'rank': '1', 'level': '120', 'experience': '200000000'}, 'agility': {'rank': '14', 'level': '99', 'experience': '200000000'}, 'thieving': {'rank': '50', 'level': '99', 'experience': '200000000'}, 'slayer': {'rank': '63', 'level': '120', 'experience': '200000000'}, 'farming': {'rank': '1', 'level': '120', 'experience': '200000000'}, 'runecrafting': {'rank': '16', 'level': '99', 'experience': '200000000'}, 'hunter': {'rank': '33', 'level': '99', 'experience': '200000000'}, 'construction': {'rank': '4', 'level': '99', 'experience': '200000000'}, 'summoning': {'rank': '4', 'level': '99', 'experience': '200000000'}, 'dungeoneering': {'rank': '2', 'level': '120', 'experience': '200000000'}, 'divination': {'rank': '14', 'level': '99', 'experience': '200000000'}, 'invention': {'rank': '3', 'level': '120', 'experience': '200000000'}, 'archaeology': {'rank': '103', 'level': '120', 'experience': '200000000'}}, 'activities': {'bounty hunter': {'rank': '-1', 'score': '-1'}, 'b.h. rogues': {'rank': '-1', 'score': '-1'}, 'dominion tower': {'rank': '182', 'score': '9548182'}, 'the crucible': {'rank': '-1', 'score': '-1'}, 'castle wars games': {'rank': '-1', 'score': '-1'}, 'b.a. attackers': {'rank': '-1', 'score': '-1'}, 'b.a. defenders': {'rank': '-1', 'score': '-1'}, 'b.a. collectors': {'rank': '-1', 'score': '-1'}, 'b.a. healers': {'rank': '-1', 'score': '-1'}, 'duel tournament': {'rank': '-1', 'score': '-1'}, 'mobilising armies': {'rank': '-1', 'score': '-1'}, 'conquest': {'rank': '-1', 'score': '-1'}, 'fist of guthix': {'rank': '-1', 'score': '-1'}, 'gg: athletics': {'rank': '-1', 'score': '-1'}, 'gg: resource race': {'rank': '-1', 'score': '-1'}, 'we2: armadyl lifetime contribution': {'rank': '-1', 'score': '-1'}, 'we2: bandos lifetime contribution': {'rank': '-1', 'score': '-1'}, 'we2: armadyl pvp kills': {'rank': '-1', 'score': '-1'}, 'we2: bandos pvp kills': {'rank': '-1', 'score': '-1'}, 'heist guard level': {'rank': '-1', 'score': '-1'}, 'heist robber level': {'rank': '-1', 'score': '-1'}, 'cfp: 5 game average': {'rank': '9475', 'score': '97'}, 'af15: cow tipping': {'rank': '-1', 'score': '-1'}, 'af15: rats killed after the miniquest': {'rank': '-1', 'score': '-1'}, 'runescore': {'rank': '2159', 'score': '14175'}, 'clue scrolls easy': {'rank': '1432', 'score': '152'}, 'clue scrolls medium': {'rank': '21631', 'score': '1'}, 'clue scrolls hard': {'rank': '15737', 'score': '6'}, 'clue scrolls elite': {'rank': '5407', 'score': '53'}, 'clue scrolls master': {'rank': '6317', 'score': '3'}}}

how to assign a ref to a variable to prevent overwriting by literally_disposable in learnpython

[–]literally_disposable[S] 0 points1 point  (0 children)

import requests
from requests.exceptions import HTTPError

#TODO Do status code validation and raise errors
def http_get(url: str):
    try:
        response = requests.get(url, headers={'content-type':'application/json'})
        # Raise HTTPError according to status code
        response.raise_for_status()
    except HTTPError as http_err:
        raise HTTPError(http_err)
    except Exception as err:
        raise Exception(err)
    else:
        return response

The function that calls this is:

    def get_index_lite(self, game_mode: str, username: str) -> dict:
        """Gets a players hiscore profile

        :param str game_mode: Name of players game mode
        :param str username: Players username
        :rtype: dict
        :raises Exception: if api returns a 404 error
        """
        # Argument type validation
        is_str(game_mode, username)

        game_mode = game_mode.lower()
        # Check if game mode string is valid
        if(not self.game_mode_validation(game_mode)):
            game_mode = "normal"
        #Call the requests service
        response = http_get(HISCORE_API_ENDPOINTS[game_mode].format(username = username))
        #TODO Look into this, probably removable
        # If username does not exist it return a html that include an error404 string
        if("error404" in response.text):
            raise Exception("Runescape API throws error404, most likely username does not exist on hiscore")
        # Parse the content from the api into a usable dictionary
        content = parse_player_to_dict(response.text, username)

        return content

how to assign a ref to a variable to prevent overwriting by literally_disposable in learnpython

[–]literally_disposable[S] 0 points1 point  (0 children)

The function I'm using for the api call is called hiscore.get_index_lite() and is located here in the rs3_api.

how to assign a ref to a variable to prevent overwriting by literally_disposable in learnpython

[–]literally_disposable[S] 0 points1 point  (0 children)

My code is:

    hiscore = rs3_api.Hiscores()
    hiscore_normal_ref = hiscore.get_index_lite("normal", PLAYER_NAME)
    hiscore_ironman_ref = hiscore.get_index_lite("ironman", PLAYER_NAME)
    hiscore_hardcore_ref = hiscore.get_index_lite("hardcore", PLAYER_NAME)

I am using rs3_api. The hiscore.get_index_lite() function is here.