Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #85377

Re: How to Mock a mongodb

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <prvs=475971540=jeanmichel@sequans.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status UNSURE 0.289
X-Spam-Level **
X-Spam-Evidence '*H*': 0.44; '*S*': 0.02; 'subject:How': 0.10; 'def': 0.12; '[2].': 0.16; 'client:': 0.16; 'direction?': 0.16; 'objects.': 0.16; 'thanks,': 0.17; 'trying': 0.19; 'appears': 0.22; 'import': 0.22; 'header:In-Reply-To:1': 0.27; 'to:2**1': 0.27; 'point': 0.28; 'patch': 0.29; 'code': 0.31; 'class': 0.32; '-----': 0.33; 'problem.': 0.35; 'test': 0.35; 'but': 0.35; 'skip:s 60': 0.36; 'wrong': 0.37; 'email addr:python.org': 0.37; 'thank': 0.38; 'to:addr:python-list': 0.38; 'subject:': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'chain': 0.60; 'entire': 0.61; 'you.': 0.62; "you've": 0.63; 'information': 0.63; 'real': 0.63; 'received:194': 0.64; 'email name:python-list': 0.65; 'notice:': 0.67; 'person,': 0.68; 'privileged.': 0.69; 'disclose': 0.74; '2015': 0.84; 'complexity': 0.84; 'mock': 0.84; 'medium.': 0.91; 'trouble.': 0.91
X-IronPort-AV E=Sophos;i="5.09,544,1418079600"; d="scan'208";a="3948063"
Date Mon, 9 Feb 2015 14:56:35 +0100 (CET)
From Jean-Michel Pichavant <jeanmichel@sequans.com>
To Xavier Pegenaute <xpegenaute@poblenousensefils.net>, python-list@python.org
In-Reply-To <54D68D06.70807@poblenousensefils.net>
Subject Re: How to Mock a mongodb
MIME-Version 1.0
X-Mailer Zimbra 7.2.7_GA_2942 (ZimbraWebClient - GC37 (Linux)/7.2.7_GA_2942)
Content-Type text/plain; charset="utf-8"
Content-Transfer-Encoding base64
X-Mailman-Approved-At Mon, 09 Feb 2015 15:31:07 +0100
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.18560.1423492268.18130.python-list@python.org> (permalink)
Lines 30
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1423492268 news.xs4all.nl 2876 [2001:888:2000:d::a6]:39902
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:85377

Show key headers only | View raw


----- Original Message -----
> From: "Xavier Pegenaute" <xpegenaute@poblenousensefils.net>
> To: python-list@python.org
> Sent: Saturday, 7 February, 2015 11:09:10 PM
> Subject: How to Mock a mongodb
> 
> Dear,
> 
> I am trying to mock the use of a mongo db and I am having some
> trouble.
> Appears that I am not able to return a desired value from
> mongo.find().count(). I made a proof of concept to try to reduce
> complexity of the real problem.
> 
> You can find the code which is going to be tested in [1], and the
> code
> of the test case in [2].
> 
> Do you know exactly wat's wrong with it?, or may you point me to some
> direction?
> 
> Thanks,
> Xavi

You've mocked the pymongo.cursor but since you've mocked the entire mongo client, this mocked client will never return a pymongo.cursor, it will return only mock objects.

What you can do is mock the entire chain call of you mocked client:


from mock import patch
from mmongo import MongoHelper

class TestMongoHelper(object):

  @patch("mmongo.pymongo")
  def test_count_data(self, fake_mongo_client):
    mhelper = MongoHelper()

    # mock self.db[MongoHelper.db_name][MongoHelper.coll_name].find().count()
    fake_mongo_client.__getitem__.return_value.__getitem__.return_value.find.return_value.count.return_value = 5


JM


-- IMPORTANT NOTICE: 

The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: How to Mock a mongodb Jean-Michel Pichavant <jeanmichel@sequans.com> - 2015-02-09 14:56 +0100

csiph-web