1

I am in the midst of writing unit tests for an application I am working on. My tests work fine on my 2012 Macbook pro but not my 2015 iMac. Both are running the latest OSX and the django version is version 1.9.1 with custom modifications that can be seen here: https://github.com/shuttl-io/django. The modifications where to the template loading mechanisms.

Before I move on, my tests are organized thusly:

app/
    tests/
        test_models/
           __init__.py
           test_*.py
        test_views/
            __init__.py
            test_*.py
        test_forms/
            test_*.py
            __init__.py

And in those __init__ files I include all of the test_*.py files.

Anyway, In my tests, I have a few mock classes inside of a test file of test_models. It appears that my 2012 MbP will makemigrations and migrate the test database with the mock classes into the test db. My test file looks like this: http://pastebin.com/m9VKDLhE. How ever on my iMac, the mock classes at the top of my file isn't migrated. So I read up on that issue and decided to add a testing app that was written somewhere. The tests have the same organization and the mock classes moved to models.py. Then in my settings.py file I have this snippet:

if "test" in sys.argv:
    INSTALLED_APPS.append("testing")
    if "makemigrations" in sys.argv:
        ndx = sys.argv.index("test")
        sys.argv.pop(ndx)
        pass
    pass

Based around the snippet posted in here: https://code.djangoproject.com/ticket/7835#trac-change-3-1226837305000000

Now this works and puts the mock files into the DB and gets rid of the OperationalError. However this adds more complex errors that I don't know about.

Before I talk about the errors, Here is the class that is causing the errors: http://pastebin.com/n3906RC8. The publishable class it inherits from is not a model and is only a Abstract Base Class with a few methods that need to be implemented.

Now the errors I got initially was this error:

======================================================================
ERROR: test_renderSite (testing.tests.test_webpage.test_models.test_webpage.WebsiteTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/Yoseph/shuttl/testing/tests/test_webpage/test_models/test_webpage.py", line 80, in test_renderSite
    siteMap = self.website.getSiteMap()
  File "/Users/Yoseph/shuttl/Webpage/models.py", line 155, in getSiteMap
    return self.root.render()
  File "/Users/Yoseph/shuttl/Webpage/models.py", line 252, in render
    for i in self.children:
  File "/Users/Yoseph/shuttl/Webpage/models.py", line 240, in children
    for i in Class.objects.filter(parent=self):
  File "/Users/Yoseph/.venv/shuttl/src/django/django/db/models/manager.py", line 122, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/Users/Yoseph/.venv/shuttl/src/django/django/db/models/query.py", line 790, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "/Users/Yoseph/.venv/shuttl/src/django/django/db/models/query.py", line 808, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "/Users/Yoseph/.venv/shuttl/src/django/django/db/models/sql/query.py", line 1243, in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
  File "/Users/Yoseph/.venv/shuttl/src/django/django/db/models/sql/query.py", line 1269, in _add_q
    allow_joins=allow_joins, split_subq=split_subq,
  File "/Users/Yoseph/.venv/shuttl/src/django/django/db/models/sql/query.py", line 1174, in build_filter
    self.check_related_objects(field, value, opts)
  File "/Users/Yoseph/.venv/shuttl/src/django/django/db/models/sql/query.py", line 1071, in check_related_objects
    self.check_query_object_type(value, opts, field)
  File "/Users/Yoseph/.venv/shuttl/src/django/django/db/models/sql/query.py", line 1055, in check_query_object_type
    (value, opts.object_name))
ValueError: Cannot query "root": Must be "Directory" instance.

I printed out the type and it is a Directory instance. I solved this issue by changing for i in Class.objects.filter(parent=self): to for i in Class.objects.filter(parent_id=self.id): This fixed that error but caused a different error that made even less sense:

======================================================================
ERROR: test_renderSite (testing.tests.test_webpage.test_models.test_webpage.WebsiteTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/Yoseph/shuttl/testing/tests/test_webpage/test_models/test_webpage.py", line 90, in test_renderSite
    self.assertEqual(self.website.getSiteMap(), testMap)
  File "/Users/Yoseph/shuttl/Webpage/models.py", line 155, in getSiteMap
    return self.root.render()
  File "/Users/Yoseph/shuttl/Webpage/models.py", line 253, in render
    renderMap["children"].append(i.render())
AttributeError: 'Directory' object has no attribute 'render'

----------------------------------------------------------------------

If you refer to the second pastebin, you'll notice that the Directory class does actually have a render method. I am completely stumped. Do you guys have any guesses? Thanks in advance.

Test File: http://pastebin.com/m9VKDLhE Model Snippet: http://pastebin.com/n3906RC8

I also posted this question in the django users group here: https://groups.google.com/forum/#!topic/django-users/94VMeCGXbZ0

rady
  • 428
  • 5
  • 12
  • As a general observation, I'm not sure it is advisable to modify core Django unless you absolutely cannot achieve what you want without doing so - and things like custom template loading can definitely be done without modifying the core. – solarissmoke May 23 '16 at 04:13
  • And I did make a custom template loader. The issue that led me to modify django core is that I'm loading actual files based on a path in a database. This is also restricted to certain domains. So a file could be uploaded to Domain X with a name hello.txt but django may save it as fileToUpload_xyz123.txt. But the user knows that the file is called hello.txt and wants to view that file when they are on domain X. I modified the Template loading mechanisms to take an argument domain. I tried to add the domain to the template_dirs list but that was hacky and led to even more trouble. – rady May 23 '16 at 11:58
  • Replace the .txt files with .html files because I'm working with templates and not TXT files. The issue my custom fix fixed was to fix the extend tags. – rady May 23 '16 at 12:00

0 Answers0