Пример #1
0
    def test_set_separate_dimension_args(self):
        # Tests the case where .set() is called correctly.  Use the
        # way of calling .set() where we pass in every dimension
        # separately. Verify the data is stored and can be retrieved
        # using .get().
        doc = Entrypoint("CutSheet", self.taxonomy)

        # Write a TypeOfDevice and a DeviceCost:

        doc.set(
            "solar:TypeOfDevice",
            "Module",
            duration="forever",
            ProductIdentifierAxis="placeholder",
            TestConditionAxis="placeholder",
        )
        doc.set("solar:DeviceCost",
                100,
                instant=datetime.now(),
                ProductIdentifierAxis="placeholder",
                TestConditionAxis="placeholder",
                unit="dollars")

        self.assertEqual(doc.get("solar:TypeOfDevice", {}), "Module")
        self.assertEqual(doc.get("solar:DeviceCost", {}), 100)
Пример #2
0
    def test_facts_stored_with_context(self):
        # Test we can store 2 facts of the same concept but with different
        # contexts, and pull them both back out.
        doc = Entrypoint("CutSheet", self.taxonomy)
        concept = "solar:InverterCutSheetNotes"

        ctx_jan = Context(
            duration={
                "start": datetime(year=2018, month=1, day=1),
                "end": datetime(year=2018, month=2, day=1)
            },
            entity="JUPITER",
            ProductIdentifierAxis="placeholder",
            TestConditionAxis="solar:StandardTestConditionMember")
        ctx_feb = Context(
            duration={
                "start": datetime(year=2018, month=2, day=1),
                "end": datetime(year=2018, month=3, day=1)
            },
            entity="JUPITER",
            ProductIdentifierAxis="placeholder",
            TestConditionAxis="solar:StandardTestConditionMember")

        doc.set(concept, "Jan Value", context=ctx_jan)
        doc.set(concept, "Feb Value", context=ctx_feb)

        jan_fact = doc.get(concept, context=ctx_jan)
        feb_fact = doc.get(concept, context=ctx_feb)

        self.assertEqual(jan_fact.value, "Jan Value")
        self.assertEqual(feb_fact.value, "Feb Value")
Пример #3
0
    def test_set_context_arg(self):
        # Tests the case where .set() is called correctly, using
        # the way of calling .set() where we pass in a Context
        # object. Verify the data is stored and can be retrieved
        # using .get().
        doc = Entrypoint("CutSheet", self.taxonomy)
        ctx = Context(duration="forever",
                      entity="JUPITER",
                      ProductIdentifierAxis="placeholder",
                      TestConditionAxis="solar:StandardTestConditionMember")
        doc.set("solar:TypeOfDevice", "ModuleMember", context=ctx)

        now = datetime.now(),
        ctx = Context(instant=now,
                      entity="JUPITER",
                      ProductIdentifierAxis="placeholder",
                      TestConditionAxis="solar:StandardTestConditionMember")
        doc.set("solar:DeviceCost", 100, context=ctx, unit_name="USD")

        # Get the data bacK:
        typeFact = doc.get(
            "solar:TypeOfDevice",
            Context(duration="forever",
                    entity="JUPITER",
                    ProductIdentifierAxis="placeholder",
                    TestConditionAxis="solar:StandardTestConditionMember"))
        self.assertEqual(typeFact.value, "ModuleMember")
        costFact = doc.get(
            "solar:DeviceCost",
            Context(instant=now,
                    entity="JUPITER",
                    ProductIdentifierAxis="placeholder",
                    TestConditionAxis="solar:StandardTestConditionMember"))
        self.assertEqual(costFact.value, 100)
Пример #4
0
    def test_set_separate_dimension_args(self):
        # Tests the case where .set() is called correctly.  Use the
        # way of calling .set() where we pass in every dimension
        # separately. Verify the data is stored and can be retrieved
        # using .get().
        doc = Entrypoint("CutSheet", self.taxonomy)

        # Write a TypeOfDevice and a DeviceCost:

        doc.set("solar:TypeOfDevice",
                "ModuleMember",
                duration="forever",
                ProductIdentifierAxis="placeholder",
                TestConditionAxis="solar:StandardTestConditionMember")
        now = datetime.now()
        doc.set("solar:DeviceCost",
                100,
                instant=now,
                ProductIdentifierAxis="placeholder",
                TestConditionAxis="solar:StandardTestConditionMember",
                unit_name="USD")

        typeFact = doc.get(
            "solar:TypeOfDevice",
            Context(duration="forever",
                    ProductIdentifierAxis="placeholder",
                    TestConditionAxis="solar:StandardTestConditionMember"))
        self.assertEqual(typeFact.value, "ModuleMember")
        costFact = doc.get(
            "solar:DeviceCost",
            Context(instant=now,
                    ProductIdentifierAxis="placeholder",
                    TestConditionAxis="solar:StandardTestConditionMember"))
        self.assertEqual(costFact.value, 100)
        self.assertEqual(costFact.unit, "USD")
Пример #5
0
    def test_conversion_to_json(self):
        doc = Entrypoint("CutSheet", self.taxonomy)
        doc.set(
            "solar:TypeOfDevice",
            "ModuleMember",
            entity="JUPITER",
            duration="forever",
            ProductIdentifierAxis="placeholder",
            TestConditionAxis="solar:StandardTestConditionMember",
        )
        now = datetime.now()
        doc.set("solar:DeviceCost",
                100,
                entity="JUPITER",
                instant=now,
                ProductIdentifierAxis="placeholder",
                TestConditionAxis="solar:StandardTestConditionMember",
                unit_name="USD")
        jsonstring = doc.to_JSON_string()

        root = json.loads(jsonstring)

        # should have 2 facts:
        self.assertEqual(len(root['facts']), 2)

        # each should have expected 'value' and 'aspects':
        typeFacts = [
            x for x in root['facts']
            if x['aspects']['xbrl:concept'] == 'solar:TypeOfDevice'
        ]
        self.assertEqual(len(typeFacts), 1)
        typeFact = typeFacts[0]
        self.assertEqual(typeFact['value'], "ModuleMember")
        self.assertEqual(typeFact['aspects']['solar:ProductIdentifierAxis'],
                         'placeholder')
        self.assertEqual(typeFact['aspects']['solar:TestConditionAxis'],
                         "solar:StandardTestConditionMember")
        self.assertEqual(typeFact['aspects']['xbrl:entity'], 'JUPITER')
        self.assertEqual(typeFact['aspects']['xbrl:period'], 'forever')
        # TODO if there's no unit is it correct to write 'xbrl:unit':'None' or to leave out
        # xbrl:unit?  Currently assuming we leave it out:
        self.assertNotIn("xbrl:unit", typeFact["aspects"])

        costFacts = [
            x for x in root['facts']
            if x['aspects']['xbrl:concept'] == 'solar:DeviceCost'
        ]
        self.assertEqual(len(costFacts), 1)

        costFact = costFacts[0]
        self.assertEqual(costFact['value'], '100')
        self.assertEqual(costFact['aspects']['solar:ProductIdentifierAxis'],
                         'placeholder')
        self.assertEqual(costFact['aspects']['solar:TestConditionAxis'],
                         "solar:StandardTestConditionMember")
        self.assertEqual(costFact['aspects']['xbrl:entity'], 'JUPITER')
        self.assertEqual(costFact['aspects']['xbrl:instant'],
                         now.strftime("%Y-%m-%d"))
        self.assertEqual(costFact['aspects']['xbrl:unit'], 'USD')
Пример #6
0
    def test_set_raises_exception(self):
        # Tests the case where .set() is called incorrectly. It should
        # raise exceptions if required information is missing.
        doc = Entrypoint("CutSheet", self.taxonomy)
        with self.assertRaises(Exception):
            doc.set("solar:TypeOfDevice", "ModuleMember", {})

        with self.assertRaises(Exception):
            doc.set("solar:DeviceCost", 100, {})
Пример #7
0
    def test_tableless_facts(self):
        # Some entry points, like MonthlyOperatingReport, seem to have concepts
        # in them that are not part of any table:
        doc = Entrypoint("MonthlyOperatingReport", self.taxonomy)

        doc.set("solar:MonthlyOperatingReportEffectiveDate",
                date(year=2018, month=6, day=1),
                entity="JUPITER",
                duration="forever")

        fact = doc.get("solar:MonthlyOperatingReportEffectiveDate",
                       Context(entity="JUPITER", duration="forever"))

        self.assertEqual(fact.value, date(year=2018, month=6, day=1))
Пример #8
0
    def test_set_context_arg(self):
        # Tests the case where .set() is called correctly, using
        # the way of calling .set() where we pass in a Context
        # object. Verify the data is stored and can be retrieved
        # using .get().
        doc = Entrypoint("CutSheet", self.taxonomy)
        ctx = Context(duration="forever",
                      entity="JUPITER",
                      ProductIdentifierAxis="placeholder",
                      TestConditionAxis="placeholder")
        doc.set("solar:TypeOfDevice", "Module", context=ctx)

        ctx = Context(instant=datetime.now(),
                      entity="JUPITER",
                      ProductIdentifierAxis="placeholder",
                      TestConditionAxis="placeholder")
        doc.set("solar:DeviceCost", 100, context=ctx, unit="dollars")
Пример #9
0
    def test_reject_invalid_datatype(self):
        doc = Entrypoint("CutSheet", self.taxonomy)
        with self.assertRaises(Exception):
            # A non-integer is given, this should fail:
            doc.set("solar:TrackerNumberOfControllers",
                    0.5,
                    entity="JUPITER",
                    duration="forever",
                    ProductIdentifierAxis="placeholder",
                    TestConditionAxis="solar:StandardTestConditionMember")

        with self.assertRaises(Exception):
            # A string that can't be parsed into an integer should fail:
            doc.set("solar:TrackerNumberOfControllers",
                    "abcdefg",
                    entity="JUPITER",
                    duration="forever",
                    ProductIdentifierAxis="placeholder",
                    TestConditionAxis="solar:StandardTestConditionMember")

        # But a string that can be parsed into an integer should succeed:
        doc.set("solar:TrackerNumberOfControllers",
                "2",
                entity="JUPITER",
                duration="forever",
                ProductIdentifierAxis="placeholder",
                TestConditionAxis="solar:StandardTestConditionMember")
Пример #10
0
    def test_set_and_get(self):
        # Tests the case where .set() is called correctly. Verify the
        # data is stored and can be retrieved using .get().
        doc = Entrypoint("CutSheet")

        # Write a TypeOfDevice and a DeviceCost:

        doc.set(
            "solar:TypeOfDevice", "Module", {
                "duration": "forever",
                "solar:ProductIdentifierAxis": "placeholder",
                "solar:TestConditionAxis": "placeholder"
            })
        doc.set(
            "solar:DeviceCost", 100, {
                "instant": datetime.now(),
                "solar:ProductIdentifierAxis": "placeholder",
                "solar:TestConditionAxis": "placeholder"
            })

        self.assertEqual(doc.get("solar:TypeOfDevice", {}), "Module")
        self.assertEqual(doc.get("solar:DeviceCost", {}), 100)
Пример #11
0
    def test_reject_missing_or_invalid_units(self):
        # issue #28
        # -- reject attempt to set a fact if it doesn't have a unit and the unit is required
        # -- reject attempt to set a fact using a unit name that doesn't match taxonomy
        # -- reject attempt to set a fact using a unit that is the wrong type
        now = datetime.now()
        doc = Entrypoint("CutSheet", self.taxonomy)
        with self.assertRaises(Exception):
            # Unit is required but not provided, so this should fail:
            doc.set("solar:DeviceCost",
                    100,
                    entity="JUPITER",
                    instant=now,
                    ProductIdentifierAxis="placeholder",
                    TestConditionAxis="solar:StandardTestConditionMember")

        with self.assertRaises(Exception):
            # Zorkmids is not a real unit, so this should fail:
            doc.set("solar:DeviceCost",
                    100,
                    entity="JUPITER",
                    instant=now,
                    ProductIdentifierAxis="placeholder",
                    TestConditionAxis="solar:StandardTestConditionMember",
                    unit_name="zorkmids")

        with self.assertRaises(Exception):
            # kWh is a real unit but the wrong type, so this should fail:
            doc.set("solar:DeviceCost",
                    100,
                    entity="JUPITER",
                    instant=now,
                    ProductIdentifierAxis="placeholder",
                    TestConditionAxis="solar:StandardTestConditionMember",
                    unit_name="kWh")

        # USD is a valid unit, so this should succeed:
        doc.set("solar:DeviceCost",
                100,
                entity="JUPITER",
                instant=now,
                ProductIdentifierAxis="placeholder",
                TestConditionAxis="solar:StandardTestConditionMember",
                unit_name="USD")
Пример #12
0
    def test_set_default_context_values(self):
        # Test setting default values, for example something like:
        doc = Entrypoint("CutSheet", self.taxonomy)
        now = datetime.now()
        doc.set_default_context({
            "entity": "JUPITER",
            "solar:TestConditionAxis": "solar:StandardTestConditionMember",
            PeriodType.instant: now,
            PeriodType.duration: "forever"
        })
        # Could also support setting default unit, even though that's not part of context:

        # If we set a fact that wants an instant context, it should use 'now':
        doc.set("solar:DeviceCost",
                "100",
                unit_name="USD",
                ProductIdentifierAxis="placeholder")
        # This would normally raise an exception because it's missing instant, entity, and
        # TestConditionAxis. But we set defaults for those, so they should be filled in:
        fact = doc.get(
            "solar:DeviceCost",
            Context(ProductIdentifierAxis="placeholder",
                    TestConditionAxis="solar:StandardTestConditionMember",
                    entity="JUPITER",
                    instant=now))
        self.assertEqual(fact.value, "100")
        self.assertEqual(fact.unit, "USD")
        self.assertEqual(fact.context.entity, "JUPITER")
        self.assertEqual(fact.context.instant, now)

        # TODO test method of calling set() where we pass in Context object.

        # If we set a fact that wants a duration context, it should use jan 1 - jan 31:
        doc.set("solar:ModuleNameplateCapacity",
                "0.3",
                unit_name="W",
                ProductIdentifierAxis="placeholder")
        # Would normally raise an exception because missing duration, entity, and
        # TestConditionAxis. But we set defaults for those, so they should be filled in:
        fact = doc.get(
            "solar:ModuleNameplateCapacity",
            Context(ProductIdentifierAxis="placeholder",
                    TestConditionAxis="solar:StandardTestConditionMember",
                    entity="JUPITER",
                    duration="forever"))
        self.assertEqual(fact.value, "0.3")
        self.assertEqual(fact.unit, "W")
        self.assertEqual(fact.context.entity, "JUPITER")
        self.assertEqual(fact.context.duration, "forever")

        # Try setting ALL the fields in set_default_context and then pass in NO context fields,
        # that should work too:
        doc.set_default_context({
            "entity": "JUPITER",
            "solar:TestConditionAxis": "solar:StandardTestConditionMember",
            PeriodType.instant: now,
            "solar:ProductIdentifierAxis": "placeholder"
        })
        doc.set("solar:DeviceCost", "99", unit_name="USD")
        fact = doc.get(
            "solar:DeviceCost",
            Context(ProductIdentifierAxis="placeholder",
                    TestConditionAxis="solar:StandardTestConditionMember",
                    entity="JUPITER",
                    instant=now))
        self.assertEqual(fact.value, "99")
Пример #13
0
    def test_conversion_to_xml(self):
        doc = Entrypoint("CutSheet", self.taxonomy)
        doc.set("solar:TypeOfDevice",
                "ModuleMember",
                entity="JUPITER",
                duration="forever",
                ProductIdentifierAxis="placeholder",
                TestConditionAxis="solar:StandardTestConditionMember")
        now = datetime.now()
        doc.set("solar:DeviceCost",
                100,
                entity="JUPITER",
                instant=now,
                ProductIdentifierAxis="placeholder",
                TestConditionAxis="solar:StandardTestConditionMember",
                unit_name="USD")
        xml = doc.to_XML_string()
        root = etree.fromstring(xml)

        self.assertEqual(len(root.getchildren()), 6)
        # top-level xml should have child <link:schemaRef>, one <unit>, two <context>s and two <fact>s.
        schemaRef = root.getchildren()[0]
        self.assertEqual(schemaRef.tag,
                         "{http://www.xbrl.org/2003/linkbase}schemaRef")

        # expect to see 2 contexts with id "solar:CutSheetDetailsTable_0" and "solar:CutSheetDetailsTable_1"
        contexts = root.findall('{http://www.xbrl.org/2003/instance}context')
        self.assertEqual(len(contexts), 2)
        for context in contexts:
            # both should have entity tag containing identifier containing text JUPITER
            self.assertTrue( context.attrib["id"] == 'solar:CutSheetDetailsTable_0' or \
                             context.attrib["id"] == 'solar:CutSheetDetailsTable_1' )

            entity = context.find("{http://www.xbrl.org/2003/instance}entity")
            identifier = entity.find(
                "{http://www.xbrl.org/2003/instance}identifier")
            self.assertEqual(identifier.text, "JUPITER")

            # both should have segment containing xbrldi:explicitMember dimension="<axis name>"
            # containing text "placeholder"
            # (wait i have segment inside of entity?  is that correct?)
            segment = entity.find("{http://www.xbrl.org/2003/instance}segment")

            axes = segment.findall("{http://xbrl.org/2006/xbrldi}typedMember")
            axis_names = [x.attrib["dimension"] for x in axes]

            self._check_arrays_equivalent(
                axis_names,
                ['solar:ProductIdentifierAxis', 'solar:TestConditionAxis'])
            for axis in axes:
                if axis.attrib["dimension"] == 'solar:ProductIdentifierAxis':
                    self.assertEqual(
                        axis.getchildren()[0].tag,
                        "{http://xbrl.us/Solar/v1.2/2018-03-31/solar}ProductIdentifierDomain"
                    )
                    self.assertEqual(axis.getchildren()[0].text, "placeholder")
                elif axis.attrib["dimension"] == 'solar:TestConditionsAxis':
                    self.assertEqual(
                        axis.getchildren()[0].tag,
                        "{http://xbrl.us/Solar/v1.2/2018-03-31/solar}TestConditionDomain"
                    )
                    self.assertEqual(axis.getchildren()[0].text,
                                     "solar:StandardTestConditionMember")

            # one should have period containing <forever/> other should have period containing <instant> containing today's date.
            period = context.find("{http://www.xbrl.org/2003/instance}period")
            tag = period.getchildren()[0].tag
            self.assertTrue(tag == "{http://www.xbrl.org/2003/instance}instant" or\
                            tag == "{http://www.xbrl.org/2003/instance}forever")

        # Expect one unit tag with id=USD containing <measure>units:USD</measure>
        unit_tag = root.findall("{http://www.xbrl.org/2003/instance}unit")
        self.assertEqual(len(unit_tag), 1)
        self.assertEqual(unit_tag[0].attrib["id"], "USD")
        self.assertEqual(unit_tag[0].getchildren()[0].text, "units:USD")

        # Expect to see two facts solar:DeviceCost and solar:TypeOfDevice,
        # each containing text of the fact value
        costFact = root.find(
            '{http://xbrl.us/Solar/v1.2/2018-03-31/solar}DeviceCost')
        typeFact = root.find(
            '{http://xbrl.us/Solar/v1.2/2018-03-31/solar}TypeOfDevice')
        self.assertEqual(costFact.text, "100")
        self.assertEqual(typeFact.text, "ModuleMember")
        # They should have contextRef and (in the case of cost) unitRef attributes:
        self.assertEqual(typeFact.attrib['contextRef'],
                         "solar:CutSheetDetailsTable_0")
        self.assertEqual(costFact.attrib['unitRef'], "USD")
        self.assertEqual(costFact.attrib['contextRef'],
                         "solar:CutSheetDetailsTable_1")