diff --git a/benchmarks/base.py b/benchmarks/base.py index bdfcfa4493..ab5cc12cb3 100644 --- a/benchmarks/base.py +++ b/benchmarks/base.py @@ -141,7 +141,6 @@ def benchmark(thread_class): log.debug("Sleeping for two seconds...") time.sleep(2.0) - # Generate the query if options.read: query = "SELECT * FROM {0} WHERE thekey = '{{key}}'".format(TABLE) @@ -240,7 +239,6 @@ def parse_options(): parser.add_option('--read', action='store_true', dest='read', default=False, help='Read mode') - options, args = parser.parse_args() options.hosts = options.hosts.split(',') diff --git a/benchmarks/callback_full_pipeline.py b/benchmarks/callback_full_pipeline.py index 5eafa5df8b..72af570e8e 100644 --- a/benchmarks/callback_full_pipeline.py +++ b/benchmarks/callback_full_pipeline.py @@ -43,7 +43,7 @@ def insert_next(self, previous_result=sentinel): self.event.set() i = next(self.num_started) - if i <= self.num_queries: + if i <= self.num_queries: key = "{0}-{1}".format(self.thread_num, i) future = self.run_query(key, timeout=None) future.add_callbacks(self.insert_next, self.insert_next) diff --git a/benchmarks/future_full_throttle.py b/benchmarks/future_full_throttle.py index f85eb99b0d..2ee37e9231 100644 --- a/benchmarks/future_full_throttle.py +++ b/benchmarks/future_full_throttle.py @@ -20,6 +20,7 @@ log = logging.getLogger(__name__) + class Runner(BenchmarkThread): def run(self): diff --git a/cassandra/column_encryption/_policies.py b/cassandra/column_encryption/_policies.py index e1519f6b79..8daedb6a24 100644 --- a/cassandra/column_encryption/_policies.py +++ b/cassandra/column_encryption/_policies.py @@ -33,7 +33,8 @@ AES256_KEY_SIZE = 256 AES256_KEY_SIZE_BYTES = int(AES256_KEY_SIZE / 8) -ColData = namedtuple('ColData', ['key','type']) +ColData = namedtuple('ColData', ['key', 'type']) + class AES256ColumnEncryptionPolicy(ColumnEncryptionPolicy): diff --git a/cassandra/cqlengine/columns.py b/cassandra/cqlengine/columns.py index 7d50687d95..2b2b5c966b 100644 --- a/cassandra/cqlengine/columns.py +++ b/cassandra/cqlengine/columns.py @@ -600,6 +600,7 @@ def to_python(self, value): value = value.date() return util.Date(value) + class Time(Column): """ Stores a timezone-naive time-of-day, with nanosecond precision @@ -625,6 +626,7 @@ def to_python(self, value): return value return util.Time(value) + class Duration(Column): """ Stores a duration (months, days, nanoseconds) diff --git a/cassandra/cqlengine/connection.py b/cassandra/cqlengine/connection.py index 55437d7b7f..8c52913ba4 100644 --- a/cassandra/cqlengine/connection.py +++ b/cassandra/cqlengine/connection.py @@ -183,11 +183,11 @@ def register_connection(name, hosts=None, consistency=None, lazy_connect=False, log.warning("Registering connection '{0}' when it already exists.".format(name)) if session is not None: - invalid_config_args = (hosts is not None or - consistency is not None or - lazy_connect is not False or - retry_connect is not False or - cluster_options is not None) + invalid_config_args = (hosts is not None + or consistency is not None + or lazy_connect is not False + or retry_connect is not False + or cluster_options is not None) if invalid_config_args: raise CQLEngineException( "Session configuration arguments and 'session' argument are mutually exclusive" diff --git a/cassandra/cqlengine/functions.py b/cassandra/cqlengine/functions.py index 69bdc3feb4..36b1b30aa6 100644 --- a/cassandra/cqlengine/functions.py +++ b/cassandra/cqlengine/functions.py @@ -18,9 +18,11 @@ from cassandra.cqlengine import UnicodeMixin, ValidationError + def get_total_seconds(td): return td.total_seconds() + class QueryValue(UnicodeMixin): """ Base class for query filter values. Subclasses of these classes can diff --git a/cassandra/cqlengine/management.py b/cassandra/cqlengine/management.py index 66b391b714..12246f6b00 100644 --- a/cassandra/cqlengine/management.py +++ b/cassandra/cqlengine/management.py @@ -150,6 +150,7 @@ def _drop_keyspace(name, connection=None): else: _drop_keyspace(name) + def _get_index_name_by_column(table, column_name): """ Find the index name for a given table and column. @@ -251,7 +252,7 @@ def _sync_table(model, connection=None): col_meta = table_columns[db_name] if col_meta.cql_type != col.db_type: msg = format_log_context('Existing table {0} has column "{1}" with a type ({2}) differing from the model type ({3}).' - ' Model should be updated.', keyspace=ks_name, connection=connection) + ' Model should be updated.', keyspace=ks_name, connection=connection) msg = msg.format(cf_name, db_name, col_meta.cql_type, col.db_type) warnings.warn(msg) log.warning(msg) @@ -362,7 +363,7 @@ def _sync_type(ks_name, type_model, omit_subtypes=None, connection=None): field_type = type_meta.field_types[defined_fields.index(field.db_field_name)] if field_type != field.db_type: msg = format_log_context('Existing user type {0} has field "{1}" with a type ({2}) differing from the model user type ({3}).' - ' UserType should be updated.', keyspace=ks_name, connection=connection) + ' UserType should be updated.', keyspace=ks_name, connection=connection) msg = msg.format(type_name_qualified, field.db_field_name, field_type, field.db_type) warnings.warn(msg) log.warning(msg) diff --git a/cassandra/cqlengine/models.py b/cassandra/cqlengine/models.py index f0f5a207ec..f448f9160a 100644 --- a/cassandra/cqlengine/models.py +++ b/cassandra/cqlengine/models.py @@ -56,6 +56,7 @@ class PolymorphicModelException(ModelException): class UndefinedKeyspaceWarning(Warning): pass + DEFAULT_KEYSPACE = None @@ -412,16 +413,16 @@ def __init__(self, **values): def __repr__(self): return '{0}({1})'.format(self.__class__.__name__, - ', '.join('{0}={1!r}'.format(k, getattr(self, k)) - for k in self._defined_columns.keys() - if k != self._discriminator_column_name)) + ', '.join('{0}={1!r}'.format(k, getattr(self, k)) + for k in self._defined_columns.keys() + if k != self._discriminator_column_name)) def __str__(self): """ Pretty printing of models by their primary key """ return '{0} <{1}>'.format(self.__class__.__name__, - ', '.join('{0}={1}'.format(k, getattr(self, k)) for k in self._primary_keys.keys())) + ', '.join('{0}={1}'.format(k, getattr(self, k)) for k in self._primary_keys.keys())) @classmethod def _routing_key_from_values(cls, pk_values, protocol_version): @@ -562,7 +563,6 @@ def column_family_name(cls, include_keyspace=True): return cf_name - @classmethod def _raw_column_family_name(cls): if not cls._table_name: @@ -574,7 +574,7 @@ def _raw_column_family_name(cls): table_name = cls.__table_name__.lower() if cls.__table_name__ != table_name: warn(("Model __table_name__ will be case sensitive by default in 4.0. " - "You should fix the __table_name__ value of the '{0}' model.").format(cls.__name__)) + "You should fix the __table_name__ value of the '{0}' model.").format(cls.__name__)) cls._table_name = table_name else: if cls._is_polymorphic and not cls._is_polymorphic_base: diff --git a/cassandra/cqlengine/query.py b/cassandra/cqlengine/query.py index 329bc7fade..ebd5b0ea8f 100644 --- a/cassandra/cqlengine/query.py +++ b/cassandra/cqlengine/query.py @@ -112,7 +112,6 @@ def contains_(self, item): """ return WhereClause(str(self), ContainsOperator(), item) - def __eq__(self, other): return WhereClause(str(self), EqualsOperator(), self._to_database(other)) @@ -149,7 +148,6 @@ class BatchQuery(object): _connection = None _connection_explicit = False - def __init__(self, batch_type=None, timestamp=None, consistency=None, execute_on_exception=False, timeout=conn.NOT_SET, connection=None): """ @@ -384,7 +382,7 @@ def __init__(self, model): self._count = None self._batch = None - self._ttl = None + self._ttl = None self._consistency = None self._timestamp = None self._if_not_exists = False @@ -730,8 +728,8 @@ def filter(self, *args, **kwargs): query_val = [column.to_database(v) for v in val] elif isinstance(val, BaseQueryFunction): query_val = val - elif (isinstance(operator, ContainsOperator) and - isinstance(column, (columns.List, columns.Set, columns.Map))): + elif (isinstance(operator, ContainsOperator) + and isinstance(column, (columns.List, columns.Set, columns.Map))): # For ContainsOperator and collections, we query using the value, not the container query_val = val else: @@ -1076,7 +1074,7 @@ def _validate_select_where(self): # relationship with a primary key or indexed field. We also allow # custom indexes to be queried with any operator (a difference # between a secondary index) - equal_ops = [self.model._get_column_by_db_name(w.field) \ + equal_ops = [self.model._get_column_by_db_name(w.field) for w in self._where if not isinstance(w.value, Token) and (isinstance(w.operator, EqualsOperator) or self.model._get_column_by_db_name(w.field).custom_index)] @@ -1366,9 +1364,8 @@ def _execute(self, statement): connection = self.instance._get_connection() if self.instance else self.model._get_connection() if self._batch: if self._batch._connection: - if not self._batch._connection_explicit and connection and \ - connection != self._batch._connection: - raise CQLEngineException('BatchQuery queries must be executed on the same connection') + if not self._batch._connection_explicit and connection and connection != self._batch._connection: + raise CQLEngineException('BatchQuery queries must be executed on the same connection') else: # set the BatchQuery connection from the model self._batch._connection = connection @@ -1477,7 +1474,7 @@ def save(self): if self.instance._has_counter or self.instance._can_update(): if self.instance._has_counter: warn("'create' and 'save' actions on Counters are deprecated. It will be disallowed in 4.0. " - "Use the 'update' mechanism instead.", DeprecationWarning) + "Use the 'update' mechanism instead.", DeprecationWarning) return self.update() else: insert = InsertStatement(self.column_family_name, ttl=self._ttl, timestamp=self._timestamp, if_not_exists=self._if_not_exists) diff --git a/cassandra/cqlengine/statements.py b/cassandra/cqlengine/statements.py index b20b07ef56..b53dbdadca 100644 --- a/cassandra/cqlengine/statements.py +++ b/cassandra/cqlengine/statements.py @@ -152,6 +152,7 @@ def update_context(self, ctx): def get_context_size(self): return 0 + # alias for convenience IsNotNull = IsNotNullClause @@ -216,10 +217,10 @@ class SetUpdateClause(ContainerUpdateClause): def __unicode__(self): qs = [] ctx_id = self.context_id - if (self.previous is None and - self._assignments is None and - self._additions is None and - self._removals is None): + if (self.previous is None + and self._assignments is None + and self._additions is None + and self._removals is None): qs += ['"{0}" = %({1})s'.format(self.field, ctx_id)] if self._assignments is not None: qs += ['"{0}" = %({1})s'.format(self.field, ctx_id)] @@ -251,10 +252,10 @@ def _analyze(self): def get_context_size(self): if not self._analyzed: self._analyze() - if (self.previous is None and - not self._assignments and - self._additions is None and - self._removals is None): + if (self.previous is None + and not self._assignments + and self._additions is None + and self._removals is None): return 1 return int(bool(self._assignments)) + int(bool(self._additions)) + int(bool(self._removals)) @@ -262,10 +263,10 @@ def update_context(self, ctx): if not self._analyzed: self._analyze() ctx_id = self.context_id - if (self.previous is None and - self._assignments is None and - self._additions is None and - self._removals is None): + if (self.previous is None + and self._assignments is None + and self._additions is None + and self._removals is None): ctx[str(ctx_id)] = set() if self._assignments is not None: ctx[str(ctx_id)] = self._assignments diff --git a/cassandra/graph/__init__.py b/cassandra/graph/__init__.py index 1d33345aad..db023dea5a 100644 --- a/cassandra/graph/__init__.py +++ b/cassandra/graph/__init__.py @@ -15,4 +15,4 @@ # limitations under the License. # This is only for backward compatibility when migrating from dse-driver. -from cassandra.datastax.graph import * \ No newline at end of file +from cassandra.datastax.graph import * diff --git a/cassandra/io/__init__.py b/cassandra/io/__init__.py index 588a655d98..635f0d9e60 100644 --- a/cassandra/io/__init__.py +++ b/cassandra/io/__init__.py @@ -13,4 +13,3 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - diff --git a/cassandra/io/asyncioreactor.py b/cassandra/io/asyncioreactor.py index 007e10d5c4..229dbb60c4 100644 --- a/cassandra/io/asyncioreactor.py +++ b/cassandra/io/asyncioreactor.py @@ -177,7 +177,6 @@ async def _push_msg(self, chunks): for chunk in chunks: self._write_queue.put_nowait(chunk) - async def handle_write(self): while True: try: diff --git a/cassandra/io/asyncorereactor.py b/cassandra/io/asyncorereactor.py index e1bcafb39e..db4cb3b7c9 100644 --- a/cassandra/io/asyncorereactor.py +++ b/cassandra/io/asyncorereactor.py @@ -48,6 +48,7 @@ _dispatcher_map = {} + def _cleanup(loop): if loop: loop._cleanup() @@ -389,7 +390,7 @@ def close(self): self.error_all_requests( ConnectionShutdown("Connection to %s was closed" % self.endpoint)) - #This happens when the connection is shutdown while waiting for the ReadyMessage + # This happens when the connection is shutdown while waiting for the ReadyMessage if not self.connected_event.is_set(): self.last_error = ConnectionShutdown("Connection to %s was closed" % self.endpoint) @@ -416,8 +417,8 @@ def handle_write(self): sent = self.send(next_msg) self._readable = True except socket.error as err: - if (err.args[0] in NONBLOCKING or - err.args[0] in (ssl.SSL_ERROR_WANT_READ, ssl.SSL_ERROR_WANT_WRITE)): + if (err.args[0] in NONBLOCKING + or err.args[0] in (ssl.SSL_ERROR_WANT_READ, ssl.SSL_ERROR_WANT_WRITE)): with self.deque_lock: self.deque.appendleft(next_msg) else: diff --git a/cassandra/io/libevreactor.py b/cassandra/io/libevreactor.py index 76a53b9bdd..c5fb4628d3 100644 --- a/cassandra/io/libevreactor.py +++ b/cassandra/io/libevreactor.py @@ -319,8 +319,8 @@ def handle_write(self, watcher, revents, errno=None): try: sent = self._socket.send(next_msg) except socket.error as err: - if (err.args[0] in NONBLOCKING or - err.args[0] in (ssl.SSL_ERROR_WANT_READ, ssl.SSL_ERROR_WANT_WRITE)): + if (err.args[0] in NONBLOCKING + or err.args[0] in (ssl.SSL_ERROR_WANT_READ, ssl.SSL_ERROR_WANT_WRITE)): if err.args[0] in NONBLOCKING: self._socket_writable = False with self._deque_lock: diff --git a/docs/conf.py b/docs/conf.py index 4c0dfb58d7..f1adec0711 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -23,7 +23,7 @@ # -- General configuration ----------------------------------------------------- # If your documentation needs a minimal Sphinx version, state it here. -#needs_sphinx = '1.0' +# needs_sphinx = '1.0' # Add any Sphinx extension module names here, as strings. They can be extensions # coming with Sphinx (named 'sphinx.ext.*') or your custom ones. @@ -36,7 +36,7 @@ source_suffix = '.rst' # The encoding of source files. -#source_encoding = 'utf-8-sig' +# source_encoding = 'utf-8-sig' # The master toctree document. master_doc = 'index' @@ -59,37 +59,37 @@ # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. -#language = None +# language = None # There are two options for replacing |today|: either, you set today to some # non-false value, then it is used: -#today = '' +# today = '' # Else, today_fmt is used as the format for a strftime call. -#today_fmt = '%B %d, %Y' +# today_fmt = '%B %d, %Y' # List of patterns, relative to source directory, that match files and # directories to ignore when looking for source files. exclude_patterns = ['_build'] # The reST default role (used for this markup: `text`) to use for all documents. -#default_role = None +# default_role = None # If true, '()' will be appended to :func: etc. cross-reference text. -#add_function_parentheses = True +# add_function_parentheses = True # If true, the current module name will be prepended to all description # unit titles (such as .. function::). -#add_module_names = True +# add_module_names = True # If true, sectionauthor and moduleauthor directives will be shown in the # output. They are ignored by default. -#show_authors = False +# show_authors = False # The name of the Pygments (syntax highlighting) style to use. pygments_style = 'sphinx' # A list of ignored prefixes for module index sorting. -#modindex_common_prefix = [] +# modindex_common_prefix = [] # -- Options for HTML output --------------------------------------------------- @@ -101,26 +101,26 @@ # Theme options are theme-specific and customize the look and feel of a theme # further. For a list of options available for each theme, see the # documentation. -#html_theme_options = {} +# html_theme_options = {} # Add any paths that contain custom themes here, relative to this directory. html_theme_path = ['./themes'] # The name for this set of Sphinx documents. If None, it defaults to # " v documentation". -#html_title = None +# html_title = None # A shorter title for the navigation bar. Default is the same as html_title. -#html_short_title = None +# html_short_title = None # The name of an image file (relative to this directory) to place at the top # of the sidebar. -#html_logo = None +# html_logo = None # The name of an image file (within the static path) to use as favicon of the # docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 # pixels large. -#html_favicon = None +# html_favicon = None # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, @@ -129,51 +129,51 @@ # If not '', a 'Last updated on:' timestamp is inserted at every page bottom, # using the given strftime format. -#html_last_updated_fmt = '%b %d, %Y' +# html_last_updated_fmt = '%b %d, %Y' # If true, SmartyPants will be used to convert quotes and dashes to # typographically correct entities. -#html_use_smartypants = True +# html_use_smartypants = True # Custom sidebar templates, maps document names to template names. html_sidebars = { - '**': [ - 'about.html', - 'navigation.html', - 'relations.html', - 'searchbox.html' - ] + '**': [ + 'about.html', + 'navigation.html', + 'relations.html', + 'searchbox.html' + ] } # Additional templates that should be rendered to pages, maps page names to # template names. -#html_additional_pages = {} +# html_additional_pages = {} # If false, no module index is generated. -#html_domain_indices = True +# html_domain_indices = True # If false, no index is generated. html_use_index = False # If true, the index is split into individual pages for each letter. -#html_split_index = False +# html_split_index = False # If true, links to the reST sources are added to the pages. -#html_show_sourcelink = True +# html_show_sourcelink = True # If true, "Created using Sphinx" is shown in the HTML footer. Default is True. -#html_show_sphinx = True +# html_show_sphinx = True # If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. -#html_show_copyright = True +# html_show_copyright = True # If true, an OpenSearch description file will be output, and all pages will # contain a tag referring to it. The value of this option must be the # base URL from which the finished HTML is served. -#html_use_opensearch = '' +# html_use_opensearch = '' # This is the file name suffix for HTML files (e.g. ".xhtml"). -#html_file_suffix = None +# html_file_suffix = None # Output file base name for HTML help builder. htmlhelp_basename = 'CassandraDriverdoc' @@ -182,10 +182,10 @@ # -- Options for LaTeX output -------------------------------------------------- # The paper size ('letter' or 'a4'). -#latex_paper_size = 'letter' +# latex_paper_size = 'letter' # The font size ('10pt', '11pt' or '12pt'). -#latex_font_size = '10pt' +# latex_font_size = '10pt' # Grouping the document tree into LaTeX files. List of tuples # (source start file, target name, title, author, documentclass [howto/manual]). @@ -195,26 +195,26 @@ # The name of an image file (relative to this directory) to place at the top of # the title page. -#latex_logo = None +# latex_logo = None # For "manual" documents, if this is true, then toplevel headings are parts, # not chapters. -#latex_use_parts = False +# latex_use_parts = False # If true, show page references after internal links. -#latex_show_pagerefs = False +# latex_show_pagerefs = False # If true, show URL addresses after external links. -#latex_show_urls = False +# latex_show_urls = False # Additional stuff for the LaTeX preamble. -#latex_preamble = '' +# latex_preamble = '' # Documents to append as an appendix to all manuals. -#latex_appendices = [] +# latex_appendices = [] # If false, no module index is generated. -#latex_domain_indices = True +# latex_domain_indices = True # -- Options for manual page output -------------------------------------------- @@ -222,6 +222,5 @@ # One entry per manual page. List of tuples # (source start file, name, description, authors, manual section). man_pages = [ - ('index', 'cassandra-driver', u'Cassandra Driver Documentation', - [u'DataStax'], 1) + ('index', 'cassandra-driver', u'Cassandra Driver Documentation', [u'DataStax'], 1) ] diff --git a/example_core.py b/example_core.py index 56e8924d1d..3227d5e410 100644 --- a/example_core.py +++ b/example_core.py @@ -84,5 +84,6 @@ def main(): session.execute("DROP KEYSPACE " + KEYSPACE) + if __name__ == "__main__": main() diff --git a/example_mapper.py b/example_mapper.py index 8105dbe2b1..4936971f7e 100755 --- a/example_mapper.py +++ b/example_mapper.py @@ -121,5 +121,6 @@ def main(): management.drop_keyspace(KEYSPACE) + if __name__ == "__main__": main() diff --git a/examples/concurrent_executions/execute_async_with_queue.py b/examples/concurrent_executions/execute_async_with_queue.py index 44a91a530c..0c670f6f7f 100644 --- a/examples/concurrent_executions/execute_async_with_queue.py +++ b/examples/concurrent_executions/execute_async_with_queue.py @@ -64,4 +64,4 @@ def clear_queue(): end = time.time() print("Finished executing {} queries with a concurrency level of {} in {:.2f} seconds.". - format(TOTAL_QUERIES, CONCURRENCY_LEVEL, (end-start))) + format(TOTAL_QUERIES, CONCURRENCY_LEVEL, (end - start))) diff --git a/examples/concurrent_executions/execute_with_threads.py b/examples/concurrent_executions/execute_with_threads.py index 69126de6ec..afa7de078b 100644 --- a/examples/concurrent_executions/execute_with_threads.py +++ b/examples/concurrent_executions/execute_with_threads.py @@ -71,4 +71,4 @@ def run(self): end = time.time() print("Finished executing {} queries with a concurrency level of {} in {:.2f} seconds.". - format(TOTAL_QUERIES, CONCURRENCY_LEVEL, (end-start))) + format(TOTAL_QUERIES, CONCURRENCY_LEVEL, (end - start))) diff --git a/setup.py b/setup.py index 9d1b388606..df86fab901 100644 --- a/setup.py +++ b/setup.py @@ -32,15 +32,13 @@ is_supported = is_supported_platform and is_supported_arch # ========================== A few upfront checks ========================== -platform_unsupported_msg = \ -""" +platform_unsupported_msg = """ =============================================================================== The optional C extensions are not supported on this platform. =============================================================================== """ -arch_unsupported_msg = \ -""" +arch_unsupported_msg = """ =============================================================================== The optional C extensions are not supported on big-endian systems. =============================================================================== @@ -53,13 +51,15 @@ # ========================== Extensions ========================== pyproject_toml = Path(__file__).parent / "pyproject.toml" -with open(pyproject_toml,"rb") as f: +with open(pyproject_toml, "rb") as f: pyproject_data = toml.load(f) driver_project_data = pyproject_data["tool"]["cassandra-driver"] + def key_or_false(k): return driver_project_data[k] if k in driver_project_data else False + try_murmur3 = key_or_false("build-murmur3-extension") and is_supported try_libev = key_or_false("build-libev-extension") and is_supported try_cython = key_or_false("build-cython-extensions") and is_supported @@ -96,19 +96,19 @@ def key_or_false(k): exts.extend(cythonize( [Extension('cassandra.%s' % m, ['cassandra/%s.py' % m], extra_compile_args=compile_args) - for m in cython_candidates], - nthreads=build_concurrency, - exclude_failures=True)) + for m in cython_candidates], + nthreads=build_concurrency, + exclude_failures=True)) exts.extend(cythonize( Extension("*", ["cassandra/*.pyx"], extra_compile_args=compile_args), - nthreads=build_concurrency)) + nthreads=build_concurrency)) except Exception as exc: sys.stderr.write("Failed to cythonize one or more modules. These will not be compiled as extensions (optional).\n") sys.stderr.write("Cython error: %s\n" % exc) # ========================== And finally setup() itself ========================== setup( - ext_modules = exts -) \ No newline at end of file + ext_modules=exts +) diff --git a/tests/integration/__init__.py b/tests/integration/__init__.py index a9da91009a..8a8c2125ec 100644 --- a/tests/integration/__init__.py +++ b/tests/integration/__init__.py @@ -1055,9 +1055,9 @@ def __new__(cls, **kwargs): # introduced by CASSANDRA-15234 class Cassandra41CCMCluster(CCMCluster): __test__ = False - IN_MS_REGEX = re.compile('^(\w+)_in_ms$') - IN_KB_REGEX = re.compile('^(\w+)_in_kb$') - ENABLE_REGEX = re.compile('^enable_(\w+)$') + IN_MS_REGEX = re.compile(r'^(\w+)_in_ms$') + IN_KB_REGEX = re.compile(r'^(\w+)_in_kb$') + ENABLE_REGEX = re.compile(r'^enable_(\w+)$') def _get_config_key(self, k, v): if "." in k: diff --git a/tests/unit/__init__.py b/tests/unit/__init__.py index 588a655d98..635f0d9e60 100644 --- a/tests/unit/__init__.py +++ b/tests/unit/__init__.py @@ -13,4 +13,3 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - diff --git a/tests/unit/test_cluster.py b/tests/unit/test_cluster.py index 69a65855a0..9fe4f3a3d2 100644 --- a/tests/unit/test_cluster.py +++ b/tests/unit/test_cluster.py @@ -96,11 +96,12 @@ class MockOrderedPolicy(RoundRobinPolicy): def make_query_plan(self, working_keyspace=None, query=None): return sorted(self.all_hosts, key=lambda x: x.endpoint.ssl_options['server_hostname']) + class ClusterTest(unittest.TestCase): def test_tuple_for_contact_points(self): cluster = Cluster(contact_points=[('localhost', 9045), ('127.0.0.2', 9046), '127.0.0.3'], port=9999) - localhost_addr = set([addr[0] for addr in [t for (_,_,_,_,t) in socket.getaddrinfo("localhost",80)]]) + localhost_addr = set([addr[0] for addr in [t for (_, _, _, _, t) in socket.getaddrinfo("localhost", 80)]]) for cp in cluster.endpoints_resolved: if cp.address in localhost_addr: self.assertEqual(cp.port, 9045) @@ -136,6 +137,7 @@ def test_requests_in_flight_threshold(self): # a pretty good approximation. def test_query_plan_for_sni_contains_unique_addresses(self): node_cnt = 5 + def _mocked_proxy_dns_resolution(self): return [(socket.AF_UNIX, socket.SOCK_STREAM, 0, None, ('127.0.0.%s' % (i,), 9042)) for i in range(node_cnt)] @@ -238,13 +240,13 @@ def test_protocol_downgrade_test(self): lower = ProtocolVersion.get_lower_supported(ProtocolVersion.DSE_V2) self.assertEqual(ProtocolVersion.DSE_V1, lower) lower = ProtocolVersion.get_lower_supported(ProtocolVersion.DSE_V1) - self.assertEqual(ProtocolVersion.V5,lower) + self.assertEqual(ProtocolVersion.V5, lower) lower = ProtocolVersion.get_lower_supported(ProtocolVersion.V5) - self.assertEqual(ProtocolVersion.V4,lower) + self.assertEqual(ProtocolVersion.V4, lower) lower = ProtocolVersion.get_lower_supported(ProtocolVersion.V4) - self.assertEqual(ProtocolVersion.V3,lower) + self.assertEqual(ProtocolVersion.V3, lower) lower = ProtocolVersion.get_lower_supported(ProtocolVersion.V3) - self.assertEqual(ProtocolVersion.V2,lower) + self.assertEqual(ProtocolVersion.V2, lower) lower = ProtocolVersion.get_lower_supported(ProtocolVersion.V2) self.assertEqual(ProtocolVersion.V1, lower) lower = ProtocolVersion.get_lower_supported(ProtocolVersion.V1) diff --git a/tests/unit/test_concurrent.py b/tests/unit/test_concurrent.py index 18e8381185..f0b41ea7ae 100644 --- a/tests/unit/test_concurrent.py +++ b/tests/unit/test_concurrent.py @@ -52,7 +52,7 @@ def __init__(self, reverse): # hardcoded to avoid paging logic self.has_more_pages = False - if(reverse): + if (reverse): self.priority = 100 else: self.priority = 0 @@ -106,8 +106,8 @@ def stopped(self): return self._stopper.isSet() def run(self): - while(not self.stopped()): - if(self.handler.has_next_callback()): + while (not self.stopped()): + if (self.handler.has_next_callback()): pending_callback = self.handler.get_next_callback() priority_num = pending_callback[0] if (priority_num % 10) == 0 and self.slowdown: @@ -118,6 +118,7 @@ def run(self): self._stopper.wait(.001) return + class ConcurrencyTest((unittest.TestCase)): def test_results_ordering_forward(self): @@ -195,7 +196,7 @@ def insert_and_validate_list_results(self, reverse, slowdown): t.start() results = execute_concurrent(mock_session, statements_and_params) - while(not our_handler.pending_callbacks.empty()): + while (not our_handler.pending_callbacks.empty()): time.sleep(.01) t.stop() self.validate_result_ordering(results) diff --git a/tests/unit/test_connection.py b/tests/unit/test_connection.py index 3bca654c55..7840fcc8c9 100644 --- a/tests/unit/test_connection.py +++ b/tests/unit/test_connection.py @@ -285,7 +285,7 @@ def run_heartbeat(self, get_holders_fun, count=2, interval=0.05, timeout=0.05): ch = ConnectionHeartbeat(interval, get_holders_fun, timeout=timeout) # wait until the thread is started wait_until(lambda: get_holders_fun.call_count > 0, 0.01, 100) - time.sleep(interval * (count-1)) + time.sleep(interval * (count - 1)) ch.stop() self.assertTrue(get_holders_fun.call_count) @@ -295,7 +295,7 @@ def test_empty_connections(self, *args): self.run_heartbeat(get_holders, count) - self.assertGreaterEqual(get_holders.call_count, count-1) + self.assertGreaterEqual(get_holders.call_count, count - 1) self.assertLessEqual(get_holders.call_count, count) holder = get_holders.return_value[0] holder.get_connections.assert_has_calls([call()] * get_holders.call_count) diff --git a/tests/unit/test_control_connection.py b/tests/unit/test_control_connection.py index 618bb42b1f..328f9205f1 100644 --- a/tests/unit/test_control_connection.py +++ b/tests/unit/test_control_connection.py @@ -49,8 +49,8 @@ def __init__(self): def get_host(self, endpoint_or_address, port=None): if not isinstance(endpoint_or_address, EndPoint): for host in self.hosts.values(): - if (host.address == endpoint_or_address and - (port is None or host.broadcast_rpc_port is None or host.broadcast_rpc_port == port)): + if (host.address == endpoint_or_address + and (port is None or host.broadcast_rpc_port is None or host.broadcast_rpc_port == port)): return host else: return self.hosts.get(endpoint_or_address) @@ -130,7 +130,7 @@ def __init__(self): ] self.peer_results_v2 = [ - ["native_address", "native_port", "peer", "peer_port", "schema_version", "data_center", "rack", "tokens", "host_id"], + ["native_address", "native_port", "peer", "peer_port", "schema_version", "data_center", "rack", "tokens", "host_id"], [["192.168.1.1", 9042, "10.0.0.1", 7042, "a", "dc1", "rack1", ["1", "101", "201"], "uuid1"], ["192.168.1.2", 9042, "10.0.0.2", 7040, "a", "dc1", "rack1", ["2", "102", "202"], "uuid2"]] ] diff --git a/tests/unit/test_exception.py b/tests/unit/test_exception.py index 4758970d9c..6f7ed6e83a 100644 --- a/tests/unit/test_exception.py +++ b/tests/unit/test_exception.py @@ -31,7 +31,7 @@ def extract_consistency(self, msg): :param msg: message with consistency value :return: String representing consistency value """ - match = re.search("'consistency':\s+'([\w\s]+)'", msg) + match = re.search(r"'consistency':\s+'([\w\s]+)'", msg) return match and match.group(1) def test_timeout_consistency(self): diff --git a/tests/unit/test_host_connection_pool.py b/tests/unit/test_host_connection_pool.py index d8b5ca976e..8dd7785c83 100644 --- a/tests/unit/test_host_connection_pool.py +++ b/tests/unit/test_host_connection_pool.py @@ -24,6 +24,7 @@ from cassandra.pool import Host, NoConnectionsAvailable from cassandra.policies import HostDistance, SimpleConvictionPolicy + class _PoolTests(unittest.TestCase): __test__ = False PoolImpl = None @@ -259,4 +260,3 @@ class HostConnectionTests(_PoolTests): __test__ = True PoolImpl = HostConnection uses_single_connection = True - diff --git a/tests/unit/test_metadata.py b/tests/unit/test_metadata.py index 76e47a4331..e70256e2f2 100644 --- a/tests/unit/test_metadata.py +++ b/tests/unit/test_metadata.py @@ -67,7 +67,6 @@ def test_replication_factor_equality(self): self.assertNotEqual(ReplicationFactor.create('3'), ReplicationFactor.create('3/1')) - class StrategiesTest(unittest.TestCase): @classmethod @@ -255,7 +254,7 @@ def test_nts_token_performance(self): host = Host('dc1.{0}'.format(i), SimpleConvictionPolicy) host.set_location_info('dc1', "rack1") for vnode_num in range(vnodes_per_host): - md5_token = MD5Token(current_token+vnode_num) + md5_token = MD5Token(current_token + vnode_num) token_to_host_owner[md5_token] = host ring.append(md5_token) current_token += 1000 @@ -377,19 +376,22 @@ def test_protect_names(self): Test cassandra.metadata.protect_names output """ self.assertEqual(protect_names(['tests']), ['tests']) - self.assertEqual(protect_names( - [ - 'tests', - 'test\'s', - 'tests ?!@#$%^&*()', - '1' - ]), + self.assertEqual( + protect_names( + [ + 'tests', + 'test\'s', + 'tests ?!@#$%^&*()', + '1' + ] + ), [ 'tests', "\"test's\"", '"tests ?!@#$%^&*()"', '"1"' - ]) + ] + ) def test_protect_value(self): """ @@ -625,13 +627,13 @@ def test_build_index_as_cql(self): row = {'index_name': 'index_name_here', 'index_type': 'index_type_here'} index_meta = parser._build_index_metadata(column_meta, row) self.assertEqual(index_meta.as_cql_query(), - 'CREATE INDEX index_name_here ON keyspace_name_here.table_name_here (column_name_here)') + 'CREATE INDEX index_name_here ON keyspace_name_here.table_name_here (column_name_here)') row['index_options'] = '{ "class_name": "class_name_here" }' row['index_type'] = 'CUSTOM' index_meta = parser._build_index_metadata(column_meta, row) self.assertEqual(index_meta.as_cql_query(), - "CREATE CUSTOM INDEX index_name_here ON keyspace_name_here.table_name_here (column_name_here) USING 'class_name_here'") + "CREATE CUSTOM INDEX index_name_here ON keyspace_name_here.table_name_here (column_name_here) USING 'class_name_here'") class UnicodeIdentifiersTests(unittest.TestCase): diff --git a/tests/unit/test_orderedmap.py b/tests/unit/test_orderedmap.py index a26994dd7b..2a8c8f74e3 100644 --- a/tests/unit/test_orderedmap.py +++ b/tests/unit/test_orderedmap.py @@ -19,6 +19,7 @@ from cassandra.util import OrderedMap, OrderedMapSerializedKey from cassandra.cqltypes import EMPTY, UTF8Type, lookup_casstype + class OrderedMapTest(unittest.TestCase): def test_init(self): a = OrderedMap(zip(['one', 'three', 'two'], [1, 3, 2])) @@ -78,7 +79,7 @@ def test_get(self): for v, k in enumerate(keys): self.assertEqual(om.get(k), v) - + self.assertEqual(om.get('notthere', 'default'), 'default') self.assertIsNone(om.get('notthere')) @@ -107,7 +108,7 @@ def test_getitem(self): for v, k in enumerate(keys): self.assertEqual(om[k], v) - + with self.assertRaises(KeyError): om['notthere'] diff --git a/tests/unit/test_policies.py b/tests/unit/test_policies.py index beaa1c81e3..e7cafce226 100644 --- a/tests/unit/test_policies.py +++ b/tests/unit/test_policies.py @@ -884,8 +884,8 @@ def test_schedule_no_max(self): test_iter = 10000 policy = ExponentialReconnectionPolicy(base_delay=base_delay, max_delay=max_delay, max_attempts=None) sched_slice = list(islice(policy.new_schedule(), 0, test_iter)) - self._assert_between(sched_slice[0], base_delay*0.85, base_delay*1.15) - self._assert_between(sched_slice[-1], max_delay*0.85, max_delay*1.15) + self._assert_between(sched_slice[0], base_delay * 0.85, base_delay * 1.15) + self._assert_between(sched_slice[-1], max_delay * 0.85, max_delay * 1.15) self.assertEqual(len(sched_slice), test_iter) def test_schedule_with_max(self): @@ -897,12 +897,12 @@ def test_schedule_with_max(self): self.assertEqual(len(schedule), max_attempts) for i, delay in enumerate(schedule): if i == 0: - self._assert_between(delay, base_delay*0.85, base_delay*1.15) + self._assert_between(delay, base_delay * 0.85, base_delay * 1.15) elif i < 6: value = base_delay * (2 ** i) - self._assert_between(delay, value*85/100, value*1.15) + self._assert_between(delay, value * 85 / 100, value * 1.15) else: - self._assert_between(delay, max_delay*85/100, max_delay*1.15) + self._assert_between(delay, max_delay * 85 / 100, max_delay * 1.15) def test_schedule_exactly_one_attempt(self): base_delay = 2.0 @@ -951,8 +951,8 @@ def test_schedule_with_jitter(self): schedule = ep.new_schedule() for i in range(64): exp_delay = min(base_delay * (2 ** i), max_delay) - min_jitter_delay = max(base_delay, exp_delay*85/100) - max_jitter_delay = min(max_delay, exp_delay*115/100) + min_jitter_delay = max(base_delay, exp_delay * 85 / 100) + max_jitter_delay = min(max_delay, exp_delay * 115 / 100) delay = next(schedule) self._assert_between(delay, min_jitter_delay, max_jitter_delay) @@ -1463,7 +1463,7 @@ def get_replicas(keyspace, packed_key): query_plan = list(query_plan) self.assertEqual(query_plan[0], Host(DefaultEndPoint("127.0.0.2"), SimpleConvictionPolicy)) self.assertEqual(set(query_plan[1:]), {Host(DefaultEndPoint("127.0.0.3"), SimpleConvictionPolicy), - Host(DefaultEndPoint("127.0.0.5"), SimpleConvictionPolicy)}) + Host(DefaultEndPoint("127.0.0.5"), SimpleConvictionPolicy)}) def test_create_whitelist(self): cluster = Mock(spec=Cluster) diff --git a/tests/unit/test_protocol.py b/tests/unit/test_protocol.py index 08516eba9e..1dbe7e07a6 100644 --- a/tests/unit/test_protocol.py +++ b/tests/unit/test_protocol.py @@ -218,7 +218,8 @@ def test_batch_message_with_keyspace(self): keyspace='ks' ) batch.send_body(io, protocol_version=5) - self._check_calls(io, + self._check_calls( + io, ((b'\x00',), (b'\x00\x03',), (b'\x00',), (b'\x00\x00\x00\x06',), (b'stmt a',), (b'\x00\x01',), (b'\x00\x00\x00\x07',), ('param a',), diff --git a/tests/unit/test_response_future.py b/tests/unit/test_response_future.py index f9d32780de..7c415ca174 100644 --- a/tests/unit/test_response_future.py +++ b/tests/unit/test_response_future.py @@ -112,8 +112,12 @@ def test_schema_change_result(self): rf = self.make_response_future(session) rf.send_request() - event_results={'target_type': SchemaTargetType.TABLE, 'change_type': SchemaChangeType.CREATED, - 'keyspace': "keyspace1", "table": "table1"} + event_results = { + 'target_type': SchemaTargetType.TABLE, + 'change_type': SchemaChangeType.CREATED, + 'keyspace': "keyspace1", + "table": "table1" + } result = Mock(spec=ResultMessage, kind=RESULT_KIND_SCHEMA_CHANGE, schema_change_event=event_results) @@ -170,8 +174,8 @@ def test_read_timeout_error_message(self): rf = ResponseFuture(session, message, query, 1) rf.send_request() - result = Mock(spec=ReadTimeoutErrorMessage, info={"data_retrieved": "", "required_responses":2, - "received_responses":1, "consistency": 1}) + result = Mock(spec=ReadTimeoutErrorMessage, info={"data_retrieved": "", "required_responses": 2, + "received_responses": 1, "consistency": 1}) rf._set_result(None, None, None, result) self.assertRaises(Exception, rf.result) @@ -184,8 +188,8 @@ def test_write_timeout_error_message(self): rf = ResponseFuture(session, message, query, 1) rf.send_request() - result = Mock(spec=WriteTimeoutErrorMessage, info={"write_type": 1, "required_responses":2, - "received_responses":1, "consistency": 1}) + result = Mock(spec=WriteTimeoutErrorMessage, info={"write_type": 1, "required_responses": 2, + "received_responses": 1, "consistency": 1}) rf._set_result(None, None, None, result) self.assertRaises(Exception, rf.result) @@ -200,7 +204,7 @@ def test_unavailable_error_message(self): rf._query_retries = 1 rf.send_request() - result = Mock(spec=UnavailableErrorMessage, info={"required_replicas":2, "alive_replicas": 1, "consistency": 1}) + result = Mock(spec=UnavailableErrorMessage, info={"required_replicas": 2, "alive_replicas": 1, "consistency": 1}) rf._set_result(None, None, None, result) self.assertRaises(Exception, rf.result) @@ -436,7 +440,7 @@ def test_errback(self): rf.add_errback(self.assertIsInstance, Exception) - result = Mock(spec=UnavailableErrorMessage, info={"required_replicas":2, "alive_replicas": 1, "consistency": 1}) + result = Mock(spec=UnavailableErrorMessage, info={"required_replicas": 2, "alive_replicas": 1, "consistency": 1}) result.to_exception.return_value = Exception() rf._set_result(None, None, None, result) @@ -494,7 +498,7 @@ def test_multiple_errbacks(self): rf.add_errback(callback2, arg2, **kwargs2) expected_exception = Unavailable("message", 1, 2, 3) - result = Mock(spec=UnavailableErrorMessage, info={"required_replicas":2, "alive_replicas": 1, "consistency": 1}) + result = Mock(spec=UnavailableErrorMessage, info={"required_replicas": 2, "alive_replicas": 1, "consistency": 1}) result.to_exception.return_value = expected_exception rf._set_result(None, None, None, result) rf._event.set() @@ -518,7 +522,7 @@ def test_add_callbacks(self): errback=self.assertIsInstance, errback_args=(Exception,)) result = Mock(spec=UnavailableErrorMessage, - info={"required_replicas":2, "alive_replicas": 1, "consistency": 1}) + info={"required_replicas": 2, "alive_replicas": 1, "consistency": 1}) result.to_exception.return_value = Exception() rf._set_result(None, None, None, result) self.assertRaises(Exception, rf.result) @@ -617,7 +621,7 @@ def test_timeout_does_not_release_stream_id(self): pool = self.make_pool() session._pools.get.return_value = pool connection = Mock(spec=Connection, lock=RLock(), _requests={}, request_ids=deque(), - orphaned_request_ids=set(), orphaned_threshold=256) + orphaned_request_ids=set(), orphaned_threshold=256) pool.borrow_connection.return_value = (connection, 1) rf = self.make_response_future(session) diff --git a/tests/unit/test_segment.py b/tests/unit/test_segment.py index e94bcf9809..941ca93d88 100644 --- a/tests/unit/test_segment.py +++ b/tests/unit/test_segment.py @@ -111,12 +111,12 @@ def test_encode_compressed_header_not_self_contained_msg(self): compressed_length = len(segment_codec_lz4.compress(self.max_msg)) segment_codec_lz4.encode_header(buffer, compressed_length, len(self.max_msg), False) self.assertEqual(buffer.tell(), 8) + + # Format: 17-bit len + 17-bit mask + 1-bit flag (0=not self-contained) + 5-bit padding self.assertEqual( self._header_to_bits(buffer.getvalue()), - ("{:017b}".format(compressed_length) + - "11111111111111111" - "0" # not self-contained - "00000")) + "{:017b}".format(compressed_length) + "11111111111111111" + "0" + "00000" + ) def test_decode_uncompressed_header(self): buffer = BytesIO() @@ -142,7 +142,7 @@ def test_decode_header_fails_if_corrupted(self): buffer = BytesIO() segment_codec_no_compression.encode_header(buffer, len(self.small_msg), -1, True) # corrupt one byte - buffer.seek(buffer.tell()-1) + buffer.seek(buffer.tell() - 1) buffer.write(b'0') buffer.seek(0) @@ -197,7 +197,7 @@ def test_decode_multi_segments(self): def test_decode_fails_if_corrupted(self): buffer = BytesIO() segment_codec_lz4.encode(buffer, self.small_msg) - buffer.seek(buffer.tell()-1) + buffer.seek(buffer.tell() - 1) buffer.write(b'0') buffer.seek(0) header = segment_codec_lz4.decode_header(buffer) diff --git a/tests/unit/test_sortedset.py b/tests/unit/test_sortedset.py index 875485f824..8e91315a24 100644 --- a/tests/unit/test_sortedset.py +++ b/tests/unit/test_sortedset.py @@ -22,6 +22,7 @@ from datetime import datetime from itertools import permutations + class SortedSetTest(unittest.TestCase): def test_init(self): input = [5, 4, 3, 2, 1, 1, 1] @@ -42,8 +43,8 @@ def test_contains(self): self.assertTrue(i in ss) self.assertFalse(i not in ss) - hi = max(expected)+1 - lo = min(expected)-1 + hi = max(expected) + 1 + lo = min(expected) - 1 self.assertFalse(hi in ss) self.assertFalse(lo in ss) @@ -212,7 +213,7 @@ def test_getitem(self): ss[len(ss)] def test_delitem(self): - expected = [1,2,3,4] + expected = [1, 2, 3, 4] ss = sortedset(expected) for i in range(len(ss)): self.assertListEqual(list(ss), expected[i:]) @@ -287,7 +288,7 @@ def test_operators(self): self.assertEqual(tmp, sortedset()) # __rand__ - self.assertEqual(set([1]) & ss12, ss1) + self.assertEqual({1} & ss12, ss1) # __or__ self.assertEqual(ss1 | ss12, ss12) @@ -311,7 +312,7 @@ def test_operators(self): self.assertEqual(tmp, ss12) # __ror__ - self.assertEqual(set([1]) | ss12, ss12) + self.assertEqual({1} | ss12, ss12) # __sub__ self.assertEqual(ss1 - ss12, set()) @@ -334,21 +335,21 @@ def test_operators(self): self.assertEqual(tmp, sortedset([2])) # __rsub__ - self.assertEqual(set((1,2,3)) - ss12, set((3,))) + self.assertEqual({1, 2, 3} - ss12, {3}) # __xor__ - self.assertEqual(ss1 ^ ss12, set([2])) - self.assertEqual(ss12 ^ ss1, set([2])) + self.assertEqual(ss1 ^ ss12, {2}) + self.assertEqual(ss12 ^ ss1, {2}) self.assertEqual(ss12 ^ ss12, set()) self.assertEqual(ss12 ^ set(), ss12) # __ixor__ tmp = sortedset(ss1) tmp ^= ss12 - self.assertEqual(tmp, set([2])) + self.assertEqual(tmp, {2}) tmp = sortedset(ss12) tmp ^= ss1 - self.assertEqual(tmp, set([2])) + self.assertEqual(tmp, {2}) tmp = sortedset(ss12) tmp ^= ss12 self.assertEqual(tmp, set()) @@ -357,10 +358,10 @@ def test_operators(self): self.assertEqual(tmp, ss12) # __rxor__ - self.assertEqual(set([1, 2]) ^ ss1, (set([2]))) + self.assertEqual({1, 2} ^ ss1, ({2})) def test_reduce_pickle(self): - ss = sortedset((4,3,2,1)) + ss = sortedset((4, 3, 2, 1)) import pickle s = pickle.dumps(ss) self.assertEqual(pickle.loads(s), ss) diff --git a/tests/unit/test_timestamps.py b/tests/unit/test_timestamps.py index 676cb6442a..bae2927637 100644 --- a/tests/unit/test_timestamps.py +++ b/tests/unit/test_timestamps.py @@ -122,7 +122,7 @@ def test_basic_log_content(self): warning_threshold=1e-6, warning_interval=1e-6 ) - #The units of _last_warn is seconds + # The units of _last_warn is seconds tsg._last_warn = 12 tsg._next_timestamp(20, tsg.last) diff --git a/tests/unit/test_types.py b/tests/unit/test_types.py index ba01538b2a..93c86014e6 100644 --- a/tests/unit/test_types.py +++ b/tests/unit/test_types.py @@ -250,10 +250,10 @@ def test_collection_null_support(self): """ int_list = ListType.apply_parameters([Int32Type]) value = ( - int32_pack(2) + # num items - int32_pack(-1) + # size of item1 - int32_pack(4) + # size of item2 - int32_pack(42) # item2 + int32_pack(2) # num items + + int32_pack(-1) # size of item1 + + int32_pack(4) # size of item2 + + int32_pack(42) # item2 ) self.assertEqual( [None, 42], @@ -267,13 +267,13 @@ def test_collection_null_support(self): ) value = ( - int32_pack(2) + # num items - int32_pack(4) + # key size of item1 - int32_pack(42) + # key item1 - int32_pack(-1) + # value size of item1 - int32_pack(-1) + # key size of item2 - int32_pack(4) + # value size of item2 - int32_pack(42) # value of item2 + int32_pack(2) # num items + + int32_pack(4) # key size of item1 + + int32_pack(42) # key item1 + + int32_pack(-1) # value size of item1 + + int32_pack(-1) # key size of item2 + + int32_pack(4) # value size of item2 + + int32_pack(42) # value of item2 ) map_list = MapType.apply_parameters([Int32Type, Int32Type]) @@ -341,11 +341,11 @@ def _round_trip_compare_fn(self, first, second): second_norm = self._normalize_set(second) self.assertEqual(first_norm, second_norm) elif isinstance(first, dict): - for ((fk,fv), (sk,sv)) in zip(first.items(), second.items()): + for ((fk, fv), (sk, sv)) in zip(first.items(), second.items()): self._round_trip_compare_fn(fk, sk) self._round_trip_compare_fn(fv, sv) else: - self.assertEqual(first,second) + self.assertEqual(first, second) def _round_trip_test(self, data, ctype_str): ctype = parse_casstype_args(ctype_str) @@ -355,91 +355,97 @@ def _round_trip_test(self, data, ctype_str): self.assertEqual(serialized_size * len(data), len(data_bytes)) result = ctype.deserialize(data_bytes, 0) self.assertEqual(len(data), len(result)) - for idx in range(0,len(data)): + for idx in range(0, len(data)): self._round_trip_compare_fn(data[idx], result[idx]) def test_round_trip_basic_types_with_fixed_serialized_size(self): - self._round_trip_test([True, False, False, True], \ - "org.apache.cassandra.db.marshal.VectorType(org.apache.cassandra.db.marshal.BooleanType, 4)") - self._round_trip_test([3.4, 2.9, 41.6, 12.0], \ - "org.apache.cassandra.db.marshal.VectorType(org.apache.cassandra.db.marshal.FloatType, 4)") - self._round_trip_test([3.4, 2.9, 41.6, 12.0], \ - "org.apache.cassandra.db.marshal.VectorType(org.apache.cassandra.db.marshal.DoubleType, 4)") - self._round_trip_test([3, 2, 41, 12], \ - "org.apache.cassandra.db.marshal.VectorType(org.apache.cassandra.db.marshal.LongType, 4)") - self._round_trip_test([3, 2, 41, 12], \ - "org.apache.cassandra.db.marshal.VectorType(org.apache.cassandra.db.marshal.Int32Type, 4)") - self._round_trip_test([uuid.uuid1(), uuid.uuid1(), uuid.uuid1(), uuid.uuid1()], \ - "org.apache.cassandra.db.marshal.VectorType(org.apache.cassandra.db.marshal.TimeUUIDType, 4)") - self._round_trip_test([3, 2, 41, 12], \ - "org.apache.cassandra.db.marshal.VectorType(org.apache.cassandra.db.marshal.ShortType, 4)") + self._round_trip_test([True, False, False, True], + "org.apache.cassandra.db.marshal.VectorType(org.apache.cassandra.db.marshal.BooleanType, 4)") + self._round_trip_test([3.4, 2.9, 41.6, 12.0], + "org.apache.cassandra.db.marshal.VectorType(org.apache.cassandra.db.marshal.FloatType, 4)") + self._round_trip_test([3.4, 2.9, 41.6, 12.0], + "org.apache.cassandra.db.marshal.VectorType(org.apache.cassandra.db.marshal.DoubleType, 4)") + self._round_trip_test([3, 2, 41, 12], + "org.apache.cassandra.db.marshal.VectorType(org.apache.cassandra.db.marshal.LongType, 4)") + self._round_trip_test([3, 2, 41, 12], + "org.apache.cassandra.db.marshal.VectorType(org.apache.cassandra.db.marshal.Int32Type, 4)") + self._round_trip_test([uuid.uuid1(), uuid.uuid1(), uuid.uuid1(), uuid.uuid1()], + "org.apache.cassandra.db.marshal.VectorType(org.apache.cassandra.db.marshal.TimeUUIDType, 4)") + self._round_trip_test([3, 2, 41, 12], + "org.apache.cassandra.db.marshal.VectorType(org.apache.cassandra.db.marshal.ShortType, 4)") def test_round_trip_basic_types_without_fixed_serialized_size(self): # Varints - self._round_trip_test([3, 2, 41, 12], \ - "org.apache.cassandra.db.marshal.VectorType(org.apache.cassandra.db.marshal.IntegerType, 4)") + self._round_trip_test([3, 2, 41, 12], + "org.apache.cassandra.db.marshal.VectorType(org.apache.cassandra.db.marshal.IntegerType, 4)") # ASCII text - self._round_trip_test(["abc", "def", "ghi", "jkl"], \ - "org.apache.cassandra.db.marshal.VectorType(org.apache.cassandra.db.marshal.AsciiType, 4)") + self._round_trip_test(["abc", "def", "ghi", "jkl"], + "org.apache.cassandra.db.marshal.VectorType(org.apache.cassandra.db.marshal.AsciiType, 4)") # UTF8 text - self._round_trip_test(["abc", "def", "ghi", "jkl"], \ - "org.apache.cassandra.db.marshal.VectorType(org.apache.cassandra.db.marshal.UTF8Type, 4)") + self._round_trip_test(["abc", "def", "ghi", "jkl"], + "org.apache.cassandra.db.marshal.VectorType(org.apache.cassandra.db.marshal.UTF8Type, 4)") # Time is something of a weird one. By rights, it should be a fixed size type but C* code marks it as variable # size. We're forced to follow the C* code base (since that's who'll be providing the data we're parsing) so # we match what they're doing. - self._round_trip_test([datetime.time(1,1,1), datetime.time(2,2,2), datetime.time(3,3,3)], \ - "org.apache.cassandra.db.marshal.VectorType(org.apache.cassandra.db.marshal.TimeType, 3)") + self._round_trip_test([datetime.time(1, 1, 1), datetime.time(2, 2, 2), datetime.time(3, 3, 3)], + "org.apache.cassandra.db.marshal.VectorType(org.apache.cassandra.db.marshal.TimeType, 3)") # Duration (contains varints) - self._round_trip_test([util.Duration(1,1,1), util.Duration(2,2,2), util.Duration(3,3,3)], \ - "org.apache.cassandra.db.marshal.VectorType(org.apache.cassandra.db.marshal.DurationType, 3)") + self._round_trip_test([util.Duration(1, 1, 1), util.Duration(2, 2, 2), util.Duration(3, 3, 3)], + "org.apache.cassandra.db.marshal.VectorType(org.apache.cassandra.db.marshal.DurationType, 3)") def test_round_trip_collection_types(self): # List (subtype of fixed size) - self._round_trip_test([[1, 2, 3, 4], [5, 6], [7, 8, 9, 10], [11, 12]], \ + self._round_trip_test( + [[1, 2, 3, 4], [5, 6], [7, 8, 9, 10], [11, 12]], "org.apache.cassandra.db.marshal.VectorType(org.apache.cassandra.db.marshal.ListType \ (org.apache.cassandra.db.marshal.Int32Type), 4)") # Set (subtype of fixed size) - self._round_trip_test([set([1, 2, 3, 4]), set([5, 6]), set([7, 8, 9, 10]), set([11, 12])], \ + self._round_trip_test( + [{1, 2, 3, 4}, {5, 6}, {7, 8, 9, 10}, {11, 12}], "org.apache.cassandra.db.marshal.VectorType(org.apache.cassandra.db.marshal.SetType \ (org.apache.cassandra.db.marshal.Int32Type), 4)") # Map (subtype of fixed size) - self._round_trip_test([{1:1.2}, {2:3.4}, {3:5.6}, {4:7.8}], \ - "org.apache.cassandra.db.marshal.VectorType(org.apache.cassandra.db.marshal.MapType \ + self._round_trip_test( + [{1: 1.2}, {2: 3.4}, {3: 5.6}, {4: 7.8}], + "org.apache.cassandra.db.marshal.VectorType(org.apache.cassandra.db.marshal.MapType \ (org.apache.cassandra.db.marshal.Int32Type,org.apache.cassandra.db.marshal.FloatType), 4)") # List (subtype without fixed size) - self._round_trip_test([["one","two"], ["three","four"], ["five","six"], ["seven","eight"]], \ + self._round_trip_test( + [["one", "two"], ["three", "four"], ["five", "six"], ["seven", "eight"]], "org.apache.cassandra.db.marshal.VectorType(org.apache.cassandra.db.marshal.ListType \ (org.apache.cassandra.db.marshal.AsciiType), 4)") # Set (subtype without fixed size) - self._round_trip_test([set(["one","two"]), set(["three","four"]), set(["five","six"]), set(["seven","eight"])], \ + self._round_trip_test( + [{"one", "two"}, {"three", "four"}, {"five", "six"}, {"seven", "eight"}], "org.apache.cassandra.db.marshal.VectorType(org.apache.cassandra.db.marshal.SetType \ (org.apache.cassandra.db.marshal.AsciiType), 4)") # Map (subtype without fixed size) - self._round_trip_test([{1:"one"}, {2:"two"}, {3:"three"}, {4:"four"}], \ - "org.apache.cassandra.db.marshal.VectorType(org.apache.cassandra.db.marshal.MapType \ - (org.apache.cassandra.db.marshal.IntegerType,org.apache.cassandra.db.marshal.AsciiType), 4)") + self._round_trip_test( + [{1: "one"}, {2: "two"}, {3: "three"}, {4: "four"}], + "org.apache.cassandra.db.marshal.VectorType(org.apache.cassandra.db.marshal.MapType \ + (org.apache.cassandra.db.marshal.IntegerType,org.apache.cassandra.db.marshal.AsciiType), 4)") # List of lists (subtype without fixed size) - data = [[["one","two"],["three"]], [["four"],["five"]], [["six","seven","eight"]], [["nine"]]] + data = [[["one", "two"], ["three"]], [["four"], ["five"]], [["six", "seven", "eight"]], [["nine"]]] ctype = "org.apache.cassandra.db.marshal.VectorType\ (org.apache.cassandra.db.marshal.ListType\ (org.apache.cassandra.db.marshal.ListType\ (org.apache.cassandra.db.marshal.AsciiType)), 4)" self._round_trip_test(data, ctype) # Set of sets (subtype without fixed size) - data = [set([frozenset(["one","two"]),frozenset(["three"])]),\ - set([frozenset(["four"]),frozenset(["five"])]),\ - set([frozenset(["six","seven","eight"])]), - set([frozenset(["nine"])])] + data = [{frozenset(["one", "two"]), frozenset(["three"])}, + {frozenset(["four"]), frozenset(["five"])}, + {frozenset(["six", "seven", "eight"])}, + {frozenset(["nine"])}] ctype = "org.apache.cassandra.db.marshal.VectorType\ (org.apache.cassandra.db.marshal.SetType\ (org.apache.cassandra.db.marshal.SetType\ (org.apache.cassandra.db.marshal.AsciiType)), 4)" self._round_trip_test(data, ctype) # Map of maps (subtype without fixed size) - data = [{100:{1:"one",2:"two",3:"three"}},\ - {200:{4:"four",5:"five"}},\ - {300:{}},\ - {400:{6:"six"}}] + data = [{100: {1: "one", 2: "two", 3: "three"}}, + {200: {4: "four", 5: "five"}}, + {300: {}}, + {400: {6: "six"}}] ctype = "org.apache.cassandra.db.marshal.VectorType\ (org.apache.cassandra.db.marshal.MapType\ (org.apache.cassandra.db.marshal.Int32Type,\ @@ -449,12 +455,14 @@ def test_round_trip_collection_types(self): def test_round_trip_vector_of_vectors(self): # Subytpes of subtypes with a fixed size - self._round_trip_test([[1.2, 3.4], [5.6, 7.8], [9.10, 11.12], [13.14, 15.16]], \ + self._round_trip_test( + [[1.2, 3.4], [5.6, 7.8], [9.10, 11.12], [13.14, 15.16]], "org.apache.cassandra.db.marshal.VectorType(org.apache.cassandra.db.marshal.VectorType \ (org.apache.cassandra.db.marshal.FloatType,2), 4)") # Subytpes of subtypes without a fixed size - self._round_trip_test([["one", "two"], ["three", "four"], ["five", "six"], ["seven", "eight"]], \ + self._round_trip_test( + [["one", "two"], ["three", "four"], ["five", "six"], ["seven", "eight"]], "org.apache.cassandra.db.marshal.VectorType(org.apache.cassandra.db.marshal.VectorType \ (org.apache.cassandra.db.marshal.AsciiType,2), 4)") @@ -552,8 +560,7 @@ def test_month_rounding_creation_failure(self): feb_stamp = ms_timestamp_from_datetime( datetime.datetime(2018, 2, 25, 18, 59, 59, 0) ) - dr = DateRange(OPEN_BOUND, - DateRangeBound(feb_stamp, DateRangePrecision.MONTH)) + dr = DateRange(OPEN_BOUND, DateRangeBound(feb_stamp, DateRangePrecision.MONTH)) dt = datetime_from_timestamp(dr.upper_bound.milliseconds / 1000) self.assertEqual(dt.day, 28) @@ -581,9 +588,9 @@ def test_encode_precision_error(self): DateRangeType._encode_precision('INVALID') def test_deserialize_single_value(self): - serialized = (int8_pack(0) + - int64_pack(self.timestamp) + - int8_pack(3)) + serialized = (int8_pack(0) + + int64_pack(self.timestamp) + + int8_pack(3)) self.assertEqual( DateRangeType.deserialize(serialized, 5), util.DateRange(value=util.DateRangeBound( @@ -593,11 +600,11 @@ def test_deserialize_single_value(self): ) def test_deserialize_closed_range(self): - serialized = (int8_pack(1) + - int64_pack(self.timestamp) + - int8_pack(2) + - int64_pack(self.timestamp) + - int8_pack(6)) + serialized = (int8_pack(1) + + int64_pack(self.timestamp) + + int8_pack(2) + + int64_pack(self.timestamp) + + int8_pack(6)) self.assertEqual( DateRangeType.deserialize(serialized, 5), util.DateRange( @@ -613,9 +620,9 @@ def test_deserialize_closed_range(self): ) def test_deserialize_open_high(self): - serialized = (int8_pack(2) + - int64_pack(self.timestamp) + - int8_pack(3)) + serialized = (int8_pack(2) + + int64_pack(self.timestamp) + + int8_pack(3)) deserialized = DateRangeType.deserialize(serialized, 5) self.assertEqual( deserialized, @@ -629,9 +636,9 @@ def test_deserialize_open_high(self): ) def test_deserialize_open_low(self): - serialized = (int8_pack(3) + - int64_pack(self.timestamp) + - int8_pack(4)) + serialized = (int8_pack(3) + + int64_pack(self.timestamp) + + int8_pack(4)) deserialized = DateRangeType.deserialize(serialized, 5) self.assertEqual( deserialized, @@ -651,9 +658,9 @@ def test_deserialize_single_open(self): ) def test_serialize_single_value(self): - serialized = (int8_pack(0) + - int64_pack(self.timestamp) + - int8_pack(5)) + serialized = (int8_pack(0) + + int64_pack(self.timestamp) + + int8_pack(5)) deserialized = DateRangeType.deserialize(serialized, 5) self.assertEqual( deserialized, @@ -666,11 +673,11 @@ def test_serialize_single_value(self): ) def test_serialize_closed_range(self): - serialized = (int8_pack(1) + - int64_pack(self.timestamp) + - int8_pack(5) + - int64_pack(self.timestamp) + - int8_pack(0)) + serialized = (int8_pack(1) + + int64_pack(self.timestamp) + + int8_pack(5) + + int64_pack(self.timestamp) + + int8_pack(0)) deserialized = DateRangeType.deserialize(serialized, 5) self.assertEqual( deserialized, @@ -687,9 +694,9 @@ def test_serialize_closed_range(self): ) def test_serialize_open_high(self): - serialized = (int8_pack(2) + - int64_pack(self.timestamp) + - int8_pack(2)) + serialized = (int8_pack(2) + + int64_pack(self.timestamp) + + int8_pack(2)) deserialized = DateRangeType.deserialize(serialized, 5) self.assertEqual( deserialized, @@ -703,9 +710,9 @@ def test_serialize_open_high(self): ) def test_serialize_open_low(self): - serialized = (int8_pack(2) + - int64_pack(self.timestamp) + - int8_pack(3)) + serialized = (int8_pack(2) + + int64_pack(self.timestamp) + + int8_pack(3)) deserialized = DateRangeType.deserialize(serialized, 5) self.assertEqual( deserialized, @@ -789,9 +796,9 @@ def test_deserialize_zero_datetime(self): @test_category data_types """ DateRangeType.deserialize( - (int8_pack(1) + - int64_pack(0) + int8_pack(0) + - int64_pack(0) + int8_pack(0)), + (int8_pack(1) + + int64_pack(0) + int8_pack(0) + + int64_pack(0) + int8_pack(0)), 5 ) @@ -885,15 +892,18 @@ def test_deserialize_date_range_hours(self): @test_category data_types """ - self._deserialize_date_range({"minute": 0, "second": 0, "microsecond": 0}, - DateRangePrecision.HOUR, - # This lambda function given a truncated date adds - # one hour minus one microsecond in microseconds - lambda x: x + - 59 * 60 * 1000 + - 59 * 1000 + - 999, - lambda original_value, i: original_value + i * 900 * 50 * 60) + self._deserialize_date_range( + {"minute": 0, "second": 0, "microsecond": 0}, + DateRangePrecision.HOUR, + # This lambda function given a truncated date adds + # one hour minus one microsecond in microseconds + lambda x: ( + x + + 59 * 60 * 1000 + + 59 * 1000 + + 999 + ), + lambda original_value, i: original_value + i * 900 * 50 * 60) def test_deserialize_date_range_day(self): """ @@ -905,16 +915,19 @@ def test_deserialize_date_range_day(self): @test_category data_types """ - self._deserialize_date_range({"hour": 0, "minute": 0, "second": 0, "microsecond": 0}, - DateRangePrecision.DAY, - # This lambda function given a truncated date adds - # one day minus one microsecond in microseconds - lambda x: x + - 23 * 60 * 60 * 1000 + - 59 * 60 * 1000 + - 59 * 1000 + - 999, - lambda original_value, i: original_value + i * 900 * 50 * 60 * 24) + self._deserialize_date_range( + {"hour": 0, "minute": 0, "second": 0, "microsecond": 0}, + DateRangePrecision.DAY, + # This lambda function given a truncated date adds + # one day minus one microsecond in microseconds + lambda x: ( + x + + 23 * 60 * 60 * 1000 + + 59 * 60 * 1000 + + 59 * 1000 + + 999 + ), + lambda original_value, i: original_value + i * 900 * 50 * 60 * 24) @unittest.skip("This is currently failing, see PYTHON-912") def test_deserialize_date_range_month(self): @@ -969,7 +982,7 @@ def get_upper_bound(seconds): diff = time.mktime(dt.timetuple()) - time.mktime(self.epoch.timetuple()) return diff * 1000 + 999 # This doesn't work for big values because it loses precision - #return int((dt - self.epoch).total_seconds() * 1000) + # return int((dt - self.epoch).total_seconds() * 1000) self._deserialize_date_range({"month": 1, "day": 1, "hour": 0, "minute": 0, "second": 0, "microsecond": 0}, DateRangePrecision.YEAR, get_upper_bound, @@ -1069,8 +1082,8 @@ def test_date_order(self): date_format = "%Y-%m-%d" dates_from_value = [ - Date((datetime.datetime.strptime(dtstr, date_format) - - datetime.datetime(1970, 1, 1)).days) + Date((datetime.datetime.strptime(dtstr, date_format) + - datetime.datetime(1970, 1, 1)).days) for dtstr in ("2017-01-02", "2017-01-06", "2017-01-10", "2017-01-14") ] dates_from_value_equal = [Date(1), Date(1)] @@ -1080,7 +1093,7 @@ def test_date_order(self): dates_from_datetime = [Date(datetime.datetime.strptime(dtstr, date_format)) for dtstr in ("2017-01-03", "2017-01-07", "2017-01-11", "2017-01-15")] dates_from_datetime_equal = [Date(datetime.datetime.strptime("2017-01-01", date_format)), - Date(datetime.datetime.strptime("2017-01-01", date_format))] + Date(datetime.datetime.strptime("2017-01-01", date_format))] check_sequence_consistency(self, dates_from_datetime) check_sequence_consistency(self, dates_from_datetime_equal, equal=True) diff --git a/tests/unit/test_util_types.py b/tests/unit/test_util_types.py index ead027291f..a9defc7792 100644 --- a/tests/unit/test_util_types.py +++ b/tests/unit/test_util_types.py @@ -209,7 +209,7 @@ class VersionTests(unittest.TestCase): def test_version_parsing(self): versions = [ - # Test cases here adapted from the Java driver cases + # Test cases here adapted from the Java driver cases # (https://github.com/apache/cassandra-java-driver/blob/4.19.2/core/src/test/java/com/datastax/oss/driver/api/core/VersionTest.java) ('1.2.19', (1, 2, 19, 0, "")), ('1.2', (1, 2, 0, 0, "")), @@ -276,14 +276,13 @@ def test_version_compare(self): self.assertTrue(Version('4.0') == Version('4.0.0.0')) self.assertTrue(Version('4.0') > Version('3.9.3')) - equal_tuples = [ (Version('4.0-SNAPSHOT'), Version('4.0-SNAPSHOT')), (Version('4.0.0-SNAPSHOT'), Version('4.0-SNAPSHOT')), (Version('4.0.0-SNAPSHOT'), Version('4.0.0-SNAPSHOT')), (Version('4.0.0.5-SNAPSHOT'), Version('4.0.0.5-SNAPSHOT')) ] - for (a,b) in equal_tuples: + for (a, b) in equal_tuples: self.assertEqual(a, b) self.assertEqual(hash(a), hash(b)) @@ -296,7 +295,7 @@ def test_version_compare(self): (Version('4.0-SNAPSHOT2'), Version('4.0.0-SNAPSHOT1')), (Version('4.0.0-alpha1-SNAPSHOT'), Version('4.0.0-SNAPSHOT')) ] - for (a,b) in left_greater_tuples: + for (a, b) in left_greater_tuples: self.assertGreater(a, b) # Test the version limit for v4 schema parsing in cassandra.metadata to make sure