|
| 1 | +from django.test import TestCase |
| 2 | + |
| 3 | +from django_lifecycle import NotSet |
| 4 | +from django_lifecycle.conditions import WhenFieldChangesTo |
| 5 | +from django_lifecycle.conditions import WhenFieldHasChanged |
| 6 | +from django_lifecycle.conditions import WhenFieldIsNot |
| 7 | +from django_lifecycle.conditions import WhenFieldIsNow |
| 8 | +from django_lifecycle.conditions import WhenFieldWas |
| 9 | +from django_lifecycle.conditions import WhenFieldWasNot |
| 10 | +from tests.testapp.models import UserAccount |
| 11 | + |
| 12 | + |
| 13 | +class ConditionsTests(TestCase): |
| 14 | + @property |
| 15 | + def stub_data(self): |
| 16 | + return { |
| 17 | + "username": "homer.simpson", |
| 18 | + "first_name": "Homer", |
| 19 | + "last_name": "Simpson", |
| 20 | + "password": "donuts", |
| 21 | + } |
| 22 | + |
| 23 | + def test_has_changed_specs(self): |
| 24 | + condition = WhenFieldHasChanged("first_name", has_changed=True) |
| 25 | + |
| 26 | + data = self.stub_data |
| 27 | + data["first_name"] = "Homer" |
| 28 | + UserAccount.objects.create(**data) |
| 29 | + user_account = UserAccount.objects.get() |
| 30 | + |
| 31 | + self.assertFalse(condition(user_account)) |
| 32 | + user_account.first_name = "Ned" |
| 33 | + self.assertTrue(condition(user_account)) |
| 34 | + |
| 35 | + def test_check_is_now_condition_wildcard_should_pass(self): |
| 36 | + condition = WhenFieldIsNow("first_name", is_now="*") |
| 37 | + data = self.stub_data |
| 38 | + data["first_name"] = "Homer" |
| 39 | + UserAccount.objects.create(**data) |
| 40 | + user_account = UserAccount.objects.get() |
| 41 | + user_account.first_name = "Ned" |
| 42 | + self.assertTrue(condition(user_account)) |
| 43 | + |
| 44 | + def test_check_is_now_condition_matching_value_should_pass(self): |
| 45 | + condition = WhenFieldIsNow("first_name", is_now="Ned") |
| 46 | + data = self.stub_data |
| 47 | + data["first_name"] = "Homer" |
| 48 | + UserAccount.objects.create(**data) |
| 49 | + user_account = UserAccount.objects.get() |
| 50 | + user_account.first_name = "Ned" |
| 51 | + self.assertTrue(condition(user_account)) |
| 52 | + |
| 53 | + def test_check_is_now_condition_not_matched_value_should_not_pass(self): |
| 54 | + condition = WhenFieldIsNow("first_name", is_now="Bart") |
| 55 | + data = self.stub_data |
| 56 | + data["first_name"] = "Homer" |
| 57 | + UserAccount.objects.create(**data) |
| 58 | + user_account = UserAccount.objects.get() |
| 59 | + self.assertFalse(condition(user_account)) |
| 60 | + |
| 61 | + def test_check_was_not_condition_should_pass_when_not_set(self): |
| 62 | + condition = WhenFieldWasNot("first_name", was_not=NotSet) |
| 63 | + data = self.stub_data |
| 64 | + UserAccount.objects.create(**data) |
| 65 | + user_account = UserAccount.objects.get() |
| 66 | + self.assertTrue(condition(user_account)) |
| 67 | + |
| 68 | + def test_check_was_not_condition_not_matching_value_should_pass(self): |
| 69 | + condition = WhenFieldWasNot("first_name", was_not="Bart") |
| 70 | + |
| 71 | + data = self.stub_data |
| 72 | + data["first_name"] = "Homer" |
| 73 | + UserAccount.objects.create(**data) |
| 74 | + user_account = UserAccount.objects.get() |
| 75 | + self.assertTrue(condition(user_account)) |
| 76 | + |
| 77 | + def test_check_was_not_condition_matched_value_should_not_pass(self): |
| 78 | + condition = WhenFieldWasNot("first_name", was_not="Homer") |
| 79 | + |
| 80 | + data = self.stub_data |
| 81 | + data["first_name"] = "Homer" |
| 82 | + UserAccount.objects.create(**data) |
| 83 | + user_account = UserAccount.objects.get() |
| 84 | + self.assertFalse(condition(user_account)) |
| 85 | + |
| 86 | + def test_check_was_condition_wildcard_should_pass(self): |
| 87 | + condition = WhenFieldWas("first_name", was="*") |
| 88 | + |
| 89 | + data = self.stub_data |
| 90 | + data["first_name"] = "Homer" |
| 91 | + UserAccount.objects.create(**data) |
| 92 | + user_account = UserAccount.objects.get() |
| 93 | + self.assertTrue(condition(user_account)) |
| 94 | + |
| 95 | + def test_check_was_condition_matching_value_should_pass(self): |
| 96 | + condition = WhenFieldWas("first_name", was="Homer") |
| 97 | + |
| 98 | + data = self.stub_data |
| 99 | + data["first_name"] = "Homer" |
| 100 | + UserAccount.objects.create(**data) |
| 101 | + user_account = UserAccount.objects.get() |
| 102 | + self.assertTrue(condition(user_account)) |
| 103 | + |
| 104 | + def test_check_was_condition_not_matched_value_should_not_pass(self): |
| 105 | + condition = WhenFieldWas("first_name", was="Bart") |
| 106 | + |
| 107 | + data = self.stub_data |
| 108 | + data["first_name"] = "Homer" |
| 109 | + UserAccount.objects.create(**data) |
| 110 | + user_account = UserAccount.objects.get() |
| 111 | + self.assertFalse(condition(user_account)) |
| 112 | + |
| 113 | + def test_is_not_condition_should_pass(self): |
| 114 | + condition = WhenFieldIsNot("first_name", is_not="Ned") |
| 115 | + |
| 116 | + data = self.stub_data |
| 117 | + data["first_name"] = "Homer" |
| 118 | + UserAccount.objects.create(**data) |
| 119 | + user_account = UserAccount.objects.get() |
| 120 | + self.assertTrue(condition(user_account)) |
| 121 | + |
| 122 | + def test_is_not_condition_should_not_pass(self): |
| 123 | + condition = WhenFieldIsNot("first_name", is_not="Ned") |
| 124 | + |
| 125 | + data = self.stub_data |
| 126 | + data["first_name"] = "Ned" |
| 127 | + UserAccount.objects.create(**data) |
| 128 | + user_account = UserAccount.objects.get() |
| 129 | + self.assertFalse(condition(user_account)) |
| 130 | + |
| 131 | + def test_changes_to_condition_should_pass(self): |
| 132 | + condition = WhenFieldChangesTo("last_name", changes_to="Flanders") |
| 133 | + data = self.stub_data |
| 134 | + UserAccount.objects.create(**data) |
| 135 | + user_account = UserAccount.objects.get() |
| 136 | + user_account.last_name = "Flanders" |
| 137 | + self.assertTrue(condition(user_account)) |
| 138 | + |
| 139 | + def test_changes_to_condition_included_in_update_fields_should_fire_hook(self): |
| 140 | + condition = WhenFieldChangesTo("last_name", changes_to="Flanders") |
| 141 | + user_account = UserAccount.objects.create(**self.stub_data) |
| 142 | + user_account.last_name = "Flanders" |
| 143 | + self.assertTrue(condition(user_account, update_fields=["last_name"])) |
| 144 | + |
| 145 | + def test_changes_to_condition_not_included_in_update_fields_should_not_fire_hook( |
| 146 | + self, |
| 147 | + ): |
| 148 | + condition = WhenFieldChangesTo("last_name", changes_to="Flanders") |
| 149 | + user_account = UserAccount.objects.create(**self.stub_data) |
| 150 | + user_account.last_name = "Flanders" |
| 151 | + self.assertFalse(condition(user_account, update_fields=["first_name"])) |
| 152 | + |
| 153 | + def test_changes_to_condition_should_not_pass(self): |
| 154 | + condition = WhenFieldChangesTo("last_name", changes_to="Flanders") |
| 155 | + data = self.stub_data |
| 156 | + data["first_name"] = "Marge" |
| 157 | + data["last_name"] = "Simpson" |
| 158 | + UserAccount.objects.create(**data) |
| 159 | + user_account = UserAccount.objects.get() |
| 160 | + user_account.last_name = "Bouvier" |
| 161 | + self.assertFalse(condition(user_account)) |
0 commit comments