-
Notifications
You must be signed in to change notification settings - Fork 26
RDBC-1018/1019/1023 Fix change tracking metadata aliasing, type-coercion, add per-entity change API #268
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
Merged
Merged
RDBC-1018/1019/1023 Fix change tracking metadata aliasing, type-coercion, add per-entity change API #268
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
fc203cd
RDBC-1018: break metadata dict alias in DocumentInfo.get_new_document…
redknightlois 573074e
RDBC-1019: fix type-coercion in value comparison (arrays and scalar f…
redknightlois cb4ac17
RDBC-1023: add session.advanced.what_changed_for(entity) and get_trac…
redknightlois File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
242 changes: 242 additions & 0 deletions
242
ravendb/tests/session_tests/test_change_tracking_metadata.py
This file contains hidden or 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,242 @@ | ||
| """C# ref: FastTests/Client/WhatChanged.cs — WhatChanged_should_be_idempotent_operation (RavenDB-9150)""" | ||
|
|
||
| from ravendb.tests.test_base import TestBase | ||
|
|
||
|
|
||
| class User: | ||
| def __init__(self, name: str = "", age: int = 0): | ||
| self.name = name | ||
| self.age = age | ||
|
|
||
|
|
||
| class TestRavenDBWhatChangedIdempotent(TestBase): | ||
| def setUp(self): | ||
| super().setUp() | ||
|
|
||
| def test_what_changed_should_be_idempotent_operation(self): | ||
| """No modifications — two calls return the same empty result.""" | ||
| with self.store.open_session() as session: | ||
| session.store(User(name="Alice", age=30), "users/1") | ||
| session.save_changes() | ||
|
|
||
| with self.store.open_session() as session: | ||
| session.load("users/1", User) | ||
|
|
||
| result_1 = session.advanced.what_changed() | ||
| result_2 = session.advanced.what_changed() | ||
|
|
||
| self.assertEqual(result_1, result_2) | ||
|
|
||
| def test_what_changed_should_be_idempotent_operation_with_changes(self): | ||
| """With modifications — two calls return the same non-empty result.""" | ||
| with self.store.open_session() as session: | ||
| session.store(User(name="user1"), "users/2") | ||
| session.store(User(name="user2", age=1), "users/3") | ||
| session.save_changes() | ||
|
|
||
| with self.store.open_session() as session: | ||
| user1 = session.load("users/2", User) | ||
| user2 = session.load("users/3", User) | ||
|
|
||
| user1.age = 10 | ||
| session.delete(user2) | ||
|
|
||
| result_1 = session.advanced.what_changed() | ||
| result_2 = session.advanced.what_changed() | ||
|
|
||
| self.assertEqual(2, len(result_1)) | ||
| self.assertEqual(len(result_1), len(result_2)) | ||
|
|
||
| def test_what_changed_detects_metadata_modification(self): | ||
| with self.store.open_session() as session: | ||
| session.store(User(name="Bob"), "users/4") | ||
| session.save_changes() | ||
|
|
||
| with self.store.open_session() as session: | ||
| user = session.load("users/4", User) | ||
| meta = session.advanced.get_metadata_for(user) | ||
| meta["@custom-tag"] = "v1" | ||
|
|
||
| changes = session.advanced.what_changed() | ||
| self.assertIn("users/4", changes) | ||
|
|
||
| def test_what_changed_and_save_changes_agree_on_metadata_write(self): | ||
| with self.store.open_session() as session: | ||
| session.store(User(name="Carol"), "users/5") | ||
| session.save_changes() | ||
|
|
||
| with self.store.open_session() as session: | ||
| user = session.load("users/5", User) | ||
| meta = session.advanced.get_metadata_for(user) | ||
| meta["@custom-tag"] = "v2" | ||
|
|
||
| pending = session.advanced.what_changed() | ||
| self.assertIn("users/5", pending) | ||
|
|
||
| session.save_changes() | ||
|
|
||
| with self.store.open_session() as session: | ||
| reloaded_meta = session.advanced.get_metadata_for(session.load("users/5", User)) | ||
| self.assertEqual("v2", reloaded_meta.get("@custom-tag")) | ||
|
|
||
|
|
||
| class TestRavenDBWhatChangedMetadataOps(TestBase): | ||
| def setUp(self): | ||
| super().setUp() | ||
|
|
||
| def test_has_changed_returns_true_for_metadata_only_modification(self): | ||
| with self.store.open_session() as session: | ||
| session.store(User(name="Alice"), "users/1") | ||
| session.save_changes() | ||
|
|
||
| with self.store.open_session() as session: | ||
| user = session.load("users/1", User) | ||
| meta = session.advanced.get_metadata_for(user) | ||
| meta["@custom"] = "v1" | ||
|
|
||
| self.assertTrue(session.advanced.has_changed(user)) | ||
|
|
||
| def test_has_changed_returns_false_when_no_modification(self): | ||
| with self.store.open_session() as session: | ||
| session.store(User(name="Bob"), "users/2") | ||
| session.save_changes() | ||
|
|
||
| with self.store.open_session() as session: | ||
| session.load("users/2", User) | ||
| user = session.load("users/2", User) | ||
|
|
||
| self.assertFalse(session.advanced.has_changed(user)) | ||
|
|
||
| def test_has_changes_returns_true_for_metadata_only_modification(self): | ||
| with self.store.open_session() as session: | ||
| session.store(User(name="Carol"), "users/3") | ||
| session.save_changes() | ||
|
|
||
| with self.store.open_session() as session: | ||
| user = session.load("users/3", User) | ||
| meta = session.advanced.get_metadata_for(user) | ||
| meta["@custom"] = "v1" | ||
|
|
||
| self.assertTrue(session.advanced.has_changes()) | ||
|
|
||
| def test_has_changes_returns_false_when_no_modification(self): | ||
| with self.store.open_session() as session: | ||
| session.store(User(name="Dave"), "users/4") | ||
| session.save_changes() | ||
|
|
||
| with self.store.open_session() as session: | ||
| session.load("users/4", User) | ||
| self.assertFalse(session.advanced.has_changes()) | ||
|
|
||
| def test_what_changed_detects_metadata_key_deletion(self): | ||
| with self.store.open_session() as session: | ||
| session.store(User(name="Eve"), "users/5") | ||
| session.save_changes() | ||
|
|
||
| with self.store.open_session() as session: | ||
| user = session.load("users/5", User) | ||
| meta = session.advanced.get_metadata_for(user) | ||
| meta["@custom"] = "to-be-deleted" | ||
| session.save_changes() | ||
|
|
||
| with self.store.open_session() as session: | ||
| user = session.load("users/5", User) | ||
| meta = session.advanced.get_metadata_for(user) | ||
| self.assertEqual("to-be-deleted", meta.get("@custom")) | ||
| del meta["@custom"] | ||
|
|
||
| changes = session.advanced.what_changed() | ||
| self.assertIn("users/5", changes) | ||
|
|
||
| def test_metadata_deletion_is_detected_and_persisted(self): | ||
| with self.store.open_session() as session: | ||
| session.store(User(name="Frank"), "users/6") | ||
| session.save_changes() | ||
|
|
||
| with self.store.open_session() as session: | ||
| user = session.load("users/6", User) | ||
| meta = session.advanced.get_metadata_for(user) | ||
| meta["@tag"] = "remove-me" | ||
| session.save_changes() | ||
|
|
||
| with self.store.open_session() as session: | ||
| user = session.load("users/6", User) | ||
| meta = session.advanced.get_metadata_for(user) | ||
| del meta["@tag"] | ||
|
|
||
| self.assertIn("users/6", session.advanced.what_changed()) | ||
| session.save_changes() | ||
|
|
||
| with self.store.open_session() as session: | ||
| meta = session.advanced.get_metadata_for(session.load("users/6", User)) | ||
| self.assertNotIn("@tag", meta) | ||
|
|
||
|
|
||
| class Doc: | ||
| def __init__(self, name: str = ""): | ||
| self.name = name | ||
|
|
||
|
|
||
| class TestRavenDBWhatChangedMetadataMutations(TestBase): | ||
| def setUp(self): | ||
| super().setUp() | ||
| with self.store.open_session() as session: | ||
| d = Doc() | ||
| session.store(d, "d/1") | ||
| meta = session.advanced.get_metadata_for(d) | ||
| meta["Test-A"] = ["a", "a", "a"] | ||
| meta["Test-C"] = ["c", "c", "c"] | ||
| session.save_changes() | ||
|
|
||
| def test_metadata_array_value_change_detected(self): | ||
| """meta["Test-A"] = ["b","a","c"] -> 2 ARRAY_VALUE_CHANGED entries.""" | ||
| with self.store.open_session() as session: | ||
| d = session.load("d/1", Doc) | ||
| meta = session.advanced.get_metadata_for(d) | ||
| meta["Test-A"] = ["b", "a", "c"] | ||
|
|
||
| changes = session.advanced.what_changed() | ||
|
|
||
| self.assertIn("d/1", changes) | ||
| self.assertEqual(2, len(changes["d/1"])) | ||
|
|
||
| def test_metadata_key_removal_detected(self): | ||
| """meta.Remove("Test-A") -> 1 REMOVED_FIELD entry.""" | ||
| with self.store.open_session() as session: | ||
| d = session.load("d/1", Doc) | ||
| meta = session.advanced.get_metadata_for(d) | ||
| meta.pop("Test-A") | ||
|
|
||
| changes = session.advanced.what_changed() | ||
|
|
||
| self.assertIn("d/1", changes) | ||
| change_types = [str(c["change"]) for c in changes["d/1"]] | ||
| self.assertIn("removed_field", change_types) | ||
|
|
||
| def test_metadata_remove_two_add_two_detected(self): | ||
| """Remove Test-A, Test-C; add Test-B, Test-D -> 4 entries.""" | ||
| with self.store.open_session() as session: | ||
| d = session.load("d/1", Doc) | ||
| meta = session.advanced.get_metadata_for(d) | ||
| meta.pop("Test-A") | ||
| meta.pop("Test-C") | ||
| meta["Test-B"] = ["b", "b", "b"] | ||
| meta["Test-D"] = ["d", "d", "d"] | ||
|
|
||
| changes = session.advanced.what_changed() | ||
|
|
||
| self.assertIn("d/1", changes) | ||
| self.assertEqual(4, len(changes["d/1"])) | ||
|
|
||
| def test_metadata_remove_one_add_one_detected(self): | ||
| """Remove Test-A; add Test-B -> 2 entries.""" | ||
| with self.store.open_session() as session: | ||
| d = session.load("d/1", Doc) | ||
| meta = session.advanced.get_metadata_for(d) | ||
| meta.pop("Test-A") | ||
| meta["Test-B"] = ["b", "b", "b"] | ||
|
|
||
| changes = session.advanced.what_changed() | ||
|
|
||
| self.assertIn("d/1", changes) | ||
| self.assertEqual(2, len(changes["d/1"])) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please provide a comment on why we're calling
dicthere