Using tinyDB the application is persisting data to a json file. I need to unittest if certain data is already contained in the database file.
I'm creating an alternate database setup in the setUP method in the variable db. I get the error below. Why is the db variable not available to the namespace in the function that's being tested?
When running the unittest I get:
ERROR: test_contains (__main__.TestDB)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test.py", line 65, in setUp
print type(db)
NameError: global name 'db' is not defined
Code to test in project/app/sandbox.py
from tinydb import TinyDB, Query
db = TinyDB('db.json')
Check_keyword = Query()
def test_db(keyword):
if db.contains(Check_keyword.keyword == keyword ):
print "keyword already in db"
return True
Unittest in project/test.py
from tinydb import TinyDB, Query
from app.sandbox import test_db
class TestDB(unittest.TestCase):
def setUp(self):
self.db = TinyDB('test_db.json')
self.Check_keyword = Query()
def test_contains(self):
data = "doctor salarybljkhl"
result = test_db(data)
self.assertEqual(result, True)