<html>
  <head>

    <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
  </head>
  <body text="#000000" bgcolor="#FFFFFF">
    An additional question to those below is how request is associated
    with my response. My function does not take a request object so I am
    unclear on how my mock request object is associated with my response
    object 'r'. Thank you in advance.<br>
    <div class="moz-forward-container">Christine<br>
      <br>
      -------- Original Message --------
      <table class="moz-email-headers-table" cellpadding="0"
        cellspacing="0" border="0">
        <tbody>
          <tr>
            <th align="RIGHT" nowrap="nowrap" valign="BASELINE">Subject:
            </th>
            <td>mock object not being used?</td>
          </tr>
          <tr>
            <th align="RIGHT" nowrap="nowrap" valign="BASELINE">Date: </th>
            <td>Wed, 18 Sep 2013 19:58:29 -0700</td>
          </tr>
          <tr>
            <th align="RIGHT" nowrap="nowrap" valign="BASELINE">From: </th>
            <td>Christine Jones <a class="moz-txt-link-rfc2396E" href="mailto:cjjones@pixar.com">&lt;cjjones@pixar.com&gt;</a></td>
          </tr>
          <tr>
            <th align="RIGHT" nowrap="nowrap" valign="BASELINE">To: </th>
            <td><a class="moz-txt-link-abbreviated" href="mailto:testing-in-python@lists.idyll.org">testing-in-python@lists.idyll.org</a></td>
          </tr>
        </tbody>
      </table>
      <br>
      <br>
      <pre>Thank you for responding Michael and Marius to my initial question. I 
tried both methods below Michael but it seems my mock object is not 
being used in a either function. Perhaps my assumptions  of how mock 
should work are incorrect. Looking at the first test (updated with your 
fix) and the function in my view, I assume that I can replace the way 
'players' is set in my function with the 'players' value in my test. 
Then when I set 'r' in my test it calls the signups function and runs 
through the function using my mock object. However, my assert statement 
fails. What am I missing or misunderstanding. Thanks for all the help.

Test for if branch:
======================

# READ - GET signups - 200 (assumes &gt; 0 signups)
    def test_signups_get(self):
      today = datetime.datetime.now()
      from lunchlottoweb import models
      with patch.object(models, "LottoPlayer") as MockLottoPlayer:
         players = [NonCallableMock(employeelogin='cjjones', employeedept='systems', signupdate=today, lottowinner=False),
                    NonCallableMock(employeelogin='dlovrn', employeedept='systems', signupdate=today, lottowinner=False)]
         MockLottoPlayer.all = players
         request = MagicMock(name='signups_multiple_exist')
         r = self.app.get('/signups')
         assert 'cjjones' in r.data

My function:
======================
# admin page: today's signups
@app.route('/signups', methods=['GET'])
def signups():
  today = date.today()
  players = LottoPlayer.query.filter(cast(LottoPlayer.signupdate, Date) == today).distinct(LottoPlayer.employeelogin).all()
  player_count = players.__len__()
  if player_count &gt; 1:
      return render_template("signups_today.html", signups=players)
  else:
      return render_template("signups_today.html")




On 09/16/2013 12:20 PM, <a class="moz-txt-link-abbreviated" href="mailto:testing-in-python-request@lists.idyll.org">testing-in-python-request@lists.idyll.org</a> wrote:
&gt; Send testing-in-python mailing list submissions to
&gt;         <a class="moz-txt-link-abbreviated" href="mailto:testing-in-python@lists.idyll.org">testing-in-python@lists.idyll.org</a>
&gt;
&gt; To subscribe or unsubscribe via the World Wide Web, visit
&gt;         <a class="moz-txt-link-freetext" href="http://lists.idyll.org/listinfo/testing-in-python">http://lists.idyll.org/listinfo/testing-in-python</a>
&gt; or, via email, send a message with subject or body 'help' to
&gt;         <a class="moz-txt-link-abbreviated" href="mailto:testing-in-python-request@lists.idyll.org">testing-in-python-request@lists.idyll.org</a>
&gt;
&gt; You can reach the person managing the list at
&gt;         <a class="moz-txt-link-abbreviated" href="mailto:testing-in-python-owner@lists.idyll.org">testing-in-python-owner@lists.idyll.org</a>
&gt;
&gt; When replying, please edit your Subject line so it is more specific
&gt; than "Re: Contents of testing-in-python digest..."
&gt;
&gt;
&gt; Today's Topics:
&gt;
&gt;     1. Re: unittest/mock question (Michael Foord)
&gt;     2. Re: unittest/mock question (Marius Gedminas)
&gt;
&gt;
&gt; ----------------------------------------------------------------------
&gt;
&gt; Message: 1
&gt; Date: Mon, 16 Sep 2013 11:28:18 +0100
&gt; From: Michael Foord <a class="moz-txt-link-rfc2396E" href="mailto:michael@voidspace.org.uk">&lt;michael@voidspace.org.uk&gt;</a>
&gt; Subject: Re: [TIP] unittest/mock question
&gt; To: Christine Jones <a class="moz-txt-link-rfc2396E" href="mailto:cjjones@pixar.com">&lt;cjjones@pixar.com&gt;</a>
&gt; Cc: <a class="moz-txt-link-abbreviated" href="mailto:testing-in-python@lists.idyll.org">testing-in-python@lists.idyll.org</a>
&gt; Message-ID: <a class="moz-txt-link-rfc2396E" href="mailto:E6D76F0F-A6F0-4097-970F-7A01B3C8B3D2@voidspace.org.uk">&lt;E6D76F0F-A6F0-4097-970F-7A01B3C8B3D2@voidspace.org.uk&gt;</a>
&gt; Content-Type: text/plain; charset=us-ascii
&gt;
&gt;
&gt; On 14 Sep 2013, at 00:26, Christine Jones <a class="moz-txt-link-rfc2396E" href="mailto:cjjones@pixar.com">&lt;cjjones@pixar.com&gt;</a> wrote:
&gt;
&gt;&gt; Hello,
&gt;&gt; I've just started using Python unittest and mock. I've gone through the three Michael Foord tutorials, and read the docs, however, I cannot figure my way around this issue.
&gt;&gt;
&gt;&gt; I am using Python 2.6 and mock 1.0.1.
&gt;&gt;
&gt;&gt; I have the following function and tests. In brief, I am not able to test both branches of my if/else statement unless I comment out one or the other tests. Specifically, if I run both tests below only the last one passes. However, if I comment out the last one, the first one runs and passes.
&gt;&gt;
&gt;&gt; I was under the impression that patch automatically unmonkey patches when the context or function is left but it seems like maybe this is not happening. Or I am missing some other key concept. Any help would be greatly appreciated. Thank you in advance!
&gt;&gt; Christine
&gt;&gt;
&gt;&gt; My function:
&gt;&gt; ======================
&gt;&gt; # admin page: today's signups
&gt;&gt; @app.route('/signups', methods=['GET'])
&gt;&gt; def signups():
&gt;&gt;    today = date.today()
&gt;&gt;    players = LottoPlayer.query.filter(cast(LottoPlayer.signupdate, Date) ==        today).distinct(LottoPlayer.employeelogin).all()
&gt;&gt;    player_count = players.__len__()
&gt;&gt;    if player_count &gt; 1:
&gt;&gt;        return render_template("signups_today.html", signups=players)
&gt;&gt;    else:
&gt;&gt;        return render_template("signups_today.html")
&gt;&gt;
&gt;&gt; Test for if branch:
&gt;&gt; ======================
&gt;&gt; # READ - GET signups - 200 (assumes &gt; 0 signups)
&gt;&gt;    def test_signups_get(self):
&gt;&gt;        today = datetime.datetime.now()
&gt;&gt;        from lunchlottoweb import models
&gt;&gt;        with patch.object(models, "LottoPlayer"):
&gt;&gt;           players = [NonCallableMock(employeelogin='cjjones', employeedept='systems', signupdate=today, lottowinner=False),
&gt;&gt;                      NonCallableMock(employeelogin='dlovirin', employeedept='systems', signupdate=today, lottowinner=False)]
&gt;&gt;           LottoPlayer.all = players
&gt;&gt;           request = MagicMock(name='signups_multiple_exist')
&gt;&gt;           r = self.app.get('/signups')
&gt;&gt;           assert 'cjjones' in r.data
&gt;&gt;
&gt;&gt; Test for else branch:
&gt;&gt; ======================
&gt;&gt;    # READ - GET signups when there are 0 signups
&gt;&gt;    def test_signups_get_when_none(self):
&gt;&gt;        now = datetime.datetime.now()
&gt;&gt;        yesterday = now - datetime.timedelta(days = 1)
&gt;&gt;        from lunchlottoweb import models
&gt;&gt;        with patch.object(models, 'LottoPlayer'):
&gt;&gt;            players = [NonCallableMock(employeelogin='jshmo', employeedept='systems', signupdate=yesterday, lottowinner=False),
&gt;&gt;                      NonCallableMock(employeelogin='foo', employeedept='systems', signupdate=yesterday, lottowinner=False)]
&gt;&gt;            LottoPlayer.all = players
&gt;&gt;
&gt;
&gt; You're patching models.LottoPlayer - but directly modifying the LottoPlayer object (which is *not* your patched version).
&gt;
&gt; Change LottoPlayer.all in your code above to models.LottoPlayer, *or* do the following, to fix your problem:
&gt;
&gt;         with patch.object(models, 'LottoPlayer') as MockLottoPlayer:
&gt;                 ...
&gt;                 MockLottoPlayer.all = players
&gt;
&gt; Michael
&gt;
&gt;&gt;            request = MagicMock(name='signupsrequest')
&gt;&gt;            r = self.app.get('/signups')
&gt;&gt;            assert 'No one has signed up yet' in r.data
&gt;&gt;
&gt;&gt; _______________________________________________
&gt;&gt; testing-in-python mailing list
&gt;&gt; <a class="moz-txt-link-abbreviated" href="mailto:testing-in-python@lists.idyll.org">testing-in-python@lists.idyll.org</a>
&gt;&gt; <a class="moz-txt-link-freetext" href="http://lists.idyll.org/listinfo/testing-in-python">http://lists.idyll.org/listinfo/testing-in-python</a>
&gt;
&gt; --
&gt; <a class="moz-txt-link-freetext" href="http://www.voidspace.org.uk/">http://www.voidspace.org.uk/</a>
&gt;
&gt;
&gt; May you do good and not evil
&gt; May you find forgiveness for yourself and forgive others
&gt; May you share freely, never taking more than you give.
&gt; -- the sqlite blessing
&gt; <a class="moz-txt-link-freetext" href="http://www.sqlite.org/different.html">http://www.sqlite.org/different.html</a>
&gt;
&gt;
&gt;
&gt;
&gt;
&gt;
&gt;
&gt;
&gt; ------------------------------
&gt;
&gt; Message: 2
&gt; Date: Mon, 16 Sep 2013 13:58:03 +0300
&gt; From: Marius Gedminas <a class="moz-txt-link-rfc2396E" href="mailto:marius@gedmin.as">&lt;marius@gedmin.as&gt;</a>
&gt; Subject: Re: [TIP] unittest/mock question
&gt; To: <a class="moz-txt-link-abbreviated" href="mailto:testing-in-python@lists.idyll.org">testing-in-python@lists.idyll.org</a>
&gt; Message-ID: &lt;20130916105803.GB11184@platonas&gt;
&gt; Content-Type: text/plain; charset="us-ascii"
&gt;
&gt; On Fri, Sep 13, 2013 at 04:26:25PM -0700, Christine Jones wrote:
&gt;&gt; I've just started using Python unittest and mock. I've gone through
&gt;&gt; the three Michael Foord tutorials, and read the docs, however, I
&gt;&gt; cannot figure my way around this issue.
&gt;&gt;
&gt;&gt; I am using Python 2.6 and mock 1.0.1.
&gt;&gt;
&gt;&gt; I have the following function and tests. In brief, I am not able to
&gt;&gt; test both branches of my if/else statement unless I comment out one or
&gt;&gt; the other tests. Specifically, if I run both tests below only the last
&gt;&gt; one passes. However, if I comment out the last one, the first one runs
&gt;&gt; and passes.
&gt;&gt;
&gt;&gt; I was under the impression that patch automatically unmonkey patches
&gt;&gt; when the context or function is left but it seems like maybe this is
&gt;&gt; not happening. Or I am missing some other key concept. Any help would
&gt;&gt; be greatly appreciated. Thank you in advance!
&gt;&gt;
&gt;&gt; Christine
&gt;&gt;
&gt;&gt; My function:
&gt;&gt; ======================
&gt;&gt; # admin page: today's signups
&gt;&gt; @app.route('/signups', methods=['GET'])
&gt;&gt; def signups():
&gt;&gt;     today = date.today()
&gt;&gt;     players = LottoPlayer.query.filter(cast(LottoPlayer.signupdate, Date) ==        today).distinct(LottoPlayer.employeelogin).all()
&gt; What ORM is this?  It may matter.
&gt;
&gt;&gt;     player_count = players.__len__()
&gt; That's an unusual spelling of
&gt;
&gt;       player_count = len(players)
&gt;
&gt;&gt;     if player_count &gt; 1:
&gt;&gt;         return render_template("signups_today.html", signups=players)
&gt;&gt;     else:
&gt;&gt;         return render_template("signups_today.html")
&gt;&gt;
&gt;&gt; Test for if branch:
&gt;&gt; ======================
&gt;&gt; # READ - GET signups - 200 (assumes &gt; 0 signups)
&gt;&gt;     def test_signups_get(self):
&gt;&gt;         today = datetime.datetime.now()
&gt;&gt;         from lunchlottoweb import models
&gt;&gt;         with patch.object(models, "LottoPlayer"):
&gt; I think you're supposed to do
&gt;
&gt;           with patch.object(models, "LottoPlayer") as LottoPlayer:
&gt;
&gt; here.  Not 100% sure.
&gt;
&gt;&gt;            players = [NonCallableMock(employeelogin='cjjones', employeedept='systems', signupdate=today, lottowinner=False),
&gt;&gt;                       NonCallableMock(employeelogin='dlovirin', employeedept='systems', signupdate=today, lottowinner=False)]
&gt;&gt;            LottoPlayer.all = players
&gt; Because if you don't, then you're modifying some global object, but I
&gt; don't see where it came from.
&gt;
&gt;&gt;            request = MagicMock(name='signups_multiple_exist')
&gt;&gt;            r = self.app.get('/signups')
&gt;&gt;            assert 'cjjones' in r.data
&gt;&gt;
&gt;&gt; Test for else branch:
&gt;&gt; ======================
&gt;&gt;     # READ - GET signups when there are 0 signups
&gt;&gt;     def test_signups_get_when_none(self):
&gt;&gt;         now = datetime.datetime.now()
&gt;&gt;         yesterday = now - datetime.timedelta(days = 1)
&gt;&gt;         from lunchlottoweb import models
&gt;&gt;         with patch.object(models, 'LottoPlayer'):
&gt; Same here
&gt;
&gt;&gt;             players = [NonCallableMock(employeelogin='jshmo', employeedept='systems', signupdate=yesterday, lottowinner=False),
&gt;&gt;                       NonCallableMock(employeelogin='foo', employeedept='systems', signupdate=yesterday, lottowinner=False)]
&gt;&gt;             LottoPlayer.all = players
&gt; But then again at the moment you're modifying the same global object
&gt; from both tests, so I don't quite understand what is happening...
&gt;
&gt; Perhaps the ORM layer caches something?
&gt;
&gt;&gt;             request = MagicMock(name='signupsrequest')
&gt;&gt;             r = self.app.get('/signups')
&gt;&gt;             assert 'No one has signed up yet' in r.data
&gt; Marius Gedminas


-- 
Christine
P I X A R

</pre>
      <br>
    </div>
    <br>
  </body>
</html>