Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8274447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:44:05+00:00 2026-06-08T07:44:05+00:00

What I want to do I’m trying to write unit test to check that

  • 0

What I want to do

I’m trying to write unit test to check that my regexp is constructed properly:

# mediamanager/models.py

import re

from django.conf import settings

filetypes_re = {}
for key, exts in settings.MM_FILETYPES.items():
    filetypes_re[key] = re.compile(r'({})'.format('|'.join(exts)))

NOTE: Actually, I’m not sure why I’m writing this unittest at all, since this piece of code is more than straighforward… But that’s not the point.

As you can see, the final regexp depends on variable settings.MM_FILETYPES which can be set by user. I need to test specific input and for this case, Django provides decorator @override_settings which temporary overrides settings values:

# mediamanager/tests.py

import unittest
from django.test.utils import override_settings
from mediamanager.models import (filetypes_re, …)  # import everything we want to test

class ModelsTestCase(unittest.TestCase):
    @override_settings(MM_FILETYPES={'image': ['jpg', 'png', 'gif'],
                                     'document': ['pdf', 'txt'],
                                     'audio': ['mp3', 'wav']})
    def test_filetype_re(self):
        filetypes_re_exp = {'image': '(jpg|png|gif)',
                            'document': '(pdf|txt)',
                            'audio': '(mp3|wav)'}

        for key, value in filetypes_re_exp.items():
            self.assertEqual(value, filetypes_re[key].pattern)

This test unfortunatelly don’t pass. Module mediamanager.models is loaded before the settings are overriden and therefore the filetypes_re is compiled using old settings. I need to reload it (somehow) to new settings take effect.

Problem

I altered unittest in this way:

@override_settings(MM_FILETYPES={'image': ['jpg', 'png', 'gif'],
                                 'document': ['pdf', 'txt'],
                                 'audio': ['mp3', 'wav']})
def test_filetype_re(self):
    import mediamanager.models  # obtaining module object from sys.modules have the same result
    reload(mediamanager.models)
    filetypes_re = mediamanager.models.filetypes_re
    filetypes_re_exp = {'image': '(jpg|png|gif)',
                        'document': '(pdf|txt)',
                        'audio': '(mp3|wav)'}

    for key, value in filetypes_re_exp.items():
        self.assertEqual(value, filetypes_re[key].pattern)

And test passed. But probably because I imported other objects from mediamanager.models module, other tests in this test case failed. Not all of them, only two (which is strange). EDIT: It’s not strange at all. Only fails tests which runs after test_filetyes_re and reload() call.

Questions

How to ‘reload’ module mediamanager.models in a way that:

  1. filetypes_re are evaluated with new settings?
  2. All other objects imported from same model remains unaffected?

Should I rewrite a piece of code only because it became unstable after reload (I mean other those objects from mediamanager.models which don’t pass test after reload)? I’ve read some articles about reloading modules that usually it’s not a good idea.

Is there a better way to define module level objects, like this regexp, to make testing easier?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T07:44:06+00:00Added an answer on June 8, 2026 at 7:44 am

    there are a couple of ways. off the top of my head:

    # mediamanager/models.py
    
    import re
    
    from django.conf import settings
    
    def get_filetypes_re(mm_filetypes=settings.MM_FILETYPES):
        filetypes_re = {}
        for key, exts in settings.MM_FILETYPES.items():
        filetypes_re[key] = re.compile(r'({})'.format('|'.join(exts)))
        return filetypes_re 
    

    and your test:

    MM_FILETYPES={'image': ['jpg', 'png', 'gif'],
                            'document': ['pdf', 'txt'],
                            'audio': ['mp3', 'wav']})
    
    def test_filetype_re(self):
        filetypes_re = mediamanager.models.get_filetypes_re(mm_filetypes=MM_FILETYPES)
        filetypes_re_exp = {'image': '(jpg|png|gif)',
                            'document': '(pdf|txt)',
                            'audio': '(mp3|wav)'}
    
        for key, value in filetypes_re_exp.items():
            self.assertEqual(value, filetypes_re[key].pattern)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Want to find one and modify that to read, write file on mongoDB gridfs
Want to run javascript function from parent window in child window Example I have
Want to open firefox from terminal at linux with firebug enabled // Terminal $
want to make a check in my installer before starting installation if any other
want to create a temporary table that has an auto_increment field plus a field
Want to verify that my understanding of how this works. Have a C++ Class
want to share a link from my application in windows phone 7. Any one
Want to pause the multiple views from updating when Pause button is pressed In
I have a French site that I want to parse, but am running into
Want to play a file that on path like this /var/spool/sound/dog.wav present on server.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.