FactoryBoy / factory_boy

A test fixtures replacement for Python
https://factoryboy.readthedocs.io/
MIT License
3.46k stars 387 forks source link

Optionally allow running tests with mongomock #1081

Open jelly opened 2 weeks ago

jelly commented 2 weeks ago

The problem

factory-boy is packaged in Arch Linux and tests run while building the package in this buildchroot. The mongodb tests require a running mongodb server, mongodb is neither packaged nor is it possible to run a service during building.

Proposed solution

I've patched factory-boy to use mongomock, this succesfully ran the tests. Maybe factory-boy can optionally allow running the tests with mongomock when available.

@@ -10,6 +10,8 @@
 except ImportError:
     raise unittest.SkipTest("mongodb tests disabled.")

+import mongomock
+
 import factory
 from factory.mongoengine import MongoEngineFactory

@@ -57,6 +59,7 @@
             # PyMongo>=2.1 has a 20s timeout, use 100ms instead
             serverselectiontimeoutms=cls.server_timeout_ms,
             uuidRepresentation='standard',
+            mongo_client_class=mongomock.MongoClient,
         )

     @classmethod
francoisfreitag commented 2 weeks ago

I see that these tests should be skipped when mongoengine is not available. Why not skip the tests since mongodb is not available?

jelly commented 2 weeks ago

That is also an option, but it is still nice to run the tests. In theory users can run a mongodb server via podman and we do provide mongoengine in the repositories.

francoisfreitag commented 5 days ago

I have opened a pull request to support that use case, please feel free to try out the patch and let us know if that would work for you!