Example #1
0
 def testToJson(self):
     pred = "test-multi-pred"
     obj = [
         SimpleLink(pred="test-simple-pred-1", obj="test-simple-obj-1"),
         SimpleLink(pred="test-simple-pred-1", obj="test-simple-obj-2"),
         SimpleLink(pred="test-simple-pred-2", obj="test-simple-obj-3"),
     ]
     link = MultiLink(pred=pred, obj=obj)
     expected_link_dict = {
         "pred":
         "test-multi-pred",
         "obj": [
             {
                 "pred": "test-simple-pred-1",
                 "obj": "test-simple-obj-1",
                 "type": "simple"
             },
             {
                 "pred": "test-simple-pred-1",
                 "obj": "test-simple-obj-2",
                 "type": "simple"
             },
             {
                 "pred": "test-simple-pred-2",
                 "obj": "test-simple-obj-3",
                 "type": "simple"
             },
         ],
         "type":
         "multi",
     }
     link_dict = link.to_dict()
     self.assertDictEqual(expected_link_dict, link_dict)
Example #2
0
 def testToRdf(self):
     pred = "test-multi-pred"
     obj = [
         SimpleLink(pred="test-simple-pred-1", obj="test-simple-obj-1"),
         SimpleLink(pred="test-simple-pred-2", obj="test-simple-obj-2"),
         SimpleLink(pred="test-simple-pred-3", obj="test-simple-obj-3"),
     ]
     link = MultiLink(pred=pred, obj=obj)
     bnode = BNode()
     graph = Graph()
     namespace = Namespace("test:")
     node_cache = NodeCache()
     link.to_rdf(subj=bnode,
                 namespace=namespace,
                 graph=graph,
                 node_cache=node_cache)
     results = graph.query(
         "select ?p ?o where {?s a <test:test-multi-pred> ; ?p ?o} order by ?p ?o"
     )
     result_tuples = []
     for result in results:
         self.assertEqual(2, len(result))
         result_tuples.append((str(result[0]), str(result[1])))
     expected_result_tuples = [
         ("http://www.w3.org/1999/02/22-rdf-syntax-ns#type",
          "test:test-multi-pred"),
         ("test:test-simple-pred-1", "test-simple-obj-1"),
         ("test:test-simple-pred-2", "test-simple-obj-2"),
         ("test:test-simple-pred-3", "test-simple-obj-3"),
     ]
     self.assertEqual(result_tuples, expected_result_tuples)
Example #3
0
 def testToJson(self):
     pred = "test-pred"
     obj = "test-obj"
     link = SimpleLink(pred=pred, obj=obj)
     expected_link_dict = {"type": "simple", "pred": pred, "obj": obj}
     link_dict = link.to_dict()
     self.assertDictEqual(expected_link_dict, link_dict)
Example #4
0
    def parse(self, data: Dict[str, Any], context: Dict[str,
                                                        Any]) -> List[Link]:
        """Parse this field and return a list of Links.

       Args:
           data: dictionary of data to parse
           context: context dict containing data from higher level parsing code.

        Returns:
            List of SimpleLink objects. At most one link will be returned.
        """
        value = data.get(self.source_key)
        if value is None:
            if self.default_value is not None:
                return [SimpleLink(pred=self.alti_key, obj=self.default_value)]
            if self.optional:
                return []
            raise ScalarFieldSourceKeyNotFoundException(
                f"Expected key '{self.source_key}' in data, present keys: {', '.join(data.keys())}"
            )
        if isinstance(value, SCALAR_TYPES):
            return [SimpleLink(pred=self.alti_key, obj=value)]
        raise ScalarFieldValueNotAScalarException(
            (f"Expected data for key '{self.source_key}' to be one "
             f"of {SCALAR_TYPES}, is {type(value)}: {value}"))
Example #5
0
 def testToLpgLargeInt(self):
     pred = "test-pred"
     obj = 9223372036854775808
     link = SimpleLink(pred=pred, obj=obj)
     expected_link_dict = {"test-pred": "9223372036854775808"}
     parent = {}
     vertices = []
     edges = []
     link.to_lpg(parent, vertices, edges, "")
     self.assertDictEqual(expected_link_dict, parent)
Example #6
0
 def testToLpgSmallInt(self):
     pred = "test-pred"
     obj = 1
     link = SimpleLink(pred=pred, obj=obj)
     expected_link_dict = {"test-pred": 1}
     parent = {}
     vertices = []
     edges = []
     link.to_lpg(parent, vertices, edges, "")
     self.assertDictEqual(expected_link_dict, parent)
Example #7
0
 def testToLpgPrefix(self):
     pred = "test-pred"
     obj = "test-obj"
     link = SimpleLink(pred=pred, obj=obj)
     expected_link_dict = {"abctest-pred": "test-obj"}
     parent = {}
     vertices = []
     edges = []
     link.to_lpg(parent, vertices, edges, "abc")
     self.assertDictEqual(expected_link_dict, parent)
Example #8
0
 def create_resource(cls: Type["UnscannedAccountResourceSpec"],
                     account_id: str, errors: List[str]) -> Resource:
     links: List[Link] = []
     links.append(SimpleLink(pred="account_id", obj=account_id))
     for error in errors:
         link = SimpleLink(pred="error", obj=error)
         links.append(link)
     return Resource(
         resource_id=cls.generate_arn("", "", account_id),
         type_name=cls.get_full_type_name(),
         links=links,
     )
 def create_resource(cls: Type["UnscannedAccountResourceSpec"],
                     account_id: str, errors: List[str]) -> Resource:
     links: List[Link] = []
     links.append(SimpleLink(pred="account_id", obj=account_id))
     if errors:
         error = "\n".join(errors)
         links.append(
             SimpleLink(pred="error", obj=f"{error} - {uuid.uuid4()}"))
     return Resource(
         resource_id=cls.generate_arn(resource_id=account_id),
         type_name=cls.get_full_type_name(),
         links=links,
     )
Example #10
0
 def testToLpg(self):
     pred = "test-multi-pred"
     obj = [
         SimpleLink(pred="test-simple-pred-1", obj="test-simple-obj-1"),
         SimpleLink(pred="test-simple-pred-2", obj="test-simple-obj-2"),
         SimpleLink(pred="test-simple-pred-3", obj="test-simple-obj-3"),
     ]
     link = MultiLink(pred=pred, obj=obj)
     expected_link_dict = {
         "test-multi-pred.test-simple-pred-1": "test-simple-obj-1",
         "test-multi-pred.test-simple-pred-2": "test-simple-obj-2",
         "test-multi-pred.test-simple-pred-3": "test-simple-obj-3",
     }
     parent = {}
     vertices = []
     edges = []
     link.to_lpg(parent, vertices, edges, "")
     self.assertDictEqual(expected_link_dict, parent)
Example #11
0
 def testToRdfSmallInt(self):
     pred = "test-pred"
     obj = 20
     link = SimpleLink(pred=pred, obj=obj)
     bnode = BNode()
     graph = Graph()
     namespace = Namespace("test:")
     node_cache = NodeCache()
     link.to_rdf(subj=bnode,
                 namespace=namespace,
                 graph=graph,
                 node_cache=node_cache)
     results = graph.query("select ?s ?p ?o where {?s ?p ?o}")
     self.assertEqual(1, len(results))
     for result in results:
         _, _, o = result
         self.assertEqual(
             o.datatype, URIRef("http://www.w3.org/2001/XMLSchema#integer"))
Example #12
0
 def testToRdf(self):
     pred = "test-pred"
     obj = "test-obj"
     link = SimpleLink(pred=pred, obj=obj)
     bnode = BNode()
     graph = Graph()
     namespace = Namespace("test:")
     node_cache = NodeCache()
     link.to_rdf(subj=bnode,
                 namespace=namespace,
                 graph=graph,
                 node_cache=node_cache)
     results = graph.query("select ?s ?p ?o where {?s ?p ?o}")
     self.assertEqual(1, len(results))
     for result in results:
         s, p, o = result
         self.assertEqual(s, bnode)
         self.assertEqual(str(p), "test:test-pred")
         self.assertEqual(str(o), "test-obj")
Example #13
0
 def setUp(self):
     resource_a1 = Resource(resource_id="123",
                            type_name="test:a",
                            links=[SimpleLink(pred="has-foo", obj="goo")])
     resource_a2 = Resource(resource_id="456", type_name="test:a")
     resource_b1 = Resource(
         resource_id="abc",
         type_name="test:b",
         links=[ResourceLinkLink(pred="has-a", obj="123")])
     resource_b2 = Resource(resource_id="def",
                            type_name="test:b",
                            links=[SimpleLink(pred="name", obj="sue")])
     resources = [resource_a1, resource_a2, resource_b1, resource_b2]
     self.graph_set = GraphSet(
         name="test-name",
         version="1",
         start_time=1234,
         end_time=4567,
         resources=resources,
         errors=["test err 1", "test err 2"],
         stats=MultilevelCounter(),
     )
Example #14
0
 def test_orphaned_ref(self):
     resource_a1 = Resource(resource_id="123",
                            type_name="test:a",
                            links=[SimpleLink(pred="has-foo", obj="goo")])
     resource_b1 = Resource(
         resource_id="abc",
         type_name="test:b",
         links=[ResourceLinkLink(pred="has-a", obj="456")])
     resources = [resource_a1, resource_b1]
     graph_set = GraphSet(
         name="test-name",
         version="1",
         start_time=1234,
         end_time=4567,
         resources=resources,
         errors=["test err 1", "test err 2"],
         stats=MultilevelCounter(),
     )
     with self.assertRaises(GraphSetOrphanedReferencesException):
         graph_set.validate()
Example #15
0
    def parse(self, data: Union[str, bool, int, float],
              context: Dict[str, Any]) -> List[Link]:
        """Parse this field and return a list of Links. For a ScalarField at most one link
        will be returned.

        Args:
            data: scalar value
            context: context dict containing data from higher level parsing code.

        Returns:
            List of Link objects. At most one link will be returned.
        """
        parent_alti_key = self.get_parent_alti_key(data, context)
        links: List[Link] = []
        if isinstance(data, SCALAR_TYPES):
            link = SimpleLink(pred=parent_alti_key, obj=data)
            links.append(link)
            return links
        raise ScalarFieldValueNotAScalarException(
            (f"Expected data to be one of {SCALAR_TYPES}, is "
             f"{type(data)}: {data}"))
Example #16
0
    def test_valid_merge(self):
        resource_a1 = Resource(resource_id="123",
                               type_name="test:a",
                               links=[SimpleLink(pred="has-foo", obj="goo")])
        resource_a2 = Resource(resource_id="456", type_name="test:a")
        resource_b1 = Resource(
            resource_id="abc",
            type_name="test:b",
            links=[ResourceLinkLink(pred="has-a", obj="123")])
        resource_b2 = Resource(resource_id="def",
                               type_name="test:b",
                               links=[SimpleLink(pred="name", obj="sue")])
        graph_set_1 = GraphSet(
            name="graph-1",
            version="1",
            start_time=10,
            end_time=20,
            resources=[resource_a1, resource_a2],
            errors=["errora1", "errora2"],
            stats=MultilevelCounter(),
        )
        graph_set_2 = GraphSet(
            name="graph-1",
            version="1",
            start_time=15,
            end_time=25,
            resources=[resource_b1, resource_b2],
            errors=["errorb1", "errorb2"],
            stats=MultilevelCounter(),
        )
        graph_set_1.merge(graph_set_2)

        self.assertEqual(graph_set_1.name, "graph-1")
        self.assertEqual(graph_set_1.version, "1")
        self.assertEqual(graph_set_1.start_time, 10)
        self.assertEqual(graph_set_1.end_time, 25)
        self.assertCountEqual(graph_set_1.errors,
                              ["errora1", "errora2", "errorb1", "errorb2"])
        expected_resource_dicts = [
            {
                "type": "test:a",
                "links": [{
                    "pred": "has-foo",
                    "obj": "goo",
                    "type": "simple"
                }]
            },
            {
                "type": "test:a"
            },
            {
                "type": "test:b",
                "links": [{
                    "pred": "has-a",
                    "obj": "123",
                    "type": "resource_link"
                }]
            },
            {
                "type": "test:b",
                "links": [{
                    "pred": "name",
                    "obj": "sue",
                    "type": "simple"
                }]
            },
        ]
        resource_dicts = [
            resource.to_dict() for resource in graph_set_1.resources
        ]
        self.assertCountEqual(expected_resource_dicts, resource_dicts)