-
-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests to verify behavior of dumping and loading foreign key fields
- Loading branch information
Showing
2 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import json | ||
|
||
from django.contrib.auth.models import User | ||
from django.test import TestCase | ||
|
||
from corehq.apps.dump_reload.sql.load import SqlDataLoader | ||
from corehq.apps.users.models import SQLUserData | ||
from corehq.form_processor.models.cases import CaseTransaction | ||
from corehq.form_processor.tests.utils import create_case | ||
|
||
|
||
class TestSqlDataLoader(TestCase): | ||
|
||
def test_loading_foreign_keys_using_iterable_natural_key(self): | ||
user = User.objects.create(username='testuser') | ||
model = { | ||
"model": "users.sqluserdata", | ||
"fields": { | ||
"domain": "test", | ||
"user_id": "testuser", | ||
"django_user": ["testuser"], | ||
"modified_on": "2024-01-01T12:00:00.000000Z", | ||
"profile": None, | ||
"data": {"test": "1"}, | ||
}, | ||
} | ||
serialized_model = json.dumps(model) | ||
|
||
SqlDataLoader().load_objects([serialized_model]) | ||
|
||
user_data = SQLUserData.objects.get(django_user=user) | ||
self.assertEqual(user_data.django_user.pk, user.pk) | ||
|
||
def test_loading_foreign_keys_using_non_iterable_natural_key(self): | ||
# create_case will create a CaseTransaction too so test verifies the serialized one is saved properly | ||
cc_case = create_case('test', case_id='abc123', save=True) | ||
model = { | ||
"model": "form_processor.casetransaction", | ||
"fields": { | ||
"case": "abc123", | ||
"form_id": "fk-test", | ||
"sync_log_id": None, | ||
"server_date": "2024-01-01T12:00:00.000000Z", | ||
"_client_date": None, | ||
"type": 1, | ||
"revoked": False, | ||
"details": {}, | ||
}, | ||
} | ||
serialized_model = json.dumps(model) | ||
|
||
SqlDataLoader().load_objects([serialized_model]) | ||
|
||
transaction = CaseTransaction.objects.partitioned_query('abc123').get(case=cc_case, form_id='fk-test') | ||
self.assertEqual(transaction.case_id, 'abc123') |