コード例 #1
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
            """
        )
コード例 #2
0
    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
            """
        )
コード例 #3
0
    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
            """
        )
コード例 #4
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
            """
        )
コード例 #5
0
    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
            """)
コード例 #6
0
    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
            """)
コード例 #7
0
    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
            """)
コード例 #8
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"
            """)
コード例 #9
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
            """)
コード例 #10
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
        """)
コード例 #11
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
            """
        )
コード例 #12
0
    def with_default_values(expect):
        sample = SampleWithDefaults("a")

        sample.datafile.save()

        expect(read("tmp/sample.yml")) == dedent("""
            without_default: a
            """)
コード例 #13
0
    def with_delitem(expect):
        sample = Sample()

        del sample.items[0]

        expect(read('tmp/sample.yml')) == dedent("""
            items:
              -
            """)
コード例 #14
0
    def with_setattr(expect):
        sample = Sample()

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

        expect(read('tmp/sample.yml')) == dedent("""
            item: b
            """)
コード例 #15
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
            """)
コード例 #16
0
    def with_setitem(expect):
        sample = Sample()

        sample.items[0] = 2

        expect(read('tmp/sample.yml')) == dedent("""
            items:
              - 2
            """)
コード例 #17
0
    def with_nones(expect):
        sample = SampleWithOptionals(None, None)

        sample.datafile.save()

        expect(read("tmp/sample.yml")) == dedent("""
            required: 0.0
            optional:
            """)
コード例 #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
            """)
コード例 #19
0
ファイル: test_saving.py プロジェクト: tonybenoy/datafiles
    def with_custom_values(expect):
        sample = SampleWithDefaults('a', 'b')

        sample.datafile.save()

        expect(read('tmp/sample.yml')) == dedent("""
            without_default: a
            with_default: b
            """)
コード例 #20
0
    def with_setattr_on_nested_dataclass(expect):
        sample = SampleWithNesting(2)

        logbreak("Setting nested attribute")
        sample.nested.name = 'd'

        logbreak("Reading file")
        expect(read('tmp/sample.yml')) == dedent("""
            item: 2
            nested:
              name: d
            """)
コード例 #21
0
    def with_extra_newlines(sample, expect):
        sample.text = "\nabc\ndef\n\n"

        expect(read("tmp/sample.yml")) == dedent("""
            text: |
              abc
              def
            """)

        sample.datafile.load()
        sample.datafile.save()

        expect(sample.text) == "abc\ndef\n"
コード例 #22
0
    def when_nested_dataclass_is_none(expect):
        @datafile
        class Name:
            value: str

        @datafile("../tmp/samples/{self.key}.yml")
        class Sample:

            key: int
            name: Optional[Name]
            value: float = 0.0

        sample = Sample(42, None)

        expect(read("tmp/samples/42.yml")) == dedent("""
            name:
            """)
コード例 #23
0
    def with_comments_on_list_items(expect):
        sample = SampleWithListOfDataclasses()

        write(
            'tmp/sample.yml',
            """
            # Header

            items:  # Subheader

              # Section

              - name: a     # Item
                score: 1

              # Section

              - name: b     # Item
                score: 2
            """,
        )

        sample.datafile.load()
        sample.items[1].name = 'c'
        sample.datafile.save()

        expect(read('tmp/sample.yml')) == dedent(
            """
            # Header

            items:  # Subheader

              # Section

              - name: a     # Item
                score: 1

              # Section

              - name: c     # Item
                score: 2
            """
        )
コード例 #24
0
    def with_extra_lines(expect):
        sample = SampleWithOptionals(1, 2)

        write(
            "tmp/sample.yml",
            """
            required: 1.0

            optional: 2.0
            """,
        )

        sample.datafile.load()
        sample.optional = 3
        sample.datafile.save()

        expect(read("tmp/sample.yml")) == dedent("""
            required: 1.0

            optional: 3.0
            """)
コード例 #25
0
    def with_multiple_lines(sample, expect):
        sample.text = '\n'.join(f'Line {i+1}' for i in range(3))

        expect(read('tmp/sample.yml')) == dedent("""
            text: |
              Line 1
              Line 2
              Line 3
            """)

        write(
            'tmp/sample.yml',
            """
            text: |
              Line 4
              Line 5
              Line 6
            """,
        )

        expect(sample.text) == "Line 4\nLine 5\nLine 6\n"
コード例 #26
0
    def with_multiple_lines(sample, expect):
        sample.text = "\n".join(f"Line {i+1}" for i in range(3))

        expect(read("tmp/sample.yml")) == dedent("""
            text: |
              Line 1
              Line 2
              Line 3
            """)

        write(
            "tmp/sample.yml",
            """
            text: |
              Line 4
              Line 5
              Line 6
            """,
        )

        expect(sample.text) == "Line 4\nLine 5\nLine 6\n"
コード例 #27
0
    def with_comments(expect):
        sample = SampleWithOptionals(1, 2)

        write(
            "tmp/sample.yml",
            """
            # Header
            required: 1.0       # Line
            optional: 2.0
            """,
        )

        sample.datafile.load()
        sample.required = 3
        sample.datafile.save()

        expect(read("tmp/sample.yml")) == dedent("""
            # Header
            required: 3.0       # Line
            optional: 2.0
            """)
コード例 #28
0
def test_auto_with_sample_file(expect):
    write(
        'tmp/sample.yml',
        """
        homogeneous_list:
          - 1
          - 2
        heterogeneous_list:
          - 1
          - 'abc'
        empty_list: []
        """,
    )

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

    logbreak("Reading attributes")
    expect(sample.homogeneous_list) == [1, 2]
    expect(sample.heterogeneous_list) == [1, 'abc']
    expect(sample.empty_list) == []

    logbreak("Updating attribute")
    sample.homogeneous_list.append(3.4)
    sample.heterogeneous_list.append(5.6)
    sample.empty_list.append(7.8)

    logbreak("Reading file")
    expect(read('tmp/sample.yml')) == dedent("""
        homogeneous_list:
          - 1
          - 2
          - 3
        heterogeneous_list:
          - 1
          - 'abc'
          - 5.6
        empty_list:
          - 7.8
        """)
コード例 #29
0
    def with_quotations(expect):
        sample = Sample(None, None, None, "42")

        write(
            'tmp/sample.yml',
            """
            str_: "42"
            """,
        )

        sample.datafile.load()
        sample.float_ = 1
        sample.datafile.save()

        expect(read('tmp/sample.yml')) == dedent(
            """
            str_: "42"
            bool_: false
            int_: 0
            float_: 1.0
            """
        )
コード例 #30
0
ファイル: test_saving.py プロジェクト: tonybenoy/datafiles
    def with_comments_in_nested_objects(expect):
        sample = SampleWithNestingAndDefaults(None)

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

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

        logbreak("Loading")
        sample.datafile.load()

        logbreak("Modifying")
        sample.score = 3
        sample.nested.score = 4

        logbreak("Saving")
        sample.datafile.save()

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

            nested:
              # Nested heading
              name: n
              score: 4.0
            """)