Trouble with intercepting/playing back submodule (sqlalchemy.exc)

Bug #1315129 reported by Marc Abramowitz
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
CaptureMock
Fix Released
Undecided
Unassigned

Bug Description

This might be a bug or it might be my misunderstanding of how things are supposed to work.

I have this program and CaptureMock config:

```
$ cat foobar.py
import capturemock

capturemock.process_startup()

def foo():
    import sqlalchemy
    print(sqlalchemy.__version__)
    from sqlalchemy.exc import OperationalError
    print(OperationalError)

foo()

 $cat config
[python]
intercepts = sqlalchemy,sqlalchemy.exc
```

Then I attempt to record/replay to a file.

```
$ cat /dev/null > foo.mock

$ ls -l foo.mock
-rw-r--r-- 1 marca staff 0 May 1 12:49 foo.mock

$ CAPTUREMOCK_PROCESS_START=config CAPTUREMOCK_MODE=2 CAPTUREMOCK_RECORD_FILE=foo.mock CAPTUREMOCK_REPLAY_FILE=foo.mock python foobar.py
0.8.6
<class 'sqlalchemy.exc.OperationalError'>

$ cat foo.mock
<-PYT:import sqlalchemy
<-PYT:sqlalchemy.__version__
->RET:'0.8.6'
```

So here it recorded that I imported sqlalchemy and called sqlalchemy.__version__ but it didn't record my import of sqlalchemy.exc or usage of OperationalError... That doesn't seem right to me. Then when I run it again to replay what's in the file, it complains because it can't find the submodule sqlalchemy.exc:

```
$ CAPTUREMOCK_PROCESS_START=config CAPTUREMOCK_MODE=2 CAPTUREMOCK_RECORD_FILE=foo.mock CAPTUREMOCK_REPLAY_FILE=foo.mock python foobar.py
0.8.6
Traceback (most recent call last):
  File "foobar.py", line 13, in <module>
    foo()
  File "foobar.py", line 9, in foo
    from sqlalchemy.exc import OperationalError
ImportError: No module named exc
```

Is this a bug or am I misnderstanding how things are supposed to work?

Revision history for this message
Geoff Bache (geoff.bache) wrote :

Yes, this is a bug alright. It's curious because I've seen similar things work with other submodules (there are some in the test suite) so I don't yet know what's different about sqlalchemy in this respect.

Revision history for this message
Geoff Bache (geoff.bache) wrote :

OK, I've identified two things that cause problems here:

1) The double-importing, once via the main package and once via the submodule. For some reason this confuses things.

2) Printing an intercepted class (rather than object). This doesn't seem to call any hooks, and our methodology with class objects is not to record them until something happens with them, which usually looks nicer... but in this case nothing ever happens with it. I don't know if you really need to do this or if it was just illustrating the point.

Don't know if it helps to workaround, but here's a closely related foobar.py that works just fine:

```
$ cat foobar.py
import capturemock

capturemock.process_startup()

def foo():
    import sqlalchemy
    print(sqlalchemy.__version__)
    print(sqlalchemy.exc.OperationalError("one", "two", "three"))

foo()
```

Will take another look at this either on Thursday or next week.

Revision history for this message
Geoff Bache (geoff.bache) wrote :

I checked in a fix for the double-import issue now.

I haven't really thought of a good way to fix the class-printout issue yet, though I don't really know if it's important to you or was just illustrating the other issue.

Revision history for this message
Marc Abramowitz (msabramo) wrote : Re: [Bug 1315129] Re: Trouble with intercepting/playing back submodule (sqlalchemy.exc)

Thanks! Yeah, I don't actually need to print out the class. That was just
for my little test program.

I hope to try out CaptureMock again soon with the real code I was using.
I'm trying to mock out a SQL Server database so that I can run the tests
quickly and without needing the database.

On Thu, May 8, 2014 at 12:27 PM, Geoff Bache <email address hidden> wrote:

> I checked in a fix for the double-import issue now.
>
> I haven't really thought of a good way to fix the class-printout issue
> yet, though I don't really know if it's important to you or was just
> illustrating the other issue.
>
> --
> You received this bug notification because you are subscribed to the bug
> report.
> https://bugs.launchpad.net/bugs/1315129
>
> Title:
> Trouble with intercepting/playing back submodule (sqlalchemy.exc)
>
> Status in CaptureMock:
> New
>
> Bug description:
> This might be a bug or it might be my misunderstanding of how things
> are supposed to work.
>
> I have this program and CaptureMock config:
>
> ```
> $ cat foobar.py
> import capturemock
>
> capturemock.process_startup()
>
>
> def foo():
> import sqlalchemy
> print(sqlalchemy.__version__)
> from sqlalchemy.exc import OperationalError
> print(OperationalError)
>
>
> foo()
>
> $cat config
> [python]
> intercepts = sqlalchemy,sqlalchemy.exc
> ```
>
> Then I attempt to record/replay to a file.
>
> ```
> $ cat /dev/null > foo.mock
>
> $ ls -l foo.mock
> -rw-r--r-- 1 marca staff 0 May 1 12:49 foo.mock
>
> $ CAPTUREMOCK_PROCESS_START=config CAPTUREMOCK_MODE=2
> CAPTUREMOCK_RECORD_FILE=foo.mock CAPTUREMOCK_REPLAY_FILE=foo.mock python
> foobar.py
> 0.8.6
> <class 'sqlalchemy.exc.OperationalError'>
>
> $ cat foo.mock
> <-PYT:import sqlalchemy
> <-PYT:sqlalchemy.__version__
> ->RET:'0.8.6'
> ```
>
> So here it recorded that I imported sqlalchemy and called
> sqlalchemy.__version__ but it didn't record my import of
> sqlalchemy.exc or usage of OperationalError... That doesn't seem right
> to me. Then when I run it again to replay what's in the file, it
> complains because it can't find the submodule sqlalchemy.exc:
>
> ```
> $ CAPTUREMOCK_PROCESS_START=config CAPTUREMOCK_MODE=2
> CAPTUREMOCK_RECORD_FILE=foo.mock CAPTUREMOCK_REPLAY_FILE=foo.mock python
> foobar.py
> 0.8.6
> Traceback (most recent call last):
> File "foobar.py", line 13, in <module>
> foo()
> File "foobar.py", line 9, in foo
> from sqlalchemy.exc import OperationalError
> ImportError: No module named exc
> ```
>
> Is this a bug or am I misnderstanding how things are supposed to work?
>
> To manage notifications about this bug go to:
> https://bugs.launchpad.net/capturemock/+bug/1315129/+subscriptions
>

Revision history for this message
Geoff Bache (geoff.bache) wrote :

OK, I'll mark this fixed then and worry about class printing if anyone ever needs to. It involves handling calls to the metaclass __repr__ method and I can't see an easy way to do that.

Changed in capturemock:
status: New → Fix Committed
Changed in capturemock:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.