Exemple #1
0
 def test_init(self):
     opts = GraphOptions(**self.api_params)
     self._verify_api_params(opts, self.api_params)
     self._verify_api_params(GraphOptions(), {
         'graph_source': 'g',
         'graph_language': 'gremlin-groovy'
     })
Exemple #2
0
 def test_init(self):
     opts = GraphOptions(**self.api_params)
     self._verify_api_params(opts, self.api_params)
     self._verify_api_params(GraphOptions(), {
         'graph_source': 'g',
         'graph_language': 'gremlin-groovy',
         'graph_protocol': GraphProtocol.GRAPHSON_1_0
     })
Exemple #3
0
    def test_get_options(self):
        # nothing set --> base map
        base = GraphOptions(**self.api_params)
        self.assertEqual(GraphOptions().get_options_map(base), base._graph_options)

        # something set overrides
        kwargs = self.api_params.copy()  # this test concept got strange after we added default values for a couple GraphOption attrs
        kwargs['graph_name'] = 'unit_test'
        other = GraphOptions(**kwargs)
        options = base.get_options_map(other)
        updated = self.opt_mapping['graph_name']
        self.assertEqual(options[updated], six.b('unit_test'))
        for name in (n for n in self.opt_mapping.values() if n != updated):
            self.assertEqual(options[name], base._graph_options[name])

        # base unchanged
        self._verify_api_params(base, self.api_params)
    def test_consistency_passing(self):
        """
        Test to validated that graph consistency levels are properly surfaced to the base driver

        @since 1.0.0
        @jira_ticket PYTHON-509
        @expected_result graph consistency levels are surfaced correctly
        @test_category dse graph
        """
        cl_attrs = ('graph_read_consistency_level', 'graph_write_consistency_level')

        # Iterates over the graph options and constructs an array containing
        # The graph_options that correlate to graoh read and write consistency levels
        graph_params = [a[2] for a in _graph_options if a[0] in cl_attrs]

        s = self.session
        default_profile = s.cluster.profile_manager.profiles[EXEC_PROFILE_GRAPH_DEFAULT]
        default_graph_opts = default_profile.graph_options
        try:
            # Checks the default graph attributes and ensures that both  graph_read_consistency_level and graph_write_consistency_level
            # Are None by default
            for attr in cl_attrs:
                self.assertIsNone(getattr(default_graph_opts, attr))

            res = s.execute_graph("null")
            for param in graph_params:
                self.assertNotIn(param, res.response_future.message.custom_payload)

            # session defaults are passed
            opts = GraphOptions()
            opts.update(default_graph_opts)
            cl = {0: ConsistencyLevel.ONE, 1: ConsistencyLevel.LOCAL_QUORUM}
            for k, v in cl.items():
                setattr(opts, cl_attrs[k], v)
            default_profile.graph_options = opts

            res = s.execute_graph("null")

            for k, v in cl.items():
                self.assertEqual(res.response_future.message.custom_payload[graph_params[k]], six.b(ConsistencyLevel.value_to_name[v]))

            # passed profile values override session defaults
            cl = {0: ConsistencyLevel.ALL, 1: ConsistencyLevel.QUORUM}
            opts = GraphOptions()
            opts.update(default_graph_opts)
            for k, v in cl.items():
                attr_name = cl_attrs[k]
                setattr(opts, attr_name, v)
                self.assertNotEqual(getattr(default_profile.graph_options, attr_name), getattr(opts, attr_name))
            tmp_profile = s.execution_profile_clone_update(EXEC_PROFILE_GRAPH_DEFAULT, graph_options=opts)
            res = s.execute_graph("null", execution_profile=tmp_profile)

            for k, v in cl.items():
                self.assertEqual(res.response_future.message.custom_payload[graph_params[k]], six.b(ConsistencyLevel.value_to_name[v]))
        finally:
            default_profile.graph_options = default_graph_opts
Exemple #5
0
 def test_with_graph_protocol(self):
     opts = GraphOptions(graph_protocol='graphson-2-0')
     self.assertEqual(
         opts._graph_options, {
             'graph-source': b'g',
             'graph-language': b'gremlin-groovy',
             'graph-results': b'graphson-2-0'
         })
Exemple #6
0
    def test_consistency_levels(self):
        read_cl = ConsistencyLevel.ONE
        write_cl = ConsistencyLevel.LOCAL_QUORUM

        # set directly
        opts = GraphOptions(graph_read_consistency_level=read_cl,
                            graph_write_consistency_level=write_cl)
        self.assertEqual(opts.graph_read_consistency_level, read_cl)
        self.assertEqual(opts.graph_write_consistency_level, write_cl)

        # mapping from base
        opt_map = opts.get_options_map()
        self.assertEqual(opt_map['graph-read-consistency'],
                         six.b(ConsistencyLevel.value_to_name[read_cl]))
        self.assertEqual(opt_map['graph-write-consistency'],
                         six.b(ConsistencyLevel.value_to_name[write_cl]))

        # empty by default
        new_opts = GraphOptions()
        opt_map = new_opts.get_options_map()
        self.assertNotIn('graph-read-consistency', opt_map)
        self.assertNotIn('graph-write-consistency', opt_map)

        # set from other
        opt_map = new_opts.get_options_map(opts)
        self.assertEqual(opt_map['graph-read-consistency'],
                         six.b(ConsistencyLevel.value_to_name[read_cl]))
        self.assertEqual(opt_map['graph-write-consistency'],
                         six.b(ConsistencyLevel.value_to_name[write_cl]))
Exemple #7
0
    def test_graph_source_convenience_attributes(self):
        opts = GraphOptions()
        self.assertEqual(opts.graph_source, b'g')
        self.assertFalse(opts.is_analytics_source)
        self.assertTrue(opts.is_graph_source)
        self.assertFalse(opts.is_default_source)

        opts.set_source_default()
        self.assertIsNotNone(opts.graph_source)
        self.assertFalse(opts.is_analytics_source)
        self.assertFalse(opts.is_graph_source)
        self.assertTrue(opts.is_default_source)

        opts.set_source_analytics()
        self.assertIsNotNone(opts.graph_source)
        self.assertTrue(opts.is_analytics_source)
        self.assertFalse(opts.is_graph_source)
        self.assertFalse(opts.is_default_source)

        opts.set_source_graph()
        self.assertIsNotNone(opts.graph_source)
        self.assertFalse(opts.is_analytics_source)
        self.assertTrue(opts.is_graph_source)
        self.assertFalse(opts.is_default_source)
Exemple #8
0
    def connect(self):
        if self.initialized is False:
            raise Exception(
                'Please initialize the connection parameters first with SessionManager.save_credentials'
            )

        if self._session is None:
            if os.getenv('USE_ASTRA') == 'false':
                graph_name = os.getenv('KEYSPACE')
                ep_graphson3 = GraphExecutionProfile(
                    row_factory=graph_graphson3_row_factory,
                    graph_options=GraphOptions(
                        graph_protocol=GraphProtocol.GRAPHSON_3_0,
                        graph_name=graph_name))
                connectionPoint = [os.getenv('CONNECTION_POINTS')]

                cluster = Cluster(contact_points=connectionPoint,
                                  execution_profiles={'core': ep_graphson3})
                self._session = cluster.connect()
            else:
                # This is how you use the Astra secure connect bundle to connect to an Astra database
                # note that the database username and password required.
                # note that no contact points or any other driver customization is required.
                astra_config = {
                    'secure_connect_bundle': self.secure_connect_bundle_path
                }
                cluster = Cluster(cloud=astra_config,
                                  auth_provider=PlainTextAuthProvider(
                                      self.username, self.password))
                self._session = cluster.connect(keyspace=self.keyspace)
                # have the driver return results as dict
                self._session.row_factory = dict_factory

            # have the driver return LocationUDT as a dict
            cluster.register_user_type(self.keyspace, 'location_udt', dict)

        return self._session
Exemple #9
0
 def test_del_attr(self):
     opts = GraphOptions(**self.api_params)
     test_params = self.api_params.copy()
     del test_params['graph_source']
     del opts.graph_source
     self._verify_api_params(opts, test_params)
Exemple #10
0
    def test_set_attr(self):
        expected = 'test@@@@'
        opts = GraphOptions(graph_name=expected)
        self.assertEqual(opts.graph_name, six.b(expected))
        expected = 'somethingelse####'
        opts.graph_name = expected
        self.assertEqual(opts.graph_name, six.b(expected))

        # will update options with set value
        another = GraphOptions()
        self.assertIsNone(another.graph_name)
        another.update(opts)
        self.assertEqual(another.graph_name, six.b(expected))

        opts.graph_name = None
        self.assertIsNone(opts.graph_name)
        # will not update another with its set-->unset value
        another.update(opts)
        self.assertEqual(another.graph_name, six.b(expected))  # remains unset
        opt_map = another.get_options_map(opts)
        self.assertEqual(opt_map, another._graph_options)
Exemple #11
0
 def test_update(self):
     opts = GraphOptions(**self.api_params)
     new_params = dict((k, str(int(v) + 1)) for k, v in self.api_params.items())
     opts.update(GraphOptions(**new_params))
     self._verify_api_params(opts, new_params)
Exemple #12
0
 def test_init_unknown_kwargs(self):
     with warnings.catch_warnings(record=True) as w:
         GraphOptions(unknown_param=42)
     self.assertEqual(len(w), 1)
     self.assertRegexpMatches(str(w[0].message), r"^Unknown keyword.*GraphOptions.*")
 def test_graph_source_is_preserve_with_graph_analytics_execution_profile(
         self):
     options = GraphOptions(graph_source='doesnt_matter')
     ep = GraphAnalyticsExecutionProfile(graph_options=options)
     self.assertEqual(ep.graph_options.graph_source,
                      b'a')  # graph source is set automatically
 def test_graph_source_can_be_set_with_graph_execution_profile(self):
     options = GraphOptions(graph_source='a')
     ep = GraphExecutionProfile(graph_options=options)
     self.assertEqual(ep.graph_options.graph_source, b'a')