Skip to content

test: Add test case to show set() many update #144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions example/test_full/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from collections import Counter
from django.db import models
import sys
from computedfields.models import ComputedFieldsModel, computed, precomputed, ComputedField
Expand Down Expand Up @@ -1107,3 +1108,46 @@ class Meta:
class HaProxy(Ha):
class Meta:
proxy = True


class Tag(ComputedFieldsModel):
name = models.CharField(max_length=32, unique=True)


run_counters = Counter()


class Advert(ComputedFieldsModel):
name = models.CharField(max_length=32)

tags = models.ManyToManyField(Tag, related_name="adverts")

@computed(
field=models.CharField(max_length=500),
depends=[("tags", ["name"])],
)
def all_tags(self) -> str:
run_counters.update(["all_tags"])
if not self.pk:
return ""
return ", ".join(self.tags.values_list("name", flat=True))

def __str__(self) -> str:
return f"{self.name}"

class Room(ComputedFieldsModel):
name = models.CharField(max_length=32)
advert = models.ForeignKey(Advert, related_name="rooms", on_delete=models.CASCADE)

@computed(
field=models.BooleanField(),
depends=[("advert.tags", ["name"])],
)
def is_ready(self) -> bool:
run_counters.update(["is_ready"])
if not self.pk:
return False
return self.advert.tags.filter(name="ready").exists()

def __str__(self) -> str:
return f"{self.name}"
18 changes: 18 additions & 0 deletions example/test_full/tests/test_advert_tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from django.test import TestCase
from .. import models


class TestAdvertTags(TestCase):
def test(self):
tag_ready = models.Tag.objects.create(name="ready")
advert_1 = models.Advert.objects.create(name="A1")
room_11 = models.Room.objects.create(name="R11", advert=advert_1)
advert_1.tags.add(tag_ready)
advert_2 = models.Advert.objects.create(name="A2")
room_21 = models.Room.objects.create(name="R21", advert=advert_2)
assert models.run_counters["all_tags"] == 3
assert models.run_counters["is_ready"] == 3

advert_2.tags.add(tag_ready)
assert models.run_counters["all_tags"] == 4
assert models.run_counters["is_ready"] == 4
Loading