dirty-equals icon indicating copy to clipboard operation
dirty-equals copied to clipboard

adding `insert_assert` function and make pytest-plugin

Open samuelcolvin opened this issue 2 years ago • 3 comments

See https://gist.github.com/samuelcolvin/625a1655c2a73e02469fc3c27285ca42

To use now:

install:

pip install git+https://github.com/samuelcolvin/dirty-equals.git@insert_assert

With that you should be able to add insert_assert() into your tests and it should "just work ™",

Example code:

def test_string():
    thing = 'foobar'
    insert_assert(thing)

def test_list_callable():
    def foobar():
        return ['foo', 1, b'bytes']
    insert_assert(foobar())

def test_comprehension():
    insert_assert([f'x{i}' for i in range(10)])

Still to do:

  • [x] tests, or at least ignore where impossible
  • [ ] docs

samuelcolvin avatar Sep 27 '22 11:09 samuelcolvin

Codecov Report

Merging #51 (6a70b31) into main (724494c) will decrease coverage by 1.70%. The diff coverage is 90.85%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #51      +/-   ##
==========================================
- Coverage   99.72%   98.02%   -1.71%     
==========================================
  Files          11       12       +1     
  Lines         737      911     +174     
  Branches      188      226      +38     
==========================================
+ Hits          735      893     +158     
- Misses          2       15      +13     
- Partials        0        3       +3     
Impacted Files Coverage Δ
dirty_equals/pytest_plugin.py 90.80% <90.80%> (ø)
dirty_equals/version.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 724494c...6a70b31. Read the comment docs.

codecov[bot] avatar Sep 27 '22 11:09 codecov[bot]

I think it would be really interesting to enable something like this:

# insert_assert(
#   resp.json(),
#   repl={
#     '[]["id"]': IsUUID(),
#     '[0]["created_at"]': IsDatetime(
#       approx=datetime.utcnow(),
#       delta=timedelta(seconds=1),
#     )
#   }
# )
expected = [
  {
    "id": IsUUID(),
    "created_at": IsDatetime(
      approx=datetime.utcnow(),
      delta=timedelta(seconds=1),
    ),
    "price": 123.0,
  }
]
assert resp.json() == expected

(source: https://twitter.com/adriangb01/status/1575196309891719168)

adriangb avatar Sep 28 '22 19:09 adriangb

On reflection, I think I'll move this to https://github.com/samuelcolvin/python-devtools.

Rationale:

  • this fix better with what devtools is for
  • shared requirements and even some shared logic
  • the recommendation is not to install devtools in CI, thus the insert_assert could never pass on CI

samuelcolvin avatar Sep 29 '22 10:09 samuelcolvin

replaced by https://github.com/samuelcolvin/python-devtools/pull/126, would be nice to try to add support for dirty-equals types (even though this has moved to another repo), in particular:

  • stuff where the repr breaks black, the formatting vanishes which is ugly.
  • stuff like datetimes close to now and uuids which will change often.

samuelcolvin avatar Apr 05 '23 17:04 samuelcolvin