Module:DescriptionFromDataItem/testcases

From OpenStreetMap Wiki
Jump to navigation Jump to search
-- See /doc page for the unit test runner
-- Use  =p.run()  to run from the debug console
local mod = require('Module:DescriptionFromDataItem')
local i18n = mw.loadData('Module:DescriptionFromDataItem/data').translations
local ScribuntoUnit = require('Module:ScribuntoUnit')
local p = ScribuntoUnit:new()

local mykey = 'Key:my.dbg.key'

mod.trackedLanguages = { 'fr', 'ru', 'ja' }

local function fauxItem()
    local testItem = {
        id = "Q42",
        schemaVersion = 2,
        claims = {
            P2 = { -- instance of
                {
                    rank = 'normal',
                    mainsnak={datavalue={value={id='Q7'}, type='wikibase-entityid'}},
                }
            },
            P6 = {
                { -- status - inuse
                    rank = 'preferred',
                    mainsnak={datavalue={value={id='Q14'}, type='wikibase-entityid'}},
                },{ -- status - approved in PL
                    rank = 'normal',
                    mainsnak={datavalue={value={id='Q15'}, type='wikibase-entityid'}},
                    qualifiers = {
                        P26 = {{datavalue={value={id='Q7806'}, type='wikibase-entityid'}}}
                    }
                }
            },
            -- use on node, default = yes
            P33 = { {
                        rank = 'preferred',
                        mainsnak={datavalue={value={id='Q8000'}, type='wikibase-entityid'}},
                    } },
            -- use on way, default = yes
            P34 = { {
                        rank = 'preferred',
                        mainsnak={datavalue={value={id='Q8000'}, type='wikibase-entityid'}},
                    }, {
                    },{
                        rank = 'normal', -- DE and PT use "no"
                        mainsnak={datavalue={value={id='Q8001'}, type='wikibase-entityid'}},
                        qualifiers = { P26 = {
                            {datavalue={value={id='Q6994'}, type='wikibase-entityid'}},
                            {datavalue={value={id='Q7807'}, type='wikibase-entityid'}},
                        } }
                    } },
            -- use on area, default = no
            P35 = { {
                        rank = 'preferred',
                        mainsnak={datavalue={value={id='Q8001'}, type='wikibase-entityid'}},
                    }, {
                    },{
                        rank = 'normal', -- PL use "yes"
                        mainsnak={datavalue={value={id='Q8000'}, type='wikibase-entityid'}},
                        qualifiers = { P26 = {
                            {datavalue={value={id='Q4669'}, type='wikibase-entityid'}},
                        } }
                    } },
            -- use on relation, default = no
            P36 = { {
                        rank = 'normal', -- FR use "no", everyone else is undefined
                        mainsnak={datavalue={value={id='Q8001'}, type='wikibase-entityid'}},
                        qualifiers = { P26 = {
                            {datavalue={value={id='Q7792'}, type='wikibase-entityid'}},
                        } }
                    } },
        },
        labels = {
            en = { value = 'label-en' },
            ru = { value = 'label-ru' }
        },
        descriptions = {
            en = { value = 'desc-en' },
            fr = { value = 'desc-fr' }
        },
        sitelinks = { wiki = { title = mykey }}
    }
    return mw.wikibase.entity.create(testItem)
end

local function runMain(args)
    args.currentTitle = args.title or mykey
    args.debuglua = args.debuglua == nil and true or args.debuglua
    local frame = mw.getCurrentFrame():newChild{ title = 'Module:DescriptionFromDataItem', args = args }
    return mod.main(frame)
end

mod.wbGetEntity = function(entity)
    local result = entity == 'Q42' and fauxItem() or nil
    mw.log('wbGetEntity' .. mw.text.jsonEncode({entity=entity, result=result}))
    return result
end

mod.wbGetEntityIdForTitle = function(title)
    local result = title == mykey and 'Q42' or nil
    mw.log('wbGetEntityIdForTitle' .. mw.text.jsonEncode({title=title, result=result}))
    return result
end

mod.wbGetLabel = function(qid)
    local result
    if qid == 'Q42' then
        result = fauxItem():getLabel()
    end
    mw.log('wbGetLabel' .. mw.text.jsonEncode({qid=qid, result=result}))
    return result
end

mod.wbGetLabelByLang = function(qid, langCode)
    local result
    if qid == 'Q42' then
        local val = fauxItem():getLabel(langCode)
        result = val and val.value or nil
    end
    mw.log('wbGetLabelByLang ' .. mw.text.jsonEncode({qid=qid, langCode=langCode, result=result}))
    return result
end

function p:test_english()
    mw.log('------------------ Running test_english()')
    local result = runMain{}
    local args = result.args
    mw.log(mw.text.jsonEncode(result, mw.text.JSON_PRETTY))
    self:assertEquals('key', args.type, 'type')
    self:assertEquals('Q14', args.status, 'status')
    self:assertEquals('desc-en&nbsp;<span class=wb-edit-pencil>[[File:Arbcom ru editing.svg|12px|'.. i18n.desc_edit.en ..'|link=Item:Q42]]</span>', args.description, 'description')
    self:assertEquals('yes', args.onNode, 'onNode')
    self:assertEquals('yes', args.onWay, 'onWay')
    self:assertEquals('no', args.onArea, 'onArea')
    self:assertEquals(nil, args.onRelation, 'onRelation')
    self:assertEquals('[[Category:Item with no description in language RU|my.dbg.key]][[Category:Item with no description in language JA|my.dbg.key]]', result.categories, 'categories')
end

function p:test_french()
    mw.log('------------------ Running test_french()')
    local result = runMain{lang='fr'}
    local args = result.args
    mw.log(mw.text.jsonEncode(result, mw.text.JSON_PRETTY))
    self:assertEquals('key', args.type, 'type')
    self:assertEquals('Q14', args.status, 'status')
    self:assertEquals('desc-fr&nbsp;<span class=wb-edit-pencil>[[File:Arbcom ru editing.svg|12px|'..(i18n.desc_edit.fr or i18n.desc_edit.en)..'|link=Item:Q42]]</span>', args.description, 'description')
    self:assertEquals('yes', args.onNode, 'onNode')
    self:assertEquals('yes', args.onWay, 'onWay')
    self:assertEquals('no', args.onArea, 'onArea')
    self:assertEquals('no', args.onRelation, 'onRelation')
    self:assertEquals(nil, result.categories, 'categories')
end

function p:test_portuguese()
    mw.log('------------------ Running test_portuguese()')
    local result = runMain{lang='pt'}
    local args = result.args
    mw.log(mw.text.jsonEncode(result, mw.text.JSON_PRETTY))
    self:assertEquals('key', args.type, 'type')
    self:assertEquals('Q14', args.status, 'status')
    self:assertEquals('desc-en&nbsp;<span class=wb-edit-pencil>[[File:Arbcom ru editing.svg|12px|'..(i18n.desc_edit.pt or i18n.desc_edit.en)..'|link=Item:Q42]]</span>', args.description, 'description')
    self:assertEquals('yes', args.onNode, 'onNode')
    self:assertEquals('no', args.onWay, 'onWay')
    self:assertEquals('no', args.onArea, 'onArea')
    self:assertEquals(nil, args.onRelation, 'onRelation')
    self:assertEquals(nil, result.categories, 'categories')
end

function p:test_german()
    mw.log('------------------ Running test_german()')
    local result = runMain{lang='de'}
    local args = result.args
    mw.log(mw.text.jsonEncode(result, mw.text.JSON_PRETTY))
    self:assertEquals('key', args.type, 'type')
    self:assertEquals('Q14', args.status, 'status')
    self:assertEquals('desc-en&nbsp;<span class=wb-edit-pencil>[[File:Arbcom ru editing.svg|12px|'..(i18n.desc_edit.de or i18n.desc_edit.en)..'|link=Item:Q42]]</span>', args.description, 'description')
    self:assertEquals('yes', args.onNode, 'onNode')
    self:assertEquals('no', args.onWay, 'onWay')
    self:assertEquals('no', args.onArea, 'onArea')
    self:assertEquals(nil, args.onRelation, 'onRelation')
    self:assertEquals(nil, result.categories, 'categories')
end

function p:test_polish()
    mw.log('------------------ Running test_polish()')
    local result = runMain{lang='pl'}
    local args = result.args
    mw.log(mw.text.jsonEncode(result, mw.text.JSON_PRETTY))
    self:assertEquals('key', args.type, 'type')
    self:assertEquals('Q15', args.status, 'status')
    self:assertEquals('desc-en&nbsp;<span class=wb-edit-pencil>[[File:Arbcom ru editing.svg|12px|'..(i18n.desc_edit.pl or i18n.desc_edit.en)..'|link=Item:Q42]]</span>', args.description, 'description')
    self:assertEquals('yes', args.onNode, 'onNode')
    self:assertEquals('yes', args.onWay, 'onWay')
    self:assertEquals('no', args.onArea, 'onArea')
    self:assertEquals(nil, args.onRelation, 'onRelation')
    self:assertEquals(nil, result.categories, 'categories')
end

function p:test_polish_group()
    mw.log('------------------ Running test_polish_group()')
    local result = runMain{lang='pl', group='Blah'}
    local args = result.args
    mw.log(mw.text.jsonEncode(result, mw.text.JSON_PRETTY))
    self:assertEquals('key', args.type, 'type')
    self:assertEquals('Q15', args.status, 'status')
    self:assertEquals('desc-en&nbsp;<span class=wb-edit-pencil>[[File:Arbcom ru editing.svg|12px|'..(i18n.desc_edit.pl or i18n.desc_edit.en)..'|link=Item:Q42]]</span>', args.description, 'description')
    self:assertEquals('yes', args.onNode, 'onNode')
    self:assertEquals('yes', args.onWay, 'onWay')
    self:assertEquals('no', args.onArea, 'onArea')
    self:assertEquals(nil, args.onRelation, 'onRelation')
    self:assertEquals('[[Category:Not copied group|my.dbg.key]][[Category:Blah|my.dbg.key]]', result.categories, 'categories')
    self:assertEquals('Blah', args.group, 'group')
end

function p:test_no_dataitem()
    mw.log('------------------ Running test_english()')
    local result = runMain{title='Tag:theatre:type=amphi'}
    local args = result.args
    mw.log(mw.text.jsonEncode(result, mw.text.JSON_PRETTY))
    self:assertEquals('key', args.type, 'type')
    self:assertEquals('Q14', args.status, 'status')
    self:assertEquals('desc-en&nbsp;<span class=wb-edit-pencil>[[File:Arbcom ru editing.svg|12px|'.. i18n.desc_edit.en ..'|link=Item:Q42]]</span>', args.description, 'description')
    self:assertEquals('yes', args.onNode, 'onNode')
    self:assertEquals('yes', args.onWay, 'onWay')
    self:assertEquals('no', args.onArea, 'onArea')
    self:assertEquals(nil, args.onRelation, 'onRelation')
    self:assertEquals('[[Category:Item with no description in language RU|my.dbg.key]][[Category:Item with no description in language JA|my.dbg.key]]', result.categories, 'categories')
end

function p:test_no_dataitem()
    mw.log('------------------ Running test_english()')
    local result = runMain{title='Tag:theatre:type=amphi'}
    local args = result.args
    mw.log(mw.text.jsonEncode(result, mw.text.JSON_PRETTY))
    self:assertEquals('value', args.type, 'type')
    self:assertEquals('Template:ValueDescription', args.templatename, 'templatename')
    self:assertEquals('[[Category:Missing data item|theatre:type=amphi]]', result.categories, 'categories')
end

function p:test_no_dataitem_key()
    mw.log('------------------ Running test_english()')
    local result = runMain{title='Tag:theatre:type=amphi', key='theatre:type'}
    local args = result.args
    mw.log(mw.text.jsonEncode(result, mw.text.JSON_PRETTY))
    self:assertEquals('key', args.type, 'type')
    self:assertEquals('Template:KeyDescription', args.templatename, 'templatename')
    self:assertEquals('[[Category:Missing data item|theatre:type]]', result.categories, 'categories')
end

function p:test_no_dataitem_value()
    mw.log('------------------ Running test_english()')
    local result = runMain{title='Key:theatre:type', key='theatre:type', value='amphi'}
    local args = result.args
    mw.log(mw.text.jsonEncode(result, mw.text.JSON_PRETTY))
    self:assertEquals('value', args.type, 'type')
    self:assertEquals('Template:ValueDescription', args.templatename, 'templatename')
    self:assertEquals('[[Category:Missing data item|theatre:type=amphi]]', result.categories, 'categories')
end

return p