Page Menu
Home
Software Heritage
Search
Configure Global Search
Log In
Files
F9345214
test_utils.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
3 KB
Subscribers
None
test_utils.py
View Options
# Copyright (C) 2017-2021 The Software Heritage developers
# See the AUTHORS file at the top-level directory of this distribution
# License: GNU Affero General Public License version 3, or any later version
# See top-level LICENSE file for more information
import
pytest
from
swh.web.browse.utils
import
(
gen_link
,
gen_person_mail_link
,
gen_revision_link
,
get_mimetype_and_encoding_for_content
,
prepare_content_for_display
,
re_encode_content
,
)
from
swh.web.common.utils
import
reverse
def
test_get_mimetype_and_encoding_for_content
():
text
=
b
"Hello world!"
assert
get_mimetype_and_encoding_for_content
(
text
)
==
(
"text/plain"
,
"us-ascii"
,)
def
test_gen_link
():
assert
(
gen_link
(
"https://www.softwareheritage.org/"
,
"swh"
)
==
'<a href="https://www.softwareheritage.org/">swh</a>'
)
def
test_gen_revision_link
():
revision_id
=
"28a0bc4120d38a394499382ba21d6965a67a3703"
revision_url
=
reverse
(
"browse-revision"
,
url_args
=
{
"sha1_git"
:
revision_id
})
assert
gen_revision_link
(
revision_id
,
link_text
=
None
,
link_attrs
=
None
)
==
'<a href="
%s
">
%s
</a>'
%
(
revision_url
,
revision_id
)
assert
gen_revision_link
(
revision_id
,
shorten_id
=
True
,
link_attrs
=
None
)
==
'<a href="
%s
">
%s
</a>'
%
(
revision_url
,
revision_id
[:
7
])
def
test_gen_person_mail_link
():
person_full
=
{
"name"
:
"John Doe"
,
"email"
:
"john.doe@swh.org"
,
"fullname"
:
"John Doe <john.doe@swh.org>"
,
}
assert
gen_person_mail_link
(
person_full
)
==
'<a href="mailto:
%s
">
%s
</a>'
%
(
person_full
[
"email"
],
person_full
[
"name"
],
)
link_text
=
"Mail"
assert
gen_person_mail_link
(
person_full
,
link_text
=
link_text
)
==
'<a href="mailto:
%s
">
%s
</a>'
%
(
person_full
[
"email"
],
link_text
)
person_partial_email
=
{
"name"
:
None
,
"email"
:
None
,
"fullname"
:
"john.doe@swh.org"
}
assert
gen_person_mail_link
(
person_partial_email
)
==
'<a href="mailto:
%s
">
%s
</a>'
%
(
person_partial_email
[
"fullname"
],
person_partial_email
[
"fullname"
],
)
person_partial
=
{
"name"
:
None
,
"email"
:
None
,
"fullname"
:
"John Doe <john.doe@swh.org>"
,
}
assert
gen_person_mail_link
(
person_partial
)
==
person_partial
[
"fullname"
]
person_none
=
{
"name"
:
None
,
"email"
:
None
,
"fullname"
:
None
}
assert
gen_person_mail_link
(
person_none
)
==
"None"
@pytest.mark.parametrize
(
"path, expected_language"
,
[(
"CMakeLists.txt"
,
"cmake"
),
(
"path/CMakeLists.txt"
,
"cmake"
)],
)
def
test_prepare_content_display_language_for_filename
(
path
,
expected_language
):
content_display
=
prepare_content_for_display
(
content_data
=
b
""
,
mime_type
=
""
,
path
=
path
)
assert
content_display
[
"language"
]
==
expected_language
def
test_re_encode_content_for_shift_jis_encoding
():
data
=
b
"/*
\x8a\xd6\x98
A
\x82\xcc\x95\xb6\x8e\x9a\x83
R
\x81
[
\x83
h
\x95\xcf\x8a\xb7
*/"
mime_type
,
encoding
=
get_mimetype_and_encoding_for_content
(
data
)
_
,
encoding
,
re_encoded_data
=
re_encode_content
(
mime_type
,
encoding
,
data
)
assert
encoding
==
"SHIFT_JIS"
assert
data
.
decode
(
encoding
)
==
re_encoded_data
.
decode
(
"utf-8"
)
assert
re_encoded_data
.
decode
(
"utf-8"
)
==
"/* 関連の文字コード変換 */"
File Metadata
Details
Attached
Mime Type
text/x-python
Expires
Fri, Jul 4, 3:13 PM (4 d, 8 h ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3350728
Attached To
rDWAPPS Web applications
Event Timeline
Log In to Comment