Re: [Zope] Help writing test where bobobase_modification_time changes

2008-04-11 Thread Peter Bengtsson
Thanks for the tips Chris and Dieter, I actually solved it in a very simple way:


from OFS.Image import File
from time import time
class MockFile(File):
 This file works the same as a normal File expect that
in manage_edit() the internal modification time is set immediately.
Normally Zope objects get their internal modification set when
the transaction manager commits.


def manage_edit(self, title, content_type, precondition='',
filedata=None, REQUEST=None):
# here's the mock hack, we set the _p_mtime NOW instead of letting
# the transaction manager do it at the end.
self._p_mtime = time()
return File.manage_edit(self, title, content_type,
precondition=precondition,
filedata=filedata, REQUEST=REQUEST)

This worked beautifully in the integration test. I'm happy.

On 10/04/2008, Chris Withers [EMAIL PROTECTED] wrote:
 Peter Bengtsson wrote:

  How can I solve this? Make one big transaction two without having to
  use transaction.get().commit() which I don't think I can use in unit
  tests as per Dieter's advice.
 

  I'd suggest monkeypatching bobobase_modification_time on the objects you're
 testing with. You're testing your cache code, not the implementation of
 those objects, so replacing them with (partially) dummy objects is great for
 avoiding this kind of problem and is a pattern I've used frequently...

  cheers,

  Chris

  --
  Simplistix - Content Management, Zope  Python Consulting
- http://www.simplistix.co.uk



-- 
Peter Bengtsson,
work www.fry-it.com
home www.peterbe.com
hobby www.issuetrackerproduct.com
___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
 http://mail.zope.org/mailman/listinfo/zope-announce
 http://mail.zope.org/mailman/listinfo/zope-dev )


Re: [Zope] Help writing test where bobobase_modification_time changes

2008-04-10 Thread Chris Withers

Peter Bengtsson wrote:

How can I solve this? Make one big transaction two without having to
use transaction.get().commit() which I don't think I can use in unit
tests as per Dieter's advice.


I'd suggest monkeypatching bobobase_modification_time on the objects 
you're testing with. You're testing your cache code, not the 
implementation of those objects, so replacing them with (partially) 
dummy objects is great for avoiding this kind of problem and is a 
pattern I've used frequently...


cheers,

Chris

--
Simplistix - Content Management, Zope  Python Consulting
   - http://www.simplistix.co.uk
___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
http://mail.zope.org/mailman/listinfo/zope-announce

http://mail.zope.org/mailman/listinfo/zope-dev )


Re: [Zope] Help writing test where bobobase_modification_time changes

2008-04-09 Thread Dieter Maurer
Peter Bengtsson wrote at 2008-4-8 19:08 +0100:
 ...
The reason it fails on the last line is that the time difference
between the first time and the second time is nil. Adding a
time.sleep(1) won't change anything because the
bobobase_modification_time() doesn't change.

How can I solve this?

Not easy.

bobobase_modification_time() is in fact a DateTime representation
of _p_serial. And _p_serial is the transaction id (also a timestamp)
that has committed the object state belonging to this object incarnation.

Unfortunately, you must not commit a transaction in a test suite
(as this may cause interference with other tests).

There is a small chance that a savepoint changed _p_serial --
but the chance is only small...

You might be able to write _p_serial yourself and thereby
trick bobobase_modification_time().



-- 
Dieter
___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
 http://mail.zope.org/mailman/listinfo/zope-announce
 http://mail.zope.org/mailman/listinfo/zope-dev )


[Zope] Help writing test where bobobase_modification_time changes

2008-04-08 Thread Peter Bengtsson
I'll try to explain as briefly as possible.
I've got a working cache pattern that keeps a cached version of a Zope
file's content and only when it changes (by 
bobobase_modification_time) the cache is invalidated and recreated.
Here's the gist of my test

class Test(ZopeTestCase.FunctionalTestCase):

def test_recreateCache(self):
  bla 
 self.folder.manage_addFile('foo.js','FOO data')
 url = self.folder.mytool.cacheFile('foo.js') # pseudo code this time
 path = urlparse(url)[2]
 response = self.publish(path)
 self.assert... tests on response...

 _file = getattr(self.folder, 'foo.js')
 _file.manage_edit(_file.title, _file.content_type,
  filedata='new data')

 newurl = self.folder.mytool.cacheFile('foo.js')
 assert newurl != url, url hasn't changed :(

The reason it fails on the last line is that the time difference
between the first time and the second time is nil. Adding a
time.sleep(1) won't change anything because the
bobobase_modification_time() doesn't change.

How can I solve this? Make one big transaction two without having to
use transaction.get().commit() which I don't think I can use in unit
tests as per Dieter's advice.

Grateful for help.
Peter


-- 
Peter Bengtsson,
work www.fry-it.com
home www.peterbe.com
hobby www.issuetrackerproduct.com
___
Zope maillist  -  Zope@zope.org
http://mail.zope.org/mailman/listinfo/zope
**   No cross posts or HTML encoding!  **
(Related lists - 
 http://mail.zope.org/mailman/listinfo/zope-announce
 http://mail.zope.org/mailman/listinfo/zope-dev )