|
| 1 | +package com.regnosys.rosetta.generator.python.generated_syntax; |
| 2 | + |
| 3 | +import jakarta.inject.Inject; |
| 4 | +import com.regnosys.rosetta.tests.RosettaInjectorProvider; |
| 5 | +import com.regnosys.rosetta.generator.python.PythonGeneratorTestUtils; |
| 6 | +import org.eclipse.xtext.testing.InjectWith; |
| 7 | +import org.eclipse.xtext.testing.extensions.InjectionExtension; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 10 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 11 | +import java.util.Map; |
| 12 | + |
| 13 | +@ExtendWith(InjectionExtension.class) |
| 14 | +@InjectWith(RosettaInjectorProvider.class) |
| 15 | +public class PythonMetaDataGeneratorTest { |
| 16 | + |
| 17 | + @Inject |
| 18 | + private PythonGeneratorTestUtils testUtils; |
| 19 | + |
| 20 | + private Map<String, CharSequence> python = null; |
| 21 | + |
| 22 | + private Map<String, CharSequence> getPython() { |
| 23 | + if (python == null) { |
| 24 | + python = testUtils.generatePythonFromString( |
| 25 | + """ |
| 26 | + namespace test.generated_syntax.metadata : <"generate Python unit tests from Rosetta."> |
| 27 | +
|
| 28 | + type A: |
| 29 | + [metadata key] |
| 30 | + fieldA string (1..1) |
| 31 | +
|
| 32 | + type NodeRef: |
| 33 | + typeA A (0..1) |
| 34 | + aReference A (0..1) |
| 35 | + [metadata reference] |
| 36 | +
|
| 37 | + type AttributeRef: |
| 38 | + dateField date (0..1) |
| 39 | + [metadata id] |
| 40 | + dateReference date (0..1) |
| 41 | + [metadata reference] |
| 42 | +
|
| 43 | + type Root: |
| 44 | + [rootType] |
| 45 | + nodeRef NodeRef (0..1) |
| 46 | + attributeRef AttributeRef (0..1) |
| 47 | +
|
| 48 | + type SchemeTest: |
| 49 | + [metadata scheme] |
| 50 | + a string (1..1) |
| 51 | + """); |
| 52 | + } |
| 53 | + return python; |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void testAProxy() { |
| 58 | + Map<String, CharSequence> python = getPython(); |
| 59 | + testUtils.assertGeneratedContainsExpectedString( |
| 60 | + python.get("src/test/generated_syntax/metadata/A.py").toString(), |
| 61 | + """ |
| 62 | + # pylint: disable=unused-import |
| 63 | + from test._bundle import test_generated_syntax_metadata_A as A |
| 64 | +
|
| 65 | + # EOF |
| 66 | + """); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testNodeRefProxy() { |
| 71 | + Map<String, CharSequence> python = getPython(); |
| 72 | + testUtils.assertGeneratedContainsExpectedString( |
| 73 | + python.get("src/test/generated_syntax/metadata/NodeRef.py").toString(), |
| 74 | + """ |
| 75 | + # pylint: disable=unused-import |
| 76 | + from test._bundle import test_generated_syntax_metadata_NodeRef as NodeRef |
| 77 | +
|
| 78 | + # EOF |
| 79 | + """); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testAttributeRefProxy() { |
| 84 | + Map<String, CharSequence> python = getPython(); |
| 85 | + testUtils.assertGeneratedContainsExpectedString( |
| 86 | + python.get("src/test/generated_syntax/metadata/AttributeRef.py").toString(), |
| 87 | + """ |
| 88 | + # pylint: disable=unused-import |
| 89 | + from test._bundle import test_generated_syntax_metadata_AttributeRef as AttributeRef |
| 90 | +
|
| 91 | + # EOF |
| 92 | + """); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void testRootProxy() { |
| 97 | + Map<String, CharSequence> python = getPython(); |
| 98 | + testUtils.assertGeneratedContainsExpectedString( |
| 99 | + python.get("src/test/generated_syntax/metadata/Root.py").toString(), |
| 100 | + """ |
| 101 | + # pylint: disable=unused-import |
| 102 | + from test._bundle import test_generated_syntax_metadata_Root as Root |
| 103 | +
|
| 104 | + # EOF |
| 105 | + """); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void testBundleExists() { |
| 110 | + Map<String, CharSequence> python = getPython(); |
| 111 | + assertTrue(python.containsKey("src/test/_bundle.py"), "The bundle should be in the generated Python"); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void testExpectedBundleA() { |
| 116 | + Map<String, CharSequence> python = getPython(); |
| 117 | + String generatedBundle = python.get("src/test/_bundle.py").toString(); |
| 118 | + String expectedA = """ |
| 119 | + class test_generated_syntax_metadata_A(BaseDataClass): |
| 120 | + _ALLOWED_METADATA = {'@key', '@key:external'} |
| 121 | + _FQRTN = 'test.generated_syntax.metadata.A' |
| 122 | + fieldA: str = Field(..., description='') |
| 123 | + """; |
| 124 | + testUtils.assertGeneratedContainsExpectedString(generatedBundle, expectedA); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + public void testExpectedBundleAttributeRef() { |
| 129 | + Map<String, CharSequence> python = getPython(); |
| 130 | + String generatedBundle = python.get("src/test/_bundle.py").toString(); |
| 131 | + String expectedAttributeRef = """ |
| 132 | + class test_generated_syntax_metadata_AttributeRef(BaseDataClass): |
| 133 | + _FQRTN = 'test.generated_syntax.metadata.AttributeRef' |
| 134 | + dateField: Optional[Annotated[DateWithMeta, DateWithMeta.serializer(), DateWithMeta.validator(('@key', '@key:external'))]] = Field(None, description='') |
| 135 | + dateReference: Optional[Annotated[DateWithMeta, DateWithMeta.serializer(), DateWithMeta.validator(('@ref', '@ref:external'))]] = Field(None, description='') |
| 136 | + \s\s\s\s |
| 137 | + _KEY_REF_CONSTRAINTS = { |
| 138 | + 'dateField': {'@key', '@key:external'}, |
| 139 | + 'dateReference': {'@ref', '@ref:external'} |
| 140 | + } |
| 141 | + """; |
| 142 | + testUtils.assertGeneratedContainsExpectedString(generatedBundle, expectedAttributeRef); |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + public void testExpectedBundleNodeRef() { |
| 147 | + Map<String, CharSequence> python = getPython(); |
| 148 | + String generatedBundle = python.get("src/test/_bundle.py").toString(); |
| 149 | + String expectedNodeRef = """ |
| 150 | + class test_generated_syntax_metadata_NodeRef(BaseDataClass): |
| 151 | + _FQRTN = 'test.generated_syntax.metadata.NodeRef' |
| 152 | + typeA: Optional[Annotated[test_generated_syntax_metadata_A, test_generated_syntax_metadata_A.serializer(), test_generated_syntax_metadata_A.validator()]] = Field(None, description='') |
| 153 | + aReference: Optional[Annotated[test_generated_syntax_metadata_A, test_generated_syntax_metadata_A.serializer(), test_generated_syntax_metadata_A.validator(('@key', '@key:external', '@ref', '@ref:external'))]] = Field(None, description='') |
| 154 | + \s\s\s\s |
| 155 | + _KEY_REF_CONSTRAINTS = { |
| 156 | + 'aReference': {'@key', '@key:external', '@ref', '@ref:external'} |
| 157 | + } |
| 158 | + """; |
| 159 | + testUtils.assertGeneratedContainsExpectedString(generatedBundle, expectedNodeRef); |
| 160 | + } |
| 161 | + |
| 162 | + @Test |
| 163 | + public void testExpectedBundleRoot() { |
| 164 | + Map<String, CharSequence> python = getPython(); |
| 165 | + String generatedBundle = python.get("src/test/_bundle.py").toString(); |
| 166 | + String expectedRoot = """ |
| 167 | + class test_generated_syntax_metadata_Root(BaseDataClass): |
| 168 | + _FQRTN = 'test.generated_syntax.metadata.Root' |
| 169 | + nodeRef: Optional[Annotated[test_generated_syntax_metadata_NodeRef, test_generated_syntax_metadata_NodeRef.serializer(), test_generated_syntax_metadata_NodeRef.validator()]] = Field(None, description='') |
| 170 | + attributeRef: Optional[Annotated[test_generated_syntax_metadata_AttributeRef, test_generated_syntax_metadata_AttributeRef.serializer(), test_generated_syntax_metadata_AttributeRef.validator()]] = Field(None, description='') |
| 171 | + """; |
| 172 | + testUtils.assertGeneratedContainsExpectedString(generatedBundle, expectedRoot); |
| 173 | + } |
| 174 | + |
| 175 | + @Test |
| 176 | + public void testExpectedBundleScheme() { |
| 177 | + Map<String, CharSequence> python = getPython(); |
| 178 | + String generatedBundle = python.get("src/test/_bundle.py").toString(); |
| 179 | + String expectedScheme = """ |
| 180 | + class test_generated_syntax_metadata_SchemeTest(BaseDataClass): |
| 181 | + _ALLOWED_METADATA = {'@scheme'} |
| 182 | + _FQRTN = 'test.generated_syntax.metadata.SchemeTest' |
| 183 | + a: str = Field(..., description='') |
| 184 | + """; |
| 185 | + testUtils.assertGeneratedContainsExpectedString(generatedBundle, expectedScheme); |
| 186 | + } |
| 187 | +} |
0 commit comments