Search code examples
djangotestcase

Django - Query_set returns an empty arraylist when it is ran in the test


I am trying to run a TestCase on my model.

I already have a MySQL database (specifically MariaDB through a HeidiSQL GUI) created and connected with the respective data inside for this project.

My test.py code is as follows:

class TestArrivalProbabilities(TestCase):
    def test_get_queryset_test(self):
        print("Hello Steve!")
        i = 1
        self.assertEqual(i, 1)
        l = [3, 4]
        self.assertIn(4, l)

    def test_get_queryset_again(self):
        query_set = ArrivalProbabilities.objects.all()
        print(query_set)
        n = len(query_set)
        print(n)  # Print each row
        bin_entries = []
        bin_edges = []
        for i in range(n):
            print(query_set[i])
            if query_set[i].binEntry is not None:
                bin_entries.append(query_set[i].binEntry)
            bin_edges.append(query_set[i].binEdge)
        print(bin_entries, bin_edges)

        hist = (numpy.array(bin_entries), numpy.array(bin_edges))

However, the output in the terminal is this:

(venv) C:\Users\Steve\uni-final-project>python manage.py test     
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
<QuerySet []>
0
[] []
.Hello Steve!
.
----------------------------------------------------------------------
Ran 2 tests in 0.016s

OK
Destroying test database for alias 'default'...

I have tried to figure out why the MySQL database I built isn't being used. I read up that Django creates a Test 'dummy database' to use on a test and then tears it down after but am I missing something really obvious?

I don't think it is a connection issue as I have pip installed mysqlclient. And I have tried to use the https://docs.djangoproject.com/en/3.2/topics/db/queries/#creating-objects but I still get the same result.

I have read the documentation but I am struggling with certain aspects of it as I am new to software development and this course is quite a steep learning curve.

I checked to see if this question wasn't asked before but I couldn't see any answers to it. Apologies in advance if it has been answered somewhere.

Any help in the right direction or a solution is much appreciated.

Thanks.


Solution

  • First of all you should read something related to testing not django's creating-objects. When you want to test your code usually there are some approaches regarding database. In official django documentation its shows how to deal with database instances. Simply when you start your test it creates a test database and runs your testcases. For that you can use a setUp function add this and try again please:

    class TestArrivalProbabilities(TestCase):
        def setUp(self):
            ....
            ArrivalProbabilities.objects.create(...)
            ArrivalProbabilities.objects.create(...)
            ....
    

    after this part you no longer will see empty queryset. You can also use fixtures