def with_append_on_nested_dataclass(expect):
        sample = SampleWithNesting(1)

        logbreak("Appending to nested list: 2")
        sample.nested.items.append(2)

        expect(read("tmp/sample.yml")) == dedent(
            """
            item: 1
            nested:
              items:
                - 2
            """
        )

        logbreak("Appending to nested list: 3")
        sample.nested.items.append(3)

        expect(read("tmp/sample.yml")) == dedent(
            """
            item: 1
            nested:
              items:
                - 2
                - 3
            """
        )
Example #2
0
    def with_update(expect):
        sample = Sample()

        logbreak()
        sample.data.update({'b': 2})

        expect(read('tmp/sample.yml')) == dedent(
            """
            data:
              a: 1
              b: 2
            """
        )

        sample.datafile.load()

        logbreak()
        sample.data.update({'c': 3})

        expect(read('tmp/sample.yml')) == dedent(
            """
            data:
              a: 1
              b: 2
              c: 3
            """
        )
    def with_update(expect):
        sample = Sample()

        logbreak()
        sample.data.update({"b": 2})

        expect(read("tmp/sample.yml")) == dedent(
            """
            data:
              a: 1
              b: 2
            """
        )

        sample.datafile.load()

        logbreak()
        sample.data.update({"c": 3})

        expect(read("tmp/sample.yml")) == dedent(
            """
            data:
              a: 1
              b: 2
              c: 3
            """
        )
    def with_append(expect):
        sample = Sample()

        logbreak("Appending to list: 2")
        sample.items.append(2)

        expect(read("tmp/sample.yml")) == dedent(
            """
            items:
              - 1
              - 2
            """
        )

        sample.datafile.load()

        logbreak("Appending to list: 3")
        sample.items.append(3)

        expect(read("tmp/sample.yml")) == dedent(
            """
            items:
              - 1
              - 2
              - 3
            """
        )
    def with_float_to_integer(sample, expect):
        sample.number = 1.23

        expect(read("tmp/sample.yml")) == dedent("""
            number: 1.23
            """)

        sample.number = 4

        expect(read("tmp/sample.yml")) == dedent("""
            number: 4
            """)
Example #6
0
def test_generic_converters(expect):
    S = TypeVar("S")
    T = TypeVar("T")

    class Pair(Generic[S, T], converters.Converter):
        first: S
        second: T

        def __init__(self, first: S, second: T) -> None:
            self.first = first
            self.second = second

        @classmethod
        def to_python_value(cls, deserialized_data, *, target_object=None):
            paired = zip(cls.CONVERTERS, deserialized_data)  # type: ignore
            values = [convert.to_python_value(val) for convert, val in paired]
            return cls(*values)

        @classmethod
        def to_preserialization_data(cls,
                                     python_value,
                                     *,
                                     default_to_skip=None):
            values = [python_value.first, python_value.second]
            paired = zip(cls.CONVERTERS, values)  # type: ignore
            return [
                convert.to_preserialization_data(val)
                for convert, val in paired
            ]

    @datafile("../tmp/sample.yml")
    class Dictish:
        contents: List[Pair[str, converters.Number]]

    d = Dictish([Pair[str, converters.Number]("pi", 3.14)])  # type: ignore
    expect(d.datafile.text) == dedent("""
        contents:
          -   - pi
              - 3.14
        """)

    d = Dictish(Missing)  # type: ignore
    expect(d.contents[0].first) == "pi"
    expect(d.contents[0].second) == 3.14

    d.datafile.text = dedent("""
        contents:
          -   - degrees
              - 360
        """)
    expect(d.contents[0].first) == "degrees"
    expect(d.contents[0].second) == 360
    def with_nested_mutables(expect):
        write(
            "tmp/sample.yml",
            """
            name: Test
            roles:
              category1:
                - value1
                - value2
              category2:
                - something
                - else
            """,
        )

        logbreak("Inferring object")
        sample = auto("tmp/sample.yml")

        logbreak("Updating attributes")
        sample.roles["category1"].append("value3")

        logbreak("Reading file")
        expect(read("tmp/sample.yml")) == dedent("""
            name: Test
            roles:
              category1:
                - value1
                - value2
                - value3
              category2:
                - something
                - else
            """)
    def with_floats(expect):
        write(
            "tmp/sample.yml",
            """
            language: python
            python:
              - 3.7
              - 3.8
            """,
        )

        logbreak("Inferring object")
        sample = auto("tmp/sample.yml")

        logbreak("Updating attribute")
        sample.python.append(4)

        logbreak("Reading file")
        expect(read("tmp/sample.yml")) == dedent("""
            language: python
            python:
              - 3.7
              - 3.8
              - 4.0
            """)
def test_float_inference(expect):
    write(
        'tmp/sample.yml',
        """
        language: python
        python:
          - 3.7
          - 3.8
        """,
    )

    logbreak("Inferring object")
    sample = auto('tmp/sample.yml')

    logbreak("Updating attribute")
    sample.python.append(4)

    logbreak("Reading file")
    expect(read('tmp/sample.yml')) == dedent("""
        language: python
        python:
          - 3.7
          - 3.8
          - 4.0
        """)
Example #10
0
    def with_quotes(expect):
        @datafile("../tmp/sample.yml", manual=True)
        class Sample:
            s1: str = ""
            s2: str = ""
            s3: str = ""

        sample = Sample()

        write(
            "tmp/sample.yml",
            """
            s1: a
            s2: 'b'
            s3: "c"
            """,
        )

        sample.datafile.load()
        sample.s1 = "d"
        sample.s2 = "e"
        sample.s3 = "f"
        sample.datafile.save()

        expect(read("tmp/sample.yml")) == dedent("""
            s1: d
            s2: 'e'
            s3: "f"
            """)
Example #11
0
    def with_comments_in_nested_objects(expect):
        sample = SampleWithNestingAndDefaults(None)

        write(
            "tmp/sample.yml",
            """
            # Header
            name: a
            score: 1.0      # Line

            nested:
              # Nested header
              name: n
              score: 2
            """,
        )

        sample.datafile.load()
        sample.score = 3
        sample.nested.score = 4
        sample.datafile.save()

        expect(read("tmp/sample.yml")) == dedent("""
            # Header
            name: a
            score: 3.0      # Line

            nested:
              # Nested header
              name: n
              score: 4.0
            """)
Example #12
0
 def it_handles_lists_of_dicts(expect):
     data = [{"one": 1, "two": 2}]
     text = formats.serialize(data, ".yaml")
     expect(text) == dedent("""
     - one: 1
       two: 2
     """)
Example #13
0
    def with_comments_on_nested_lines(expect):
        sample = SampleWithNestingAndDefaults(None)

        write(
            'tmp/sample.yml',
            """
            # Header
            name: a
            score: 1        # Line

            nested:
              # Nested header
              name: n
              score: 2      # Nested line
            """,
        )

        sample.datafile.load()
        sample.score = 3
        sample.nested.score = 4
        sample.datafile.save()

        expect(read('tmp/sample.yml')) == dedent(
            """
            # Header
            name: a
            score: 3.0      # Line

            nested:
              # Nested header
              name: n
              score: 4.0    # Nested line
            """
        )
Example #14
0
    def with_default_values(expect):
        sample = SampleWithDefaults("a")

        sample.datafile.save()

        expect(read("tmp/sample.yml")) == dedent("""
            without_default: a
            """)
Example #15
0
    def with_custom_values(expect):
        sample = SampleWithDefaults('a', 'b')

        sample.datafile.save()

        expect(read('tmp/sample.yml')) == dedent("""
            without_default: a
            with_default: b
            """)
Example #16
0
    def with_default_values_and_full_save(expect):
        sample = SampleWithDefaults("a", "foo")

        sample.datafile.save(include_default_values=True)

        expect(read("tmp/sample.yml")) == dedent("""
            without_default: a
            with_default: foo
            """)
Example #17
0
    def with_nones(expect):
        sample = SampleWithOptionals(None, None)

        sample.datafile.save()

        expect(read("tmp/sample.yml")) == dedent("""
            required: 0.0
            optional:
            """)
Example #18
0
    def with_values(expect):
        sample = SampleWithOptionals(1, 2)

        sample.datafile.save()

        expect(read("tmp/sample.yml")) == dedent("""
            required: 1.0
            optional: 2.0
            """)
Example #19
0
    def with_custom_fields(expect):
        sample = SampleWithCustomFields("foo", "bar")

        sample.datafile.save()

        with open("tmp/sample.yml") as f:
            expect(f.read()) == dedent("""
                included: foo
                """)
    def with_delitem(expect):
        sample = Sample()

        del sample.items[0]

        expect(read('tmp/sample.yml')) == dedent("""
            items:
              -
            """)
Example #21
0
 def it_indents_blocks_by_default(expect, data):
     text = formats.serialize(data, ".yaml")
     expect(text) == dedent("""
     key: value
     items:
       - 1
       - a
       -
     """)
Example #22
0
    def with_custom_fields(expect):
        sample = SampleWithCustomFields('foo', 'bar')

        sample.datafile.save()

        with open('tmp/sample.yml') as f:
            expect(f.read()) == dedent("""
                included: foo
                """)
    def with_setattr(expect):
        sample = Sample()

        logbreak("Setting attribute")
        sample.item = 'b'

        expect(read('tmp/sample.yml')) == dedent("""
            item: b
            """)
    def with_setitem(expect):
        sample = Sample()

        sample.items[0] = 2

        expect(read('tmp/sample.yml')) == dedent("""
            items:
              - 2
            """)
Example #25
0
    def without_initial_values(sample, expect):
        sample.datafile.save()

        with open("tmp/sample.yml") as f:
            expect(f.read()) == dedent("""
                bool_: false
                int_: 0
                float_: 0.0
                str_: ''
                """)
    def with_setattr(expect):
        sample = Sample()

        sample.item = 42  # type: ignore

        expect(sample.datafile.text) == dedent("""
            item: '42'
            """)

        expect(sample.item) == '42'
Example #27
0
    def with_default_values(expect):
        sample = SampleWithNestingAndDefaults("a")

        sample.datafile.save()

        with open("tmp/sample.yml") as f:
            expect(f.read()) == dedent("""
                name: a
                nested: {}
                """)
Example #28
0
 def it_can_render_lists_inline(expect, data, monkeypatch):
     monkeypatch.setattr(settings, 'INDENT_YAML_BLOCKS', False)
     text = formats.serialize(data, '.yaml', formatter=formats.PyYAML)
     expect(text) == dedent("""
     key: value
     items:
     - 1
     - a
     -
     """)
Example #29
0
    def when_empty(expect):
        sample = SampleWithList([])

        sample.datafile.save()

        with open("tmp/sample.yml") as f:
            expect(f.read()) == dedent("""
                items:
                  -
                """)
Example #30
0
    def with_convertable_initial_values(expect):
        sample = Sample(1, 2, 3, 4)

        sample.datafile.save()

        with open("tmp/sample.yml") as f:
            expect(f.read()) == dedent("""
                bool_: true
                int_: 2
                float_: 3.0
                str_: '4'
                """)