remove a thought experiment.
This commit is contained in:
parent
bed4ae6bbd
commit
c899d0b8b7
@ -89,46 +89,3 @@ class TestQueryBuilder(TestCase):
|
||||
self.assertEqual(len(entities), len(self.celestials))
|
||||
self.assertEqual(callback_one.call_count, len(self.celestials))
|
||||
self.assertEqual(callback_two.call_count, len(self.celestials))
|
||||
|
||||
def test_double_value_technique(self):
|
||||
"""
|
||||
Records open a new sort of technique for late calling details from dataclasses.
|
||||
imagine you have a dataclass called EntityIndex, which has only one field from the database: id.
|
||||
|
||||
however it has custom fields, where you store a lambda expression.
|
||||
|
||||
you could e.g. use it in a subquery, while still access the data.
|
||||
however this will hit the database twice if you evaluate the iterator yourself, as the lambda is not lazy.
|
||||
|
||||
note this is mainly a thought experiment.
|
||||
"""
|
||||
|
||||
planet_queryset = Celestial.objects.filter(orbits__name='Sol', celestial_type__lte=4)
|
||||
|
||||
@dataclass
|
||||
class DetailedEntity:
|
||||
id: int
|
||||
name: str
|
||||
|
||||
@dataclass
|
||||
class IndexEntity:
|
||||
id: int
|
||||
detail: DetailedEntity
|
||||
|
||||
def get_details_exec(data):
|
||||
return Celestial.objects.filter(pk=data.get('id')).records(DetailedEntity).first()
|
||||
|
||||
get_details = mock.Mock(side_effect=get_details_exec)
|
||||
|
||||
# retrieves data per key only.
|
||||
my_planets = planet_queryset.records(IndexEntity, detail=Lambda(get_details))
|
||||
my_moons = Celestial.objects.filter(orbits__in=my_planets).records(IndexEntity, detail=Lambda(get_details)) # legal
|
||||
# django does not consume the iterator internally for subqueries:
|
||||
self.assertEqual(get_details.call_count, 0)
|
||||
# consume it ourselves...
|
||||
for planet in my_planets:
|
||||
self.assertIsNotNone(planet.detail.name)
|
||||
self.assertEqual(get_details.call_count, len(self.planets))
|
||||
# but...
|
||||
self.assertEqual(len(my_moons), len(self.moons))
|
||||
self.assertEqual(get_details.call_count, len(self.planets) + len(self.moons))
|
||||
|
Loading…
Reference in New Issue
Block a user