Coverage for gws-app/gws/lib/xmlx/_test/namespace_test.py: 100%

55 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-10-16 22:59 +0200

1"""Tests for the namespace module""" 

2 

3import gws 

4import gws.test.util as u 

5 

6import gws.lib.xmlx as xmlx 

7 

8 

9def test_find_by_uri(): 

10 uri = 'http://www.opengis.net/gml' 

11 ns = xmlx.namespace.find_by_uri(uri) 

12 assert ns and ns.uid == 'gml2' 

13 

14 

15def test_get(): 

16 xmlns = 'gml' 

17 ns = xmlx.namespace.get(xmlns) 

18 assert ns and ns.uid == 'gml' 

19 

20 

21# def test_register(): 

22# ns = gws.XmlNamespace(uid='foo', xmlns='bar', uri='http://www.foobar.de') 

23# xmlx.namespace.register(ns) 

24# assert ns.uid in xmlx.namespace._INDEX.uid 

25# assert ns.xmlns in xmlx.namespace._INDEX.xmlns 

26# assert ns.uri in xmlx.namespace._INDEX.uri 

27 

28 

29def test_split_name_empty(): 

30 name = '' 

31 assert xmlx.namespace.split_name(name) == ('', '', '') 

32 

33 

34def test_split_name(): 

35 name = '{http://example.com/namespace}element' 

36 assert xmlx.namespace.split_name(name) == ('', 'http://example.com/namespace', 'element') 

37 

38 

39def test_split_name_colon(): 

40 name = 'somens:tag' 

41 assert xmlx.namespace.split_name(name) == ('somens', '', 'tag') 

42 

43 

44def test_split_name_else(): 

45 name = 'name' 

46 assert xmlx.namespace.split_name(name) == ('', '', 'name') 

47 

48 

49def test_extract_xmlns(): 

50 ns = 'gml:tag' 

51 assert xmlx.namespace.extract(ns) == (xmlx.namespace.find_by_xmlns('gml'), 'tag') 

52 

53 

54def test_extract_uri(): 

55 uri = '{http://www.opengis.net/gml}tag' 

56 assert xmlx.namespace.extract(uri) == (xmlx.namespace.find_by_uri('http://www.opengis.net/gml'), 'tag') 

57 

58 

59def test_extract_else(): 

60 name = 'foo' 

61 assert xmlx.namespace.extract(name) == (None, 'foo') 

62 

63 

64def test_qualify_name(): 

65 name = 'gml:foo' 

66 ns = xmlx.namespace.get('soap') 

67 assert xmlx.namespace.qualify_name(name, ns, replace=False) == 'gml:foo' 

68 

69 

70def test_qualify_name_replace(): 

71 name = 'gml:foo' 

72 ns = xmlx.namespace.get('soap') 

73 assert xmlx.namespace.qualify_name(name, ns, replace=True) == 'soap:foo' 

74 

75 

76def test_qualify_name_else(): 

77 name = 'foo' 

78 ns = None 

79 assert xmlx.namespace.qualify_name(name, ns) == 'foo' 

80 

81 

82def test_unqualify_name(): 

83 assert xmlx.namespace.unqualify_name('name') == 'name' 

84 

85 

86def test_declarations_default(): 

87 ns = xmlx.namespace.require('soap') 

88 assert xmlx.namespace.declarations({'': ns}) == {'xmlns': 'http://www.w3.org/2003/05/soap-envelope'} 

89 

90 

91def test_declarations_element(): 

92 ns_map = { 

93 '': xmlx.namespace.require('soap'), 

94 'gml': xmlx.namespace.require('gml'), 

95 } 

96 assert xmlx.namespace.declarations(ns_map) == { 

97 'xmlns': 'http://www.w3.org/2003/05/soap-envelope', 

98 'xmlns:gml': 'http://www.opengis.net/gml/3.2', 

99 } 

100 

101 

102def test_declarations_schema(): 

103 ns_map = { 

104 '': xmlx.namespace.require('soap'), 

105 'gml': xmlx.namespace.require('gml'), 

106 } 

107 assert xmlx.namespace.declarations(ns_map, with_schema_locations=True) == { 

108 'xmlns': 'http://www.w3.org/2003/05/soap-envelope', 

109 'xmlns:gml': 'http://www.opengis.net/gml/3.2', 

110 'xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance', 

111 'xsi:schemaLocation': 'http://www.w3.org/2003/05/soap-envelope http://www.w3.org/2003/05/soap-envelope/ http://www.opengis.net/gml/3.2 http://schemas.opengis.net/gml/3.2.1/gml.xsd', 

112 }