File: outsource.md

package info (click to toggle)
python-inline-snapshot 0.31.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,708 kB
  • sloc: python: 9,261; makefile: 39; sh: 32
file content (66 lines) | stat: -rw-r--r-- 2,215 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
`outsource()` can be used to declare that a value should be stored in an external object when you create it.
This is useful in cases where you already know that a value can only be stored externally, and you want to return it from a function, for example.

<!-- inline-snapshot: first_block outcome-passed=1 outcome-errors=1 -->
``` python
from inline_snapshot import outsource, register_format_alias, snapshot

register_format_alias(".png", ".bin")


def check_captcha(input_data):
    # Perform operations on input_data ...
    the_data = b"image data ..."

    return {
        "size": "200x100",
        "difficulty": 8,
        "picture": outsource(the_data, suffix=".png"),
    }


def test_captcha():
    assert check_captcha("abc") == snapshot()
```

inline-snapshot always generates an external object in this case.

<!-- inline-snapshot: create outcome-passed=1 outcome-errors=1 -->
``` python hl_lines="3 4 20 21 22 23 24 25 26"
from inline_snapshot import outsource, register_format_alias, snapshot

from inline_snapshot import external

register_format_alias(".png", ".bin")


def check_captcha(input_data):
    # Perform operations on input_data ...
    the_data = b"image data ..."

    return {
        "size": "200x100",
        "difficulty": 8,
        "picture": outsource(the_data, suffix=".png"),
    }


def test_captcha():
    assert check_captcha("abc") == snapshot(
        {
            "size": "200x100",
            "difficulty": 8,
            "picture": external("hash:0da2cc316111*.png"),
        }
    )
```

`outsource()` was the only way to create external objects until inline-snapshot 0.24. External objects can now also be created with `external()` and used like `snapshot()`.

!!! info
    It is not possible to specify the storage protocol when you call `outsource()` because this is something that should be under the control of the user who uses this external object.

!!! limitation
    `outsource()` currently always uses the *hash* protocol when it creates a new external object.
    This is a limitation that will be addressed in the future.
    It is possible to change it later to `#!python external("uuid:")` manually if you want to store it in a different location.