Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Error
Traceback (most recent call last):
File "C:\Users\google\PycharmProjects\BookManagementSystem\BookManagerTest.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’

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading