Tom
Hi, what’s the correct way to patch out an entity for testing?
Alexander
Wdym
Alexander
You can create an in-memory SQLite database for a test and pre-fill it with specific entity instances
Tom
Wdym
I’m trying to patch out an entity entirely for a unittest, with an @patch decorator, but when it hits the patch it binds the db and generates mappings
Tom
You can create an in-memory SQLite database for a test and pre-fill it with specific entity instances
If I’m binding in my entities file, is there a way to rebind to an inmem db for the test suite?
Alexander
Currently not, I recommend to bind outside of entity definition file
Tom
Ok will give that a shot thanks
Tom
Thanks for the help! Making progress! I think because I'm using an in-mem DB for the test cases, I can't seem to use objects between dbsessions. E.g. if I create an object and commit in the suite's setUp step as self.entity = Entity(), I can't access self.enttiy in test_x, but I can do an entity = Entity.get(), just want to make sure I'm not doing something wrong. Also, where should the bind go? If I just throw it into any of my files as soon as I import into the test file the bind occurs, and then when the bind is attempted in the test file it fails because the bind already exists
Alexander
With current implementation, the easiest you can do for testing is to define entity classes inside function: def define_entities(db): class Foo(db.Entity): ... class Bar(db.Entity): ... Then in your test's setUp method you can do self.db = Database() define_entities(db) db.bind(**test_db_params) The drawback of defining entity classes in a function is PyCharm cannot understand what methods and attributes entity classes have In tests you can access entity classes as db attributes: foo = self.db.Foo(...) query = select(bar for bar in self.db.Bar if ...) More complicated way is to define each entity in a separate method. Then you can assign classes to outer variables and make the slightly more accessible for type system: def define_foo(db): class Foo(db.Entity): ... return Foo def define_bar(db) class Bar(db.Entity): ... return Bar Then in setUp method in can do: self.db = Database() self.Foo = define_foo(db) self.Bar = define_bar(db) db.bind(**test_db_params) And then you can use self.Foo instead of self.db.Foo with slightly better type support in IDE
Alexander
To existing entities
Matt
Hey guys, just wanted to let you know that the online editor seems to be down 🙂 {"msg":"Server error”} when trying to create a diagram or save.
Christian
Same for me: Sporadic errors (one on logging in to editor.ponyorm.com) that return json {"msg":"Server error"}, but could login after reload. Now the tabs ("Models", "SQLite",...) display "Something went wrong"
Alexander
Thank you for reporting! Should work now
Matt
Yep, you fixed it! Thanks Alexander!
Radim
Hello does entity.collection.clear() delete items of the collection if the item has no further references?
Alexander
It's a magic. I forgot how is is called in Pony... Maybe another Alexander can name it
Diego
hello
Diego
is there any way to update a model after is first created?
Alexander
Built-in Pony - no. You can use some migration manager and make migrations by yourself. I used Flyway with Pony at some project - it worked pretty well.
Diego
thaaanks!
Diego
I understand that they don't
SlavaMGTU
I'm looking for an Entity-Relationship Diagram Editor. I need to convert SQLite code to diagram. How can I do that? Tell me please.
Diego
hello, is there any place where i can find bechmarks of the ORM?
Volbil
Permalink Bot
https://github.com/tortoise/orm-benchmarks
Permanent link to the tortoise/orm-benchmarks project you mentioned. (?)
M❄️hdi
how is better? the more or the less? (vertical bar)
Volbil
There is table with numbers and description
Diego
cool
Diego
I have tried to do my own benchmarks, and the results are not the same :(. Maybe the way i am accessing pony is wrong
Diego
Volbil
Try without this
Volbil
[:] I mean
Diego
db_session is required when working with the database. I get this error
Volbil
Also you probably need to do loop under the session if you want to do any changes
Volbil
Move for loop one tab right and remove [:]
Volbil
I think it would provide better results
Diego
ohh i get you
Diego
i tried that before, it performs the same or a little worse
Diego
I'm returning 5000 objects from my api. Pony does it in 90ms and django orm in 80.
Diego
Diego
Diego
im using in both differente postgre db
Volbil
Returning this many objects is not great idea
Diego
what i found is that pony does way better in cold starts. but django does better in the following requests
Diego
I know i should use pagination in prod. Im just trying to benchmark
Stone
hey bois, is there asyncio ports of Pony?
Christian
hey bois, is there asyncio ports of Pony?
Not currently, but you can make it work within contstraints: https://docs.ponyorm.org/integration_with_fastapi.html#async-and-db-session
Anatol
Приветствую, А как правильно перегнать в словарь результат select? К примеру, не срабатывает с x = select(d for d in DataSession) to_dict(x) и в варианте select(d for d in DataSession)[:] тоже Entity instance or a sequence of instances expected. to_dict из from pony.orm.serialization import to_dict
Anatol
https://docs.ponyorm.org/api_reference.html?highlight=dict#Entity.to_dict
Спасибо, доку видел. Т.е. только по одному выходит - напрямую QueryResult не дает законвертить похоже. Узнал в гитхабе - спасибо за такой крутой ORM. Только начал осваиваться, потом может смогу пользу принести.
Anatol
results = select(f for f in Foo)[:] results_list = results.to_list()
ага, спасибо. упустил это сам сделал через [to_dict(r) for r in results]
Anatol
ага, спасибо. упустил это сам сделал через [to_dict(r) for r in results]
Понял почему упустил - вначале попробовал .to_json() - получил ошибку про права пользователя на веб интерфейс и я подумал что все .to_... это о той кухне
Alexander
:)
Christian
Hi. I'm trying to figure out the optimal way to combine flask+pony+pytest for testing. I'm using the suggested techniques in https://github.com/ponyorm/pony/issues/32 but I'm still running into the problem of having a db-factory function generating different db objects that mess my tests up. My solution is using the @cache decorator to turn the db-factory function into a singleton. Is this the way to do this or am I doing something overly complicated here?
Alexander
I would end up with a singleton too.
Christian
I would end up with a singleton too.
That's reassuring, thanks.
Alexander
The only thing I would go with a common singleton implementation _db = None def get_db(): global _db if _db is None: _db = Database(...) ... return _db Cause it may be confusing for other devs with a `@cache`one.
Christian
There is no way to call a PonyORM Database by a unique identifier, right? For Flask, the idea of database URIs that can be attached to the current_app and called when necessary from the context seems the most elegant solution.
Alexander
Built-in - no. You can create own mapping.
Nikita
Hello everybody, is there any information about multiple files for models? I'd be very gratitude if you tell me how to separate models or it's bad idea or you have link to topic in docs
Christian
Hello everybody, is there any information about multiple files for models? I'd be very gratitude if you tell me how to separate models or it's bad idea or you have link to topic in docs
Separating the model definitions into several files works fine. Simply (without testing in mind) I would do it like this: # ------------------------- # file: /src/models/user.py from pony.orm import * db = Database() class User(db.Entity): login = Required(str) ... # ----------------------------- # file: /src/models/__init__.py from .user import db, User, ... from .animal import Duck, Lion, ... # ------------------- # file: /src/start.py from src.models import db db.bind(provider="sqlite", filename=":memory:") db.generate_mapping(create_tables=True) ...
Christian
...and you could leave out __init__.py, but I find it nice to import all models from just src.models
Christian
Yes. # -------------------- # file: /src/models/animal.py from src.models import db class Duck(db.Entity): firstname = Required(str)
Christian
In that way everything maps to the same Database object.
Nikita
You're the best thanks a lot
Christian
Welcome! :)
Damiano
raise OperationalError(e) pony.orm.dbapiprovider.OperationalError: database is locked
Damiano
First time ever getting this error. What's this?
Ziya
raise OperationalError(e) pony.orm.dbapiprovider.OperationalError: database is locked
Different processes tries to modify/read same resource -))
Damiano
Different processes tries to modify/read same resource -))
Oh, okay. I'm now running some multithreading.
aswiro
What type of data should I specify when creating a table?Hi, tell me I need to write down the number 1009998887766.
Volbil
What type of data should I specify when creating a table?Hi, tell me I need to write down the number 1009998887766.
https://docs.ponyorm.org/api_reference.html?highlight=size#cmdoption-arg-size