How to assign a value to a property in django - python

i have a new property in my model however I'd like to assign a test value in it for my test script.
this is my code:
models.py
mycode = models.UUIDField(null=True)
#property
def haveCode(self):
if self.mycode == uuid.UUID('{00000000-0000-0000-0000-000000000000}'):
return False
else
return True
and this is the test script that i am working on. I wanted to have a test value for haveCode:
test = Test()
test.mycode = uuid.UUID('{00000000-0000-0000-0000-000000000000}')
test.save()
checkTest = Test()
#this is only to pass the test
#delete this when start coding
checkTest.haveCode = True
assertEqual(test.haveCode, True)
however I got an error in checkTest.haveCode = True since this is just a property and not an attribute.
how to assign True to it? I appreciate your help

You can 'mock' that property using the mock library
from mock import patch, PropertyMock
#patch.object(Test, 'haveCode', new_callable=PropertyMock)
def myTest(test_haveCode_mock):
test_haveCode_mock.return_value = True
checkTest = Test()
assertEqual(checkTest.haveCode, True)
patch.stopall() # when you want to release all mocks

Related

Not sure why MyMock.env["key1"].search.side_effect=["a", "b"] works but MyMock.env["key1"] = ["a"] with MyMock.env["key2"] = ["b"] does not work

I had created a simple example to illustrate my issue. First is the setup say mydummy.py:
class TstObj:
def __init__(self, name):
self.name = name
def search(self):
return self.name
MyData = {}
MyData["object1"] = TstObj("object1")
MyData["object2"] = TstObj("object2")
MyData["object3"] = TstObj("object3")
def getObject1Data():
return MyData["object1"].search()
def getObject2Data():
return MyData["object2"].search()
def getObject3Data():
return MyData["object3"].search()
def getExample():
res = f"{getObject1Data()}{getObject2Data()}{getObject3Data()}"
return res
Here is the test that failed.
def test_get_dummy1():
dummy.MyData = MagicMock()
mydummy.MyData["object1"].search.side_effect = ["obj1"]
mydummy.MyData["object2"].search.side_effect = ["obj2"]
mydummy.MyData["object3"].search.side_effect = ["obj3"]
assert mydummy.getExample() == "obj1obj2obj3"
The above failed with run time error:
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/mock.py:1078: StopIteration
Here is the test that passed:
def test_get_dummy2():
dummy.MyData = MagicMock()
mydummy.MyData["object1"].search.side_effect = ["obj1", "obj2", "obj3"]
assert mydummy.getExample() == "obj1obj2obj3"
Am I missing something? I would have expected test_get_dummy1() to work and test_get_dummy2() to fail and not vice versa. Where and how can I find/learn more information about mocking to explain what is going on...
MyData["object1"] is converted to this function call: MyData.__getitem__("object1"). When you call your getExample method, the __getitem__ method is called 3 times with 3 parameters ("object1", "object2", "object3").
To mock the behavior you could have written your test like so:
def test_get_dummy_alternative():
mydummy.MyData = MagicMock()
mydummy.MyData.__getitem__.return_value.search.side_effect = ["obj1", "obj2", "obj3"]
assert mydummy.getExample() == "obj1obj2obj3"
Note the small change from your version: mydummy.MyData["object1"]... became: mydummy.MyData.__getitem__.return_value.... This is the regular MagicMock syntax - we want to to change the return value of the __getitem__ method.
BONUS:
I often struggle with mock syntax and understanding what's happening under the hood. This is why I wrote a helper library: the pytest-mock-generator. It can show you the actual calls made to the mock object.
To use it in your case you could have added this "exploration test":
def test_get_dummy_explore(mg):
mydummy.MyData = MagicMock()
mydummy.getExample()
mg.generate_asserts(mydummy.MyData, name='mydummy.MyData')
When you execute this test, the following output is printed to the console, which contains all the asserts to the actual calls to the mock:
from mock import call
mydummy.MyData.__getitem__.assert_has_calls(calls=[call('object1'),call('object2'),call('object3'),])
mydummy.MyData.__getitem__.return_value.search.assert_has_calls(calls=[call(),call(),call(),])
mydummy.MyData.__getitem__.return_value.search.return_value.__str__.assert_has_calls(calls=[call(),call(),call(),])
You can easily derive from here what has to be mocked.

Set Pytest mock value to None

I have a main.py file which includes WindoMgr class, xvfb as a class attibute and two classmethods: _cleanup_lock_file and func_to_test. I only have problem with testing else block, which gets triggered when cls.xvfb is None. The else block returns default value of b which is 1.
#main.py
class WindowMgr:
xvfb = None
#classmethod
def _cleanup_lock_file(cls):
return True
#classmethod
def func_to_test(cls):
b = 1
if cls.xvfb is not None:
print(cls.xvfb) #<MagicMock name='xvfb' id='61908568'>
try:
cls.xvfb.terminate()
b = 2
except Exception:
b = 1 + 2
finally:
cls._cleanup_lock_file()
else:
return b
return b
Now, in the test_main.py file, I have patched the xvfb to mock cls.xvfb.terminate() in try block, but I want cls.xvfb to be None for the else block to be triggered. I have tried something like xvfb = None or xvfb.return_value = None. But the else block doesn't get triggered as printing cls.xvfb shows it as a MagickMock object as commented out in the code. The WindowMgr.func_to_test() return value is 2 and not 1. It should return default value of b as 1.
How do I set cls.xvfb mock object as None? Any help would be appreciated.
#test_main.py
from main import WindowMgr
def test_main_func(mocker):
xvfb = mocker.patch.object(WindowMgr, 'xvfb')
# To test the else block
# xvfb = None -----> doesn't work
# xvfb.return_value = None ----> doesn't work
assert WindowMgr.func_to_test() == 1 # AssertionError: assert 2 == 1
The problem is that setting xvbf does only reassign the local variable, not the mocked variable (this is just normal Python behavior). Setting the return_value does not work, because xvbf is not a callable. However, if you use a property mock instead of a regular mock, it will behave as if it were a callable, and the value can be set via return_value:
def test_main_func(mocker):
xvfb = mocker.patch.object(WindowMgr, "xvfb",
new_callable=mocker.PropertyMock)
xvfb.return_value = None
assert WindowMgr.func_to_test() == 1
(you can access PropertyMock directly from mocker as a convenience)
Even if xvbf is a class variable and not a property, it behaves like a property from the perspective of a mock and this will work.

unittest - How to test internal parameter in a function?

I'm having some issue while creating unittest for internal parameter.
My structure is:
[1] my_animal.py contains Myclass and method: do_bite()
my_animal.py
class Myclass():
def do_bite(self):
return 1
[2] my_module.py contains jobMain("") which is using the method from my_animal.py
my_module.py
import sys
from someclass import Myclass
def jobMain(directoryPath):
flag = -1
result = Myclass()
if result.do_bite() is None:
flag = 0
if result.do_bite() is 1:
flag = 1
if result.do_bite() is 2:
flag = 2
[3] my_test.py contains the unittest to test jobMain in my_module.py
my_test.py
# Mock Myclass.dobite to None
#pytest.fixture
def mock_dobite0():
with mock.patch('my_module.Myclass') as mocked_animal:
mocked_animal.return_value.do_bite.return_value = None
yield
# Mock Myclass.dobite to 1
#pytest.fixture
def mock_dobite1():
with mock.patch('my_module.Myclass') as mocked_animal:
mocked_animal.return_value.do_bite.return_value = 1
yield
# Mock Myclass.dobite to 2
#pytest.fixture
def mock_dobite2():
with mock.patch('my_module.Myclass') as mocked_animal:
mocked_animal.return_value.do_bite.return_value = 2
yield
# My unittest to test dobite() method
def test_dobite0(mock_Myclass, mock_dobite0):
jobMain("")
def test_dobite1(mock_Myclass, mock_dobite1):
jobMain("")
def test_dobite2(mock_Myclass, mock_dobite2):
jobMain("")
My question is: How to test 'flag' parameter inside JobMain?
'flag' para must be assigned the correct value.( eg: dobite = 1 => flag = 1)
The variable para only exists in the scope of jobMain. If you want to use the variable outside jobMain the most common ways are
1) return the value
This is quite obvious. Since jobMain is a function, it returns a value. Without an explicit return statement you return None. You could just
def jobmain(pth):
# do stuff and assign flag
return flag
# and inside tests
assert jobmain("") == 1
2) Use a class instead
If you want the jobMain to remember some state, then it is common practice to use objects. Then flag would be attribute of the object and could be accessed from outside, after you call any method (function) of JobMain. For example
class JobMain:
def __init__(self):
self.flag = -1
def run(self, pth):
result = Myclass()
if result.do_bite() is None:
self.flag = 0
if result.do_bite() is 1:
self.flag = 1
if result.do_bite() is 2:
self.flag = 2
# and inside test
job = JobMain()
job.run()
assert job.flag == 1
Note
I just copy-pasted your code for setting the flag. Note that you call do_bite() many times, if the resulting value is None or 1. Also, when testing against a number, one should use == instead of is.
How to test 'flag' parameter inside JobMain?
You don't. It's an internal variable. Testing it would be glass-box testing; the test will break if the implementation changes.
Instead, test the effect of flag. This is black-box testing. Only the interface is tested. If the implementation changes the test still works allowing the code to be aggressively refactored.
Note: If you don't hard code result = Myclass() you don't need to mock. Pass it in as an argument with the default being Myclass().
def jobMain(directoryPath, result=Myclass()):
Then you don't need to patch Myclass(). Instead, pass in a mock object.
# I don't know unittest.mock very well, but something like this.
mock = Mock(Myclass)
mock.do_bite.return_value = 2
jobMain('', result=mock)
This also makes the code more flexible outside of testing.

Is there a way to set variables at runtime in python

I am trying to set/unset some of the debug flags while my code is running. Below is the snippet of the function
class ABC:
def __init__(self,debugFlag):
self.debugFlag = False
def some_function()
if self.debugFlag is True:
print "Some debug logs"
Above is the code snippet. I want to set this debugFlag while my program is running and depends on the situation want to set/unset its value. Based on the current value of debugFlag it should print debug logs or not
How to do this in python
Explaining my problem again : Suppose I am having a script that takes 60 mins to run. I executed the script with debugFlag False. After 15 mins, I want to enable the debug logs. How can I do this without interrupting my execution.
How about:
class ABC:
def __init__(self,debugFlag):
self.debugFlag = False
def some_function():
if self.debugFlag is True:
print "Some debug logs"
def set_flag():
self.debugFlag = True
You can change the value of self.debugFlag when a condition is met, yes.
class ABC:
def __init__(self,debugFlag):
self.debugFlag = debugFlag # or omit that arg and always false
def some_function(self): # Need self
if self.debugFlag:
print "Some debug logs"
def condition_setter(self):
# Do some checks here then set debugflag to False
self.debugFlag = False
One way to accomplish this is to declare var in your modules root, and import/reference them in the same scope when you set and reference.
Imagine the directory structure of your modules like so
my_module/
- __init__.py
- another_module.py
- utils/
- __init__.py
- and_so_on.py
in my_module/__init__.py declare your __debug__ var
__debug__ = False
In your example code
import my_module
class ABC:
def __init__(self):
pass
def some_function()
if my_module.__debug__ is True:
print "Some debug logs"
Then before you actually execute the code, set your debug flag to False importing and referencing in the same fashion
import my_module
my_module.__debug__ = True
As long as you import and reference the variable the same IE:
import my_module
if my_module.__debug__ is True:
print("IN DEBUG")
It will retain its value throughout your execution
Try something like this :
class ABC():
def __init__(self, debugFlag):
self.debugFlag = debugFlag
def some_function(self):
if self.debugFlag:
print('Debug on\n')
else:
print('Debug off\n')
abc_object = ABC(False)
def check_choice():
if str(choice) == '1':
abc_object.debugFlag = True
abc_object.some_function()
elif str(choice) == '2':
abc_object.debugFlag = False
abc_object.some_function()
else :
print('Invalid input')
while True:
choice = input('Type 1 to enable debugging, 2 to disable debuigging : ')
check_choice()
Summary:
When you initialize the class it takes 1 mandatory argument debugFlag, either True or False.
When you define a function from within the class you should use the 'self' argument.
Explanation as to of why this is
'if self.debugFlag' is the same as if 'self.debugFlag is True :' The former should be used as it is more concise.
We instantiate an instance of ABC and store it in the variable abc_object. We pass it a default value for debugFlag, in this case False.
We then introduce some logic to check the state of debugFlag, and then perform some function based on the outcome of our logic.
Hope this helps. Let me know if you need further clarification.
I dont think you can do this directly from variables, but you can, for example, create a file (debug.txt for example) and check if it exists for your triggering. For example, create an empty file called debug.txt and check, inside your script, if the file exists.
Untested code:
import os
class ABC:
def __init__(self,debugFlag):
self.debugFlag = False
def some_function()
if os.path.exists("debug.txt"):
print "Some debug logs"
This way you can trigger/untrigger debug by creating/deleting the file "debug.txt".
With a slight modification of your class:
class ABC:
def __init__(self,debugFlag=False):
self.debugFlag = debugFlag # initialize the flag here or use default
def some_function()
if self.debugFlag is True:
print "Some debug logs"
After you create the object you will be able to change its attributes, like:
obj_abc = ABC() # debug off by default
obj_abc.some_function() # no debug print
obj_abc.debugFlag = True # enable debug
obj_abc.some_function() # now with debug print
obj_abc.debugFlag = False # disable debug
obj_abc.some_function() # now without debug print

set return_value of function

I have a class:
class AccountBusiness:
def save(self, account) -> Account:
if not account.account_number_is_valid():
return False
return True
and a test as:
#mock.patch.object(AccountBusiness, 'save')
def test_can_save_valid_account(self, mock_save):
mock_account = mock.create_autospec(Account)
mock_account.account_number_is_valid.return_value = False
account_business = AccountBusiness()
result = account_business.save(mock_account)
self.assertEqual(result.return_value, True)
but it shows an exception like:
AssertionError: <MagicMock name='save()()' id='48830448'> != True
I want to set the return value of account.account_number_is_valid() to False and run the test.
You are using a patch object on the instance method you are looking to test. However, you are looking to test the logic inside the save method. So mocking that out will not test any of the logic inside that method. So, the output you are actually getting here:
AssertionError: <MagicMock name='save()()' id='48830448'> != True
Should be the first hint that something is not right. Your save method is coming back as a MagicMock. You don't want this. What you actually want to do is only mock the Account class, and go accordingly from there. So, your patching here:
#mock.patch.object(AccountBusiness, 'save')
should actually only be:
#mock.patch('path.to.AccountBusiness.Account', return_value=Mock(), autospec=True)
The path.to.AccountBusiness.Account is the location of the Account class with respect to the AccountBusiness class.
So, with that patching, then the return_value of calling Account will now be your mock object that you can use for your account_number_is_valid. So, the code will actually look like this:
class MyTest(unittest.TestCase):
def setUp(self):
self.account_business = AccountBusiness()
#mock.patch('path.to.AccountBusiness.Account', return_value=Mock(), autospec=True)
def test_can_save_valid_account(self, mock_account):
mock_account_obj = mock_account.return_value
mock_account_obj.account_number_is_valid.return_value = False
self.assertFalse(self.account_business.save(mock_account_obj))
Also, pay close attention to the assertion at the end. It was changed to make use of the available assertFalse. Also, look over your own logic, as returning False for account_number_is_valid will actually return False in your save method.

Categories