0

I'm trying to write test functions for a project I'm working on. I using oracle as my database in the project and in the testing I'm using the in memory sqlite3 my problem is that some queries syntax are different. This isn't a problem when I'm unit testing because I can pass queries as parameters and adapt them but when I try to test the do() function responsible for calling all the functions this becomes undoable.

Here an example of my class:

def my_class:
 def get_insert_query(self):
    return "insert .... where my_date > sysdate"
 def insert(self,query):
     ...
 def get_update_query(self):
    return "update .... where my_date > sysdate"
 def update(self,query):
     ...
 def do(self):
     query = self.get_insert_query()
     self.insert(query)
     query = self.get_update_query()
     self.update()

I want to test do() function. I found out that's it possible to replace a function using the patch from unittest.mock to replace get_insert_query with a function that return a query adapted to sqlite3 but my problem with it that it doesn't the same order of execution and doesn't enable me to execute the rest of the function.

  • 2
    Have you considered using an Oracle XE database instead of sqlite? If you're changing the syntax of all the SQL just for testing, then what have you *really* tested? Not your actual production configuration or code. You're building infrastructure and overhead into your app that appears to *only* be necessary for the actual test environment. If you're test environment allowed the same syntax as production, all of these problems go away. – pmdba May 25 '23 at 10:26
  • 1
    I would advise the exact opposite. You should not be interacting with _any_ database whatsoever in unit tests. Functions with side effects (such as database queries) should be mocked out. You should merely check _if_ they were called, _with the correct arguments_ and/or _in the correct order_. No actual database should be involved, unless you are literally writing code that provides a database interaction layer, which I am assuming you are not. – Daniil Fajnberg May 25 '23 at 13:43
  • 1
    You are not supposed to test other people's code. I am assuming you use some database connector library or even some ORM to interact with the DB. You should assume those libraries behave as advertised and that they are tested by their maintainers. (If not, you should not be using them in the first place.) All you need to do, is check that **your** functions call those library functions **correctly** (whatever that means in your specific context). And you can do that with the tools in [`unittest.mock`](https://docs.python.org/3/library/unittest.mock.html). – Daniil Fajnberg May 25 '23 at 13:47

0 Answers0