Commits

Chris Mutel committed b596a64

More database tests passing

Comments (0)

Files changed (3)

                     e["input"] = (new_name, e["input"][1])
             return obj
 
-        return dict([((new_name, k[1]), self.relabel_exchanges(v, new_name)) \
+        return dict([((new_name, k[1]), relabel_exchanges(v, new_name)) \
             for k, v in data.iteritems()])
 
     def copy(self, name):
         assert version in [x[0] for x in self.versions()], "Version not found"
         self.backup()
         databases[self.database]["version"] = version
+        if config.p.get("use_cache", False) and self.database in config.cache:
+            config.cache[self.database] = self.load(version)
         self.process(version)
 
     def register(self, format, depends, num_processes):

bw2data/tests/database.py

 # -*- coding: utf-8 -*-
 from . import BW2DataTest
 from .. import Database, databases
+from ..errors import UnknownObject
 from fixtures import food, biosphere
 
 
 class DatabaseTest(BW2DataTest):
-    def extra_setup(self):
+    def test_setup(self):
         d = Database("biosphere")
         d.register("Tests", [], len(biosphere))
         d.write(biosphere)
         d = Database("food")
         d.register("Tests", ["biosphere"], len(food))
         d.write(food)
-
-    def test_setup(self):
         self.assertEqual(len(databases), 2)
 
     def test_copy(self):
-        pass
+        d = Database("food")
+        d.register("Tests", ["biosphere"], len(food))
+        d.write(food)
+        with self.assertRaises(AssertionError):
+            d.copy("food")
+        d.copy("repas")
+        self.assertTrue("repas" in databases.list)
 
     def test_relabel_data(self):
-        pass
+        old_data = {
+            ("old and boring", 1): {
+                "exchanges": [{"input": ("old and boring", 42), "amount": 1.0}]
+            },
+            ("old and boring", 2): {
+                "exchanges": [{"input": ("old and boring", 1), "amount": 4.0}]
+            }
+        }
+        shiny_new = {
+            ("shiny new", 1): {
+                "exchanges": [{"input": ("old and boring", 42), "amount": 1.0}]
+            },
+            ("shiny new", 2): {
+                "exchanges": [{"input": ("shiny new", 1), "amount": 4.0}]
+            }
+        }
+        db = Database("foo")
+        self.assertEqual(shiny_new, db.relabel_data(old_data, "shiny new"))
 
     def test_revert(self):
-        pass
+        d = Database("biosphere")
+        d.register("Tests", [], len(biosphere))
+        d.write(biosphere)
+        d = Database("food")
+        d.register("Tests", ["biosphere"], len(food))
+        d.write(food)
+        d.write({})
+        self.assertEqual(databases["food"]["version"], 2)
+        self.assertEqual(Database("food").load(), {})
+        d.revert(1)
+        self.assertEqual(databases["food"]["version"], 1)
+        self.assertEqual(Database("food").load(), food)
+        with self.assertRaises(AssertionError):
+            d.revert(10)
 
     def test_register(self):
         pass
 
+    def test_deregister(self):
+        d = Database("food")
+        d.register("Tests", ["biosphere"], len(food))
+        self.assertTrue("food" in databases)
+        d.deregister()
+        self.assertTrue("food" not in databases)
+
     def test_load(self):
-        pass
+        d = Database("food")
+        d.register("Tests", ["biosphere"], len(food))
+        d.write(food)
+        data = Database("food").load()
+        self.assertEqual(food, data)
 
-    def test_versions(self):
-        pass
+    def test_write_bumps_version_number(self):
+        d = Database("food")
+        d.register("Tests", ["biosphere"], len(food))
+        d.write(food)
+        self.assertEqual(databases["food"]["version"], 1)
+        d.write(food)
+        self.assertEqual(databases["food"]["version"], 2)
 
-# Test that database.write revises number of processes correctly
+    def test_write_unregistered_database_raises_error(self):
+        d = Database("food")
+        with self.assertRaises(UnknownObject):
+            d.write(food)

bw2data/tests/sparse.py

 import numpy as np
 import scipy.sparse
 import unittest
+import warnings
 from ..proxies.sparse import SparseMatrixProxy, CompressedSparseMatrixProxy
 
 
         self.assertEqual(mat.nnz, 6)
         mat[11, 21] = 3
         self.assertEqual(mat[11, 21], 3)
-        self.assertRaises(NotImplementedError, mat.__getitem__, (slice(None),
-            21))
-        self.assertRaises(NotImplementedError, mat.__setitem__, (slice(None),
-            21), 1)
+        with self.assertRaises(NotImplementedError):
+            mat[slice(None), 21]
+        with self.assertRaises(NotImplementedError):
+            mat[slice(None), 21] = 1
 
     def test_compressed_sparse_matrix_proxy(self):
         c = scipy.sparse.lil_matrix((3, 3))
             c[11, :].todense(),
             np.array((1, 0, 7))
             ))
-        c[11, 21] = 3
-        assert c[11, 21] == 3
+        with warnings.catch_warnings():
+            warnings.simplefilter("ignore")
+            c[11, 21] = 3
+            self.assertEqual(c[11, 21], 3)