Files
ormar/tests/test_model_definition/test_equality_and_hash.py
erichaydel 7c18fa55e7 Add benchmarking test suite and greatly improve performance in a few cases (#948)
* Add benchmarking test suite

* Improve amortized time of model relation loads with a large number of rows

* Improve performance of loading models with many related models

* Improve performance of loading models with many related models to O(N)ish

* Fix bug where N model creation with shared related model would build in N^2 time

* Lower blocking time for queryset results

* Add docstrings and streamline hash code

Co-authored-by: haydeec1 <Eric.Haydel@jhuapl.edu>
2022-12-10 17:12:11 +01:00

68 lines
1.6 KiB
Python

# type: ignore
import databases
import pytest
import sqlalchemy
import ormar
from ormar import ModelDefinitionError, property_field
from tests.settings import DATABASE_URL
database = databases.Database(DATABASE_URL, force_rollback=True)
metadata = sqlalchemy.MetaData()
class Song(ormar.Model):
class Meta:
tablename = "songs"
metadata = metadata
database = database
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
@pytest.fixture(autouse=True, scope="module")
def create_test_database():
engine = sqlalchemy.create_engine(DATABASE_URL)
metadata.drop_all(engine)
metadata.create_all(engine)
yield
metadata.drop_all(engine)
@pytest.mark.asyncio
async def test_equality():
async with database:
song1 = await Song.objects.create(name="Song")
song2 = await Song.objects.create(name="Song")
song3 = Song(name="Song")
song4 = Song(name="Song")
assert song1 == song1
assert song3 == song4
assert song1 != song2
assert song1 != song3
assert song3 != song1
assert song1 is not None
@pytest.mark.asyncio
async def test_hash_doesnt_change_with_fields_if_pk():
async with database:
song1 = await Song.objects.create(name="Song")
prev_hash = hash(song1)
await song1.update(name="Song 2")
assert hash(song1) == prev_hash
@pytest.mark.asyncio
async def test_hash_changes_with_fields_if_no_pk():
async with database:
song1 = Song(name="Song")
prev_hash = hash(song1)
song1.name = "Song 2"
assert hash(song1) != prev_hash