|
| 1 | +import unittest |
| 2 | +from hydra_python_core import doc_writer |
| 3 | +from unittest.mock import MagicMock, patch |
| 4 | + |
| 5 | + |
| 6 | +class TestDocWriter(unittest.TestCase): |
| 7 | + |
| 8 | + # test context class methods |
| 9 | + def test_context_with_nothing(self): |
| 10 | + """ |
| 11 | + Test method to test if correct context is generated when no arguments are passed |
| 12 | +
|
| 13 | + """ |
| 14 | + context = doc_writer.Context('https://hydrus.com/api') |
| 15 | + expected_context = { |
| 16 | + "hydra": "http://www.w3.org/ns/hydra/core#", |
| 17 | + "property": { |
| 18 | + "@type": "@id", |
| 19 | + "@id": "hydra:property" |
| 20 | + }, |
| 21 | + "supportedClass": "hydra:supportedClass", |
| 22 | + "supportedProperty": "hydra:supportedProperty", |
| 23 | + "supportedOperation": "hydra:supportedOperation", |
| 24 | + "statusCodes": "hydra:statusCodes", |
| 25 | + "label": "rdfs:label", |
| 26 | + "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#", |
| 27 | + "vocab": "https://hydrus.com/api/vocab#", |
| 28 | + # "vocab": "localhost/api/vocab#", |
| 29 | + "domain": { |
| 30 | + "@type": "@id", |
| 31 | + "@id": "rdfs:domain" |
| 32 | + }, |
| 33 | + "ApiDocumentation": "hydra:ApiDocumentation", |
| 34 | + "range": { |
| 35 | + "@type": "@id", |
| 36 | + "@id": "rdfs:range" |
| 37 | + }, |
| 38 | + "rdfs": "http://www.w3.org/2000/01/rdf-schema#", |
| 39 | + "title": "hydra:title", |
| 40 | + "expects": { |
| 41 | + "@type": "@id", |
| 42 | + "@id": "hydra:expects" |
| 43 | + }, |
| 44 | + "returns": { |
| 45 | + "@id": "hydra:returns", |
| 46 | + "@type": "@id" |
| 47 | + }, |
| 48 | + "readonly": "hydra:readonly", |
| 49 | + "writeonly": "hydra:writeonly", |
| 50 | + "possibleStatus": "hydra:possibleStatus", |
| 51 | + "required": "hydra:required", |
| 52 | + "method": "hydra:method", |
| 53 | + "statusCode": "hydra:statusCode", |
| 54 | + "description": "hydra:description", |
| 55 | + "subClassOf": { |
| 56 | + "@id": "rdfs:subClassOf", |
| 57 | + "@type": "@id" |
| 58 | + } |
| 59 | + } |
| 60 | + self.assertEqual(expected_context, context.generate()) |
| 61 | + |
| 62 | + @patch('hydra_python_core.doc_writer.HydraEntryPoint', |
| 63 | + spec=doc_writer.HydraEntryPoint) |
| 64 | + def test_context_with_entrypoint(self, mock_entry): |
| 65 | + """ |
| 66 | + Test method to test if correct context is generated when HydraEntryPoint is passed |
| 67 | +
|
| 68 | + """ |
| 69 | + |
| 70 | + hydra_entry_point_mock = mock_entry() |
| 71 | + hydra_entry_point_mock.base_url = "http://petstore.swagger.io/v2" |
| 72 | + hydra_entry_point_mock.entrypoint = "EntryPoint" |
| 73 | + |
| 74 | + context = doc_writer.Context('http://petstore.swagger.io/v2', |
| 75 | + entrypoint=hydra_entry_point_mock) |
| 76 | + |
| 77 | + expected_context = { |
| 78 | + "EntryPoint": "vocab:EntryPoint", |
| 79 | + "vocab": "http://petstore.swagger.io/v2/vocab#" |
| 80 | + } |
| 81 | + self.assertEqual(expected_context, context.generate()) |
| 82 | + |
| 83 | + def test_context_with_class(self): |
| 84 | + """ |
| 85 | + Test method to test if correct context is generated when HydraClass is passed |
| 86 | +
|
| 87 | + """ |
| 88 | + |
| 89 | + mocked_hydra_class = MagicMock() |
| 90 | + with patch( |
| 91 | + 'hydra_python_core.doc_writer.HydraClass', |
| 92 | + mocked_hydra_class, spec_set=doc_writer.HydraClass): |
| 93 | + mocked_hydra_property = MagicMock() |
| 94 | + mocked_hydra_class.id_ = "vocab:Pet" |
| 95 | + mocked_hydra_class.title = "Pet" |
| 96 | + mocked_hydra_class.desc = "Pet" |
| 97 | + with patch('hydra_python_core.doc_writer.HydraClassProp', mocked_hydra_property, |
| 98 | + spec_set=doc_writer.HydraClassProp): |
| 99 | + mocked_hydra_property.prop = "" |
| 100 | + mocked_hydra_property.readonly = "true" |
| 101 | + mocked_hydra_property.required = "false" |
| 102 | + mocked_hydra_property.title = "id" |
| 103 | + mocked_hydra_property.writeonly = "true" |
| 104 | + |
| 105 | + mocked_hydra_class.supportedProperty = [mocked_hydra_property] |
| 106 | + |
| 107 | + context = doc_writer.Context( |
| 108 | + 'http://petstore.swagger.io/v2', |
| 109 | + class_=mocked_hydra_class) |
| 110 | + |
| 111 | + expected_context = { |
| 112 | + "vocab": "http://petstore.swagger.io/v2/vocab#", |
| 113 | + "hydra": "http://www.w3.org/ns/hydra/core#", |
| 114 | + "members": "http://www.w3.org/ns/hydra/core#member", |
| 115 | + "object": "http://schema.org/object", |
| 116 | + "Pet": "vocab:Pet", |
| 117 | + "id": "" |
| 118 | + } |
| 119 | + |
| 120 | + self.assertEqual(expected_context, context.generate()) |
| 121 | + |
| 122 | + @patch('hydra_python_core.doc_writer.HydraClass', spec=doc_writer.HydraClass) |
| 123 | + @patch('hydra_python_core.doc_writer.HydraCollection', |
| 124 | + spec=doc_writer.HydraCollection) |
| 125 | + def test_context_with_collection(self, hydra_class, hydra_collection): |
| 126 | + """ |
| 127 | + Test method to test if correct context is generated when HydraCollection is passed |
| 128 | +
|
| 129 | + """ |
| 130 | + mocked_hydra_class = hydra_class() |
| 131 | + mocked_hydra_class.id_ = "vocab:Pet" |
| 132 | + mocked_hydra_class.title = "Pet" |
| 133 | + mocked_hydra_class.desc = "Pet" |
| 134 | + |
| 135 | + mocked_hydra_collection = hydra_collection() |
| 136 | + mocked_hydra_collection.class_ = mocked_hydra_class |
| 137 | + mocked_hydra_collection.name = "{}Collection".format(mocked_hydra_class.title) |
| 138 | + context = doc_writer.Context( |
| 139 | + 'http://petstore.swagger.io/v2', |
| 140 | + collection=mocked_hydra_collection) |
| 141 | + expected_context = { |
| 142 | + "vocab": "http://petstore.swagger.io/v2/vocab#", |
| 143 | + "hydra": "http://www.w3.org/ns/hydra/core#", |
| 144 | + "members": "http://www.w3.org/ns/hydra/core#member", |
| 145 | + "PetCollection": "vocab:PetCollection", |
| 146 | + "Pet": "vocab:Pet" |
| 147 | + } |
| 148 | + |
| 149 | + self.assertEqual(expected_context, context.generate()) |
| 150 | + |
| 151 | +if __name__ == '__main__': |
| 152 | + unittest.main() |
0 commit comments