Exemplo n.º 1
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
        """)
Exemplo n.º 2
0
    def with_empty_file(sample, expect):
        write("tmp/sample.yml", "")

        sample.datafile.load()

        expect(sample.with_default) == "foo"
        expect(sample.without_default) == ""
Exemplo n.º 3
0
    def with_empty_file(sample, expect):
        write('tmp/sample.yml', "")

        sample.datafile.load()

        expect(sample.with_default) == 'foo'
        expect(sample.without_default) == ''
Exemplo n.º 4
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
            """)
Exemplo n.º 5
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"
            """)
Exemplo n.º 6
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
            """)
Exemplo n.º 7
0
def test_comments_in_matched_files(expect):
    @datafile("../tmp/templates/{self.key}/config.yml")
    class LegacyTemplate:
        key: str
        name: str
        link: str
        default: List[str]
        aliases: List[str]

    write(
        'tmp/templates/foo/config.yml',
        """
        link: # placeholder
        default:
          - # placeholder
          - # placeholder
        aliases:
          - # placeholder
        """,
    )
    write(
        'tmp/templates/bar/config.yml',
        """
        link: http://example.com
        default:
          - abc
          - def
        aliases:
          - qux
        """,
    )

    items = list(LegacyTemplate.objects.all())
    expect(len(items)) == 2
Exemplo n.º 8
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
            """
        )
Exemplo n.º 9
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
            """)
Exemplo n.º 10
0
    def with_conversion_and_defaults(expect):
        write(
            "tmp/sample.yml",
            """
            items: 1, 2.3
            """,
        )

        sample = SampleWithSetAndDefaults()

        expect(sample.items) == {1.0, 2.3}
Exemplo n.º 11
0
    def with_conversion_and_defaults(expect):
        write(
            'tmp/sample.yml',
            """
            items: 1, 2.3
            """,
        )

        sample = SampleWithListAndDefaults()

        expect(sample.items) == [1.0, 2.3]
Exemplo n.º 12
0
    def with_conversion(expect):
        write(
            'tmp/sample.yml',
            """
            items: 1, 2.3
            """,
        )

        sample = SampleWithList(None)

        expect(sample.items) == [1.0, 2.3]
Exemplo n.º 13
0
    def with_conversion(expect):
        write(
            "tmp/sample.yml",
            """
            items: 1, 2.3
            """,
        )

        sample = SampleWithSet(None)

        expect(sample.items) == {1.0, 2.3}
Exemplo n.º 14
0
    def with_partial_file(sample, expect):
        write(
            "tmp/sample.yml",
            """
            without_default: bar
            """,
        )

        sample.datafile.load()

        expect(sample.with_default) == "foo"
        expect(sample.without_default) == "bar"
Exemplo n.º 15
0
def test_partial_load_from_disk(expect):
    write(
        "tmp/inventory/42.yml",
        """
        name: Things"
        """,
    )

    items = list(InventoryItem.objects.all())

    expect(items[0].unit_price) == 0
    expect(items[0].quantity_on_hand) == 0
Exemplo n.º 16
0
    def with_invalid_data(sample, expect):
        write(
            'tmp/sample.yml',
            """
            - foo
            - bar
            """,
        )

        sample.datafile.load()

        expect(sample.int_) == 0
Exemplo n.º 17
0
    def with_null_set_value(expect):
        write(
            "tmp/sample.yml",
            """
            items:
            -
            """,
        )

        sample = SampleWithSetOfDataclasses()

        expect(sample.items) == set()
Exemplo n.º 18
0
    def with_getattribute(expect):
        sample = Sample()

        write(
            "tmp/sample.yml",
            """
            item: b
            """,
        )

        logbreak("Getting attribute")
        expect(sample.item) == "b"
Exemplo n.º 19
0
    def with_partial_file(sample, expect):
        write(
            'tmp/sample.yml',
            """
            without_default: bar
            """,
        )

        sample.datafile.load()

        expect(sample.with_default) == 'foo'
        expect(sample.without_default) == 'bar'
Exemplo n.º 20
0
    def it_wins_against_default_init_values(expect):
        write(
            "tmp/sample.yml",
            """
            bar: e
            """,
        )

        sample = SampleWithDefaults(foo=5)

        expect(sample.foo) == 5
        expect(sample.bar) == "e"
Exemplo n.º 21
0
    def with_null_list_value(expect):
        write(
            'tmp/sample.yml',
            """
            items:
            -
            """,
        )

        sample = SampleWithListOfDataclasses()

        expect(sample.items) == []
Exemplo n.º 22
0
    def with_matching_types(expect):
        write(
            "tmp/sample.yml",
            """
            items:
            - 1.2
            - 3.4
            """,
        )

        sample = SampleWithSet(None)

        expect(sample.items) == {1.2, 3.4}
Exemplo n.º 23
0
    def it_loses_against_init_values(expect):
        write(
            "tmp/sample.yml",
            """
            foo: 3
            bar: c
            """,
        )

        sample = SampleWithDefaults(4, "d")

        expect(sample.foo) == 4
        expect(sample.bar) == "d"
Exemplo n.º 24
0
    def it_wins_when_no_init_values(expect):
        write(
            "tmp/sample.yml",
            """
            foo: 2
            bar: b
            """,
        )

        sample = SampleWithDefaults()

        expect(sample.foo) == 2
        expect(sample.bar) == "b"
Exemplo n.º 25
0
    def with_matching_types(expect):
        write(
            'tmp/sample.yml',
            """
            items:
            - 1.2
            - 3.4
            """,
        )

        sample = SampleWithList(None)

        expect(sample.items) == [1.2, 3.4]
Exemplo n.º 26
0
    def with_partial_set_value(expect):
        write(
            "tmp/sample.yml",
            """
            items:
            - name: abc
            """,
        )

        logbreak()
        sample = SampleWithSetOfDataclasses()
        logbreak()

        expect(sample.items) == {_FrozenNestedSample1(name="abc", score=0.0)}
Exemplo n.º 27
0
    def with_partial_list_value(expect):
        write(
            'tmp/sample.yml',
            """
            items:
            - name: abc
            """,
        )

        logbreak()
        sample = SampleWithListOfDataclasses()
        logbreak()

        expect(sample.items) == [_NestedSample1(name='abc', score=0.0)]
Exemplo n.º 28
0
    def when_file_exists(expect):
        write(
            "tmp/sample.yml",
            """
            a: 1.0
            b: 2.0
            c: 9.9
            """,
        )

        sample = SampleWithFactoryDefaults(1.2, 3.4)

        expect(sample.a) == 1.2
        expect(sample.b) == 3.4
        expect(sample.c) == 9.9
Exemplo n.º 29
0
    def when_file_exists(expect):
        write(
            'tmp/sample.yml',
            """
            a: 1.0
            b: 2.0
            c: 9.9
            """,
        )

        sample = SampleWithComputedDefaults(1.2, 3.4)

        expect(sample.a) == 1.2
        expect(sample.b) == 3.4
        expect(sample.c) == 9.9
Exemplo n.º 30
0
    def with_setattr(expect):
        sample = Sample()

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

        expect(sample.item) == '42'

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