Browse Source

Fill out coverage for server/nilmdb.py and remove dead code

tags/nilmdb-2.0.0
Jim Paris 1 year ago
parent
commit
9acf99ff25
2 changed files with 50 additions and 13 deletions
  1. +5
    -13
      nilmdb/server/nilmdb.py
  2. +45
    -0
      tests/test_nilmdb.py

+ 5
- 13
nilmdb/server/nilmdb.py View File

@@ -122,7 +122,7 @@ class NilmDB(object):
self.con = sqlite3.connect(sqlfilename, check_same_thread = True)
try:
self._sql_schema_update()
except Exception: # pragma: no cover
except Exception:
self.data.close()
raise

@@ -156,18 +156,18 @@ class NilmDB(object):
oldversion = version

while True:
if version not in _sql_schema_updates: # pragma: no cover
if version not in _sql_schema_updates:
raise Exception(self.basepath + ": unknown database version "
+ str(version))
update = _sql_schema_updates[version]
if "error" in update: # pragma: no cover
if "error" in update:
raise Exception(self.basepath + ": can't use database version "
+ str(version) + ": " + update["error"])
if update["next"] is None:
break
cur.executescript(update["sql"])
version = update["next"]
if self.verbose: # pragma: no cover
if self.verbose:
printf("Database schema updated to %d\n", version)

if version != oldversion:
@@ -198,7 +198,7 @@ class NilmDB(object):
iset += DBInterval(start_time, end_time,
start_time, end_time,
start_pos, end_pos)
except IntervalError: # pragma: no cover
except IntervalError:
raise NilmDBError("unexpected overlap in ranges table!")

return iset
@@ -225,10 +225,6 @@ class NilmDB(object):
# Load this stream's intervals
iset = self._get_intervals(stream_id)

# Check for overlap
if iset.intersects(interval): # pragma: no cover (gets caught earlier)
raise NilmDBError("new interval overlaps existing data")

# Check for adjacency. If there's a stream in the database
# that ends exactly when this one starts, and the database
# rows match up, we can make one interval that covers the
@@ -271,10 +267,6 @@ class NilmDB(object):
original: original DBInterval; must be already present in DB
to_remove: DBInterval to remove; must be subset of 'original'
"""
# Just return if we have nothing to remove
if remove.start == remove.end: # pragma: no cover
return

# Load this stream's intervals
iset = self._get_intervals(stream_id)



+ 45
- 0
tests/test_nilmdb.py View File

@@ -15,8 +15,10 @@ import io
import time
import requests
import socket
import sqlite3

from nilmdb.utils import serializer_proxy
from nilmdb.server.interval import Interval

testdb = "tests/testdb"

@@ -46,10 +48,53 @@ class Test00Nilmdb(object): # named 00 so it runs first
db.close()
db.close()

def test_error_cases(self):
# Test some misc error cases to get better code coverage

with assert_raises(OSError) as e:
nilmdb.server.NilmDB("/dev/null/bogus")
in_("can't create tree", str(e.exception))

# Version upgrades
con = sqlite3.connect(os.path.join(testdb, "data.sql"))
con.execute("PRAGMA user_version = 2");
con.close()
with assert_raises(Exception) as e:
db = nilmdb.server.NilmDB(testdb)
in_("can't use database version 2", str(e.exception))

con = sqlite3.connect(os.path.join(testdb, "data.sql"))
con.execute("PRAGMA user_version = -1234");
con.close()
with assert_raises(Exception) as e:
db = nilmdb.server.NilmDB(testdb)
in_("unknown database version -1234", str(e.exception))

recursive_unlink(testdb)

nilmdb.server.NilmDB.verbose = 1
(old, sys.stdout) = (sys.stdout, io.StringIO())
db = nilmdb.server.NilmDB(testdb)
(output, sys.stdout) = (sys.stdout.getvalue(), old)
nilmdb.server.NilmDB.verbose = 0
db.close()
in_("Database schema updated to 1", output)

# Corrupted database (bad ranges)
recursive_unlink(testdb)
db = nilmdb.server.NilmDB(testdb)
db.con.executescript("""
INSERT INTO streams VALUES (1, "/test", "int32_1");
INSERT INTO ranges VALUES (1, 100, 200, 100, 200);
INSERT INTO ranges VALUES (1, 150, 250, 150, 250);
""")
db.close()
db = nilmdb.server.NilmDB(testdb)
with assert_raises(nilmdb.server.NilmDBError):
db.stream_intervals("/test")
db.close()
recursive_unlink(testdb)

def test_stream(self):
db = nilmdb.server.NilmDB(testdb)
eq_(db.stream_list(), [])


Loading…
Cancel
Save