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_profile(self) -> None:
     count = self._get(label=VertexTypes.User, key='jack', extra_traversal=__.count())
     self.assertEqual(count, 0)
     # just enough to not explode
     self._upsert(label=VertexTypes.User, key='jack', email='*****@*****.**')
     # and show it ran
     count = self._get(label=VertexTypes.User, key='jack', extra_traversal=__.count())
     self.assertEqual(count, 1)
    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_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)