Esempio n. 1
0
def test_store_po_deserializer_custom(test_fs, store_po):

    class DeserializerCheck(object):
        original_data = None
        context = None

    checker = DeserializerCheck()

    class EGDeserializer(Deserializer):

        @property
        def output(self):
            checker.context = self.context
            checker.original_data = self.original_data
            return self.original_data

    @provider(deserializers, sender=Project)
    def provide_deserializers(**kwargs):
        return dict(eg_deserializer=EGDeserializer)

    with test_fs.open("data/po/complex.po") as test_file:
        test_string = test_file.read()

    # add config to the project
    project = store_po.translation_project.project
    config.get().set_config(
        "pootle.core.deserializers",
        ["eg_deserializer"],
        project)
    store_po.deserialize(test_string)
    assert checker.original_data == test_string
    assert checker.context == store_po
Esempio n. 2
0
def test_cmd_config_list_instance_object_field(capfd):
    project = Project.objects.get(code="project0")

    # no config set for anything
    call_command("config", "pootle_project.project", project.code, "-o",
                 "code")
    out, err = capfd.readouterr()
    _test_config_list(out,
                      model=Project,
                      instance=project,
                      object_field="code")

    # no config set for key foo
    call_command("config", "pootle_project.project", project.code, "-o",
                 "code", "-l", "foo")
    out, err = capfd.readouterr()
    _test_config_list(out,
                      model=Project,
                      instance=project,
                      object_field="code")

    config.get(Project, instance=project).set_config("foo", ["bar"])

    call_command("config", "pootle_project.project", project.code, "-o",
                 "code")
    out, err = capfd.readouterr()
    _test_config_list(out,
                      model=Project,
                      instance=project,
                      object_field="code")
Esempio n. 3
0
def test_cmd_config_get_model(capfd):
    # -g requires a key
    with pytest.raises(CommandError):
        call_command(
            "config",
            "pootle_project.project",
            "-g")
    # no config set for key foo
    call_command(
        "config",
        "pootle_project.project",
        "-g", "foo")
    out, err = capfd.readouterr()
    _test_config_get(out, "foo", model=Project)
    config.get(Project).set_config("foo", ["bar"])
    call_command(
        "config",
        "pootle_project.project",
        "-g", "foo")
    out, err = capfd.readouterr()
    _test_config_get(out, "foo", model=Project)

    config.get(Project).append_config("foo", ["bar"])
    # multiple objects
    with pytest.raises(CommandError):
        call_command(
            "config",
            "pootle_project.project",
            "-g", "foo")
Esempio n. 4
0
 def get_conf(self, **kwargs):
     if kwargs["object"] and not kwargs["content_type"]:
         raise CommandError("You must set --content-type (-t) when using "
                            "--object-pk (-o)")
     if kwargs["content_type"]:
         if "." not in kwargs["content_type"]:
             raise CommandError(
                 "content_type should be set with $app_label.$model_name")
         parts = kwargs["content_type"].split(".")
         model_name = parts[-1]
         app_label = ".".join(parts[:-1])
         try:
             ct = ContentType.objects.get(app_label=app_label,
                                          model=model_name)
         except ContentType.DoesNotExist as e:
             raise CommandError(e)
         ct_model = ct.model_class()
         if kwargs["object"]:
             if kwargs["object_field"]:
                 model_query = {kwargs["object_field"]: kwargs["object"]}
             else:
                 model_query = dict(pk=kwargs["object"])
             catch_exc = (ct_model.DoesNotExist,
                          ct_model.MultipleObjectsReturned, ValueError,
                          FieldError)
             try:
                 instance = ct_model.objects.get(**model_query)
             except catch_exc as e:
                 raise CommandError(e)
             return config.get(ct_model, instance=instance)
         return config.get(ct_model)
     return config.get()
Esempio n. 5
0
def test_cmd_config_append(capfd):

    # -s requires a key and value
    with pytest.raises(CommandError):
        call_command("config", "-a")
    with pytest.raises(CommandError):
        call_command("config", "-a", "foo")

    call_command("config", "-a", "foo", "bar")
    assert config.get().get_config("foo") == "bar"

    # we can add another with same k/v
    call_command("config", "-a", "foo", "bar")

    assert config.get().list_config("foo") == [
        (u'foo', u'bar'), (u'foo', u'bar')]

    # and another with different v
    call_command("config", "-a", "foo", "bar2")
    assert config.get().list_config("foo") == [
        (u'foo', u'bar'),
        (u'foo', u'bar'),
        (u'foo', u'bar2')]

    # and another with different k
    call_command("config", "-a", "foo2", "bar3")
    assert config.get().get_config("foo2") == "bar3"
Esempio n. 6
0
def test_cmd_config_set_model(capfd):

    # -s requires a key and value
    with pytest.raises(CommandError):
        call_command(
            "config",
            "pootle_project.project",
            "-s")
    with pytest.raises(CommandError):
        call_command(
            "config",
            "pootle_project.project",
            "-s", "foo")

    call_command(
        "config",
        "pootle_project.project",
        "-s", "foo", "bar")
    assert config.get(Project).get_config("foo") == "bar"

    # we can set it something else
    call_command(
        "config",
        "pootle_project.project",
        "-s", "foo", "bar2")
    assert config.get(Project).get_config("foo") == "bar2"

    config.get(Project).append_config("foo", "bar3")
    # key must be unique for -s or non-existent
    with pytest.raises(CommandError):
        call_command(
            "config",
            "pootle_project.project",
            "-s", "foo", "bar")
Esempio n. 7
0
def test_store_po_serializer_custom(test_fs, store_po):
    class SerializerCheck(object):
        original_data = None
        context = None

    checker = SerializerCheck()

    class EGSerializer(Serializer):
        @property
        def output(self):
            checker.original_data = self.original_data
            checker.context = self.context

    @provider(serializers, sender=Project)
    def provide_serializers(**kwargs):
        return dict(eg_serializer=EGSerializer)

    with test_fs.open("data/po/complex.po") as test_file:
        test_string = test_file.read()
        # ttk_po = getclass(test_file)(test_string)
    store_po.update(store_po.deserialize(test_string))

    # add config to the project
    project = store_po.translation_project.project
    config.get(project.__class__,
               instance=project).set_config("pootle.core.serializers",
                                            ["eg_serializer"])

    store_po.serialize()
    assert checker.context == store_po
    assert (not isinstance(checker.original_data, six.text_type)
            and isinstance(checker.original_data, str))
    assert checker.original_data == _store_as_string(store_po)
Esempio n. 8
0
def test_cmd_config_append(capfd):

    # -s requires a key and value
    with pytest.raises(CommandError):
        call_command("config", "-a")
    with pytest.raises(CommandError):
        call_command("config", "-a", "foo")

    call_command("config", "-a", "foo", "bar")
    assert config.get().get_config("foo") == "bar"

    # we can add another with same k/v
    call_command("config", "-a", "foo", "bar")

    assert config.get().list_config("foo") == [(u'foo', u'bar'),
                                               (u'foo', u'bar')]

    # and another with different v
    call_command("config", "-a", "foo", "bar2")
    assert config.get().list_config("foo") == [(u'foo', u'bar'),
                                               (u'foo', u'bar'),
                                               (u'foo', u'bar2')]

    # and another with different k
    call_command("config", "-a", "foo2", "bar3")
    assert config.get().get_config("foo2") == "bar3"
Esempio n. 9
0
def test_store_po_deserializer_custom(test_fs, store_po):
    class DeserializerCheck(object):
        original_data = None
        context = None

    checker = DeserializerCheck()

    class EGDeserializer(Deserializer):
        @property
        def output(self):
            checker.context = self.context
            checker.original_data = self.original_data
            return self.original_data

    @provider(deserializers, sender=Project)
    def provide_deserializers(**kwargs):
        return dict(eg_deserializer=EGDeserializer)

    with test_fs.open("data/po/complex.po") as test_file:
        test_string = test_file.read()

    # add config to the project
    project = store_po.translation_project.project
    config.get().set_config("pootle.core.deserializers", ["eg_deserializer"],
                            project)
    store_po.deserialize(test_string)
    assert checker.original_data == test_string
    assert checker.context == store_po
Esempio n. 10
0
def test_cmd_config_set_model(capfd):

    # -s requires a key and value
    with pytest.raises(CommandError):
        call_command(
            "config",
            "pootle_project.project",
            "-s")
    with pytest.raises(CommandError):
        call_command(
            "config",
            "pootle_project.project",
            "-s", "foo")

    call_command(
        "config",
        "pootle_project.project",
        "-s", "foo", "bar")
    assert config.get(Project).get_config("foo") == "bar"

    # we can set it something else
    call_command(
        "config",
        "pootle_project.project",
        "-s", "foo", "bar2")
    assert config.get(Project).get_config("foo") == "bar2"

    config.get(Project).append_config("foo", "bar3")
    # key must be unique for -s or non-existent
    with pytest.raises(CommandError):
        call_command(
            "config",
            "pootle_project.project",
            "-s", "foo", "bar")
Esempio n. 11
0
def test_cmd_config_get_model(capfd):
    # -g requires a key
    with pytest.raises(CommandError):
        call_command(
            "config",
            "pootle_project.project",
            "-g")
    # no config set for key foo
    call_command(
        "config",
        "pootle_project.project",
        "-g", "foo")
    out, err = capfd.readouterr()
    _test_config_get(out, "foo", model=Project)
    config.get(Project).set_config("foo", ["bar"])
    call_command(
        "config",
        "pootle_project.project",
        "-g", "foo")
    out, err = capfd.readouterr()
    _test_config_get(out, "foo", model=Project)

    config.get(Project).append_config("foo", ["bar"])
    # multiple objects
    with pytest.raises(CommandError):
        call_command(
            "config",
            "pootle_project.project",
            "-g", "foo")
Esempio n. 12
0
def test_cmd_config_append_instance(capfd):
    project = Project.objects.get(code="project0")

    # -s requires a key and value
    with pytest.raises(CommandError):
        call_command("config", "pootle_project.project", str(project.pk), "-a")
    with pytest.raises(CommandError):
        call_command("config", "pootle_project.project", str(project.pk), "-a",
                     "foo")

    call_command("config", "pootle_project.project", str(project.pk), "-a",
                 "foo", "bar")
    assert config.get(Project, instance=project).get_config("foo") == "bar"

    # we can add another with same k/v
    call_command("config", "pootle_project.project", str(project.pk), "-a",
                 "foo", "bar")
    assert config.get(Project, instance=project).list_config("foo") == [
        (u'foo', u'bar'), (u'foo', u'bar')
    ]

    # and another with different v
    call_command("config", "pootle_project.project", str(project.pk), "-a",
                 "foo", "bar2")
    assert config.get(Project, instance=project).list_config("foo") == [
        (u'foo', u'bar'), (u'foo', u'bar'), (u'foo', u'bar2')
    ]

    # and another with different k
    call_command("config", "pootle_project.project", str(project.pk), "-a",
                 "foo2", "bar3")
    assert config.get(Project, instance=project).get_config("foo2") == "bar3"
Esempio n. 13
0
def test_cmd_config_long_key_name(capfd):
    project = Project.objects.get(code="project0")
    config.get(Project, instance=project).append_config("foobar" * 10, "bar")
    call_command(
        "config",
        "pootle_project.project",
        str(project.pk))
    out, err = capfd.readouterr()
    _test_config_list(out, model=Project, instance=project)
Esempio n. 14
0
def test_cmd_config_long_key_name(no_config_env, capfd):
    project = Project.objects.get(code="project0")
    config.get(Project, instance=project).append_config("foobar" * 10, "bar")
    call_command(
        "config",
        "pootle_project.project",
        str(project.pk))
    out, err = capfd.readouterr()
    _test_config_list(out, model=Project, instance=project)
Esempio n. 15
0
def test_cmd_config_append_instance(capfd):
    project = Project.objects.get(code="project0")

    # -s requires a key and value
    with pytest.raises(CommandError):
        call_command(
            "config",
            "pootle_project.project",
            str(project.pk),
            "-a")
    with pytest.raises(CommandError):
        call_command(
            "config",
            "pootle_project.project",
            str(project.pk),
            "-a", "foo")

    call_command(
        "config",
        "pootle_project.project",
        str(project.pk),
        "-a", "foo", "bar")
    assert config.get(
        Project,
        instance=project).get_config("foo") == "bar"

    # we can add another with same k/v
    call_command(
        "config",
        "pootle_project.project",
        str(project.pk),
        "-a", "foo", "bar")
    assert config.get(Project, instance=project).list_config("foo") == [
        (u'foo', u'bar'), (u'foo', u'bar')]

    # and another with different v
    call_command(
        "config",
        "pootle_project.project",
        str(project.pk),
        "-a", "foo", "bar2")
    assert config.get(Project, instance=project).list_config("foo") == [
        (u'foo', u'bar'),
        (u'foo', u'bar'),
        (u'foo', u'bar2')]

    # and another with different k
    call_command(
        "config",
        "pootle_project.project",
        str(project.pk),
        "-a", "foo2", "bar3")
    assert config.get(Project, instance=project).get_config("foo2") == "bar3"
Esempio n. 16
0
def test_cmd_config_long_instance_name(project0, no_config_env, capfd):
    project0.code = "foobar" * 10
    project0.save()
    config.get(Project, instance=project0).append_config("foo", "bar")
    config.get(Project, instance=project0).append_config("foo", "bar")
    call_command("config", "pootle_project.project", project0.code, "-o",
                 "code")
    out, err = capfd.readouterr()
    _test_config_list(out,
                      model=Project,
                      instance=project0,
                      object_field="code")
Esempio n. 17
0
def test_cmd_config_long_instance_name(project0, no_config_env, capfd):
    project0.code = "foobar" * 10
    project0.save()
    config.get(Project, instance=project0).append_config("foo", "bar")
    config.get(Project, instance=project0).append_config("foo", "bar")
    call_command(
        "config",
        "pootle_project.project",
        project0.code,
        "-o", "code")
    out, err = capfd.readouterr()
    _test_config_list(out, model=Project, instance=project0, object_field="code")
Esempio n. 18
0
def test_config_setter(no_config_env):
    config.get().set_config("foo", ["bar"])
    assert config.get(key="foo") == ["bar"]

    config.get(Project).set_config("foo", ["bar2"])
    assert config.get(Project, key="foo") == ["bar2"]

    project = Project.objects.get(code="project0")
    config.get(Project).set_config("foo", ["bar3"], project)
    assert config.get(Project, instance=project, key="foo") == ["bar3"]
Esempio n. 19
0
def test_config_setter():
    config.get().set_config("foo", ["bar"])
    assert config.get(key="foo") == ["bar"]

    config.get(Project).set_config("foo", ["bar2"])
    assert config.get(Project, key="foo") == ["bar2"]

    project = Project.objects.get(code="project0")
    config.get(Project).set_config("foo", ["bar3"], project)
    assert config.get(Project, instance=project, key="foo") == ["bar3"]
Esempio n. 20
0
def test_store_set_bad_serializers(store_po):
    project = store_po.translation_project.project
    with pytest.raises(ConfigurationError):
        config.get(project.__class__, instance=project).set_config(
            "pootle.core.serializers",
            ["SERIALIZER_DOES_NOT_EXIST"])

    class EGSerializer(Serializer):
        pass

    @provider(serializers)
    def provide_serializers(**kwargs):
        return dict(eg_serializer=EGSerializer)

    # must be list
    with pytest.raises(ConfigurationError):
        config.get(project.__class__, instance=project).set_config(
            "pootle.core.serializers",
            "eg_serializer")
    with pytest.raises(ConfigurationError):
        config.get(project.__class__, instance=project).set_config(
            "pootle.core.serializers",
            dict(serializer="eg_serializer"))

    config.get(project.__class__, instance=project).set_config(
        "pootle.core.serializers",
        ["eg_serializer"])
Esempio n. 21
0
def test_store_set_bad_serializers(store_po):
    project = store_po.translation_project.project
    with pytest.raises(ConfigurationError):
        config.get(project.__class__,
                   instance=project).set_config("pootle.core.serializers",
                                                ["SERIALIZER_DOES_NOT_EXIST"])

    class EGSerializer(Serializer):
        pass

    @provider(serializers)
    def provide_serializers(**kwargs):
        return dict(eg_serializer=EGSerializer)

    # must be list
    with pytest.raises(ConfigurationError):
        config.get(project.__class__,
                   instance=project).set_config("pootle.core.serializers",
                                                "eg_serializer")
    with pytest.raises(ConfigurationError):
        config.get(project.__class__, instance=project).set_config(
            "pootle.core.serializers", dict(serializer="eg_serializer"))

    config.get(project.__class__,
               instance=project).set_config("pootle.core.serializers",
                                            ["eg_serializer"])
Esempio n. 22
0
def test_config_qs_chaining(no_config_env):

    config.get().set_config("foo", "bar")
    assert config.get().none().get_config("foo") == "bar"

    config.get(Project).set_config("foo", "bar2")
    assert config.get(Project).none().get_config("foo") == "bar2"

    project = Project.objects.get(code="project0")
    config.get(Project, instance=project).set_config("foo", "bar3")
    assert config.get(Project,
                      instance=project).none().get_config("foo") == "bar3"
Esempio n. 23
0
def test_config_qs_chaining():

    config.get().set_config("foo", "bar")
    assert config.get().none().get_config("foo") == "bar"

    config.get(Project).set_config("foo", "bar2")
    assert config.get(Project).none().get_config("foo") == "bar2"

    project = Project.objects.get(code="project0")
    config.get(Project, instance=project).set_config("foo", "bar3")
    assert config.get(
        Project, instance=project).none().get_config("foo") == "bar3"
Esempio n. 24
0
def test_add_store_fs_by_path(english):
    """Add a store_fs for pootle_path
    """
    project = ProjectDBFactory(source_language=english)
    language = LanguageDBFactory()
    tp = TranslationProjectFactory(project=project, language=language)
    store = StoreDBFactory(
        translation_project=tp,
        parent=tp.directory,
        name="example_store.po")
    conf = config.get(tp.project.__class__, instance=tp.project)
    conf.set_config("pootle_fs.fs_type", "localfs")
    conf.set_config("pootle_fs.fs_url", "foo")
    fs_path = "/some/fs/example_store.po"
    pootle_path = store.pootle_path
    store_fs = StoreFS.objects.create(
        pootle_path=pootle_path,
        path=fs_path)
    assert store_fs.project == project
    assert store_fs.store == store
    assert store_fs.pootle_path == pootle_path
    assert store_fs.path == fs_path
    assert store_fs.last_sync_hash is None
    assert store_fs.last_sync_mtime is None
    assert store_fs.last_sync_revision is None
Esempio n. 25
0
def test_cmd_config_list(capfd):
    # no config set for anything
    call_command("config")
    out, err = capfd.readouterr()
    _test_config_list(out)

    # no config set for key foo
    call_command("config", "-l", "foo")
    out, err = capfd.readouterr()
    _test_config_list(out)

    config.get().set_config("foo", ["bar"])

    call_command("config")
    out, err = capfd.readouterr()
    _test_config_list(out)
Esempio n. 26
0
def test_cmd_config_list(capfd):
    # no config set for anything
    call_command("config")
    out, err = capfd.readouterr()
    _test_config_list(out)

    # no config set for key foo
    call_command("config", "-l", "foo")
    out, err = capfd.readouterr()
    _test_config_list(out)

    config.get().set_config("foo", ["bar"])

    call_command("config")
    out, err = capfd.readouterr()
    _test_config_list(out)
Esempio n. 27
0
def test_cmd_config_list_model(capfd):
    # no config set for anything
    call_command("config", "pootle_project.project")
    out, err = capfd.readouterr()
    _test_config_list(out, model=Project)

    # no config set for key foo
    call_command("config", "pootle_project.project", "-l", "foo")
    out, err = capfd.readouterr()
    _test_config_list(out, model=Project)

    config.get(Project).set_config("foo", ["bar"])

    call_command("config", "pootle_project.project")
    out, err = capfd.readouterr()
    _test_config_list(out, model=Project)
Esempio n. 28
0
def test_cmd_config_list_model(capfd):
    # no config set for anything
    call_command("config", "pootle_project.project")
    out, err = capfd.readouterr()
    _test_config_list(out, model=Project)

    # no config set for key foo
    call_command("config", "pootle_project.project", "-l", "foo")
    out, err = capfd.readouterr()
    _test_config_list(out, model=Project)

    config.get(Project).set_config("foo", ["bar"])

    call_command("config", "pootle_project.project")
    out, err = capfd.readouterr()
    _test_config_list(out, model=Project)
Esempio n. 29
0
def test_add_store_fs_by_path(english):
    """Add a store_fs for pootle_path
    """
    project = ProjectDBFactory(source_language=english)
    language = LanguageDBFactory()
    tp = TranslationProjectFactory(project=project, language=language)
    store = StoreDBFactory(
        translation_project=tp,
        parent=tp.directory,
        name="example_store.po")
    conf = config.get(tp.project.__class__, instance=tp.project)
    conf.set_config("pootle_fs.fs_type", "localfs")
    conf.set_config("pootle_fs.fs_url", "foo")
    fs_path = "/some/fs/example_store.po"
    pootle_path = store.pootle_path
    store_fs = StoreFS.objects.create(
        pootle_path=pootle_path,
        path=fs_path)
    assert store_fs.project == project
    assert store_fs.store == store
    assert store_fs.pootle_path == pootle_path
    assert store_fs.path == fs_path
    assert store_fs.last_sync_hash is None
    assert store_fs.last_sync_mtime is None
    assert store_fs.last_sync_revision is None
Esempio n. 30
0
def test_fs_cmd_no_projects(capsys):
    for project in Project.objects.all():
        conf = config.get(Project, instance=project)
        conf.clear_config("pootle_fs.fs_type")
        conf.clear_config("pootle_fs.fs_url")
    call_command("fs")
    out, err = capsys.readouterr()
    assert out == "No projects configured\n"
Esempio n. 31
0
 def project_serializers(self):
     project = self.store.translation_project.project
     return (
         config.get(
             project.__class__,
             instance=project,
             key="pootle.core.serializers")
         or [])
Esempio n. 32
0
 def project_serializers(self):
     project = self.store.translation_project.project
     return (
         config.get(
             project.__class__,
             instance=project,
             key="pootle.core.serializers")
         or [])
Esempio n. 33
0
def test_fs_cmd_no_projects(capsys):
    for project in Project.objects.all():
        conf = config.get(Project, instance=project)
        conf.clear_config("pootle_fs.fs_type")
        conf.clear_config("pootle_fs.fs_url")
    call_command("fs")
    out, err = capsys.readouterr()
    assert out == "No projects configured\n"
Esempio n. 34
0
def _test_config_get(out, key, model=None, instance=None, as_repr=False):
    expected = ""
    conf = config.get(model, instance=instance)
    expected = conf.get_config(key)
    expected_class = type(expected).__name__
    expected = json.dumps(expected)
    if not as_repr:
        expected = "%s(%s)" % (expected_class, expected)
    assert expected == out
Esempio n. 35
0
def _test_config_get(out, key, model=None, instance=None, as_repr=False):
    expected = ""
    conf = config.get(model, instance=instance)
    expected = conf.get_config(key)
    expected_class = type(expected).__name__
    expected = json.dumps(expected)
    if not as_repr:
        expected = "%s(%s)" % (expected_class, expected)
    assert expected == out
Esempio n. 36
0
def test_config_no_append(no_config_env):
    @getter(config_should_not_be_appended)
    def config_should_be_appended_checker(**kwargs):
        if kwargs["key"] == "foo":
            return True

    with pytest.raises(ConfigurationError):
        config.get().append_config("foo", "bar")
    config.get().append_config("foo2", "bar")

    @getter(config_should_not_be_appended, sender=Project)
    def config_should_be_appended_model_checker(**kwargs):
        if kwargs["key"] == "foo2":
            return True

    config.get().append_config("foo2", "bar")
    with pytest.raises(ConfigurationError):
        config.get(Project).append_config("foo2", "bar")
    config.get(Project).append_config("foo3", "bar")
Esempio n. 37
0
def test_cmd_config_list_instance_object_field(capfd):
    project = Project.objects.get(code="project0")

    # no config set for anything
    call_command(
        "config",
        "pootle_project.project",
        project.code,
        "-o", "code")
    out, err = capfd.readouterr()
    _test_config_list(
        out,
        model=Project,
        instance=project,
        object_field="code")

    # no config set for key foo
    call_command(
        "config",
        "pootle_project.project",
        project.code,
        "-o", "code",
        "-l", "foo")
    out, err = capfd.readouterr()
    _test_config_list(
        out,
        model=Project,
        instance=project,
        object_field="code")

    config.get(Project, instance=project).set_config("foo", ["bar"])

    call_command(
        "config",
        "pootle_project.project",
        project.code,
        "-o", "code")
    out, err = capfd.readouterr()
    _test_config_list(
        out,
        model=Project,
        instance=project,
        object_field="code")
Esempio n. 38
0
def test_cmd_config_list_instance(no_config_env, capfd):
    project = Project.objects.get(code="project0")

    # no config set for anything
    call_command("config", "pootle_project.project", str(project.pk))
    out, err = capfd.readouterr()
    _test_config_list(out, model=Project, instance=project)

    # no config set for key foo
    call_command("config", "pootle_project.project", str(project.pk), "-l",
                 "foo")
    out, err = capfd.readouterr()
    _test_config_list(out, model=Project, instance=project)

    config.get(Project, instance=project).set_config("foo", ["bar"])

    call_command("config", "pootle_project.project", str(project.pk))
    out, err = capfd.readouterr()
    _test_config_list(out, model=Project, instance=project)
Esempio n. 39
0
 def pootle_user(self):
     User = get_user_model()
     username = config.get(self.project.__class__,
                           instance=self.project,
                           key="pootle_fs.pootle_user")
     if username:
         try:
             return User.objects.get(username=username)
         except User.DoesNotExist:
             logger.warning("Misconfigured pootle_fs user: %s", username)
Esempio n. 40
0
def test_config_no_append():

    @getter(config_should_not_be_appended)
    def config_should_be_appended_checker(**kwargs):
        if kwargs["key"] == "foo":
            return True

    with pytest.raises(ConfigurationError):
        config.get().append_config("foo", "bar")
    config.get().append_config("foo2", "bar")

    @getter(config_should_not_be_appended, sender=Project)
    def config_should_be_appended_model_checker(**kwargs):
        if kwargs["key"] == "foo2":
            return True

    config.get().append_config("foo2", "bar")
    with pytest.raises(ConfigurationError):
        config.get(Project).append_config("foo2", "bar")
    config.get(Project).append_config("foo3", "bar")
Esempio n. 41
0
def test_cmd_config_set_json(capfd, json_objects):
    call_command("config", "-j", "-s", "foo", json.dumps(json_objects))
    if isinstance(json_objects, tuple):
        json_objects = list(json_objects)
    assert config.get(key="foo") == json_objects
    capfd.readouterr()
    call_command("config", "-j", "-g", "foo")
    out, err = capfd.readouterr()
    assert json.loads(out) == json_objects
    call_command("config", "-g", "foo")
    out, err = capfd.readouterr()
    assert out == _repr_value(json_objects)
Esempio n. 42
0
 def pootle_user(self):
     User = get_user_model()
     username = config.get(
         self.project.__class__,
         instance=self.project,
         key="pootle_fs.pootle_user")
     if username:
         try:
             return User.objects.get(username=username)
         except User.DoesNotExist:
             logger.warning(
                 "Misconfigured pootle_fs user: %s",
                 username)
Esempio n. 43
0
def test_cmd_config_list_instance(no_config_env, capfd):
    project = Project.objects.get(code="project0")

    # no config set for anything
    call_command("config", "pootle_project.project", str(project.pk))
    out, err = capfd.readouterr()
    _test_config_list(out, model=Project, instance=project)

    # no config set for key foo
    call_command(
        "config",
        "pootle_project.project",
        str(project.pk),
        "-l", "foo")
    out, err = capfd.readouterr()
    _test_config_list(out, model=Project, instance=project)

    config.get(Project, instance=project).set_config("foo", ["bar"])

    call_command("config", "pootle_project.project", str(project.pk))
    out, err = capfd.readouterr()
    _test_config_list(out, model=Project, instance=project)
Esempio n. 44
0
def test_add_store_bad_path(english):
    """Try to create a store_fs where pootle_path and store.pootle_path dont
    match.
    """
    fs_path = "/some/fs/example.po"
    project = ProjectDBFactory(source_language=english)
    language = LanguageDBFactory()
    tp = TranslationProjectFactory(project=project, language=language)
    conf = config.get(project.__class__, instance=project)
    conf.set_config("pootle_fs.fs_type", "localfs")
    conf.set_config("pootle_fs.fs_url", "foo")
    store = StoreDBFactory(translation_project=tp, parent=tp.directory, name="example_store.po")
    with pytest.raises(ValidationError):
        StoreFS.objects.create(store=store, pootle_path="/some/other/path.po", path=fs_path)
Esempio n. 45
0
def test_store_po_serializer_custom(test_fs, store_po):

    class SerializerCheck(object):
        original_data = None
        context = None

    checker = SerializerCheck()

    class EGSerializer(Serializer):

        @property
        def output(self):
            checker.original_data = self.original_data
            checker.context = self.context

    @provider(serializers, sender=Project)
    def provide_serializers(**kwargs):
        return dict(eg_serializer=EGSerializer)

    with test_fs.open("data/po/complex.po") as test_file:
        test_string = test_file.read()
        # ttk_po = getclass(test_file)(test_string)
    store_po.update(store_po.deserialize(test_string))

    # add config to the project
    project = store_po.translation_project.project
    config.get(project.__class__, instance=project).set_config(
        "pootle.core.serializers",
        ["eg_serializer"])

    store_po.serialize()
    assert checker.context == store_po
    assert (
        not isinstance(checker.original_data, six.text_type)
        and isinstance(checker.original_data, str))
    assert checker.original_data == _store_as_string(store_po)
Esempio n. 46
0
 def get_conf(self, **kwargs):
     if kwargs["object"] and not kwargs["content_type"]:
         raise CommandError(
             "You must set --content-type (-t) when using "
             "--object-pk (-o)")
     if kwargs["content_type"]:
         if "." not in kwargs["content_type"]:
             raise CommandError(
                 "content_type should be set with $app_label.$model_name")
         parts = kwargs["content_type"].split(".")
         model_name = parts[-1]
         app_label = ".".join(parts[:-1])
         try:
             ct = ContentType.objects.get(
                 app_label=app_label,
                 model=model_name)
         except ContentType.DoesNotExist as e:
             raise CommandError(e)
         ct_model = ct.model_class()
         if kwargs["object"]:
             if kwargs["object_field"]:
                 model_query = {kwargs["object_field"]: kwargs["object"]}
             else:
                 model_query = dict(pk=kwargs["object"])
             catch_exc = (
                 ct_model.DoesNotExist,
                 ct_model.MultipleObjectsReturned,
                 ValueError,
                 FieldError)
             try:
                 instance = ct_model.objects.get(**model_query)
             except catch_exc as e:
                 raise CommandError(e)
             return config.get(ct_model, instance=instance)
         return config.get(ct_model)
     return config.get()
Esempio n. 47
0
def _test_config_list(out, model=None, instance=None, object_field=None):
    conf_list = config.get(model, instance=instance).list_config()
    if model:
        item_name = str(model._meta)
        if instance:
            if object_field:
                identifier = getattr(instance, object_field)
            else:
                identifier = instance.pk
            item_name = "%s[%s]" % (item_name, identifier)
    else:
        item_name = "Pootle"
    expected = []
    items = []
    name_col = 25
    key_col = 25
    for k, v in conf_list:
        if model:
            ct = str(model._meta)
            if instance:
                if object_field:
                    pk = getattr(
                        instance,
                        object_field)
                else:
                    pk = instance.pk
                name = "%s[%s]" % (ct, pk)
            else:
                name = ct
        else:
            name = "Pootle"
        if len(name) > name_col:
            name_col = len(name)
        if len(k) > key_col:
            key_col = len(k)
        items.append((name, k, v))
    format_string = "{: <%d} {: <%d} {: <30}" % (name_col, key_col)
    for name, key, value in items:
        expected.append(
            format_string.format(
                item_name, k, _repr_value(v)))
    if not items:
        assert out == "No configuration found\n"
    else:
        assert out == "%s\n" % ("\n".join(expected))
Esempio n. 48
0
def _test_config_list(out, model=None, instance=None, object_field=None):
    conf_list = config.get(model, instance=instance).list_config()
    if model:
        item_name = str(model._meta)
        if instance:
            if object_field:
                identifier = getattr(instance, object_field)
            else:
                identifier = instance.pk
            item_name = "%s[%s]" % (item_name, identifier)
    else:
        item_name = "Pootle"
    expected = []
    items = []
    name_col = 25
    key_col = 25
    for k, v in conf_list:
        if model:
            ct = str(model._meta)
            if instance:
                if object_field:
                    pk = getattr(
                        instance,
                        object_field)
                else:
                    pk = instance.pk
                name = "%s[%s]" % (ct, pk)
            else:
                name = ct
        else:
            name = "Pootle"
        if len(name) > name_col:
            name_col = len(name)
        if len(k) > key_col:
            key_col = len(k)
        items.append((name, k, v))
    format_string = "{: <%d} {: <%d} {: <30}" % (name_col, key_col)
    for name, key, value in items:
        expected.append(
            format_string.format(
                item_name, k, _repr_value(v)))
    if not items:
        assert out == "No configuration found\n"
    else:
        assert out == "%s\n" % ("\n".join(expected))
Esempio n. 49
0
def test_add_store_bad_path(po_directory, english):
    """Try to create a store_fs where pootle_path and store.pootle_path dont
    match.
    """
    fs_path = "/some/fs/example.po"
    project = ProjectDBFactory(source_language=english)
    language = LanguageDBFactory()
    tp = TranslationProjectFactory(project=project, language=language)
    conf = config.get(project.__class__, instance=project)
    conf.set_config("pootle_fs.fs_type", "localfs")
    conf.set_config("pootle_fs.fs_url", "foo")
    store = StoreDBFactory(translation_project=tp,
                           parent=tp.directory,
                           name="example_store.po")
    with pytest.raises(ValidationError):
        StoreFS.objects.create(store=store,
                               pootle_path="/some/other/path.po",
                               path=fs_path)
Esempio n. 50
0
def test_cmd_config_set_json(capfd, json_objects):
    call_command(
        "config",
        "-j",
        "-s", "foo", json.dumps(json_objects))
    if isinstance(json_objects, tuple):
        json_objects = list(json_objects)
    assert config.get(key="foo") == json_objects
    capfd.readouterr()
    call_command(
        "config",
        "-j",
        "-g", "foo")
    out, err = capfd.readouterr()
    assert json.loads(out) == json_objects
    call_command(
        "config",
        "-g", "foo")
    out, err = capfd.readouterr()
    assert out == _repr_value(json_objects)
Esempio n. 51
0
def project_fs_empty(english, tmpdir, settings):
    from pytest_pootle.factories import ProjectDBFactory

    from pootle.core.delegate import config
    from pootle_fs.utils import FSPlugin
    from pootle_project.models import Project

    project = ProjectDBFactory(source_language=english,
                               code="project_fs_empty",
                               treestyle="none")
    settings.POOTLE_FS_PATH = str(tmpdir)
    repo_path = os.path.join(str(tmpdir), "__src__")
    if not os.path.exists(repo_path):
        os.mkdir(repo_path)
    conf = config.get(Project, instance=project)
    conf.set_config("pootle_fs.fs_type", "localfs")
    conf.set_config("pootle_fs.fs_url", repo_path)
    conf.set_config(
        "pootle_fs.translation_paths",
        {"default": "/<language_code>/<dir_path>/<filename>.<ext>"})
    return FSPlugin(project)
Esempio n. 52
0
def project_fs_empty(english, tmpdir, settings):
    from pytest_pootle.factories import ProjectDBFactory

    from pootle.core.delegate import config
    from pootle_fs.utils import FSPlugin
    from pootle_project.models import Project

    project = ProjectDBFactory(
        source_language=english,
        code="project_fs_empty",
        treestyle="none")
    settings.POOTLE_FS_PATH = str(tmpdir)
    repo_path = os.path.join(str(tmpdir), "__src__")
    if not os.path.exists(repo_path):
        os.mkdir(repo_path)
    conf = config.get(Project, instance=project)
    conf.set_config("pootle_fs.fs_type", "localfs")
    conf.set_config("pootle_fs.fs_url", repo_path)
    conf.set_config(
        "pootle_fs.translation_paths",
        {"default": "/<language_code>/<dir_path>/<filename>.<ext>"})
    return FSPlugin(project)
Esempio n. 53
0
 def __config__(self):
     return config.get(
         self.context.__class__,
         instance=self.context)
Esempio n. 54
0
 def __config__(self):
     return config.get(self.context)
Esempio n. 55
0
def test_config_getter_list(no_config_env):
    assert config.get(key=[]) == []

    config.get().set_config("foo", ["bar0"])
    config.get().append_config("foo", ["bar0"])

    with pytest.raises(ConfigurationError):
        config.get(key="foo")

    # if we pass a list as key we get a k, v list mapping
    assert config.get(key=["foo"]) == [(u'foo', [u'bar0']),
                                       (u'foo', [u'bar0'])]
    assert config.get(key=[]) == [(u'foo', [u'bar0']), (u'foo', [u'bar0'])]

    config.get().set_config("foo2", ["bar1"])

    # this still works
    assert config.get(key=["foo"]) == [(u'foo', [u'bar0']),
                                       (u'foo', [u'bar0'])]

    # this still works
    assert config.get(key=["foo2"]) == [(u'foo2', [u'bar1'])]

    # both keys are returned for key=[]
    assert config.get(key=[]) == [(u'foo', [u'bar0']), (u'foo', [u'bar0']),
                                  (u'foo2', [u'bar1'])]

    assert config.get(Project, key=[]) == []

    config.get(Project).set_config("foo", ["bar2"])
    config.get(Project).append_config("foo", ["bar2"])

    with pytest.raises(ConfigurationError):
        config.get(Project, key="foo")

    # if we pass a list as key we get a k, v list mapping
    assert config.get(Project, key=["foo"]) == [(u'foo', [u'bar2']),
                                                (u'foo', [u'bar2'])]
    assert config.get(Project, key=[]) == [(u'foo', [u'bar2']),
                                           (u'foo', [u'bar2'])]

    config.get(Project).set_config("foo2", ["bar3"])

    # this still works
    assert config.get(Project, key=["foo"]) == [(u'foo', [u'bar2']),
                                                (u'foo', [u'bar2'])]

    # this still works
    assert config.get(Project, key=["foo2"]) == [(u'foo2', [u'bar3'])]

    # both keys are returned for key=[]
    assert config.get(Project, key=[]) == [(u'foo', [u'bar2']),
                                           (u'foo', [u'bar2']),
                                           (u'foo2', [u'bar3'])]

    # site config still works
    assert config.get(key=[]) == [(u'foo', [u'bar0']), (u'foo', [u'bar0']),
                                  (u'foo2', [u'bar1'])]

    project = Project.objects.get(code="project0")
    assert config.get(Project, instance=project, key=[]) == []

    config.get(Project, instance=project).set_config("foo", ["bar3"])
    config.get(Project, instance=project).append_config("foo", ["bar3"])

    with pytest.raises(ConfigurationError):
        config.get(Project, instance=project, key="foo")

    # if we pass a list as key we get a k, v list mapping
    assert config.get(Project,
                      instance=project, key=["foo"]) == [(u'foo', [u'bar3']),
                                                         (u'foo', [u'bar3'])]
    assert config.get(Project, instance=project,
                      key=[]) == [(u'foo', [u'bar3']), (u'foo', [u'bar3'])]

    config.get(Project, instance=project).set_config("foo2", ["bar4"])

    # this still works
    assert config.get(Project,
                      instance=project, key=["foo"]) == [(u'foo', [u'bar3']),
                                                         (u'foo', [u'bar3'])]

    # this still works
    assert config.get(Project, instance=project,
                      key=["foo2"]) == [(u'foo2', [u'bar4'])]

    # both keys are returned for key=[]
    assert config.get(Project, instance=project,
                      key=[]) == [(u'foo', [u'bar3']), (u'foo', [u'bar3']),
                                  (u'foo2', [u'bar4'])]

    # model config still works
    assert config.get(Project, key=[]) == [(u'foo', [u'bar2']),
                                           (u'foo', [u'bar2']),
                                           (u'foo2', [u'bar3'])]

    # site config still works
    assert config.get(key=[]) == [(u'foo', [u'bar0']), (u'foo', [u'bar0']),
                                  (u'foo2', [u'bar1'])]
Esempio n. 56
0
 def __config__(self):
     return config.get()
Esempio n. 57
0
def test_cmd_config_clear_instance(capfd):
    project = Project.objects.get(code="project0")

    # -c requires a key
    with pytest.raises(CommandError):
        call_command("config", "pootle_project.project", str(project.pk), "-c")

    # you can clear nothing
    call_command("config", "pootle_project.project", str(project.pk), "-c",
                 "foo")

    # lets add a config and clear it
    config.get(Project, instance=project).append_config("foo", "bar")
    call_command("config", "pootle_project.project", str(project.pk), "-c",
                 "foo")

    assert config.get(Project, instance=project).get_config("foo") is None

    # lets add 2 config and clear them
    config.get(Project, instance=project).append_config("foo", "bar")
    config.get(Project, instance=project).append_config("foo", "bar")
    call_command("config", "pootle_project.project", str(project.pk), "-c",
                 "foo")
    assert config.get(Project, instance=project).get_config("foo") is None

    # lets add 2 config with diff v and clear them
    config.get(Project, instance=project).append_config("foo", "bar")
    config.get(Project, instance=project).append_config("foo", "bar2")
    call_command("config", "pootle_project.project", str(project.pk), "-c",
                 "foo")
    assert config.get(Project, instance=project).get_config("foo") is None

    # lets add 2 config with diff k and clear one
    config.get(Project, instance=project).set_config("foo", "bar")
    config.get(Project, instance=project).set_config("foo2", "bar2")
    call_command("config", "pootle_project.project", str(project.pk), "-c",
                 "foo")
    assert config.get(Project, instance=project).get_config("foo2") == "bar2"
Esempio n. 58
0
def test_config_getter(no_config_env):
    # getting a non-existent key returns None
    assert config.get(key="DOESNT_EXIST") is None

    Config.objects.set_config("foo", ["bar"])
    # if key is not specified to the getter it returns the qs
    assert config.get().get(key="foo").value == ["bar"]
    # you can use get_config(key) with the qs
    assert config.get().get_config("foo") == ["bar"]
    # if key is specified it returns the key value
    assert config.get(key="foo") == ["bar"]

    Config.objects.set_config("foo", ["bar2"], Project)
    # previous tests still work
    assert config.get().get(key="foo").value == ["bar"]
    assert config.get().get_config("foo") == ["bar"]
    assert config.get(key="foo") == ["bar"]
    # specifying only model gets the queryset
    assert config.get(Project).get(key="foo").value == ["bar2"]
    # which we can use get_config with
    assert config.get(Project).get_config("foo") == ["bar2"]
    # specifying key also gets the value
    assert config.get(Project, key="foo") == ["bar2"]

    project = Project.objects.get(code="project0")
    Config.objects.set_config("foo", ["bar3"], project)
    # previous tests still work
    assert config.get().get(key="foo").value == ["bar"]
    assert config.get().get_config("foo") == ["bar"]
    assert config.get(key="foo") == ["bar"]
    assert config.get(Project).get(key="foo").value == ["bar2"]
    assert config.get(Project).get_config("foo") == ["bar2"]
    assert config.get(Project, key="foo") == ["bar2"]

    # we can get settings for an indiv project like so...
    assert config.get(Project,
                      instance=project).get(key="foo").value == ["bar3"]
    assert config.get(Project, instance=project).get_config("foo") == ["bar3"]
    assert config.get(Project, instance=project, key="foo") == ["bar3"]
Esempio n. 59
0
def test_cmd_config_clear_instance(capfd):
    project = Project.objects.get(code="project0")

    # -c requires a key
    with pytest.raises(CommandError):
        call_command(
            "config",
            "pootle_project.project",
            str(project.pk),
            "-c")

    # you can clear nothing
    call_command(
        "config",
        "pootle_project.project",
        str(project.pk),
        "-c", "foo")

    # lets add a config and clear it
    config.get(Project, instance=project).append_config("foo", "bar")
    call_command(
        "config",
        "pootle_project.project",
        str(project.pk),
        "-c", "foo")

    assert config.get(Project, instance=project).get_config("foo") is None

    # lets add 2 config and clear them
    config.get(Project, instance=project).append_config("foo", "bar")
    config.get(Project, instance=project).append_config("foo", "bar")
    call_command(
        "config",
        "pootle_project.project",
        str(project.pk),
        "-c", "foo")
    assert config.get(Project, instance=project).get_config("foo") is None

    # lets add 2 config with diff v and clear them
    config.get(Project, instance=project).append_config("foo", "bar")
    config.get(Project, instance=project).append_config("foo", "bar2")
    call_command(
        "config",
        "pootle_project.project",
        str(project.pk),
        "-c", "foo")
    assert config.get(Project, instance=project).get_config("foo") is None

    # lets add 2 config with diff k and clear one
    config.get(Project, instance=project).set_config("foo", "bar")
    config.get(Project, instance=project).set_config("foo2", "bar2")
    call_command(
        "config",
        "pootle_project.project",
        str(project.pk),
        "-c", "foo")
    assert config.get(Project, instance=project).get_config("foo2") == "bar2"
Esempio n. 60
0
def test_cmd_config_clear(capfd):

    # -c requires a key and it should exist
    with pytest.raises(CommandError):
        call_command("config", "-c")

    # you can clear nothing
    call_command("config", "-c", "foo")

    # lets add a config and clear it
    config.get().append_config("foo", "bar")
    call_command("config", "-c", "foo")

    assert config.get().get_config("foo") is None

    # lets add 2 config and clear them
    config.get().append_config("foo", "bar")
    config.get().append_config("foo", "bar")
    call_command("config", "-c", "foo")
    assert config.get().get_config("foo") is None

    # lets add 2 config with diff v and clear them
    config.get().append_config("foo", "bar")
    config.get().append_config("foo", "bar2")
    call_command("config", "-c", "foo")
    assert config.get().get_config("foo") is None

    # lets add 2 config with diff k and clear one
    config.get().set_config("foo", "bar")
    config.get().set_config("foo2", "bar2")
    call_command("config", "-c", "foo")
    assert config.get().get_config("foo2") == "bar2"