Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F42135292
wikimedia_commons_sdc_writing_test.py
Zache (Zache)
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Authored By
Zache
Feb 26 2024, 4:50 AM
2024-02-26 04:50:33 (UTC+0)
Size
7 KB
Referenced Files
None
Subscribers
None
wikimedia_commons_sdc_writing_test.py
View Options
#!/usr/bin/env python3
"""FilePage tests."""
#
# (C) Pywikibot team, 2014-2023
#
# Distributed under the terms of the MIT license.
#
# Running: python pwb.py wikimedia_commons_sdc_writing_test.py
from
__future__
import
annotations
import
unittest
from
contextlib
import
suppress
import
pywikibot
from
pywikibot
import
pagegenerators
from
pywikibot.exceptions
import
NoWikibaseEntityError
from
tests.aspects
import
TestCase
class
TestMediaInfoEditing
(
TestCase
):
"""Test writing structured data of FilePage."""
# commons.wikimedia.beta.wmflabs.org
family
=
'commons'
code
=
'commons'
def
test_edit_label
(
self
):
"""Test label editing."""
print
(
"Test label editing. No changes will be saved as "
)
print
(
"created revisions are identical to old revision."
)
# Test label editing when file doesn't exists
page
=
pywikibot
.
FilePage
(
self
.
site
,
'File:123_4_DOESNT_EXISTS.jpg'
)
print
(
page
)
item
=
page
.
data_item
()
error_message
=
"Entity '-1' doesn't exist on commons:commons"
lang
=
'fi'
test_label
=
'test_label'
data
=
{}
label
=
'Edit label of missing file with editEntity and dict()'
data
.
update
({
'labels'
:
{
lang
:
test_label
}})
with
self
.
assertRaisesRegex
(
NoWikibaseEntityError
,
error_message
):
item
.
editEntity
(
data
,
summary
=
label
)
label
=
'Edit label of missing file with editLabels and dict()'
data
=
{
lang
:
test_label
}
with
self
.
assertRaisesRegex
(
NoWikibaseEntityError
,
error_message
):
item
.
editLabels
(
data
,
summary
=
label
)
# Test label editing when file exists
page
=
pywikibot
.
FilePage
(
self
.
site
,
'File:Sandbox Image.png'
)
print
(
page
)
item
=
page
.
data_item
()
lang
=
'fi'
label
=
'Edit label with editEntity and dict()'
data
=
{}
data
.
update
({
'labels'
:
{
lang
:
test_label
}})
item
.
editEntity
(
data
,
summary
=
label
)
item
.
get
(
force
=
True
)
self
.
assertEqual
(
item
.
labels
[
'fi'
],
test_label
)
label
=
'Edit label with editEntity and item'
item
.
labels
[
'fi'
]
=
test_label
item
.
editEntity
(
item
.
toJSON
(),
summary
=
label
)
item
.
get
(
force
=
True
)
self
.
assertEqual
(
item
.
labels
[
'fi'
],
test_label
)
label
=
'Edit label with editLabels and dict()'
data
=
{
lang
:
test_label
}
item
.
editLabels
(
data
,
summary
=
label
)
item
.
get
(
force
=
True
)
self
.
assertEqual
(
item
.
labels
[
'fi'
],
test_label
)
def
test_edit_claims
(
self
):
"""Test addClaim and removeClaim editing."""
print
(
"Test claim editing."
)
wikidata_site
=
pywikibot
.
Site
(
'wikidata'
,
'wikidata'
)
# Create claim
property_id
=
'P571'
# date of publication
new_claim
=
pywikibot
.
Claim
(
wikidata_site
,
'P571'
)
new_claim_value
=
pywikibot
.
WbTime
(
2024
,
1
,
1
)
new_claim
.
setTarget
(
new_claim_value
)
# Test adding claim to non-existing file
page
=
pywikibot
.
FilePage
(
self
.
site
,
'File:123_4_DOESNT_EXISTS.jpg'
)
print
(
page
)
item
=
page
.
data_item
()
# Insert claim to non-existing file
with
self
.
assertRaises
(
NoWikibaseEntityError
):
item
.
addClaim
(
new_claim
)
# Insert claim using site object to non-existing file
with
self
.
assertRaises
(
NoWikibaseEntityError
):
self
.
site
.
addClaim
(
item
,
new_claim
)
# Test adding claim existing file
page
=
pywikibot
.
FilePage
(
self
.
site
,
'Sandbox Image.png'
)
print
(
page
)
item
=
page
.
data_item
()
# Test adding claim using MediaInfo.addClaim()
item
.
addClaim
(
new_claim
)
# Test that claim can be found
claim_found
=
False
item
.
get
(
force
=
True
)
for
statement
in
item
.
statements
.
get
(
property_id
):
value
=
statement
.
getTarget
()
if
value
==
new_claim_value
:
claim_found
=
True
summary
=
f
'Removing {property_id} with {value}'
item
.
removeClaims
(
statement
,
summary
=
summary
)
self
.
assertTrue
(
claim_found
)
# Test that the claim was removed
claim_found
=
False
item
.
get
(
force
=
True
)
for
statement
in
item
.
statements
.
get
(
property_id
,
[]):
value
=
statement
.
getTarget
()
if
value
==
new_claim_value
:
claim_found
=
True
self
.
assertFalse
(
claim_found
)
# Add claim using site object
self
.
site
.
addClaim
(
item
,
new_claim
)
# Test that the claim can be found and remove it using site object
claim_found
=
False
item
.
get
(
force
=
True
)
for
statement
in
item
.
statements
.
get
(
property_id
):
value
=
statement
.
getTarget
()
remove_statements
=
[]
if
value
==
new_claim_value
:
remove_statements
.
append
(
statement
)
claim_found
=
True
self
.
assertTrue
(
claim_found
)
# Note removeClaims() parameter needs to be array
summary
=
f
'Removing {property_id} with {value} using site object'
self
.
site
.
removeClaims
(
remove_statements
,
summary
=
summary
)
# Test that the claims were actually removed
claim_found
=
False
item
.
get
(
force
=
True
)
for
statement
in
item
.
statements
.
get
(
property_id
,
[]):
value
=
statement
.
getTarget
()
if
value
==
new_claim_value
:
claim_found
=
True
self
.
assertFalse
(
claim_found
)
site
=
pywikibot
.
Site
(
'commons'
,
'commons'
)
gen
=
pagegenerators
.
RecentChangesPageGenerator
(
site
=
site
,
namespaces
=
[
6
],
# File namespace
changetype
=
"new"
,
redirect
=
False
,
total
=
5000
)
new_claim_value
=
'image/jpeg'
# Test item.addClaim() when there is no existing mediainfo
for
page
in
gen
:
if
not
page
.
exists
():
continue
if
'mediainfo'
not
in
page
.
latest_revision
.
slots
:
if
'jpg'
not
in
page
.
title
():
continue
print
(
page
)
item
=
page
.
data_item
()
test_claim
=
pywikibot
.
Claim
(
wikidata_site
,
'P1163'
)
test_claim
.
setTarget
(
new_claim_value
)
item
.
addClaim
(
test_claim
)
item
.
get
(
force
=
True
)
for
statement
in
item
.
statements
.
get
(
property_id
,
[]):
value
=
statement
.
getTarget
()
self
.
assertTrue
(
value
==
new_claim_value
)
break
# Test site.addClaim() when there is no existing mediainfo
for
page
in
gen
:
if
not
page
.
exists
():
continue
if
'mediainfo'
not
in
page
.
latest_revision
.
slots
:
if
'jpg'
not
in
page
.
title
():
continue
print
(
page
)
item
=
page
.
data_item
()
test_claim
=
pywikibot
.
Claim
(
wikidata_site
,
'P1163'
)
test_claim
.
setTarget
(
new_claim_value
)
site
.
addClaim
(
item
,
test_claim
)
item
.
get
(
force
=
True
)
for
statement
in
item
.
statements
.
get
(
property_id
,
[]):
value
=
statement
.
getTarget
()
self
.
assertTrue
(
value
==
new_claim_value
)
break
if
__name__
==
'__main__'
:
with
suppress
(
SystemExit
):
unittest
.
main
()
File Metadata
Details
Attached
Mime Type
text/x-python
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
14696418
Default Alt Text
wikimedia_commons_sdc_writing_test.py (7 KB)
Attached To
Mode
T357608: DataSite.addClaim() throws a NoWikibaseEntity if MediaInfo is missing
Attached
Detach File
Event Timeline
Log In to Comment