Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_xmi_save_load_EDate(tmpdir):
from datetime import datetime
f = tmpdir.mkdir('pyecore-tmp').join('default_date.xmi')
# Build a simple metamodel
Root = Ecore.EClass('Root')
Root.eStructuralFeatures.append(Ecore.EAttribute('date', Ecore.EDate))
pack = Ecore.EPackage('mypack', nsURI='http://mypack/1.0',
nsPrefix='mypack_pref')
pack.eClassifiers.append(Root)
date = datetime.utcnow()
r = Root()
r.date = date
rset = ResourceSet()
resource = rset.create_resource(URI(str(f)))
resource.append(r)
resource.save()
rset = ResourceSet()
rset.metamodel_registry[pack.nsURI] = pack
resource = rset.get_resource(URI(str(f)))
assert resource.contents[0].date == date
def test_json_save_multiple_roots_roundtrip(tmpdir):
A = Ecore.EClass('A')
A.eStructuralFeatures.append(Ecore.EAttribute('name', Ecore.EString))
pack = Ecore.EPackage('pack', 'packuri', 'pack')
pack.eClassifiers.append(A)
f = tmpdir.mkdir('pyecore-tmp').join('multiple.json')
resource = JsonResource(URI(str(f)))
resource.append(A(name='root1'))
resource.append(A(name='root2'))
resource.save()
global_registry[pack.nsURI] = pack
resource = JsonResource(URI(str(f)))
resource.load()
assert len(resource.contents) == 2
assert resource.contents[0].name == 'root1'
assert resource.contents[1].name == 'root2'
del global_registry[pack.nsURI]
# Class2
# MyEnum
package = EPackage('ThePackage')
class1 = EClass('Class1')
class2 = EClass('Class2')
enum = EEnum('MyEnum', literals=['A', 'B'])
subpackage = EPackage('SubPackage')
package.eSubpackages.append(subpackage)
package.eClassifiers.append(class1)
subpackage.eClassifiers.extend([class2, enum])
task = EcoreTask()
task.element_type = EPackage
assert set(task.filtered_elements(package)) == {package, subpackage}
task.element_type = EClass
assert set(task.filtered_elements(package)) == {class1, class2}
task.element_type = EEnum
assert set(task.filtered_elements(package)) == {enum}
def lib():
package = Ecore.EPackage('mypackage')
package.nsURI = 'http://simplemetamodel/1.0'
package.nsPrefix = 'simplemm'
AbsA = Ecore.EClass('AbsA', abstract=True)
A = Ecore.EClass('A', superclass=(AbsA,))
SubA = Ecore.EClass('SubA', superclass=(A,))
MyRoot = Ecore.EClass('MyRoot')
MyRoot.a_container = Ecore.EReference('a_container', eType=AbsA, upper=-1,
containment=True)
MyRoot.eStructuralFeatures.append(MyRoot.a_container)
MyRoot.eStructuralFeatures.append(Ecore.EAttribute('trans',
eType=Ecore.EString,
transient=True))
package.eClassifiers.extend([MyRoot, A, SubA, AbsA])
package.MyRoot = MyRoot
package.SubA = SubA
package.A = A
def test_class_with_derived_features(pygen_output_dir):
rootpkg = EPackage('simpleClasses')
MyClass = EClass('MyClass')
rootpkg.eClassifiers.append(MyClass)
any_feature = EAttribute('any', EString, derived=True)
MyClass.eStructuralFeatures.append(any_feature)
mm = generate_meta_model(rootpkg, pygen_output_dir)
generated_class = mm.eClassifiers['MyClass']
assert mm.MyClass is generated_class
assert isinstance(mm.MyClass._any, EAttribute)
assert mm.MyClass._any.derived is True
assert mm.MyClass._any.name == 'any'
def test_eattribute_derived_not_changeable(pygen_output_dir):
rootpkg = EPackage('changeable_attribute')
c1 = EClass('MyClass')
rootpkg.eClassifiers.append(c1)
att1 = EAttribute('att1', EString, derived=True, changeable=True)
att2 = EAttribute('att2', EString, derived=True, changeable=False)
c1.eStructuralFeatures.extend([att2, att1])
mm = generate_meta_model(rootpkg, pygen_output_dir)
instance = mm.MyClass()
with pytest.raises(NotImplementedError):
instance.att1
with pytest.raises(NotImplementedError):
instance.att2
def test_class_with_features(pygen_output_dir):
rootpkg = EPackage('class_features')
class_ = EClass('MyClass')
rootpkg.eClassifiers.append(class_)
class_.eStructuralFeatures.append(EAttribute('number', EInt, changeable=False))
class_.eStructuralFeatures.append(EReference('ref', class_))
mm = generate_meta_model(rootpkg, pygen_output_dir)
generated_class = mm.eClassifiers['MyClass']
instance = generated_class(number=7)
assert instance.number == 7
assert not instance.ref
instance.ref = instance
assert instance.ref is instance
# we add more instances
a2 = lib.A()
root.a_container.append(a2)
# we save again
resource.save()
# we read the model
resource = XMIResource(URI(str(f)))
resource.load()
assert resource.contents != []
assert len(resource.contents[0].eContents) == 3
eClass = Ecore.EPackage(name='test', nsURI='http://test/1.0',
nsPrefix='test')
@Ecore.EMetaclass
class A(object):
name = Ecore.EAttribute('name', Ecore.EString)
age = Ecore.EAttribute('age', Ecore.EInt)
names = Ecore.EAttribute(eType=Ecore.EString, upper=-1)
def test_xmi_ecore_save_load(tmpdir):
f = tmpdir.mkdir('pyecore-tmp').join('test.xmi')
resource = XMIResource(URI(str(f)))
resource.append(eClass)
resource.save()
def test__generate_from_cli(generator_mock, cwd_module_dir):
mock_generator = generator_mock()
mock_generator.generate = mock.MagicMock()
generate_from_cli(['-e', 'input/library.ecore', '-o', 'some/folder'])
# look at arguments of generate call:
mock_generate = generator_mock().generate
model = mock_generator.generate.call_args[0][0]
path = mock_generator.generate.call_args[0][1]
assert isinstance(model, pyecore.ecore.EPackage)
assert model.name == 'library'
assert path == 'some/folder'
result.name = self.name + 'Copy'
result.abstract = self.abstract
for attribute in self.eAttributes:
result.eStructuralFeatures.append(copyEAttribute(attribute))
parent.eClassifiers.append(self)
@mapping
def copyEAttribute(self: Ecore.EAttribute) -> Ecore.EAttribute:
result.name = self.name + 'Copy'
result.lowerBound = self.lowerBound
result.upperBound = self.upperBound
result.eType = self.eType
root = Ecore.EPackage('test')
A1 = Ecore.EClass('A1')
root.eClassifiers.append(A1)
A1.eStructuralFeatures.append(Ecore.EAttribute('name', eType=Ecore.EString))
inPackage = root
result = createEPackage(inPackage)
rset = ResourceSet()
outresource = rset.create_resource(URI('testt.xmi'))
outresource.append(result)
outresource.save()