예제 #1
0
def test_single_properties(generate_classes):
    assert generate_classes([
        schema.Class("MyObject",
                     properties=[
                         schema.SingleProperty("one", "x"),
                         schema.SingleProperty("two", "y"),
                         schema.SingleProperty("three", "z"),
                     ]),
    ]) == {
        "MyObject.qll":
        (ql.Stub(name="MyObject", base_import=gen_import_prefix + "MyObject"),
         ql.Class(name="MyObject",
                  final=True,
                  properties=[
                      ql.Property(singular="One",
                                  type="x",
                                  tablename="my_objects",
                                  tableparams=["this", "result", "_", "_"]),
                      ql.Property(singular="Two",
                                  type="y",
                                  tablename="my_objects",
                                  tableparams=["this", "_", "result", "_"]),
                      ql.Property(singular="Three",
                                  type="z",
                                  tablename="my_objects",
                                  tableparams=["this", "_", "_", "result"]),
                  ])),
    }
예제 #2
0
def test_single_properties(opts, input, renderer):
    input.classes = [
        schema.Class("MyObject",
                     properties=[
                         schema.SingleProperty("one", "x"),
                         schema.SingleProperty("two", "y"),
                         schema.SingleProperty("three", "z"),
                     ]),
    ]
    assert generate(opts, renderer) == {
        import_file():
        ql.ImportList([stub_import_prefix + "MyObject"]),
        stub_path() / "MyObject.qll":
        ql.Stub(name="MyObject", base_import=gen_import_prefix + "MyObject"),
        ql_output_path() / "MyObject.qll":
        ql.Class(name="MyObject",
                 final=True,
                 properties=[
                     ql.Property(singular="One",
                                 type="x",
                                 tablename="my_objects",
                                 tableparams=["this", "result", "_", "_"]),
                     ql.Property(singular="Two",
                                 type="y",
                                 tablename="my_objects",
                                 tableparams=["this", "_", "result", "_"]),
                     ql.Property(singular="Three",
                                 type="z",
                                 tablename="my_objects",
                                 tableparams=["this", "_", "_", "result"]),
                 ])
    }
예제 #3
0
def test_single_class_property(opts, input, renderer):
    input.classes = [
        schema.Class("MyObject",
                     properties=[schema.SingleProperty("foo", "Bar")]),
        schema.Class("Bar"),
    ]
    assert generate(opts, renderer) == {
        import_file():
        ql.ImportList(
            [stub_import_prefix + cls for cls in ("Bar", "MyObject")]),
        stub_path() / "MyObject.qll":
        ql.Stub(name="MyObject", base_import=gen_import_prefix + "MyObject"),
        stub_path() / "Bar.qll":
        ql.Stub(name="Bar", base_import=gen_import_prefix + "Bar"),
        ql_output_path() / "MyObject.qll":
        ql.Class(
            name="MyObject",
            final=True,
            imports=[stub_import_prefix + "Bar"],
            properties=[
                ql.Property(singular="Foo",
                            type="Bar",
                            tablename="my_objects",
                            tableparams=["this", "result"]),
            ],
        ),
        ql_output_path() / "Bar.qll":
        ql.Class(name="Bar", final=True)
    }
예제 #4
0
def test_single_class_property(generate_classes, is_child):
    assert generate_classes([
        schema.Class("MyObject",
                     properties=[
                         schema.SingleProperty("foo", "Bar", is_child=is_child)
                     ]),
        schema.Class("Bar"),
    ]) == {
        "MyObject.qll": (ql.Stub(name="MyObject",
                                 base_import=gen_import_prefix + "MyObject"),
                         ql.Class(
                             name="MyObject",
                             final=True,
                             imports=[stub_import_prefix + "Bar"],
                             properties=[
                                 ql.Property(singular="Foo",
                                             type="Bar",
                                             tablename="my_objects",
                                             tableparams=["this", "result"],
                                             is_child=is_child),
                             ],
                         )),
        "Bar.qll": (ql.Stub(name="Bar", base_import=gen_import_prefix + "Bar"),
                    ql.Class(name="Bar", final=True)),
    }
예제 #5
0
def test_property_is_single(is_optional, is_predicate, plural, expected):
    prop = ql.Property("foo",
                       "Foo",
                       "props", ["x"],
                       plural=plural,
                       is_predicate=is_predicate,
                       is_optional=is_optional)
    assert prop.is_single is expected
예제 #6
0
def test_class_has_first_property_marked():
    props = [
        ql.Property(f"Prop{x}", f"Foo{x}", f"props{x}", [f"{x}"])
        for x in range(4)
    ]
    expected = deepcopy(props)
    expected[0].first = True
    cls = ql.Class("Class", properties=props)
    assert cls.properties == expected
예제 #7
0
파일: qlgen.py 프로젝트: raulgarciamsft/ql
def get_ql_property(cls: schema.Class, prop: schema.Property):
    common_args = dict(
        type=prop.type if not prop.is_predicate else "predicate",
        skip_qltest="no_qltest" in prop.tags,
        is_child=prop.is_child,
        is_optional=prop.is_optional,
        is_predicate=prop.is_predicate,
    )
    if prop.is_single:
        return ql.Property(
            **common_args,
            singular=inflection.camelize(prop.name),
            tablename=inflection.tableize(cls.name),
            tableparams=[
                "this"] + ["result" if p is prop else "_" for p in cls.properties if p.is_single],
        )
    elif prop.is_repeated:
        return ql.Property(
            **common_args,
            singular=inflection.singularize(inflection.camelize(prop.name)),
            plural=inflection.pluralize(inflection.camelize(prop.name)),
            tablename=inflection.tableize(f"{cls.name}_{prop.name}"),
            tableparams=["this", "index", "result"],
        )
    elif prop.is_optional:
        return ql.Property(
            **common_args,
            singular=inflection.camelize(prop.name),
            tablename=inflection.tableize(f"{cls.name}_{prop.name}"),
            tableparams=["this", "result"],
        )
    elif prop.is_predicate:
        return ql.Property(
            **common_args,
            singular=inflection.camelize(
                prop.name, uppercase_first_letter=False),
            tablename=inflection.underscore(f"{cls.name}_{prop.name}"),
            tableparams=["this"],
        )
예제 #8
0
def get_ql_property(cls: schema.Class, prop: schema.Property):
    if prop.is_single:
        return ql.Property(
            singular=inflection.camelize(prop.name),
            type=prop.type,
            tablename=inflection.tableize(cls.name),
            tableparams=["this"] + [
                "result" if p is prop else "_"
                for p in cls.properties if p.is_single
            ],
        )
    elif prop.is_repeated:
        return ql.Property(
            singular=inflection.singularize(inflection.camelize(prop.name)),
            plural=inflection.pluralize(inflection.camelize(prop.name)),
            type=prop.type,
            tablename=inflection.tableize(f"{cls.name}_{prop.name}"),
            tableparams=["this", "index", "result"],
            is_optional=prop.is_optional,
        )
    elif prop.is_optional:
        return ql.Property(
            singular=inflection.camelize(prop.name),
            type=prop.type,
            tablename=inflection.tableize(f"{cls.name}_{prop.name}"),
            tableparams=["this", "result"],
            is_optional=True,
        )
    elif prop.is_predicate:
        return ql.Property(
            singular=inflection.camelize(prop.name,
                                         uppercase_first_letter=False),
            type="predicate",
            tablename=inflection.underscore(f"{cls.name}_{prop.name}"),
            tableparams=["this"],
            is_predicate=True,
        )
예제 #9
0
def test_single_property(generate_classes):
    assert generate_classes([
        schema.Class("MyObject",
                     properties=[schema.SingleProperty("foo", "bar")]),
    ]) == {
        "MyObject.qll":
        (ql.Stub(name="MyObject", base_import=gen_import_prefix + "MyObject"),
         ql.Class(name="MyObject",
                  final=True,
                  properties=[
                      ql.Property(singular="Foo",
                                  type="bar",
                                  tablename="my_objects",
                                  tableparams=["this", "result"]),
                  ])),
    }
예제 #10
0
def test_predicate_property(generate_classes):
    assert generate_classes([
        schema.Class("MyObject",
                     properties=[schema.PredicateProperty("is_foo")]),
    ]) == {
        "MyObject.qll": (ql.Stub(name="MyObject",
                                 base_import=gen_import_prefix + "MyObject"),
                         ql.Class(name="MyObject",
                                  final=True,
                                  properties=[
                                      ql.Property(singular="isFoo",
                                                  type="predicate",
                                                  tablename="my_object_is_foo",
                                                  tableparams=["this"],
                                                  is_predicate=True),
                                  ])),
    }
예제 #11
0
def test_repeated_property(opts, input, renderer):
    input.classes = [
        schema.Class("MyObject",
                     properties=[schema.RepeatedProperty("foo", "bar")]),
    ]
    assert generate(opts, renderer) == {
        import_file():
        ql.ImportList([stub_import_prefix + "MyObject"]),
        stub_path() / "MyObject.qll":
        ql.Stub(name="MyObject", base_import=gen_import_prefix + "MyObject"),
        ql_output_path() / "MyObject.qll":
        ql.Class(name="MyObject",
                 final=True,
                 properties=[
                     ql.Property(singular="Foo",
                                 plural="Foos",
                                 type="bar",
                                 tablename="my_object_foos",
                                 tableparams=["this", "index", "result"]),
                 ])
    }
예제 #12
0
def test_predicate_property(opts, input, renderer):
    input.classes = [
        schema.Class("MyObject",
                     properties=[schema.PredicateProperty("is_foo")]),
    ]
    assert generate(opts, renderer) == {
        import_file():
        ql.ImportList([stub_import_prefix + "MyObject"]),
        stub_path() / "MyObject.qll":
        ql.Stub(name="MyObject", base_import=gen_import_prefix + "MyObject"),
        ql_output_path() / "MyObject.qll":
        ql.Class(name="MyObject",
                 final=True,
                 properties=[
                     ql.Property(singular="isFoo",
                                 type="predicate",
                                 tablename="my_object_is_foo",
                                 tableparams=["this"],
                                 is_predicate=True),
                 ])
    }
예제 #13
0
def test_repeated_property(generate_classes, is_child):
    assert generate_classes([
        schema.Class("MyObject",
                     properties=[
                         schema.RepeatedProperty("foo",
                                                 "bar",
                                                 is_child=is_child)
                     ]),
    ]) == {
        "MyObject.qll":
        (ql.Stub(name="MyObject", base_import=gen_import_prefix + "MyObject"),
         ql.Class(name="MyObject",
                  final=True,
                  properties=[
                      ql.Property(singular="Foo",
                                  plural="Foos",
                                  type="bar",
                                  tablename="my_object_foos",
                                  tableparams=["this", "index", "result"],
                                  is_child=is_child),
                  ])),
    }
예제 #14
0
def test_property_no_plural_no_indefinite_getter():
    prop = ql.Property("Prop", "Foo", "props", ["x"])
    assert prop.indefinite_getter is None
예제 #15
0
def test_property_has_first_table_param_marked():
    tableparams = ["a", "b", "c"]
    prop = ql.Property("Prop", "foo", "props", tableparams)
    assert prop.tableparams[0].first
    assert [p.param for p in prop.tableparams] == tableparams
예제 #16
0
def test_property_predicate_getter():
    prop = ql.Property("prop", is_predicate=True)
    assert prop.getter == "prop"
예제 #17
0
def test_property_getter():
    prop = ql.Property("Prop", "Foo")
    assert prop.getter == "getProp"
예제 #18
0
def test_property_is_plural(plural, expected):
    prop = ql.Property("foo", "Foo", "props", ["x"], plural=plural)
    assert prop.is_repeated is expected
예제 #19
0
def test_property_indefinite_article(name, expected_getter):
    prop = ql.Property(name, plural="X")
    assert prop.indefinite_getter == expected_getter
예제 #20
0
def test_property_is_a_class(type, expected):
    tableparams = ["a", "result", "b"]
    expected_tableparams = ["a", "x" if expected else "result", "b"]
    prop = ql.Property("Prop", type, tableparams=tableparams)
    assert prop.type_is_class is expected
    assert [p.param for p in prop.tableparams] == expected_tableparams