def test_add_child(self): # Adding a child should mutate the children on self and the parents on the child civil_engineer = Occupation(identifier='12-3', name='Civil Engineer') engineers = Occupation(identifier='12', name='major group 12') engineers.add_child(civil_engineer) assert engineers in civil_engineer.parents assert civil_engineer in engineers.children
def test_jsonld(self): # The jsonld version of an occupation should include all parent/child links, using the jsonld id civil_engineer = Occupation(identifier='12-3', name='Civil Engineer') engineers = Occupation(identifier='12', name='major group 12') engineers.add_child(civil_engineer) assert civil_engineer.jsonld_full == { '@type': 'Occupation', '@id': '12-3', 'name': 'Civil Engineer', 'hasChild': [], 'isChildOf': [{'@type': 'Occupation', '@id': '12'}], }
def test_filter_by(self): # Should be able to take an ontology and filter it by the edges, returning a new sub-ontology ontology = CompetencyOntology() comm = Competency(identifier='123', name='communication', categories=['social skills']) python = Competency(identifier='999', name='python', categories=['Technologies']) math = Competency(identifier='111', name='mathematics', categories=['Knowledge']) science = Competency(identifier='222', name='science', categories=['Knowledge']) civil_engineer = Occupation(identifier='123', name='Civil Engineer') ontology.add_competency(comm) ontology.add_competency(python) ontology.add_competency(math) ontology.add_competency(science) ontology.add_occupation(civil_engineer) ontology.add_edge(occupation=civil_engineer, competency=math) ontology.add_edge(occupation=civil_engineer, competency=science) tech_ontology = ontology.filter_by( lambda edge: 'Technologies' in edge.competency.categories) assert tech_ontology.competencies == {python} assert len(tech_ontology.occupations) == 0 civil_engineer_ontology = ontology.filter_by( lambda edge: edge.occupation == civil_engineer) assert civil_engineer_ontology.competencies == {math, science} assert civil_engineer_ontology.occupations == {civil_engineer}
def test_add_occupation(self): # Should be able to add an occupation to an ontology occupation = Occupation(identifier='456', name='Civil Engineer') ontology = CompetencyOntology() ontology.add_occupation(occupation) assert len(ontology.occupations) == 1 assert occupation in ontology.occupations
def test_create(self): # Should be able to create an edge with a competency and occupation competency = Competency(identifier='123', name='communication', categories=['social skills']) occupation = Occupation(identifier='456', name='Civil Engineer') edge = CompetencyOccupationEdge(competency=competency, occupation=occupation) assert edge.competency == competency assert edge.occupation == occupation
def test_add_occupation_merge(self): # Should be able to add an occupation that already exists, and it will merge the attributes first_child = Occupation(identifier='456', name='Civil Engineer') parent_occupation = Occupation(identifier='45', name='Engineers') ontology = CompetencyOntology() first_child.add_parent(parent_occupation) ontology.add_occupation(first_child) ontology.add_occupation(parent_occupation) parent_occupation = Occupation(identifier='45', name='Engineers') second_child = Occupation(identifier='457', name='Structural Engineer') second_child.add_parent(parent_occupation) ontology.add_occupation(second_child) ontology.add_occupation(parent_occupation) assert len(ontology.occupations) == 3 assert len(list(ontology.filter_by(lambda edge: edge.occupation.identifier == '45').occupations)[0].children) == 2
def test_add_edge(self): # Should be able to add an edge between an occupation and a competency to an ontology occupation = Occupation(identifier='456', name='Civil Engineer') competency = Competency(identifier='123', name='communication', categories=['social skills']) ontology = CompetencyOntology() ontology.add_edge(competency=competency, occupation=occupation) assert competency in ontology.competencies assert occupation in ontology.occupations assert len([edge for edge in ontology.edges if edge.occupation == occupation and edge.competency == competency]) == 1
def ontology(self): ontology = CompetencyOntology(name='Test Ontology') comm = Competency(identifier='123', name='communication', categories=['social skills']) python = Competency(identifier='999', name='python', categories=['Technologies']) math = Competency(identifier='111', name='mathematics', categories=['Knowledge']) science = Competency(identifier='222', name='science', categories=['Knowledge']) civil_engineer = Occupation(identifier='123', name='Civil Engineer') ontology.add_competency(comm) ontology.add_competency(python) ontology.add_competency(math) ontology.add_competency(science) ontology.add_occupation(civil_engineer) ontology.add_edge(occupation=civil_engineer, competency=math) ontology.add_edge(occupation=civil_engineer, competency=science) return ontology
def test_jsonld(self): competency = Competency(identifier='111', name='communication', categories=['social skills']) occupation = Occupation(identifier='123', name='Civil Engineer') edge = CompetencyOccupationEdge(competency=competency, occupation=occupation) assert edge.jsonld_full == { '@type': 'CompetencyOccupationEdge', '@id': 'competency=111&occupation=123', 'competency': { '@type': 'Competency', '@id': '111' }, 'occupation': { '@type': 'Occupation', '@id': '123' } }
def test_from_jsonld(self): jsonld_input = { '@type': 'Occupation', '@id': '12-3', 'name': 'Civil Engineer', 'hasChild': [], 'isChildOf': [{'@type': 'Occupation', '@id': '12'}], } target_occupation = Occupation(identifier='12-3', name='Civil Engineer') target_occupation.add_parent(Occupation(identifier='12', name='major group 12')) occupation = Occupation.from_jsonld(jsonld_input) assert occupation == target_occupation
def test_fromjsonld(self): jsonld_input = { '@type': 'CompetencyOccupationEdge', '@id': 'competency=111&occupation=123', 'competency': { '@type': 'Competency', '@id': '111' }, 'occupation': { '@type': 'Occupation', '@id': '123' } } competency = Competency(identifier='111') occupation = Occupation(identifier='123') target_edge = CompetencyOccupationEdge(competency=competency, occupation=occupation) edge = CompetencyOccupationEdge.from_jsonld(jsonld_input) assert edge == target_edge
def test_equivalence(self): # Two occupations with the same id should be equivalent occupation = Occupation(identifier='456', name='Civil Engineer') occupation_two = Occupation(identifier='456', name='Civil Engineer') assert occupation == occupation_two
def test_create(self): # Should be able to create an occupation with the necessary inputs occupation = Occupation(identifier='456', name='Civil Engineer') assert occupation.identifier == '456' assert occupation.name == 'Civil Engineer'