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

  • Home
  • SEARCH
  • 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 7659407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:19:45+00:00 2026-05-31T13:19:45+00:00

Having a weird problem with Python’s unittest and PyMongo. The test randomly succeeds or

  • 0

Having a weird problem with Python’s unittest and PyMongo. The test randomly succeeds or fails:

import unittest
from pymongo import Connection

from tractor import Tractor




class TestTractor(unittest.TestCase):
    def setUp(self):
        self.tractor = Tractor(1)

        self.mongo = Connection()
        self.db = self.mongo.tractor

        self.db.classes.remove({'name': {'$regex':'^test_'}})

        self.action_class_id = self.db.classes.insert({'name': 'test_action',
                                                       'metaclass': 'action'})
        self.object_class_id = self.db.classes.insert({'name': 'test_object',
                                                       'metaclass': 'object'})


    def tearDown(self):
        self.tractor = None



    def test_create_class(self):
        cid1 = self.tractor.create_action_class('test_create_action_class')
        cid2 = self.tractor.create_object_class('test_create_object_class')

        self.assertNotEqual(cid1, None)
        self.assertNotEqual(cid2, None)

        action_obj = self.db.classes.find_one({'_id': cid1})
        object_obj = self.db.classes.find_one({'_id': cid2})

        self.assertNotEqual(cid1, cid2)
        self.assertEqual(action_obj['_id'], cid1)
        self.assertEqual(object_obj['_id'], cid2)

        self.assertEqual(action_obj['name'], 'test_create_action_class')
        self.assertEqual(object_obj['name'], 'test_create_object_class')

Class being tested:

from pymongo import Connection
from pymongo.objectid import ObjectId



class Tractor(object):
    def __init__(self, uid):
        self.uid = uid
        self.mongo = Connection()
        self.db = self.mongo.tractor


    # Classes

    def create_action_class(self, name):
        return self.db.classes.insert({'name': name,
                                       'attributes': [],
                                       'metaclass': 'action'})

    def create_object_class(self, name):
        return self.db.classes.insert({'name': name,
                                       'attributes': [],
                                       'metaclass': 'object'})

Random behavior:

silver@aregh-6930-lnx ~/projects/traction/tractor $ python -m unittest discover
......ssEssssssssss
======================================================================
ERROR: test_create_class (tests.test_tractor.TestTractor)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/silver/projects/traction/tractor/tests/test_tractor.py", line 64, in test_create_class
    self.assertEqual(action_obj['_id'], cid1)
TypeError: 'NoneType' object is not subscriptable

----------------------------------------------------------------------
Ran 19 tests in 0.023s

FAILED (errors=1, skipped=12)

…

silver@aregh-6930-lnx ~/projects/traction/tractor $ python -m unittest discover
......ss.ssssssssss
----------------------------------------------------------------------
Ran 19 tests in 0.015s

OK (skipped=12)

These two results randomly happen for the same test as I rerun the test without changing anything neither in the class nor in the test.

All of this runs on my machine and I know for sure that while running the test, nobody else tinkers neither with MongoDB nor with the code.

What gives?

  • 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-05-31T13:19:45+00:00Added an answer on May 31, 2026 at 1:19 pm

    I strongly suspect the problem here is that you are not using “safe” mode for your writes.

    By default MongoDB uses “fire and forget” mode. This means that the insert command is sent to the server, but the driver doesn’t check for any server responses.

    When you switch to “safe” mode, the driver will send the insert command and it will then send a second command getLastError. This second command will return when the server has actually committed the write.

    Again, by default you are running in “fire and forget” mode, so there is indeed a potential race condition here. For unit tests you will need to run with “safe” mode on.

    The function signature for insert is defined here. However, you should also be able to make the change at the Connection level so that each connection to the DB uses “safe” mode by default.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a weird problem i'm trying to read a string from a console
I'm having a weird problem. I have this code $topic contains 海賊_(ONE_PIECE) from URI
I'm having the weird problem that after having javascript inject some dom-elements the css-rules
I'm having a weird problem with the following function, which returns a string with
I'm having a weird problem with memset, that was something to do with a
I'm having a weird problem with index organized table. I'm running Oracle 11g standard.
I am having a weird problem with sending data back to my server. This
I am having a weird problem where lots of ^M characters show up in
I'm having a weird problem with avr-gcc. If I do this: int i =
I'm having a really weird problem with my rails app and facebook's open graph

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.