From 63bda191af9c2b48a79a23074291ec20bacf4efe Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Piotr=20Ma=C5=9Blanka?= <piotr.maslanka@henrietta.com.pl>
Date: Mon, 14 Dec 2020 19:24:58 +0100
Subject: [PATCH] add coverage

---
 .circleci/config.yml    | 15 +++++++++++++--
 .coveragerc             |  1 -
 README.md               |  1 +
 docs/usage.rst          |  7 ++++++-
 requirements.txt        |  1 +
 setup.py                | 27 +++++++++------------------
 tempsdb/chunks/gzip.pyx |  3 ++-
 tempsdb/varlen.pyx      |  2 ++
 tests/test.sh           |  3 +++
 unittest.Dockerfile     |  6 +++---
 10 files changed, 40 insertions(+), 26 deletions(-)
 create mode 100644 tests/test.sh

diff --git a/.circleci/config.yml b/.circleci/config.yml
index da4824a..3514f69 100644
--- a/.circleci/config.yml
+++ b/.circleci/config.yml
@@ -12,13 +12,24 @@ jobs:
       - python/install-deps
       - python/save-cache
       - run:
+          name: Setup Code Climate test-reporter
           command: |
-            sudo pip install satella>=2.14.23 snakehouse
+          # download test reporter as a static binary
+            curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
+            chmod +x ./cc-test-reporter
+      - run:
+          command: |
+            sudo pip install nose2
           name: Install necessary modules
       - run:
           command: |
-            sudo python setup.py test
+            sudo source tests/test.sh
           name: Test
+      - run:
+          command: |
+            sudo coverage xml
+            sudo ./cc-test-reporter after-build -t coverage.py
+          name: Send back coverage results
 
 workflows:
   main:
diff --git a/.coveragerc b/.coveragerc
index 001df73..62eb0ed 100644
--- a/.coveragerc
+++ b/.coveragerc
@@ -8,7 +8,6 @@ omit=
   tests/*
   .eggs/*
   setup.py
-  tempsdb/__init__.py
 
 [report]
 include=
diff --git a/README.md b/README.md
index 9c04d17..26ff1b1 100644
--- a/README.md
+++ b/README.md
@@ -5,6 +5,7 @@
 [![PyPI](https://img.shields.io/pypi/implementation/tempsdb.svg)](https://pypi.python.org/pypi/tempsdb)
 [![Documentation Status](https://readthedocs.org/projects/tempsdb/badge/?version=latest)](http://tempsdb.readthedocs.io/en/latest/?badge=latest)
 [![Maintainability](https://api.codeclimate.com/v1/badges/657b03d115f6e001633c/maintainability)](https://codeclimate.com/github/smok-serwis/tempsdb/maintainability)
+[![Test Coverage](https://api.codeclimate.com/v1/badges/a0ff30771c71e43e8149/test_coverage)](https://codeclimate.com/github/smok-serwis/tempsdb/test_coverage)
 [![Build status](https://circleci.com/gh/smok-serwis/tempsdb.svg?style=shield)](https://app.circleci.com/pipelines/github/smok-serwis/tempsdb)
 [![Wheel](https://img.shields.io/pypi/wheel/tempsdb.svg)](https://pypi.org/project/tempsdb/)
 
diff --git a/docs/usage.rst b/docs/usage.rst
index 268e062..94e3336 100644
--- a/docs/usage.rst
+++ b/docs/usage.rst
@@ -22,9 +22,14 @@ Start off by instantiating an object
 Note that if you specify a `gzip_level` argument in
 :meth:`~tempsdb.database.Database.create_series`, GZIP compression will be used.
 
-Note that gzip-compressed series are very slow to read, since every seek needs
+Note that gzip-compressed series are very slow to read the first time you open them,
+since an index needs to be built each time that they are seek'ed.
 to start from scratch. This will be fixed in the future.
 
+Random seeks are provided by the indexed-gzip_ library.
+
+.. _indexed-gzip: https://pypi.org/project/indexed-gzip/
+
 Also, any gzip-opened series will raise a warning, since their support is experimental at best.
 
 You can create new databases via
diff --git a/requirements.txt b/requirements.txt
index 2fe238c..1108e35 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -2,3 +2,4 @@ satella
 ujson
 snakehouse
 six
+indexed_gzip
diff --git a/setup.py b/setup.py
index 86f800f..6abc38c 100644
--- a/setup.py
+++ b/setup.py
@@ -1,12 +1,9 @@
 import os
 import typing as tp
-
-from Cython.Build import cythonize
 from satella.distutils import monkey_patch_parallel_compilation
 from satella.files import find_files
 from distutils.core import setup
 
-from setuptools import Extension
 from snakehouse import Multibuild, build
 
 
@@ -16,28 +13,22 @@ def find_pyx(*path) -> tp.List[str]:
 
 monkey_patch_parallel_compilation()
 
-# extensions = [Extension("tempsdb.chunks", ['tempsdb/chunks.pyx']),
-#               Extension("tempsdb.database", ['tempsdb/database.pyx']),
-#               Extension('tempsdb.exceptions', ['tempsdb/exceptions.pyx']),
-#               Extension('tempsdb.series', ['tempsdb/series.pyx']),
-#               Extension('tempsdb.iterators', ['tempsdb/iterators.pyx'])]
-#
 directives = {'language_level': '3'}
+ext_kwargs = {}
+cythonize_kwargs = {}
 if 'CI' in os.environ:
+    ext_kwargs['define_macros'] = [("CYTHON_TRACE_NOGIL", "1")]
     directives.update(profile=True, linetrace=True, embedsignature=True)
+    cythonize_kwargs['gdb_debug'] = True
 
 
 setup(name='tempsdb',
-      version='0.5.0a9',
+      version='0.5',
       packages=['tempsdb'],
-      install_requires=['satella>=2.14.21', 'ujson'],
-      ext_modules=build([Multibuild('tempsdb', find_pyx('tempsdb')), ],
-                        compiler_directives=directives),
-      # ext_modules=cythonize(extensions,
-      #                   gdb_debug=True,
-      #                   compiler_directives={
-      #                       'language_level': '3',
-      #                   }),
+      install_requires=['satella>=2.14.24', 'ujson', 'indexed_gzip'],
+      ext_modules=build([Multibuild('tempsdb', find_pyx('tempsdb'), **ext_kwargs), ],
+                        compiler_directives=directives,
+                        **cythonize_kwargs),
       python_requires='!=2.7.*,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,!=3.6.*,!=3.7.*',
       test_suite="tests",
       zip_safe=False
diff --git a/tempsdb/chunks/gzip.pyx b/tempsdb/chunks/gzip.pyx
index db18959..0cca5b7 100644
--- a/tempsdb/chunks/gzip.pyx
+++ b/tempsdb/chunks/gzip.pyx
@@ -1,4 +1,5 @@
 import gzip
+import indexed_gzip
 import threading
 
 
@@ -6,7 +7,7 @@ cdef class ReadWriteGzipFile:
     def __init__(self, path: str, compresslevel: int = gzip._COMPRESS_LEVEL_FAST):
         self.path = path
         self.compress_level = compresslevel
-        self.ro_file = gzip.GzipFile(path, 'rb')
+        self.ro_file = indexed_gzip.IndexedGzipFile(path)
         self.rw_file = gzip.GzipFile(path, 'ab', compresslevel=self.compress_level)
         self.pointer = 0
         self.lock = threading.RLock()
diff --git a/tempsdb/varlen.pyx b/tempsdb/varlen.pyx
index 4100c47..9febcc3 100644
--- a/tempsdb/varlen.pyx
+++ b/tempsdb/varlen.pyx
@@ -552,6 +552,8 @@ cdef class VarlenSeries:
         
         Updates :attr:`~tempsdb.varlen.VarlenSeries.current_maximum_length`.
         """
+        from .series import create_series
+
         cdef:
             int new_name = len(self.series)
             int new_len = self.get_length_for(new_name)
diff --git a/tests/test.sh b/tests/test.sh
new file mode 100644
index 0000000..56d591d
--- /dev/null
+++ b/tests/test.sh
@@ -0,0 +1,3 @@
+#!/bin/bash
+coverage run -m nose2 -vv
+coverage report
diff --git a/unittest.Dockerfile b/unittest.Dockerfile
index bccaee1..5703b7a 100644
--- a/unittest.Dockerfile
+++ b/unittest.Dockerfile
@@ -1,6 +1,6 @@
 FROM python:3.8
 
-RUN pip install satella>=2.14.24 snakehouse nose2 wheel ujson coverage
+RUN pip install satella>=2.14.24 snakehouse nose2 wheel ujson coverage indexed_gzip
 
 ADD tempsdb /app/tempsdb
 ADD setup.py /app/setup.py
@@ -11,5 +11,5 @@ WORKDIR /app
 ENV CI=true
 RUN python setup.py build_ext --inplace
 ADD tests /app/tests
-
-CMD ["nose2", "-vv"]
+RUN chmod ugo+x /app/tests/test.sh
+CMD ["/app/tests/test.sh"]
-- 
GitLab