Skip to content

Commit

Permalink
Added summary and usernames field in WeblateV1Message and added test …
Browse files Browse the repository at this point in the history
…for message classes
  • Loading branch information
harriebird committed Oct 14, 2024
1 parent 6cd004f commit 8126012
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 85 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
fedora-messaging
fqdn
jsonschema
jsonschema[format]
rfc3987
strict-rfc3339
11 changes: 10 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,13 @@
with open("requirements.txt") as handle:
REQUIRES = list(handle.read().splitlines())

setup(install_requires=REQUIRES)
setup(
name="weblate_schemas",
entry_points={
"fedora.messages": [
"base.message=weblate_schemas.messages:BaseMessage",
"weblate.message=weblate_schemas.messages:WeblateV1Message",
]
},
install_requires=REQUIRES,
)
15 changes: 13 additions & 2 deletions weblate_schemas/message.py → weblate_schemas/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from datetime import datetime

from weblate_schemas import load_schema
from . import load_schema
from fedora_messaging import message


Expand All @@ -28,7 +28,7 @@ def app_icon(self) -> str:
return "https://weblate.org/static/weblate-128.png"


class WeblateMessage(BaseMessage):
class WeblateV1Message(BaseMessage):
"""Actual Weblate message class which uses the Messaging schema."""

def __init__(self, **kwargs) -> None:
Expand Down Expand Up @@ -101,6 +101,17 @@ def translation(self) -> str:
"""Return the translation language code."""
return self.body["translation"]

@property
def summary(self) -> str:
"""Return the message summary."""
return f"{self.body['action']} event occurred in {self.body['timestamp']}"

@property
def usernames(self):
"""Return the usernames involved."""
users = {self.body["author"], self.body["user"]}
return list(users)

def __str__(self) -> str:
"""Return a human-readable representation of the message."""
return f"{self.action} {self.timestamp}"
5 changes: 5 additions & 0 deletions weblate_schemas/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Copyright © Michal Čihař <[email protected]>
#
# SPDX-License-Identifier: MIT

"""Tests for Weblate Fedora Messaging."""
File renamed without changes.
72 changes: 72 additions & 0 deletions weblate_schemas/tests/test_message.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Copyright © Michal Čihař <[email protected]>
#
# SPDX-License-Identifier: MIT

"""Test the Message classes."""

from weblate_schemas.messages import WeblateV1Message
from weblate_schemas.tests.test_valid import (
merge_body,
new_string_body,
new_translation_body,
)


def test_base_message() -> None:
"""Test Message class."""
base_message = WeblateV1Message(topic="test.topic", body=merge_body)
assert base_message.app_name == "Weblate"
assert base_message.app_icon == "https://weblate.org/static/weblate-128.png"


def test_weblate_merge_message() -> None:
"""Test WeblateV1Message class with a merge event message body."""
weblate_message = WeblateV1Message(topic="test.topic", body=merge_body)
assert weblate_message.app_name == "Weblate"
assert weblate_message.app_icon == "https://weblate.org/static/weblate-128.png"
assert weblate_message.id == merge_body["id"]
assert weblate_message.action == merge_body["action"]
assert weblate_message.timestamp == merge_body["timestamp"]
assert weblate_message.url == merge_body["url"]
assert weblate_message.component == merge_body["component"]
assert (
weblate_message.summary
== "Merged repository event occurred in 2017-06-15T11:30:47.325000+00:00"
)


def test_weblate_new_string_message() -> None:
"""Test WeblateV1Message class with a new string event message body."""
weblate_message = WeblateV1Message(topic="test.topic", body=new_string_body)
assert weblate_message.id == new_string_body["id"]
assert weblate_message.action == new_string_body["action"]
assert weblate_message.timestamp == new_string_body["timestamp"]
assert weblate_message.url == new_string_body["url"]
assert weblate_message.component == new_string_body["component"]
assert weblate_message.translation == new_string_body["translation"]
assert weblate_message.source == new_string_body["source"]
assert (
weblate_message.summary
== "New source string event occurred in 2017-06-15T11:30:47.372000+00:00"
)


def test_weblate_new_translation_message() -> None:
"""Test WeblateV1Message class with a new translation event message body."""
weblate_message = WeblateV1Message(topic="test.topic", body=new_translation_body)
assert weblate_message.id == new_translation_body["id"]
assert weblate_message.action == new_translation_body["action"]
assert weblate_message.timestamp == new_translation_body["timestamp"]
assert weblate_message.url == new_translation_body["url"]
assert weblate_message.target == new_translation_body["target"]
assert weblate_message.author == new_translation_body["author"]
assert weblate_message.user == new_translation_body["user"]
assert weblate_message.project == new_translation_body["project"]
assert weblate_message.component == new_translation_body["component"]
assert weblate_message.translation == new_translation_body["translation"]
assert weblate_message.source == new_translation_body["source"]
assert weblate_message.usernames == ["testuser"]
assert (
weblate_message.summary
== "New translation event occurred in 2019-10-17T15:57:08.772591+00:00"
)
177 changes: 96 additions & 81 deletions weblate_schemas/test_valid.py → weblate_schemas/tests/test_valid.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""Test schemas are valid."""

from jsonschema import validate
from jsonschema.exceptions import ValidationError

from weblate_schemas import load_schema, validate_schema

Expand Down Expand Up @@ -294,100 +295,114 @@ def test_component():
)


merge_body = {
"id": 1,
"action": "Merged repository",
"timestamp": "2017-06-15T11:30:47.325000+00:00",
"url": "https://example.com/projects/test/test/",
"component": "test",
}

new_string_body = {
"id": 2,
"action": "New source string",
"timestamp": "2017-06-15T11:30:47.372000+00:00",
"url": "https://example.com/translate/test/test/cs/?checksum=6412684aaf018e8e",
"component": "test",
"translation": "cs",
"source": ["Hello, world!\n"],
}

new_translation_body = {
"id": 12,
"action": "New translation",
"timestamp": "2019-10-17T15:57:08.772591+00:00",
"url": "https://example.com/translate/test/test/cs/?checksum=6412684aaf018e8e",
"target": ["Ahoj svete!\n"],
"author": "testuser",
"user": "testuser",
"project": "test",
"component": "test",
"translation": "cs",
"source": ["Hello, world!\n"],
}

resource_update_body = {
"id": 6,
"action": "Resource update",
"timestamp": "2017-06-15T11:30:47.410000+00:00",
"url": "https://example.com/projects/test/test/cs/",
"project": "test",
"component": "test",
"translation": "cs",
}

removal_body = {
"id": 9,
"action": "Removed project",
"timestamp": "2019-10-17T15:57:08.559420+00:00",
"target": "test",
"user": "testuser",
}

new_contributor_body = {
"id": 11,
"action": "New contributor",
"timestamp": "2019-10-17T15:57:08.759960+00:00",
"url": "https://example.com/translate/test/test/cs/?checksum=6412684aaf018e8e",
"author": "testuser",
"user": "testuser",
"project": "test",
"component": "test",
"translation": "cs",
"source": ["Hello, world!\n"],
}

invalid_body = {
"id": 1,
"action": "Merged repository",
"timestamp": "invalid datetime",
"url": "https://example.com/projects/test/test/",
"component": "test",
}


def test_weblate_messaging_merge() -> None:
"""Test Weblate Fedora Messaging schema validate a repository merge event."""
validate_schema(
{
"id": 1,
"action": "Merged repository",
"timestamp": "2017-06-15T11:30:47.325000+00:00",
"url": "http://example.com/projects/test/test/",
"component": "test",
},
"weblate-messaging.schema.json",
)
"""Test Weblate Fedora Messaging schema to validate a repository merge event."""
validate_schema(merge_body, "weblate-messaging.schema.json")


def test_weblate_messaging_new_string() -> None:
"""Test Weblate Fedora Messaging schema validate a new source string event."""
validate_schema(
{
"id": 2,
"action": "New source string",
"timestamp": "2017-06-15T11:30:47.372000+00:00",
"url": "http://example.com/translate/test/test/cs/?checksum=6412684aaf018e8e",
"component": "test",
"translation": "cs",
"source": ["Hello, world!\n"],
},
"weblate-messaging.schema.json",
)
"""Test Weblate Fedora Messaging schema to validate a new source string event."""
validate_schema(new_string_body, "weblate-messaging.schema.json")


def test_weblate_messaging_resource_update() -> None:
"""Test Weblate Fedora Messaging schema validate a resource update event."""
validate_schema(
{
"id": 6,
"action": "Resource update",
"timestamp": "2017-06-15T11:30:47.410000+00:00",
"url": "http://example.com/projects/test/test/cs/",
"project": "test",
"component": "test",
"translation": "cs",
},
"weblate-messaging.schema.json",
)
"""Test Weblate Fedora Messaging schema to validate a resource update event."""
validate_schema(resource_update_body, "weblate-messaging.schema.json")


def test_weblate_messaging_removal() -> None:
"""Test Weblate Fedora Messaging schema validate a project removal event."""
validate_schema(
{
"id": 9,
"action": "Removed project",
"timestamp": "2019-10-17T15:57:08.559420+00:00",
"target": "test",
"user": "testuser",
},
"weblate-messaging.schema.json",
)
"""Test Weblate Fedora Messaging schema to validate a project removal event."""
validate_schema(removal_body, "weblate-messaging.schema.json")


def test_weblate_messaging_new_contributor() -> None:
"""Test Weblate Fedora Messaging schema validate a new contributor event."""
validate_schema(
{
"id": 11,
"action": "New contributor",
"timestamp": "2019-10-17T15:57:08.759960+00:00",
"url": "http://example.com/translate/test/test/cs/?checksum=6412684aaf018e8e",
"author": "testuser",
"user": "testuser",
"project": "test",
"component": "test",
"translation": "cs",
"source": ["Hello, world!\n"],
},
"weblate-messaging.schema.json",
)
"""Test Weblate Fedora Messaging schema to validate a new contributor event."""
validate_schema(new_contributor_body, "weblate-messaging.schema.json")


def test_weblate_messaging_new_translation() -> None:
"""Test Weblate Fedora Messaging schema validate a new translation event."""
validate_schema(
{
"id": 12,
"action": "New translation",
"timestamp": "2019-10-17T15:57:08.772591+00:00",
"url": "http://example.com/translate/test/test/cs/?checksum=6412684aaf018e8e",
"target": ["Ahoj svete!\n"],
"author": "testuser",
"user": "testuser",
"project": "test",
"component": "test",
"translation": "cs",
"source": ["Hello, world!\n"],
},
"weblate-messaging.schema.json",
)
"""Test Weblate Fedora Messaging schema to validate a new translation event."""
validate_schema(new_translation_body, "weblate-messaging.schema.json")


def test_weblate_invalid_body() -> None:
"""Test Weblate Fedora Messaging schema to validate an invalid body."""
is_invalid = False
try:
validate_schema(invalid_body, "weblate-messaging.schema.json")
except ValidationError:
is_invalid = True

assert is_invalid

0 comments on commit 8126012

Please sign in to comment.