K
K
Kirill Gorelov2020-02-08 11:43:20
Python
Kirill Gorelov, 2020-02-08 11:43:20

I am writing a class + test, I can’t understand why everything breaks?

Hey guys, I'm writing a class and I want to test it.
There is a class. Removed everything superfluous from here, the main thing is to understand the meaning.

from appmodule.database.db import connection

class Task:

    @classmethod
    def delete(cls, task_id):

        try:
            cursor = connection.cursor()
            sql = 'DELETE FROM task WHERE task_id = 64'
            cursor.execute(sql)
            connection.close()
        except Exception as e:
            print(e)
            return False
        
        return True

    @classmethod
    def getById(cls, task_id):

        try:
            cursor = connection.cursor()
            sql = 'SELECT * FROM task WHERE task_id = %s'
            cursor.execute(sql, (task_id))
            select_rez = cursor.fetchall()
            connection.close()
        except Exception as e:
            return False
        
        return select_rez


I write tests, again shortened
class TaskTestCase(unittest.TestCase):

    def test_1(self):
        rez_get = Task.getById(55)
        print(rez_get) # всегда возвращает False

    def test_deleteTask(self):
        print ( "id: " + self .id())
        self.assertFalse(Task.delete('string'))
        self.assertTrue(Task.delete(65))


Tests fail, I try to throw an exception, outputs "(0, '')"
.id: __main__.TaskTestCase.test_deleteTask
(0, '')
(0, '')


I have a suspicion that I'm not using pyMysql in the right way ....

And I still can't check the string and the number against the number))
That is, I give the method a string and a number, it doesn't check.
self.assertFalse(Task.delete('string'))
self.assertFalse(Task.delete(9999999999999))


I'm doing a check
if not task_id.isdigit():
            return False

if int(task_id) <= 0:
   return False


Throws exception
'int' object has no attribute 'isdigit'
and
invalid literal for int() with base 10: 'string'

Worth python 3.7

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2020-02-08
@bacon

well, how many times to repeat that you don’t need to do this, you have an Exception there, and you hush it up

except Exception as e:
    return False

and secondly, know the difference in
(task_id)
(task_id, )

further, yes, 9999999999999 does not have an isdigit method, but they explicitly tell you about it, and why int('string') is also not allowed

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question