brightway2-data / bw2data / tests / ia.py

# -*- coding: utf-8 -*-
from . import BW2DataTest
from .. import Updates, config, Database, Method, mapping, geomapping
from ..ia_data_store import abbreviate, ImpactAssessmentDataStore as IADS
from ..serialization import CompoundJSONDict
import hashlib
import os
try:
    import cPickle as pickle
except ImportError:
    import pickle


class Metadata(CompoundJSONDict):
    _filename = "mock-meta.json"

metadata = Metadata()


class MockIADS(IADS):
    """Mock IADS for testing"""
    metadata = metadata
    validator = lambda x: True
    dtype_fields = []

    def process_data(self, row):
        return (), 0


class IADSTest(BW2DataTest):
    def setUp(self):
        super(IADSTest, self).setUp()
        metadata.__init__()

    def test_unicode(self):
        iads = MockIADS(("foo", "bar"))
        self.assertEqual(
            iads.__unicode__(),
            u"Brightway2 MockIADS: foo: bar"
        )

    def test_abbreviate(self):
        self.assertEqual(
            abbreviate(("foo", "bar")),
            u"foob-%s" % hashlib.md5("foo-bar").hexdigest()
        )

    def test_copy_no_name(self):
        iads = MockIADS(("foo", "bar"))
        iads.register(paris="France")
        iads.write({1:2})
        new_one = iads.copy()
        new_name = ("foo", "Copy of bar")
        self.assertEqual(new_one.name, new_name)
        self.assertTrue(new_name in metadata)
        self.assertEqual(new_one.load(), {1:2})
        self.assertEqual(
            metadata[("foo", "bar")]["paris"],
            metadata[new_name]["paris"]
        )
        self.assertFalse(metadata[("foo", "bar")] == metadata[new_name])

    def test_copy_with_name(self):
        iads = MockIADS(("foo", "bar"))
        iads.register(paris="France")
        iads.write({1:2})
        new_name = ("bar", "foo")
        new_one = iads.copy(new_name)
        self.assertEqual(new_one.name, new_name)
        self.assertTrue(new_name in metadata)
        self.assertEqual(new_one.load(), {1:2})
        self.assertEqual(
            metadata[("foo", "bar")]["paris"],
            metadata[new_name]["paris"]
        )
        self.assertFalse(metadata[("foo", "bar")] == metadata[new_name])

    def test_register_adds_abbreviation(self):
        name = ("foo", "bar")
        self.assertFalse(name in metadata)
        iads = MockIADS(name)
        iads.register()
        self.assertEqual(metadata[name].keys(), ['abbreviation'])


class MethodTest(BW2DataTest):
    def test_write_adds_to_mapping(self):
        Database("testy").register()
        method_data = [
            [("testy", "A"), 1],
            [("testy", "B"), 1],
        ]
        method = Method(("a", "method"))
        method.register()
        method.write(method_data)
        self.assertTrue(("testy", "A") in mapping)
        self.assertTrue(("testy", "B") in mapping)
        method_data = [
            [("testy", "A"), 1, "CH"],
            [("testy", "B"), 1, "DE"],
        ]
        method.write(method_data)
        self.assertTrue("CH" in geomapping)
        self.assertTrue("DE" in geomapping)

    def test_processed_array(self):
        method = Method(("a", "method"))
        method.register()
        method.write([])
        method.process()
        fp = os.path.join(config.dir, u"processed", method.filename + u".pickle")
        array = pickle.load(open(fp, "rb"))

        fieldnames = {'flow', 'geo', 'row', 'col'}
        self.assertFalse(fieldnames.difference(set(array.dtype.names)))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.