Commits

Chris Mutel committed 594de03

Add tests for utils

  • Participants
  • Parent commits e12701f

Comments (0)

Files changed (5)

bw2data/method.py

 
     """
     def __init__(self, method, *args, **kwargs):
-        self.method = method
+        self.method = tuple(method)
         if self.method not in methods and not \
                 getattr(config, "dont_warn", False):
             warnings.warn("\n\t%s not a currently installed method" % (
             * *name* (tuple, optional): Name of the new method.
 
         """
-        name = name or self.method[:-1] + ("Copy of " + self.method[-1],)
+        name = tuple(name) or self.method[:-1] + ("Copy of " + \
+            self.method[-1],)
         new_method = Method(name)
         metadata = copy(methods[self.method])
         del metadata["abbreviation"]

bw2data/serialization.py

         return sorted(self.data.keys())
 
     def __getitem__(self, key):
+        if isinstance(key, list):
+            key = tuple(key)
         return self.data[key]
 
     def __setitem__(self, key, value):

bw2data/tests/__init__.py

 from .base import BW2DataTest
 from .database import DatabaseTest
 from .geo import GeoTest
+from .utils import UtilsTest

bw2data/tests/utils.py

+# -*- coding: utf-8 -*-
+from . import BW2DataTest
+from .. import Database, Method, methods
+from fixtures import biosphere
+from ..utils import natural_sort, random_string, combine_methods
+
+
+class UtilsTest(BW2DataTest):
+    def test_natural_sort(self):
+        data = ["s100", "s2", "s1"]
+        self.assertEqual(["s1", "s2", "s100"], natural_sort(data))
+
+    def test_random_string(self):
+        s = random_string(10)
+        self.assertEqual(len(s), 10)
+        self.assertTrue(isinstance(s, str))
+
+    def test_combine_methods(self):
+        d = Database("biosphere")
+        d.register("Tests", [], len(biosphere))
+        d.write(biosphere)
+        m1 = Method(["test method 1"])
+        m1.register("p", 2)
+        m1.write({
+            ("biosphere", 1): 1,
+            ("biosphere", 2): 2
+            })
+        m2 = Method(["test method 2"])
+        m2.register("p", 1)
+        m2.write({
+            ("biosphere", 2): 10
+            })
+        combine_methods(["test method 3"], ["test method 1"],
+            ["test method 2"])
+        cm = Method(["test method 3"])
+        self.assertEqual(cm.load(), {
+            ("biosphere", 1): 1,
+            ("biosphere", 2): 12
+            })
+        self.assertEqual(methods[["test method 3"]]["unit"], "p")
 def combine_methods(name, *ms):
     from . import Method, methods
     data = {}
-    units = set([methods[x]["unit"] for x in ms])
+    units = set([methods[tuple(x)]["unit"] for x in ms])
     for m in ms:
         for key, amount in Method(m).load().iteritems():
             data[key] = data.get(key, 0) + amount