'problems puling data out of django database and testing it
really sorry but a complete beginner here with Django and Python. I am trying to pull some data out of a database so I can assert against it in the testing faze but I am having problems understanding how to get the data out of the model. Any help would be appreciated.
My code:
Models.py
class CurrentAccountState(AccountState):
enable_personal_profile = models.BooleanField(default=False)
class CurrentAccountSetting(TimestampedModel):
ACCOUNT_SEND_TO_OPTIONS = (
("buyer", _("Receipt sent to the buyer")),
("payment_profile_contact", _("Receipt sent to payment profile contact")),
)
account_state = models.ForeignKey(
CurrentAccountState, related_name="account_settings", on_delete=models.CASCADE
)
payment_profile_id = models.CharField(max_length=36)
send_to = models.CharField(max_length=32, choices=SEND_TO_OPTIONS, default="buyer")
views.py
@action(detail=True, methods=["post"])
def update_state(self, request, pk=None):
integration = AccountServiceManager.get_by_name(str(pk))
if not account:
raise Http404()
return Response("", status=status.HTTP_200_OK)
test update_state.py
def test_update_state(self):
user = self.admin_user
self.client.force_login(user)
account = AccountFactory(
name="account name", enabled=True, provider=str_to_kebab_case("account name")
)
payload = {
"enable_personal_profile": True,
"payment_profile_settings": [
{
"payment_profile_id": "Something",
"send_to": "buyer",
},
{
"payment_profile_id": "Something else",
"send_to": "payment_profile_contact",
}
]
}
response = self.client.post(f"{self.url}/account/update_state", data=json.dumps(payload), content_type="application/json")
assert response.status_code == status.HTTP_200_OK
account_account = CurrentAccountSetting.objects.all()
assert len(account_account) == 1
assert account_account.enable_personal_payment_profile is True
What I am struggling with is understanding how I can pull the data out of the actual database so I can make assertions in the test. Currently the len(account_account) fails with saying I have an empty array coming from the queryset[]. Which makes sense as I haven't added anything to the views yet. But how could I actually get that data to the views?
Solution 1:[1]
Assuming that AccountFactory creates a CurrentAccountSetting object for you:
Maybe its because you haven't run account.save() after line 4 in test_update_state(). Or you could use create().
If not, the issue is that you haven't created a CurrentAccountSetting object anywhere in that flow.
Also in your last line of code you probably want to get the item out of the array!
assert account_account[0].enable_personal_payment_profile
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | rymanso |
