--[[ Integration tests for the mw.wikibase module @license GNU GPL v2+ @author Marius Hoch < hoo@online.de > @author Bene* < benestar.wikimedia@gmail.com > ]] local testframework = require 'Module:TestFramework' -- Integration tests local function testGetEntityType() return type( mw.wikibase.getEntity() ) end local function testGetEntitySchemaVersion() return mw.wikibase.getEntity().schemaVersion end local function testGetEntityObjectType() return type( mw.wikibase.getEntityObject() ) end local function testGetBestStatementsType() return type( mw.wikibase.getBestStatements( 'Q199024', 'P342' ) ) end local function testGetBestStatementsFormat() local directAccess = mw.dumpObject( mw.wikibase.getBestStatements( 'Q199024', 'P342' ) ) local entityAccess = mw.dumpObject( mw.wikibase.getEntity( 'Q199024' ):getBestStatements( 'P342' ) ) return directAccess == entityAccess end local function testGetAllStatementsType() return type( mw.wikibase.getAllStatements( 'Q199024', 'P342' ) ) end local function testGetAllStatementsFormat() local directAccess = mw.dumpObject( mw.wikibase.getAllStatements( 'Q32487', 'P342' ) ) local directBestAccess = mw.dumpObject( mw.wikibase.getBestStatements( 'Q32487', 'P342' ) ) local entityAccess = mw.dumpObject( mw.wikibase.getEntity( 'Q32487' ).claims.P342 ) return directBestAccess ~= directAccess and directAccess == entityAccess end local function testGetAllStatementsTableIsCloned() local statements = mw.wikibase.getAllStatements( 'Q32487', 'P342' ) table.remove(statements) -- We should get a freshly cloned table here, so the changes above won't persist local newStatements = mw.wikibase.getAllStatements( 'Q32487', 'P342' ) return #statements == #newStatements -1 end local function integrationTestTraverseReferencesWithIpairs( ) local statements = mw.wikibase.getAllStatements( 'Q32487', 'P342' ) 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( ) local statements = mw.wikibase.getAllStatements( 'Q32487', 'P342' ) 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( ) local statements = mw.wikibase.getAllStatements( 'Q32487', 'P342' ) 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 testGetEntityObjectIsCloned() mw.wikibase.getEntityObject( 'Q199024' ).id = 'a' -- We should get a freshly cloned table here, so the changes above wont persist return mw.wikibase.getEntityObject( 'Q199024' ).id end local function testGetEntityObjectSchemaVersion() return mw.wikibase.getEntityObject().schemaVersion end local function testGetEntityObjectForeignLabel() return mw.wikibase.getEntityObject( 'Q199024' ):getLabel( 'de' ) end local function testRenderSnak() local entity = mw.wikibase.getEntityObject( 'Q32487' ) local snak = entity['claims']['P342'][1]['qualifiers']['P342'][1] return mw.wikibase.renderSnak( snak ) end local function testFormatValue() local entity = mw.wikibase.getEntityObject( 'Q32487' ) local snak = entity['claims']['P342'][1]['qualifiers']['P342'][1] return mw.wikibase.formatValue( snak ) end local function testFormatValueGlobeCoordinate() local entity = mw.wikibase.getEntityObject( 'Q32489' ) local snak = entity['claims']['P625'][1]['mainsnak'] local formattedValue = mw.wikibase.formatValue( snak ) return formattedValue:match( "-maplink-" ) and not formattedValue:match( "' }, expect = { nil } }, { name = 'mw.wikibase.getLabelByLang (no label)', func = mw.wikibase.getLabelByLang, type='ToString', args = { 'Q32488', 'de' }, expect = { nil } }, { name = 'mw.wikibase.getDescriptionByLang (invalid id type)', func = mw.wikibase.getDescriptionByLang, type='ToString', args = { 1, 'de' }, expect = "bad argument #1 to 'getDescriptionByLang' (string expected, got number)" }, { name = 'mw.wikibase.getDescriptionByLang (invalid languageCode type)', func = mw.wikibase.getDescriptionByLang, type='ToString', args = { "Q42", 1.2 }, expect = "bad argument #2 to 'getDescriptionByLang' (string expected, got number)" }, { name = 'mw.wikibase.getDescriptionByLang (invalid id)', func = mw.wikibase.getDescriptionByLang, type='ToString', args = { '-1', 'de' }, expect = { nil } }, { name = 'mw.wikibase.getDescriptionByLang', func = mw.wikibase.getDescriptionByLang, type='ToString', args = { 'Q32487', 'de' }, expect = { 'Description of Q32487' } }, { name = 'mw.wikibase.getDescriptionByLang (no such item)', func = mw.wikibase.getDescriptionByLang, type='ToString', args = { 'Q1224342342', 'de' }, expect = { nil } }, { name = 'mw.wikibase.getDescriptionByLang (no such lang)', func = mw.wikibase.getDescriptionByLang, type='ToString', args = { 'Q32487', 'blahblahblah' }, expect = { nil } }, { name = 'mw.wikibase.getDescriptionByLang (invalid lang)', func = mw.wikibase.getDescriptionByLang, type='ToString', args = { 'Q32487', ':!/<>' }, expect = { nil } }, { name = 'mw.wikibase.getDescriptionByLang (no description)', func = mw.wikibase.getDescriptionByLang, type='ToString', args = { 'Q32488', 'en' }, expect = { nil } }, { name = 'mw.wikibase.description (legacy alias)', func = mw.wikibase.description, type='ToString', args = { 'Q32487' }, expect = { 'Description of Q32487' } }, { name = 'mw.wikibase.getDescription', func = mw.wikibase.getDescription, type='ToString', args = { 'Q32487' }, expect = { 'Description of Q32487' } }, { name = 'mw.wikibase.getDescription (invalid id given)', func = mw.wikibase.getDescription, args = { 12 }, expect = "bad argument #1 to 'getDescription' (string or nil expected, got number)" }, { name = 'mw.wikibase.getDescription (connected item)', func = mw.wikibase.getDescription, type='ToString', args = {}, expect = { 'Description of Q32487' } }, { name = 'mw.wikibase.getDescription (no such item)', func = mw.wikibase.getDescription, type='ToString', args = { 'Q1224342342' }, expect = { nil } }, { name = 'mw.wikibase.getDescription (no such description)', func = mw.wikibase.getDescription, type='ToString', args = { 'P342' }, expect = { nil } }, { name = 'mw.wikibase.getDescriptionWithLang (connected item)', func = mw.wikibase.getDescriptionWithLang, type='ToString', args = {}, expect = { 'Description of Q32487', 'de' } }, { name = 'mw.wikibase.getDescriptionWithLang (no such item)', func = mw.wikibase.getDescriptionWithLang, type='ToString', args = { 'Q1224342342' }, expect = { nil, nil } }, { name = 'mw.wikibase.getDescriptionWithLang (no such description)', func = mw.wikibase.getDescriptionWithLang, type='ToString', args = { 'P342' }, expect = { nil, nil } }, { name = 'mw.wikibase.sitelink (legacy alias)', func = mw.wikibase.sitelink, type='ToString', args = { 'Q32487' }, expect = { 'WikibaseClientDataAccessTest' } }, { name = 'mw.wikibase.getSitelink', func = mw.wikibase.getSitelink, type='ToString', args = { 'Q32487' }, expect = { 'WikibaseClientDataAccessTest' } }, { name = 'mw.wikibase.getSitelink (invalid id given)', func = mw.wikibase.getSitelink, type='ToString', args = {}, expect = "bad argument #1 to 'getSitelink' (string expected, got nil)" }, { name = 'mw.wikibase.getSitelink', func = mw.wikibase.getSitelink, type='ToString', args = { 'Q32488' }, expect = { nil } }, { name = 'mw.wikibase.getSitelink (with global site id)', func = mw.wikibase.getSitelink, type='ToString', args = { 'Q32487', 'fooSiteId' }, expect = { 'FooBarFoo' } }, { name = 'mw.wikibase.getSitelink (with global site id not found)', func = mw.wikibase.getSitelink, type='ToString', args = { 'Q32487', 'does-not-exist' }, expect = { nil } }, { name = 'mw.wikibase.getBadges', func = mw.wikibase.getBadges, args = { 'Q32487' }, expect = { {} } }, { name = 'mw.wikibase.getBadges (with global site id)', func = mw.wikibase.getBadges, args = { 'Q32487', 'fooSiteId' }, expect = { { 'Q10001', 'Q10002' } } }, { name = 'mw.wikibase.getBadges (global site id not found)', func = mw.wikibase.getBadges, args = { 'Q32487', 'does-not-exist' }, expect = { {} } }, { name = 'mw.wikibase.getBadges (no such item)', func = mw.wikibase.getBadges, args = { 'Q404' }, expect = { {} } }, { name = 'mw.wikibase.renderSnak', func = testRenderSnak, type='ToString', expect = { 'A qualifier Snak' } }, { name = 'mw.wikibase.renderSnak (must be table)', func = mw.wikibase.renderSnak, args = { 'meep' }, expect = "bad argument #1 to 'renderSnak' (table expected, got string)" }, { name = 'mw.wikibase.formatValue', func = testFormatValue, type='ToString', expect = { 'A qualifier Snak' } }, { name = 'mw.wikibase.formatValue tag parsing', func = testFormatValueGlobeCoordinate, type='ToString', expect = { true } }, { name = 'mw.wikibase.formatValue (must be table)', func = mw.wikibase.formatValue, args = { 'meep' }, expect = "bad argument #1 to 'formatValue' (table expected, got string)" }, { name = 'mw.wikibase.renderSnaks', func = testRenderSnaks, type='ToString', expect = { 'A qualifier Snak, Moar qualifiers' } }, { name = 'mw.wikibase.renderSnaks (must be table)', func = mw.wikibase.renderSnaks, args = { 'meep' }, expect = "bad argument #1 to 'renderSnaks' (table expected, got string)" }, { name = 'mw.wikibase.formatValues', func = testFormatValues, type='ToString', expect = { 'A qualifier Snak, Moar qualifiers' } }, { name = 'mw.wikibase.formatValues (must be table)', func = mw.wikibase.formatValues, args = { 'meep' }, expect = "bad argument #1 to 'formatValues' (table expected, got string)" }, { name = 'mw.wikibase.resolvePropertyId', func = mw.wikibase.resolvePropertyId, args = { 'LuaTestStringProperty' }, expect = { 'P342' } }, { name = 'mw.wikibase.resolvePropertyId (property id passed)', func = mw.wikibase.resolvePropertyId, args = { 'P342' }, expect = { 'P342' } }, { name = 'mw.wikibase.resolvePropertyId (label not found)', func = mw.wikibase.resolvePropertyId, args = { 'foo' }, expect = { nil } }, { name = 'mw.wikibase.getEntityUrl (by entity id)', func = testGetEntityUrl, args = { 'Q42', 'Q42' }, expect = { true } }, { name = 'mw.wikibase.getEntityUrl (connected page)', func = testGetEntityUrl, args = { 'Q32487', nil }, expect = { true } }, { name = 'mw.wikibase.getEntityUrl (must be string or nil)', func = mw.wikibase.getEntityUrl, args = { -1 }, expect = "bad argument #1 to 'getEntityUrl' (string or nil expected, got number)" }, { name = 'mw.wikibase.isValidEntityId', func = mw.wikibase.isValidEntityId, args = { "Q12" }, expect = { true } }, { name = 'mw.wikibase.isValidEntityId (invalid id)', func = mw.wikibase.isValidEntityId, args = { "Q0" }, expect = { false } }, { name = 'mw.wikibase.isValidEntityId (must be string)', func = mw.wikibase.isValidEntityId, args = { 12 }, expect = "bad argument #1 to 'isValidEntityId' (string expected, got number)" }, { name = 'mw.wikibase.getEntityUrl (invalid entity id)', func = mw.wikibase.getEntityUrl, args = { "BlahBlah" }, expect = { nil } }, { name = 'mw.wikibase.getPropertyOrder', func = mw.wikibase.getPropertyOrder, expect = { { ['P1'] = 0, ['P22'] = 1, ['P11'] = 2 } } }, { name = 'mw.wikibase.orderProperties', func = mw.wikibase.orderProperties, args = { { 'P22', 'P1', 'P44', 'Llama' } }, expect = { { 'P1', 'P22', 'P44', 'Llama' } } }, { name = 'mw.wikibase.orderProperties (must be table)', func = mw.wikibase.orderProperties, args = { function() end }, expect = "bad argument #1 to 'orderProperties' (table expected, got function)" }, { name = 'mw.wikibase.getReferencedEntityId (Q32488 references Q885588 via P456)', func = mw.wikibase.getReferencedEntityId, args = { 'Q32488', 'P456', { 'Q885588', 'Q42' } }, expect = { 'Q885588' } }, { name = 'mw.wikibase.getReferencedEntityId (target not referenced)', func = mw.wikibase.getReferencedEntityId, args = { 'Q32488', 'P456', { 'Q42' } }, expect = { nil } }, { name = 'mw.wikibase.getReferencedEntityId (fromEntityId must be string)', func = mw.wikibase.getReferencedEntityId, args = { function() end, 'P20', { 'P22' } }, expect = "bad argument #1 to 'getReferencedEntityId' (string expected, got function)" }, { name = 'mw.wikibase.getReferencedEntityId (propertyId must be string)', func = mw.wikibase.getReferencedEntityId, args = { 'Q12', 12, { 'P22' } }, expect = "bad argument #2 to 'getReferencedEntityId' (string expected, got number)" }, { name = 'mw.wikibase.getReferencedEntityId (toIds must be table)', func = mw.wikibase.getReferencedEntityId, args = { 'Q12', 'P12', 'Q22' }, expect = "bad argument #3 to 'getReferencedEntityId' (table expected, got string)" }, { name = 'mw.wikibase.getReferencedEntityId (toIds must contain strings)', func = mw.wikibase.getReferencedEntityId, args = { 'Q12', 'P12', { 'P22', 24 } }, expect = "toIds value at index 2 must be string, number given." }, { name = 'mw.wikibase.getGlobalSiteId', func = testGetGlobalSiteId, expect = { true } }, } return testframework.getTestProvider( tests )