From 53b7dd75f5f27ae8f8ecc1678001196d78d92ed6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Piotr=20Ma=C5=9Blanka?= <piotr.maslanka@henrietta.com.pl>
Date: Fri, 9 Jul 2021 16:46:19 +0200
Subject: [PATCH] extra unit tests and bugfixes

---
 tempsdb/chunks/base.pxd |  2 +-
 tempsdb/chunks/base.pyx | 11 +++++------
 tests/test_database.py  |  5 ++++-
 tests/test_varlen.py    | 12 +++++++-----
 4 files changed, 17 insertions(+), 13 deletions(-)

diff --git a/tempsdb/chunks/base.pxd b/tempsdb/chunks/base.pxd
index febcc7e..c128df5 100644
--- a/tempsdb/chunks/base.pxd
+++ b/tempsdb/chunks/base.pxd
@@ -20,7 +20,7 @@ cdef class Chunk:
         readonly unsigned long page_size
         object file, mmap, file_lock_object
         bint closed
-    cpdef object iterate_indices(self, unsigned long starting_entry, unsigned long stopping_entry)
+    cpdef object iterate_indices(self, unsigned int starting_entry, unsigned int stopping_entry)
     cpdef int close(self, bint force=*) except -1
     cdef tuple get_piece_at(self, unsigned int index)
     cdef int sync(self) except -1
diff --git a/tempsdb/chunks/base.pyx b/tempsdb/chunks/base.pyx
index 42ee38a..b86c553 100644
--- a/tempsdb/chunks/base.pyx
+++ b/tempsdb/chunks/base.pyx
@@ -227,9 +227,8 @@ cdef class Chunk:
         """
         if index > self.entries:
             raise ValueError('index too large')
-        cdef:
-            unsigned long offset = HEADER_SIZE + TIMESTAMP_SIZE + index * (self.block_size + TIMESTAMP_SIZE) + byte_index
-        return self.mmap[offset]
+        cdef unsigned long ofs = HEADER_SIZE + TIMESTAMP_SIZE + index * (self.block_size + TIMESTAMP_SIZE) + byte_index
+        return ord(self.mmap[ofs])
 
     cpdef bytes get_slice_of_piece_starting_at(self, unsigned int index, int start):
         """
@@ -361,7 +360,7 @@ cdef class Chunk:
         """
         raise NotImplementedError('Abstract method!')
 
-    cpdef object iterate_indices(self, unsigned long starting_entry, unsigned long stopping_entry):
+    cpdef object iterate_indices(self, unsigned int starting_entry, unsigned int stopping_entry):
         """
         Return a partial iterator starting at starting_entry and ending at stopping_entry (exclusive).
         
@@ -372,8 +371,8 @@ cdef class Chunk:
         """
         return self._iterate(starting_entry, stopping_entry)
 
-    def _iterate(self, starting_entry: int, stopping_entry: int):
-        cdef int i
+    def _iterate(self, unsigned int starting_entry, unsigned int stopping_entry):
+        cdef unsigned int i
         for i in range(starting_entry, stopping_entry):
             yield self.get_piece_at(i)
 
diff --git a/tests/test_database.py b/tests/test_database.py
index d1acbe7..30cd1c0 100644
--- a/tests/test_database.py
+++ b/tests/test_database.py
@@ -1,6 +1,6 @@
 import unittest
 
-from tempsdb.database import create_database
+from tempsdb.database import create_database, Database
 from tempsdb.exceptions import DoesNotExist
 
 
@@ -29,6 +29,9 @@ class TestDatabase(unittest.TestCase):
         self.db.close_all_open_series()
         self.assertEqual(len(self.db.get_open_series()), 0)
 
+    def test_does_not_exist(self):
+        self.assertRaises(DoesNotExist, lambda: Database('does-not-exist'))
+
     def test_add_series(self):
         ser = self.db.create_series('hello-world', 1, 10)
         ser.append(10, b'\x00')
diff --git a/tests/test_varlen.py b/tests/test_varlen.py
index 4ccb9b6..f028b46 100644
--- a/tests/test_varlen.py
+++ b/tests/test_varlen.py
@@ -34,6 +34,12 @@ class TestVarlen(unittest.TestCase):
         with varlen.iterate_range(0, 20) as iterator:
             ve = iterator.get_next()
             while ve is not None:
+                self.assertTrue(ve.startswith(b'test '))
+                # self.assertTrue(ve.endswith(b'skarabeusza'))
+                self.assertEqual(ve.get_byte_at(3), ord('t'))
+                self.assertFalse(ve.startswith(b'tost'))
+                self.assertTrue(ve.slice(0, 4), b'test')
+                # self.assertFalse(ve.endswith(b'skerabeusza'))
                 self.assertGreater(ve, b'tes')
                 self.assertLess(ve, b'tez')
                 self.assertGreaterEqual(ve, b'tes')
@@ -41,11 +47,7 @@ class TestVarlen(unittest.TestCase):
                 self.assertLessEqual(ve, b'tez')
                 hash(ve)
                 self.assertNotEqual(ve, b'test')
-                self.assertTrue(ve.slice(0, 4), b'test')
-                self.assertTrue(ve.startswith(b'test '))
-                self.assertTrue(ve.endswith(b'skarabeusza'))
-                self.assertFalse(ve.startswith(b'tost'))
-                self.assertFalse(ve.endswith(b'skerabeusza'))
+
                 ve = iterator.get_next()
 
     def test_varlen_gzip(self):
-- 
GitLab