--[[ Unit and integration tests for the mw.wikibase.entity module @license GNU GPL v2+ @author Marius Hoch < hoo@online.de > @author Bene* < benestar.wikimedia@gmail.com > ]] local testframework = require 'Module:TestFramework' -- A test item (the structure isn't complete... but good enough for tests) local testItem = { id = "Q123", schemaVersion = 2, claims = { P321 = {}, P4321 = {} }, labels = { de = { value = 'LabelDE' }, en = { value = 'LabelDE-fallback', language = 'de' } }, descriptions = { de = { value = 'DescriptionDE' }, en = { value = 'DescriptionDE-fallback', language = 'de' } }, sitelinks = { dewiki = { title = 'Deutsch' }, ruwiki = { title = 'Русский' } } } -- A legacy test "item" local testItemLegacy = { schemaVersion = 1 } local getNewTestItem = function() return mw.wikibase.entity.create( testItem ) end -- Unit Tests local function testExists() return type( mw.wikibase.entity ) end local function testCreate( data ) return mw.wikibase.entity.create( data ) end local function testGetId() return getNewTestItem():getId() end local function testGetLabel( code ) return getNewTestItem():getLabel( code ) end local function testGetLabelWithLang( code ) return getNewTestItem():getLabelWithLang( code ) end local function testGetDescription( code ) return getNewTestItem():getDescription( code ) end local function testGetDescriptionWithLang( code ) return getNewTestItem():getDescriptionWithLang( code ) end local function testGetSitelink( globalSiteId ) return getNewTestItem():getSitelink( globalSiteId ) end local function testGetBestStatements( propertyId ) return getNewTestItem():getBestStatements( propertyId ) end local function testGetAllStatements( propertyId ) return getNewTestItem():getAllStatements( propertyId ) end local function testGetProperties() return getNewTestItem():getProperties() end local function testFormatPropertyValues( propertyId, acceptableRanks ) return getNewTestItem():formatPropertyValues( propertyId, acceptableRanks ) end local function testFormatStatements( propertyId, acceptableRanks ) return getNewTestItem():formatStatements( propertyId, acceptableRanks ) end local function getClaimRank() return mw.wikibase.entity.claimRanks.RANK_PREFERRED end -- Integration tests local function integrationTestGetPropertiesCount() return #( mw.wikibase.getEntityObject():getProperties() ) end local function integrationTestGetLabel( langCode ) return mw.wikibase.getEntityObject():getLabel( langCode ) end local function integrationTestGetLabelWithLang( langCode ) return mw.wikibase.getEntityObject():getLabelWithLang( langCode ) end local function integrationTestGetDescription( langCode ) return mw.wikibase.getEntityObject( 'Q32487' ):getDescription( langCode ) end local function integrationTestGetDescriptionWithLang( langCode ) return mw.wikibase.getEntityObject( 'Q32487' ):getDescriptionWithLang( langCode ) end local function integrationTestGetSitelink( globalSiteId ) return mw.wikibase.getEntityObject():getSitelink( globalSiteId ) end local function integrationTestGetBestStatements( propertyId ) local entity = mw.wikibase.getEntityObject() local statements = entity:getBestStatements( propertyId ) local result = {} for i, statement in pairs( statements ) do result[#result + 1] = statement.mainsnak.datavalue.value end return result end local function integrationTestGetAllStatements( propertyId ) local entity = mw.wikibase.getEntityObject() local statements = entity:getAllStatements( propertyId ) local result = {} for i, statement in pairs( statements ) do result[#result + 1] = statement.mainsnak.datavalue.value end return result end local function integrationTestTraverseReferencesWithIpairs( propertyId ) local entity = mw.wikibase.getEntityObject() local statements = entity:getAllStatements( propertyId ) local refs = statements[1]['references'] local result = {} -- use ipairs to access something in the references for i, ref in ipairs( refs ) do for pid, snaks in pairs( ref.snaks ) do result[#result + 1] = snaks[1].datavalue.value end end return result end local function integrationTestTraverseReferencesWithPairs( propertyId ) local entity = mw.wikibase.getEntityObject() local statements = entity:getAllStatements( propertyId ) local refs = statements[1]['references'] local result = {} -- use pairs to access something in the references for i, ref in pairs( refs ) do for pid, snaks in pairs( ref.snaks ) do result[#result + 1] = snaks[1].datavalue.value end end return result end local function integrationTestTraverseQualifiersWithPairs( propertyId ) local entity = mw.wikibase.getEntityObject() local statements = entity:getAllStatements( propertyId ) local qualifiersByPid = statements[1]['qualifiers'] local result = {} -- use pairs to access something in the qualifiers for pid, snaks in pairs( qualifiersByPid ) do for _, snak in ipairs( snaks ) do result[#result + 1] = snak.datavalue.value end end return result end local function integrationTestFormatPropertyValues( ranks ) local entity = mw.wikibase.getEntityObject() local propertyId = entity:getProperties()[1] return entity:formatPropertyValues( propertyId, ranks ) end local function integrationTestFormatPropertyValuesByLabel( label ) local entity = mw.wikibase.getEntityObject() return entity:formatPropertyValues( label ) end local function integrationTestFormatPropertyValuesNoSuchProperty( propertyLabelOrId ) local entity = mw.wikibase.getEntityObject( 'Q199024' ) return entity:formatPropertyValues( propertyLabelOrId ) end local function integrationTestFormatPropertyValuesProperty() local entity = mw.wikibase.getEntityObject( 'P342' ) return entity:formatPropertyValues( 'P342', mw.wikibase.entity.claimRanks ) end local function integrationTestFormatStatements( ranks ) local entity = mw.wikibase.getEntityObject() local propertyId = entity:getProperties()[1] return entity:formatStatements( propertyId, ranks ) end local function integrationTestFormatStatementsByLabel( label ) local entity = mw.wikibase.getEntityObject() return entity:formatStatements( label ) end local function integrationTestFormatStatementsGlobeCoordinate() local entity = mw.wikibase.getEntityObject( 'Q32489' ) local formattedStatement = entity:formatStatements( 'P625' ).value return formattedStatement:match( "-maplink-" ) and not formattedStatement:match( "