def test_link_dangling_to_rt(self) -> None:
        db_name = Fixtures.next_database()
        database_uri = f'database://{db_name}'
        cluster_uri = f'{db_name}://acluster'
        self.assertEqual(
            self._get(label=VertexTypes.Database,
                      key=database_uri,
                      extra_traversal=__.count()), 0)
        self._upsert(label=VertexTypes.Database, key=database_uri, name='test')
        self.assertEqual(
            self._get(label=VertexTypes.Database,
                      key=database_uri,
                      extra_traversal=__.count()), 1)
        self.assertEqual(
            self._get(label=VertexTypes.Cluster,
                      key=cluster_uri,
                      extra_traversal=__.count()), 0)

        with self.assertRaises(StopIteration):
            self._link(vertex1_label=VertexTypes.Database,
                       vertex1_key=database_uri,
                       vertex2_label=VertexTypes.Cluster,
                       vertex2_key=cluster_uri,
                       edge_label=EdgeTypes.Cluster,
                       aproperty='hi')
    def test_upsert_rt(self) -> None:
        # test that we will insert
        db_name = Fixtures.next_database()
        database_uri = f'database://{db_name}'
        exists = self._get(label=VertexTypes.Database,
                           key=database_uri,
                           extra_traversal=__.count())
        self.assertEqual(exists, 0)
        self._upsert(label=VertexTypes.Database, key=database_uri, name='test')
        exists = self._get(label=VertexTypes.Database,
                           key=database_uri,
                           extra_traversal=__.count())
        self.assertEqual(exists, 1)

        # test that we will not insert (_get will explode if more than one vertex matches)
        self._upsert(label=VertexTypes.Database, key=database_uri, name='test')
        vertexValueMap = self._get(label=VertexTypes.Database,
                                   key=database_uri,
                                   extra_traversal=__.valueMap(),
                                   get=FromResultSet.toList)
        self.assertIsNotNone(vertexValueMap)
    def test_expire_link(self) -> None:
        db_name = Fixtures.next_database()
        database_uri = f'database://{db_name}'
        database2_uri = f'database://{db_name}2'
        cluster_uri = f'{db_name}://acluster'
        cluster2_uri = f'{db_name}2://acluster'
        self._upsert(label=VertexTypes.Database, key=database_uri, name='test')
        self._upsert(label=VertexTypes.Database,
                     key=database2_uri,
                     name='test1')
        self._upsert(label=VertexTypes.Cluster, key=cluster_uri, name='test2')
        self._upsert(label=VertexTypes.Cluster, key=cluster2_uri, name='test3')

        # use non-standard EdgeType, so aproperty doesn't explode
        edge_type = EdgeType(label='CLUSTER',
                             properties=tuple([
                                 Property(name='created',
                                          type=GremlinType.Date,
                                          required=True),
                                 Property(name='aproperty',
                                          type=GremlinType.String)
                             ]))
        self._link(vertex1_label=VertexTypes.Database,
                   vertex1_key=database_uri,
                   vertex2_label=VertexTypes.Cluster,
                   vertex2_key=cluster_uri,
                   edge_label=edge_type,
                   aproperty='hi1')
        self._link(vertex1_label=VertexTypes.Database,
                   vertex1_key=database_uri,
                   vertex2_label=VertexTypes.Cluster,
                   vertex2_key=cluster2_uri,
                   edge_label=edge_type,
                   aproperty='hi2')
        self._link(vertex1_label=VertexTypes.Database,
                   vertex1_key=database2_uri,
                   vertex2_label=VertexTypes.Cluster,
                   vertex2_key=cluster_uri,
                   edge_label=edge_type,
                   aproperty='hi3')
        self._link(vertex1_label=VertexTypes.Database,
                   vertex1_key=database2_uri,
                   vertex2_label=VertexTypes.Cluster,
                   vertex2_key=cluster2_uri,
                   edge_label=edge_type,
                   aproperty='hi4')

        with self.get_proxy().query_executor() as executor:
            _expire_link(executor=executor,
                         g=self.get_proxy().g,
                         key_property_name=self.key_property_name,
                         edge_label=edge_type,
                         vertex1_label=VertexTypes.Database,
                         vertex1_key=database_uri,
                         vertex2_label=VertexTypes.Cluster,
                         vertex2_key=cluster_uri)

        # db -> cluster link was expired properly
        rel = self.get_relationship(
            node_type1=VertexTypes.Database.value.label,
            node_key1=database_uri,
            node_type2=VertexTypes.Cluster.value.label,
            node_key2=cluster_uri)
        self.assertEqual(_safe_get(rel), None)

        # db is still linked to its other cluster (not expired)
        rel = self.get_relationship(
            node_type1=VertexTypes.Database.value.label,
            node_key1=database_uri,
            node_type2=VertexTypes.Cluster.value.label,
            node_key2=cluster2_uri)
        self.assertIsNone(_safe_get(rel).get('expired'))

        # cluster is still linked to its other db (not expired)
        rel = self.get_relationship(
            node_type1=VertexTypes.Database.value.label,
            node_key1=database2_uri,
            node_type2=VertexTypes.Cluster.value.label,
            node_key2=cluster_uri)
        self.assertIsNone(_safe_get(rel).get('expired'))
    def test_edges(self) -> None:
        db_name = Fixtures.next_database()
        database_uri = f'database://{db_name}'
        cluster1_uri = f'{db_name}://acluster'
        cluster2_uri = f'{db_name}://bcluster'
        self.assertNotEqual(cluster1_uri, cluster2_uri)
        cluster_vertex_type = VertexType.construct_type(
            label=VertexTypes.Cluster.value.label,
            properties=tuple([
                Property(name='aproperty', type=GremlinType.String),
                Property(name='b', type=GremlinType.String),
                Property(name='c', type=GremlinType.String)
            ]))
        self._upsert(label=VertexTypes.Database, key=database_uri, name='test')
        self._upsert(label=cluster_vertex_type,
                     key=cluster1_uri,
                     aproperty='one',
                     b='b')
        self._upsert(label=cluster_vertex_type,
                     key=cluster2_uri,
                     aproperty='two',
                     c='c')

        cluster_edge_type = EdgeType(label=EdgeTypes.Cluster.value.label,
                                     properties=tuple([
                                         Property(name='created',
                                                  type=GremlinType.Date,
                                                  required=True),
                                         Property(name='aproperty',
                                                  type=GremlinType.String),
                                         Property(name='b',
                                                  type=GremlinType.String),
                                         Property(name='c',
                                                  type=GremlinType.String)
                                     ]))
        self._link(vertex1_label=VertexTypes.Database,
                   vertex1_key=database_uri,
                   vertex2_label=VertexTypes.Cluster,
                   vertex2_key=cluster1_uri,
                   edge_label=cluster_edge_type,
                   aproperty='won',
                   b='bee')
        self._link(vertex1_label=VertexTypes.Database,
                   vertex1_key=database_uri,
                   vertex2_label=VertexTypes.Cluster,
                   vertex2_key=cluster2_uri,
                   edge_label=cluster_edge_type,
                   aproperty='too',
                   c='sea')

        # get the one
        e1 = _edges_from(g=self.get_proxy().g,
                         vertex1_label=VertexTypes.Database,
                         vertex1_key=database_uri,
                         vertex2_label=None,
                         vertex2_key=None,
                         edge_label=None,
                         aproperty='won').id().toList()
        self.assertEqual(len(e1), 1)

        # get the other
        e2 = _edges_from(g=self.get_proxy().g,
                         vertex1_label=VertexTypes.Database,
                         vertex1_key=database_uri,
                         vertex2_label=None,
                         vertex2_key=None,
                         edge_label=None,
                         aproperty='too').id().toList()
        self.assertEqual(len(e2), 1)
        self.assertNotEqual(e1[0], e2[0])

        # get both edges
        e = _edges_from(g=self.get_proxy().g,
                        vertex1_label=VertexTypes.Database,
                        vertex1_key=database_uri,
                        vertex2_label=None,
                        vertex2_key=None,
                        edge_label=EdgeTypes.Cluster).id().toList()
        self.assertTrue(e == e1 + e2 or e == e2 + e1)

        # get both edges
        e = _edges_from(g=self.get_proxy().g,
                        vertex1_label=VertexTypes.Database,
                        vertex1_key=database_uri,
                        vertex2_label=None,
                        vertex2_key=None,
                        edge_label=None,
                        aproperty=within('won', 'too')).id().toList()
        self.assertTrue(e == e1 + e2 or e == e2 + e1)

        # get the one
        e = _edges_from(g=self.get_proxy().g,
                        vertex1_label=VertexTypes.Database,
                        vertex1_key=database_uri,
                        vertex2_label=VertexTypes.Cluster,
                        vertex2_key=cluster1_uri,
                        edge_label=None).id().toList()
        self.assertEqual(e, e1)

        # get the other
        e = _edges_from(g=self.get_proxy().g,
                        vertex1_label=VertexTypes.Database,
                        vertex1_key=database_uri,
                        vertex2_label=VertexTypes.Cluster,
                        vertex2_key=cluster2_uri,
                        edge_label=None).id().toList()
        self.assertEqual(e, e2)

        # get the one
        e = _edges_from(g=self.get_proxy().g,
                        vertex1_label=VertexTypes.Database,
                        vertex1_key=database_uri,
                        vertex2_label=None,
                        vertex2_key=None,
                        vertex2_properties=dict(aproperty='one'),
                        edge_label=None).id().toList()
        self.assertEqual(e, e1)

        # get the other
        e = _edges_from(g=self.get_proxy().g,
                        vertex1_label=VertexTypes.Database,
                        vertex1_key=database_uri,
                        vertex2_label=None,
                        vertex2_key=None,
                        vertex2_properties=dict(aproperty='two'),
                        edge_label=None).id().toList()
        self.assertEqual(e, e2)

        # get the one
        e = _edges_from(g=self.get_proxy().g,
                        vertex1_label=VertexTypes.Database,
                        vertex1_key=database_uri,
                        vertex2_label=None,
                        vertex2_key=None,
                        vertex2_properties=dict(c=None),
                        edge_label=None).id().toList()
        self.assertEqual(e, e1)

        # get the other
        e = _edges_from(g=self.get_proxy().g,
                        vertex1_label=VertexTypes.Database,
                        vertex1_key=database_uri,
                        vertex2_label=None,
                        vertex2_key=None,
                        vertex2_properties=dict(b=None),
                        edge_label=None).id().toList()
        self.assertEqual(e, e2)

        # get none
        e = _edges_from(g=self.get_proxy().g,
                        vertex1_label=VertexTypes.Database,
                        vertex1_key=database_uri,
                        vertex2_label=None,
                        vertex2_key=None,
                        edge_label=EdgeTypes.Schema).id().toList()
        self.assertEqual(len(e), 0)

        # get none
        e = _edges_from(g=self.get_proxy().g,
                        vertex1_label=VertexTypes.Database,
                        vertex1_key=database_uri,
                        vertex2_label=VertexTypes.Schema,
                        vertex2_key=None,
                        edge_label=None).id().toList()
        self.assertEqual(len(e), 0)
    def test_link_rt(self) -> None:
        db_name = Fixtures.next_database()
        database_uri = f'database://{db_name}'
        cluster_uri = f'{db_name}://acluster'
        self.assertEqual(
            self._get(label=VertexTypes.Database,
                      key=database_uri,
                      extra_traversal=__.count()), 0)
        self.assertEqual(
            self._get(label=VertexTypes.Cluster,
                      key=cluster_uri,
                      extra_traversal=__.count()), 0)
        self._upsert(label=VertexTypes.Database, key=database_uri, name='test')
        self._upsert(label=VertexTypes.Cluster,
                     key=cluster_uri,
                     name='acluster')
        self.assertEqual(
            self._get(label=VertexTypes.Database,
                      key=database_uri,
                      extra_traversal=__.count()), 1)
        self.assertEqual(
            self._get(label=VertexTypes.Cluster,
                      key=cluster_uri,
                      extra_traversal=__.count()), 1)

        # use non-standard EdgeType, so aproperty doesn't explode
        edge_type = EdgeType(label='CLUSTER',
                             properties=tuple([
                                 Property(name='created',
                                          type=GremlinType.Date,
                                          required=True),
                                 Property(name='aproperty',
                                          type=GremlinType.String)
                             ]))

        # link
        self._link(vertex1_label=VertexTypes.Database,
                   vertex1_key=database_uri,
                   vertex2_label=VertexTypes.Cluster,
                   vertex2_key=cluster_uri,
                   edge_label=edge_type,
                   aproperty='hi')
        rel = self.get_relationship(
            node_type1=VertexTypes.Database.value.label,
            node_key1=database_uri,
            node_type2=VertexTypes.Cluster.value.label,
            node_key2=cluster_uri)
        self.assertEqual(len(rel), 1)
        self.assertEqual(set(rel[0].keys()), set(['created', 'aproperty']))
        self.assertEqual(rel[0].get('aproperty'), 'hi')

        # repeat but with aproperty unset (e.g. like we want to use _link with expired)
        self._link(vertex1_label=VertexTypes.Database,
                   vertex1_key=database_uri,
                   vertex2_label=VertexTypes.Cluster,
                   vertex2_key=cluster_uri,
                   edge_label=edge_type,
                   aproperty=None)
        rel = self.get_relationship(
            node_type1=VertexTypes.Database.value.label,
            node_key1=database_uri,
            node_type2=VertexTypes.Cluster.value.label,
            node_key2=cluster_uri)
        self.assertEqual(len(rel), 1)
        self.assertEqual(set(rel[0].keys()), set(['created']))
        self.assertEqual(rel[0].get('aproperty'), None)
    def test_upsert_thrice(self) -> None:
        executor = mock.Mock(wraps=self.get_proxy().query_executor())

        # test that we will insert
        db_name = Fixtures.next_database()
        database_uri = f'database://{db_name}'
        vertex_type = VertexType(
            label=VertexTypes.Database.value.label,
            properties=VertexTypes.Database.value.properties +
            tuple([Property(name='foo', type=GremlinType.String)]))

        exists = self._get(label=vertex_type,
                           key=database_uri,
                           extra_traversal=__.count())
        self.assertEqual(exists, 0)
        _upsert(executor=executor,
                g=self.get_proxy().g,
                key_property_name=self.get_proxy().key_property_name,
                label=vertex_type,
                key=database_uri,
                name='test',
                foo='bar')
        exists = self._get(label=vertex_type,
                           key=database_uri,
                           extra_traversal=__.count())
        self.assertEqual(exists, 1)
        id = self._get(label=vertex_type,
                       key=database_uri,
                       extra_traversal=__.id())

        executor.reset_mock()
        _upsert(executor=executor,
                g=self.get_proxy().g,
                key_property_name=self.get_proxy().key_property_name,
                label=vertex_type,
                key=database_uri,
                name='test')
        exists = self._get(label=vertex_type,
                           key=database_uri,
                           extra_traversal=__.count())
        self.assertEqual(exists, 1)
        self.assertEqual(executor.call_count, 2)
        # first one is the get:
        self.assertEqual(executor.call_args_list[0][1]['query'].bytecode,
                         __.V(id).valueMap(True).bytecode)
        # the second one should be like
        self.assertEqual(executor.call_args_list[1][1]['query'].bytecode,
                         __.V(id).id().bytecode)

        executor.reset_mock()
        _upsert(executor=executor,
                g=self.get_proxy().g,
                key_property_name=self.get_proxy().key_property_name,
                label=vertex_type,
                key=database_uri,
                name='test2',
                foo=None)
        exists = self._get(label=vertex_type,
                           key=database_uri,
                           extra_traversal=__.count())
        self.assertEqual(exists, 1)
        self.assertEqual(executor.call_count, 2)
        # first one is the get:
        self.assertEqual(executor.call_args_list[0][1]['query'].bytecode,
                         __.V(id).valueMap(True).bytecode)
        # the second one should be like
        self.assertEqual(
            executor.call_args_list[1][1]['query'].bytecode,
            __.V(id).sideEffect(__.properties('foo').drop()).property(
                Cardinality.single, 'name', 'test2').id().bytecode)