Why does Python unit test on SQLite Database fail when I have all the parameters?

Question:

I’m new to python and I’m trying unit testing. Right now I’m trying to test insertions into my SQLite database but it keeps saying I’m missing the required parameters ‘author’, ‘maincharacter’, ‘place’, ‘year’ when I’ve entered it in my insertion statement. I’m obviously missing something but I just can’t see it.

Here’s the unit test case:

import unittest

import backend


class MyTestCase(unittest.TestCase):
   def test_db_write(self):
    self.assertEqual(backend.insert("""INSERT INTO `book`", (title, author, year, maincharacter, place) 
                           ('5', 'Harry Potter', 'JK Rowling', '2000','Harry','Closet' )"""), True)
    self.assertEqual(backend.insert("""INSERT INTO `book`", (title, author, year, maincharacter, place)
                                       ('6', 'Harry Potter', 'JK Rowling', '2000','Harry','Closet')"""), True)
    self.assertEqual(backend.delete("""DELETE FROM `book` WHERE id='5' """), True)
    self.assertEqual(backend.delete("""DELETE FROM `book` WHERE id='6' """), True)


if __name__ == '__main__':
    unittest.main()

This is the error message:

Error
Traceback (most recent call last):
File "C:UsersgooglePycharmProjectsBookManagementSystemBookManagerTest.py", line 8, in test_db_write
self.assertEqual(backend.insert("""INSERT INTO book VALUES(NULL, ?,?,?,?,?)", (title, author, year, maincharacter, place)
TypeError: insert() missing 4 required positional arguments: ‘author’, ‘year’, ‘maincharacter’, and ‘place’

Asked By: google

||

Answers:

You are passing only a string into your statement. The code is missing four separate parameters. I believe it thinks your entire string is the ‘title’ parameter.

It looks like your code is set up for ORM. This is when the sql statements are executed programatically. You are writing a string as a SQL script.

Try changing the first statement to:

self.assertEqual(backend.insert('Harry Potter', 'JK Rowling', '2000','Harry','Closet' ), True)
Answered By: Timbo