paper.vala 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /********************************************************************
  2. # Copyright 2016-2017 Daniel 'grindhold' Brendle
  3. #
  4. # This file is part of liboparl.
  5. #
  6. # liboparl is free software: you can redistribute it and/or
  7. # modify it under the terms of the GNU Lesser General Public License
  8. # as published by the Free Software Foundation, either
  9. # version 3 of the License, or (at your option) any later
  10. # version.
  11. #
  12. # liboparl is distributed in the hope that it will be
  13. # useful, but WITHOUT ANY WARRANTY; without even the implied
  14. # warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. # PURPOSE. See the GNU Lesser General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Lesser General Public
  18. # License along with liboparl.
  19. # If not, see http://www.gnu.org/licenses/.
  20. *********************************************************************/
  21. using OParl;
  22. namespace OParlTest {
  23. public class PaperTest {
  24. private static GLib.HashTable<string,string> test_input;
  25. private static void init() {
  26. PaperTest.test_input = new GLib.HashTable<string,string>(GLib.str_hash, GLib.str_equal);
  27. PaperTest.test_input.insert("https://oparl.example.org/", Fixtures.system_sane);
  28. PaperTest.test_input.insert("https://oparl.example.org/bodies", Fixtures.body_list_sane);
  29. PaperTest.test_input.insert("https://oparl.example.org/meeting/0", Fixtures.meeting_sane);
  30. PaperTest.test_input.insert("https://oparl.example.org/body/0/papers/", Fixtures.paper_list_sane);
  31. PaperTest.test_input.insert("https://oparl.example.org/body/0", Fixtures.body_sane);
  32. PaperTest.test_input.insert("https://oparl.example.org/paper/0", Fixtures.paper_sane);
  33. PaperTest.test_input.insert("https://oparl.example.org/person/0", Fixtures.person_sane);
  34. PaperTest.test_input.insert("https://oparl.example.org/organization/0", Fixtures.organization_sane);
  35. }
  36. public static void add_tests () {
  37. PaperTest.init();
  38. Test.add_func ("/oparl/paper/sane_input", () => {
  39. var client = new Client();
  40. TestHelper.mock_connect(ref client, PaperTest.test_input, null);
  41. System s;
  42. try {
  43. s = client.open("https://oparl.example.org/");
  44. } catch (ParsingError e) {
  45. GLib.assert_not_reached();
  46. }
  47. try {
  48. Body b = s.get_body().nth_data(0);
  49. Paper p = b.get_paper().nth_data(0);
  50. assert (p.id == "https://oparl.example.org/paper/0");
  51. assert (p.get_body() != null);
  52. assert (p.get_body() is Body);
  53. assert (p.name == "Antwort auf Anfrage 1200/2014");
  54. assert (p.reference == "1234/2014");
  55. assert (p.date.to_string() == "2014-04-04T00:00:00+0000");
  56. assert (p.paper_type == "Beantwortung einer Anfrage");
  57. assert (p.get_related_paper() != null);
  58. assert (p.get_related_paper().nth_data(0) is Paper);
  59. assert (p.main_file != null);
  60. assert (p.main_file is OParl.File);
  61. assert (p.auxiliary_file != null);
  62. assert (p.auxiliary_file.nth_data(0) is OParl.File);
  63. assert (p.location != null);
  64. assert (p.location.nth_data(0) is Location);
  65. assert (p.get_originator_person() != null);
  66. assert (p.get_originator_person().nth_data(0) is Person);
  67. assert (p.get_originator_organization() != null);
  68. assert (p.get_originator_organization().nth_data(0) is Organization);
  69. assert (p.consultation != null);
  70. assert (p.consultation.nth_data(0) is Consultation);
  71. assert (p.get_under_direction_of() != null);
  72. assert (p.get_under_direction_of().nth_data(0) is Organization);
  73. assert (p.created.to_string() == "2013-01-08T12:05:27+0000");
  74. assert (p.modified.to_string() == "2013-01-08T12:05:27+0000");
  75. } catch (ParsingError e) {
  76. GLib.assert_not_reached();
  77. }
  78. });
  79. Test.add_func ("/oparl/paper/wrong_id_type", () => {
  80. var client = new Client();
  81. client.resolve_url.connect((url)=>{
  82. var data = PaperTest.test_input.get(url);
  83. if (url == "https://oparl.example.org/body/0/papers/") {
  84. data = data.replace(
  85. "\"https://oparl.example.org/paper/0\"", "1"
  86. );
  87. }
  88. return new ResolveUrlResult(data, true, 200);
  89. });
  90. try {
  91. System s = client.open("https://oparl.example.org/");
  92. Body b = s.get_body().nth_data(0);
  93. b.get_paper().nth_data(0);
  94. GLib.assert_not_reached();
  95. } catch (ParsingError e) {
  96. assert(e.message.contains("'id'"));
  97. }
  98. });
  99. Test.add_func ("/oparl/paper/wrong_body_type", () => {
  100. var client = new Client();
  101. TestHelper.mock_connect(ref client, PaperTest.test_input, "\"https://oparl.example.org/body/0\"");
  102. try {
  103. System s = client.open("https://oparl.example.org/");
  104. Body b = s.get_body().nth_data(0);
  105. b.get_paper().nth_data(0);
  106. GLib.assert_not_reached();
  107. } catch (ParsingError e) {
  108. assert(e.message.contains("'id'"));
  109. }
  110. });
  111. Test.add_func ("/oparl/paper/wrong_name_type", () => {
  112. var client = new Client();
  113. TestHelper.mock_connect(ref client, PaperTest.test_input, "\"Antwort auf Anfrage 1200/2014\"");
  114. try {
  115. System s = client.open("https://oparl.example.org/");
  116. Body b = s.get_body().nth_data(0);
  117. b.get_paper().nth_data(0);
  118. GLib.assert_not_reached();
  119. } catch (ParsingError e) {
  120. assert(e.message.contains("'name'"));
  121. }
  122. });
  123. Test.add_func ("/oparl/paper/wrong_reference_type", () => {
  124. var client = new Client();
  125. TestHelper.mock_connect(ref client, PaperTest.test_input, "\"1234/2014\"");
  126. try {
  127. System s = client.open("https://oparl.example.org/");
  128. Body b = s.get_body().nth_data(0);
  129. b.get_paper().nth_data(0);
  130. GLib.assert_not_reached();
  131. } catch (ParsingError e) {
  132. assert(e.message.contains("'reference'"));
  133. }
  134. });
  135. Test.add_func ("/oparl/paper/wrong_date_type", () => {
  136. var client = new Client();
  137. TestHelper.mock_connect(ref client, PaperTest.test_input, "\"2014-04-04\"");
  138. try {
  139. System s = client.open("https://oparl.example.org/");
  140. Body b = s.get_body().nth_data(0);
  141. b.get_paper().nth_data(0);
  142. GLib.assert_not_reached();
  143. } catch (ParsingError e) {
  144. assert(e.message.contains("'date'"));
  145. }
  146. });
  147. Test.add_func ("/oparl/paper/wrong_paper_type_type", () => {
  148. var client = new Client();
  149. TestHelper.mock_connect(ref client, PaperTest.test_input, "\"Beantwortung einer Anfrage\"");
  150. try {
  151. System s = client.open("https://oparl.example.org/");
  152. Body b = s.get_body().nth_data(0);
  153. b.get_paper().nth_data(0);
  154. GLib.assert_not_reached();
  155. } catch (ParsingError e) {
  156. assert(e.message.contains("'paperType'"));
  157. }
  158. });
  159. // TODO: add tests for wrong composite types?
  160. }
  161. }
  162. }