question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support ignoring fields when serializing

See original GitHub issue

Is your feature request related to a problem? Please describe.

When asserting on snapshots support filtering out properties that should not be matched on. This would allow deterministic snapshots when matching on objects that generate realtime values in tests without needing to mock.

Describe the solution you’d like

def test_my_dict(snapshot):
	my_dict = {
		"match_me": True,
		"do_not_match_me": time.time(),
		"nested_do_not_match": {
			"do_not_match_me": time.time(),
		},
	}
	assert my_dict == snapshot(exclude=("do_not_match_me",))
# name: test_my_dict
  <class 'dict'> {
    'match_me': True,
	'nested_do_not_match': <class 'dict'> {
	},
  }
---

Describe alternatives you’ve considered

This is already possible but is cumbersome to implement, and cannot easily be extended to other serialization types.

class OmitDataSerializer(DataSerializer):
	@classmethod
	def __filter_data(cls, data) -> Dict[str, Any]:
		return {
			key: data[key]
			for key in data
			if key not in ("do_not_match_me",)
		}
	
    @classmethod
    def serialize_dict(cls, data, **kwargs) -> str:
        return super().serialize_dict(cls.__filter_data(data), **kwargs)


class OmitDataExtension(AmberSnapshotExtension):
    def serialize(self, data):
        return OmitDataSerializer.serialize(data)


def test_my_dict(snapshot):
	my_dict = {
		"match_me": True,
		"do_not_match_me": time.time(),
		"nested_do_not_match": {
			"do_not_match_me": time.time(),
		},
	}
	assert my_dict == snapshot(extension=OmitDataExtension)

Additional context

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
taioncommented, Apr 30, 2020

That’s neat – it could definitely work for some cases (though there are a few places where we generate v5 UUIDs off other external IDs coming in). To clarify, though, we’re not blocked on this at all; we just run our custom “mask nondeterministic values” function on our data before asserting on the snapshot.

1reaction
noahnucommented, Mar 4, 2020

Thoughts on include and exclude instead of ignore?

We could support strings (literal property matchers) and predicates

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to exclude field from class serialization in runtime?
So when you declare a variable as transient, it should be ignored by ObjectOutputStream. Make sure that you use the transient keyword and...
Read more >
Jackson Ignore Properties on Marshalling
This tutorial will show how to ignore certain fields when serializing an object to JSON using Jackson 2.x.
Read more >
How to ignore properties with System.Text.Json
To ignore read-only fields when serializing fields, use the JsonSerializerOptions.IgnoreReadOnlyFields global setting.
Read more >
Spring Boot — Dynamically ignore fields while serializing ...
Spring Boot — Dynamically ignore fields while serializing Java Object to JSON · Properties which are to be ignored is supplied to SimpleBeanPropertyFilter....
Read more >
Gson – Exclude or Ignore Fields
By default, Gson would exclude a field from serialization and deserialization – both, if we simply mark the field as transient. Remember that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found