Beispiel #1
0
    def test_simple_attributes(self):
        p_name = "propertyName"
        p_origin = "from over there"
        p_unit = "pears"
        p_uncertainty = "12"
        p_ref = "4 8 15 16 23"
        p_def = "an odml test property"
        p_dep = "yes"
        p_dep_val = "42"

        prop = Property(name=p_name, value_origin=p_origin, unit=p_unit,
                        uncertainty=p_uncertainty, reference=p_ref, definition=p_def,
                        dependency=p_dep, dependency_value=p_dep_val)

        self.assertEqual(prop.name, p_name)
        self.assertEqual(prop.value_origin, p_origin)
        self.assertEqual(prop.unit, p_unit)
        self.assertEqual(prop.uncertainty, p_uncertainty)
        self.assertEqual(prop.reference, p_ref)
        self.assertEqual(prop.definition, p_def)
        self.assertEqual(prop.dependency, p_dep)
        self.assertEqual(prop.dependency_value, p_dep_val)

        # Test setting attributes
        prop.name = "%s_edit" % p_name
        self.assertEqual(prop.name, "%s_edit" % p_name)
        prop.value_origin = "%s_edit" % p_origin
        self.assertEqual(prop.value_origin, "%s_edit" % p_origin)
        prop.unit = "%s_edit" % p_unit
        self.assertEqual(prop.unit, "%s_edit" % p_unit)
        prop.uncertainty = 13
        self.assertEqual(prop.uncertainty, 13.0)
        prop.reference = "%s_edit" % p_ref
        self.assertEqual(prop.reference, "%s_edit" % p_ref)
        prop.definition = "%s_edit" % p_def
        self.assertEqual(prop.definition, "%s_edit" % p_def)
        prop.dependency = "%s_edit" % p_dep
        self.assertEqual(prop.dependency, "%s_edit" % p_dep)
        prop.dependency_value = "%s_edit" % p_dep_val
        self.assertEqual(prop.dependency_value, "%s_edit" % p_dep_val)

        # Test setting attributes to None when '' is passed.
        prop.value_origin = ""
        self.assertIsNone(prop.value_origin)
        prop.unit = ""
        self.assertIsNone(prop.unit)
        prop.uncertainty = ""
        self.assertIsNone(prop.uncertainty)
        prop.reference = ""
        self.assertIsNone(prop.reference)
        prop.definition = ""
        self.assertIsNone(prop.definition)
        prop.dependency = ""
        self.assertIsNone(prop.dependency)
        prop.dependency_value = ""
        self.assertIsNone(prop.dependency_value)
Beispiel #2
0
    def test_simple_attributes(self):
        p_name = "propertyName"
        p_origin = "from over there"
        p_unit = "pears"
        p_uncertainty = "12"
        p_ref = "4 8 15 16 23"
        p_def = "an odml test property"
        p_dep = "yes"
        p_dep_val = "42"

        prop = Property(name=p_name, value_origin=p_origin, unit=p_unit,
                        uncertainty=p_uncertainty, reference=p_ref, definition=p_def,
                        dependency=p_dep, dependency_value=p_dep_val)

        self.assertEqual(prop.name, p_name)
        self.assertEqual(prop.value_origin, p_origin)
        self.assertEqual(prop.unit, p_unit)
        self.assertEqual(prop.uncertainty, p_uncertainty)
        self.assertEqual(prop.reference, p_ref)
        self.assertEqual(prop.definition, p_def)
        self.assertEqual(prop.dependency, p_dep)
        self.assertEqual(prop.dependency_value, p_dep_val)

        # Test setting attributes
        prop.name = "%s_edit" % p_name
        self.assertEqual(prop.name, "%s_edit" % p_name)
        prop.value_origin = "%s_edit" % p_origin
        self.assertEqual(prop.value_origin, "%s_edit" % p_origin)
        prop.unit = "%s_edit" % p_unit
        self.assertEqual(prop.unit, "%s_edit" % p_unit)
        prop.uncertainty = 13
        self.assertEqual(prop.uncertainty, 13.0)
        prop.reference = "%s_edit" % p_ref
        self.assertEqual(prop.reference, "%s_edit" % p_ref)
        prop.definition = "%s_edit" % p_def
        self.assertEqual(prop.definition, "%s_edit" % p_def)
        prop.dependency = "%s_edit" % p_dep
        self.assertEqual(prop.dependency, "%s_edit" % p_dep)
        prop.dependency_value = "%s_edit" % p_dep_val
        self.assertEqual(prop.dependency_value, "%s_edit" % p_dep_val)

        # Test setting attributes to None when '' is passed.
        prop.value_origin = ""
        self.assertIsNone(prop.value_origin)
        prop.unit = ""
        self.assertIsNone(prop.unit)
        prop.uncertainty = ""
        self.assertIsNone(prop.uncertainty)
        prop.reference = ""
        self.assertIsNone(prop.reference)
        prop.definition = ""
        self.assertIsNone(prop.definition)
        prop.dependency = ""
        self.assertIsNone(prop.dependency)
        prop.dependency_value = ""
        self.assertIsNone(prop.dependency_value)
Beispiel #3
0
    def test_merge_check(self):
        # -- Root level Section checks

        # Test empty Section check
        source = Section(name="source")
        destination = Section(name="destination")

        destination.merge_check(source, True)

        # Test definition check
        source = Section(name="source", definition="def")
        destination = Section(name="destination", definition="def")

        destination.merge_check(source, True)
        source.definition = "other def"
        with self.assertRaises(ValueError):
            destination.merge_check(source, True)

        # Test reference check
        source = Section(name="source", reference="ref")
        destination = Section(name="destination", reference="ref")

        destination.merge_check(source, True)
        source.reference = "other ref"
        with self.assertRaises(ValueError):
            destination.merge_check(source, True)

        # -- First child level Section checks
        source = Section(name="source")
        destination = Section(name="destination")

        s_sec_one = Section(name="lvl", type="one",
                            reference="ref", definition="def", parent=source)
        s_sec_two = Section(name="unrelated", type="one",
                            reference="one", definition="one", parent=source)

        d_sec_one = Section(name="lvl", type="one",
                            reference="ref", definition="def", parent=destination)
        d_sec_two = Section(name="unrelated", type="two",
                            reference="two", definition="two", parent=destination)

        # Test Section child level definition check
        destination.merge_check(source, True)
        s_sec_one.definition = "other def"
        with self.assertRaises(ValueError):
            destination.merge_check(source, True)

        # Test Section child level reference check
        s_sec_one.definition = "def"
        s_sec_one.reference = "other ref"
        with self.assertRaises(ValueError):
            destination.merge_check(source, True)

        # -- Second child level Section checks
        source = Section(name="source")
        destination = Section(name="destination")

        s_sec_one = Section(name="lvl", type="one",
                            reference="ref", definition="def", parent=source)
        s_subsec_one = Section(name="lvl", type="two",
                               reference="ref2", definition="def2", parent=s_sec_one)
        s_sec_two = Section(name="unrelated", type="one",
                            reference="one", definition="one", parent=source)
        s_subsec_two = Section(name="lvl", type="two",
                               reference="none1", definition="none1", parent=s_sec_two)

        d_sec_one = Section(name="lvl", type="one",
                            reference="ref", definition="def", parent=destination)
        d_subsec_one = Section(name="lvl", type="two",
                               reference="ref2", definition="def2", parent=d_sec_one)
        d_sec_two = Section(name="unrelated", type="two",
                            reference="two", definition="two", parent=destination)
        d_subsec_two = Section(name="lvl", type="two",
                               reference="none2", definition="none2", parent=d_sec_two)

        # Test Section 2nd child level definition check
        # Check no definition/reference ValueError between s_subsec_two and d_subsec_one
        # since their parents will not be merged.
        destination.merge_check(source, True)

        # Raise a definition ValueError between s_subsec_one and d_subsec_one
        # since their parents will be merged.
        s_subsec_one.definition = "other def"
        with self.assertRaises(ValueError):
            destination.merge_check(source, True)

        # Test Section 2nd child level reference check
        s_subsec_one.definition = "def2"

        # Raise a reference ValueError between s_subsec_one and d_subsec_one
        # since their parents will be merged.
        s_subsec_one.reference = "other ref"
        with self.assertRaises(ValueError):
            destination.merge_check(source, True)

        # -- Root level Property checks
        # All Property checks will only test unit failure in the Section merge context.
        # Other failures are covered by the specific Property merge check tests.
        source = Section(name="source")
        destination = Section(name="destination")

        s_prop = Property(name="prop", parent=source)
        d_prop = Property(name="prop", parent=destination)

        destination.merge_check(source, True)
        s_prop.unit = "Hz"
        d_prop.unit = "s"
        with self.assertRaises(ValueError):
            destination.merge_check(source, True)

        # -- First child level Property checks
        source = Section(name="source")
        destination = Section(name="destination")

        s_prop_one = Property(name="lvl one", unit="Hz", parent=source)
        s_prop_two = Property(name="unrelated one", unit="one", parent=source)

        d_prop_one = Property(name="lvl one", unit="Hz", parent=destination)
        d_prop_two = Property(name="unrelated two", unit="two", parent=destination)

        # Test Property child level check
        destination.merge_check(source, True)

        # Test raise ValueError between s_prop_one and d_prop_one
        s_prop_one.unit = "other unit"
        with self.assertRaises(ValueError):
            destination.merge_check(source, True)

        # -- Second child level Property checks
        source = Section(name="source")
        destination = Section(name="destination")

        s_sec_one = Section(name="lvl", type="one", parent=source)
        s_subprop_one = Property(name="lvl one", unit="Hz", parent=s_sec_one)

        s_sec_two = Section(name="unrelated", type="one", parent=source)
        s_subprop_two = Property(name="unrelated one", unit="one", parent=s_sec_two)

        d_sec_one = Section(name="lvl", type="one", parent=destination)
        d_subprop_one = Property(name="lvl one", unit="Hz", parent=d_sec_one)

        d_sec_two = Section(name="unrelated", type="two", parent=destination)
        d_subprop_two = Property(name="unrelated one", unit="two", parent=d_sec_two)

        # Test Property 2nd child level definition check
        # Check no unit ValueError between s_subprop_two and d_subprop_one
        # since their parents will not be merged.
        destination.merge_check(source, True)

        # Raise a unit ValueError between s_subprop_one and d_subprop_one
        # since their parents will be merged.
        s_subprop_one.unit = "other unit"
        with self.assertRaises(ValueError):
            destination.merge_check(source, True)
Beispiel #4
0
    def test_merge_check(self):
        # Test dtype check
        source = Property(name="source", dtype="string")
        destination = Property(name="destination", dtype="string")

        destination.merge_check(source)
        source.dtype = "int"
        with self.assertRaises(ValueError):
            destination.merge_check(source)

        destination.merge_check(source, False)

        # Test value check
        source = Property(name="source", value=[1, 2, 3])
        destination = Property(name="destination", value=[4, 5, 6])
        destination.merge_check(source)

        # Test value convertable
        source = Property(name="source", value=["7", "8"])
        with self.assertRaises(ValueError):
            destination.merge_check(source)

        destination.merge_check(source, False)

        # Test value not convertable
        source = Property(name="source", value=["nine", "ten"])
        with self.assertRaises(ValueError):
            destination.merge_check(source)
        with self.assertRaises(ValueError):
            destination.merge_check(source, False)

        # Test unit check
        source = Property(name="source", unit="Hz")
        destination = Property(name="destination", unit="Hz")

        destination.merge_check(source)
        source.unit = "s"
        with self.assertRaises(ValueError):
            destination.merge_check(source)

        destination.merge_check(source, False)

        # Test uncertainty check
        source = Property(name="source", uncertainty=0.0)
        destination = Property(name="destination", uncertainty=0.0)

        destination.merge_check(source)
        source.uncertainty = 10.0
        with self.assertRaises(ValueError):
            destination.merge_check(source)

        destination.merge_check(source, False)

        # Test definition check
        source = Property(name="source", definition="Freude\t schoener\nGoetterfunken\n")
        destination = Property(name="destination",
                               definition="FREUDE schoener GOETTERfunken")

        destination.merge_check(source)
        source.definition = "Freunde schoender Goetterfunken"
        with self.assertRaises(ValueError):
            destination.merge_check(source)

        destination.merge_check(source, False)

        # Test reference check
        source = Property(name="source", reference="portal.g-node.org")
        destination = Property(name="destination", reference="portal.g-node.org")

        destination.merge_check(source)
        source.reference = "portal.g-node.org/odml/terminologies/v1.1"
        with self.assertRaises(ValueError):
            destination.merge_check(source)

        destination.merge_check(source, False)

        # Test value origin check
        source = Property(name="source", value_origin="file")
        destination = Property(name="destination", value_origin="file")

        destination.merge_check(source)
        source.value_origin = "other file"
        with self.assertRaises(ValueError):
            destination.merge_check(source)

        destination.merge_check(source, False)
Beispiel #5
0
    def test_value_extend(self):
        prop = Property(name="extend")

        # Test extend w/o Property value or dtype.
        val = [1, 2, 3]
        prop.extend(val)
        self.assertEqual(prop.dtype, DType.int)
        self.assertEqual(prop.values, val)

        # Extend with single value.
        prop.extend(4)
        self.assertEqual(prop.values, [1, 2, 3, 4])

        # Extend with list value.
        prop.extend([5, 6])
        self.assertEqual(prop.values, [1, 2, 3, 4, 5, 6])

        # Test extend w/o Property value
        prop = Property(name="extend", dtype="float")
        prop.extend([1.0, 2.0, 3.0])
        self.assertEqual(prop.values, [1.0, 2.0, 3.0])

        # Test extend with Property value
        prop = Property(name="extend", value=10)
        prop.extend([20, 30, '40'])
        self.assertEqual(prop.values, [10, 20, 30, 40])

        # Test extend fail with mismatching dtype
        with self.assertRaises(ValueError):
            prop.extend(['5', 6, 7])
        with self.assertRaises(ValueError):
            prop.extend([5, 6, 'a'])

        # Test extend via Property
        prop = Property(name="extend", value=["a", "b"])
        ext_prop = Property(name="value extend", value="c")
        prop.extend(ext_prop)
        self.assertEqual(prop.values, ["a", "b", "c"])

        ext_prop.values = ["d", "e"]
        prop.extend(ext_prop)
        self.assertEqual(prop.values, ["a", "b", "c", "d", "e"])

        ext_prop = Property(name="value extend", value=[1, 2, 3])
        with self.assertRaises(ValueError):
            prop.extend(ext_prop)
        self.assertEqual(prop.values, ["a", "b", "c", "d", "e"])

        # Test extend via Property unit check
        prop = Property(name="extend", value=[1, 2], unit="mV")
        ext_prop = Property(name="extend", value=[3, 4], unit="mV")
        prop.extend(ext_prop)
        self.assertEqual(prop.values, [1, 2, 3, 4])

        ext_prop.unit = "kV"
        with self.assertRaises(ValueError):
            prop.extend(ext_prop)
        self.assertEqual(prop.values, [1, 2, 3, 4])

        ext_prop.unit = ""
        with self.assertRaises(ValueError):
            prop.extend(ext_prop)
        self.assertEqual(prop.values, [1, 2, 3, 4])

        # Test strict flag
        prop = Property(name="extend", value=[1, 2], dtype="int")
        with self.assertRaises(ValueError):
            prop.extend([3.14, True, "5.927"])
        self.assertEqual(prop.values, [1, 2])

        prop.extend([3.14, True, "5.927"], strict=False)
        self.assertEqual(prop.values, [1, 2, 3, 1, 5])

        # Make sure non-convertible values still raise an error
        with self.assertRaises(ValueError):
            prop.extend([6, "some text"])

        prop1 = Property(name="prop", value=["A Abraham", "B Barnes", "C Clark"],
                         dtype=DType.person)
        prop1.extend("D Dickins")
        self.assertEqual(len(prop1), 4)
        self.assertRaises(ValueError, prop1.extend, 1)
        self.assertRaises(ValueError, prop1.extend, 1.3)
        self.assertRaises(ValueError, prop1.extend, True)

        prop2 = Property(name="prop", value=["https://en.wikipedia.org/wiki/Earth"],
                         dtype=DType.url)
        prop2.extend("https://en.wikipedia.org/wiki/Mars")
        self.assertEqual(len(prop2), 2)
        self.assertRaises(ValueError, prop2.extend, 1)
        self.assertRaises(ValueError, prop2.extend, 1.3)
        self.assertRaises(ValueError, prop2.extend, True)

        prop3 = Property(name="prop", value=["Earth is No. 3."], dtype=DType.text)
        prop3.extend("Mars is No. 4.")
        self.assertEqual(len(prop3), 2)
        self.assertRaises(ValueError, prop3.extend, 1)
        self.assertRaises(ValueError, prop3.extend, 1.3)
        self.assertRaises(ValueError, prop3.extend, True)

        prop4 = Property(name="tuple-test", dtype="3-tuple", values="(1; 2; 3)")
        prop4.extend(["(7; 8; 9)", "(10; 11; 12)"])
        self.assertEqual(len(prop4), 3)
        self.assertRaises(ValueError, prop4.extend, "(10; 11)")
        prop4.extend([[2, 3, 4], [5, 6, 7]])
        self.assertEqual(len(prop4), 5)
        self.assertRaises(ValueError, prop4.extend, [[10, 11]])

        prop4 = Property(name="prop4", dtype="date", values=['2011-12-01', '2011-12-02'])
        with self.assertRaises(ValueError):
            prop4.extend('2011-12-03', strict=True)
        self.assertEqual(prop4.values, [datetime.date(2011, 12, 1), datetime.date(2011, 12, 2)])
        prop4.extend('2011-12-03', strict=False)
        self.assertEqual(prop4.values, [datetime.date(2011, 12, 1), datetime.date(2011, 12, 2),
                                        datetime.date(2011, 12, 3)])
        prop4.extend([datetime.date(2011, 12, 4)], strict=True)
        self.assertEqual(prop4.values, [datetime.date(2011, 12, 1), datetime.date(2011, 12, 2),
                                        datetime.date(2011, 12, 3), datetime.date(2011, 12, 4)])
        prop4.extend([datetime.date(2011, 12, 5), datetime.date(2011, 12, 6)], strict=True)
        self.assertEqual(prop4.values, [datetime.date(2011, 12, 1), datetime.date(2011, 12, 2),
                                        datetime.date(2011, 12, 3), datetime.date(2011, 12, 4),
                                        datetime.date(2011, 12, 5), datetime.date(2011, 12, 6)])
        with self.assertRaises(ValueError):
            prop4.extend(['2011-12-03', 'abc'], strict=False)


        prop5 = Property(name="prop5", dtype="time", values=['12:00:01', '12:00:02'])
        with self.assertRaises(ValueError):
            prop5.extend('12:00:03', strict=True)
        self.assertEqual(prop5.values, [datetime.time(12, 0, 1), datetime.time(12, 0, 2)])
        prop5.extend('12:00:03', strict=False)
        self.assertEqual(prop5.values, [datetime.time(12, 0, 1), datetime.time(12, 0, 2),
                                        datetime.time(12, 0, 3)])
        prop5.extend([datetime.time(12, 0, 4)], strict=True)
        self.assertEqual(prop5.values, [datetime.time(12, 0, 1), datetime.time(12, 0, 2),
                                        datetime.time(12, 0, 3), datetime.time(12, 0, 4)])
        prop5.extend([datetime.time(12, 0, 5), datetime.time(12, 0, 6)], strict=True)
        self.assertEqual(prop5.values, [datetime.time(12, 0, 1), datetime.time(12, 0, 2),
                                        datetime.time(12, 0, 3), datetime.time(12, 0, 4),
                                        datetime.time(12, 0, 5), datetime.time(12, 0, 6)])
        with self.assertRaises(ValueError):
            prop4.extend(['12:00:07', 'abc'], strict=False)

        prop6 = Property(name="prop6", dtype="datetime",
                         values=['2011-12-01 12:00:01', '2011-12-01 12:00:02'])
        with self.assertRaises(ValueError):
            prop6.extend('2011-12-01 12:00:03', strict=True)
        self.assertEqual(prop6.values, [datetime.datetime(2011, 12, 1, 12, 0, 1),
                                        datetime.datetime(2011, 12, 1, 12, 0, 2)])
        prop6.extend('2011-12-01 12:00:03', strict=False)
        self.assertEqual(prop6.values, [datetime.datetime(2011, 12, 1, 12, 0, 1),
                                        datetime.datetime(2011, 12, 1, 12, 0, 2),
                                        datetime.datetime(2011, 12, 1, 12, 0, 3)])
        prop6.extend([datetime.datetime(2011, 12, 1, 12, 0, 4)], strict=True)
        self.assertEqual(prop6.values, [datetime.datetime(2011, 12, 1, 12, 0, 1),
                                        datetime.datetime(2011, 12, 1, 12, 0, 2),
                                        datetime.datetime(2011, 12, 1, 12, 0, 3),
                                        datetime.datetime(2011, 12, 1, 12, 0, 4)])
        prop6.extend([datetime.datetime(2011, 12, 1, 12, 0, 5),
                      datetime.datetime(2011, 12, 1, 12, 0, 6)], strict=True)
        self.assertEqual(prop6.values, [datetime.datetime(2011, 12, 1, 12, 0, 1),
                                        datetime.datetime(2011, 12, 1, 12, 0, 2),
                                        datetime.datetime(2011, 12, 1, 12, 0, 3),
                                        datetime.datetime(2011, 12, 1, 12, 0, 4),
                                        datetime.datetime(2011, 12, 1, 12, 0, 5),
                                        datetime.datetime(2011, 12, 1, 12, 0, 6)])
        with self.assertRaises(ValueError):
            prop4.extend(['2011-12-03 12:00:07', 'abc'], strict=False)
Beispiel #6
0
    def test_merge_check(self):
        # Test dtype check
        source = Property(name="source", dtype="string")
        destination = Property(name="destination", dtype="string")

        destination.merge_check(source)
        source.dtype = "int"
        with self.assertRaises(ValueError):
            destination.merge_check(source)

        destination.merge_check(source, False)

        # Test value check
        source = Property(name="source", value=[1, 2, 3])
        destination = Property(name="destination", value=[4, 5, 6])
        destination.merge_check(source)

        # Test value convertable
        source = Property(name="source", value=["7", "8"])
        with self.assertRaises(ValueError):
            destination.merge_check(source)

        destination.merge_check(source, False)

        # Test value not convertable
        source = Property(name="source", value=["nine", "ten"])
        with self.assertRaises(ValueError):
            destination.merge_check(source)
        with self.assertRaises(ValueError):
            destination.merge_check(source, False)

        # Test unit check
        source = Property(name="source", unit="Hz")
        destination = Property(name="destination", unit="Hz")

        destination.merge_check(source)
        source.unit = "s"
        with self.assertRaises(ValueError):
            destination.merge_check(source)

        destination.merge_check(source, False)

        # Test uncertainty check
        source = Property(name="source", uncertainty=0.0)
        destination = Property(name="destination", uncertainty=0.0)

        destination.merge_check(source)
        source.uncertainty = 10.0
        with self.assertRaises(ValueError):
            destination.merge_check(source)

        destination.merge_check(source, False)

        # Test definition check
        source = Property(name="source", definition="Freude\t schoener\nGoetterfunken\n")
        destination = Property(name="destination",
                               definition="FREUDE schoener GOETTERfunken")

        destination.merge_check(source)
        source.definition = "Freunde schoender Goetterfunken"
        with self.assertRaises(ValueError):
            destination.merge_check(source)

        destination.merge_check(source, False)

        # Test reference check
        source = Property(name="source", reference="portal.g-node.org")
        destination = Property(name="destination", reference="portal.g-node.org")

        destination.merge_check(source)
        source.reference = "portal.g-node.org/odml/terminologies/v1.1"
        with self.assertRaises(ValueError):
            destination.merge_check(source)

        destination.merge_check(source, False)

        # Test value origin check
        source = Property(name="source", value_origin="file")
        destination = Property(name="destination", value_origin="file")

        destination.merge_check(source)
        source.value_origin = "other file"
        with self.assertRaises(ValueError):
            destination.merge_check(source)

        destination.merge_check(source, False)
    def test_value_extend(self):
        prop = Property(name="extend")

        # Test extend w/o Property value or dtype.
        val = [1, 2, 3]
        prop.extend(val)
        self.assertEqual(prop.dtype, DType.int)
        self.assertEqual(prop.values, val)

        # Extend with single value.
        prop.extend(4)
        self.assertEqual(prop.values, [1, 2, 3, 4])

        # Extend with list value.
        prop.extend([5, 6])
        self.assertEqual(prop.values, [1, 2, 3, 4, 5, 6])

        # Test extend w/o Property value
        prop = Property(name="extend", dtype="float")
        prop.extend([1.0, 2.0, 3.0])
        self.assertEqual(prop.values, [1.0, 2.0, 3.0])

        # Test extend with Property value
        prop = Property(name="extend", value=10)
        prop.extend([20, 30, '40'])
        self.assertEqual(prop.values, [10, 20, 30, 40])

        # Test extend fail with mismatching dtype
        with self.assertRaises(ValueError):
            prop.extend(['5', 6, 7])
        with self.assertRaises(ValueError):
            prop.extend([5, 6, 'a'])

        # Test extend via Property
        prop = Property(name="extend", value=["a", "b"])
        ext_prop = Property(name="value extend", value="c")
        prop.extend(ext_prop)
        self.assertEqual(prop.values, ["a", "b", "c"])

        ext_prop.values = ["d", "e"]
        prop.extend(ext_prop)
        self.assertEqual(prop.values, ["a", "b", "c", "d", "e"])

        ext_prop = Property(name="value extend", value=[1, 2 ,3])
        with self.assertRaises(ValueError):
            prop.extend(ext_prop)
        self.assertEqual(prop.values, ["a", "b", "c", "d", "e"])

        # Test extend via Property unit check
        prop = Property(name="extend", value=[1, 2], unit="mV")
        ext_prop = Property(name="extend", value=[3, 4], unit="mV")
        prop.extend(ext_prop)
        self.assertEqual(prop.values, [1, 2, 3, 4])

        ext_prop.unit = "kV"
        with self.assertRaises(ValueError):
            prop.extend(ext_prop)
        self.assertEqual(prop.values, [1, 2, 3, 4])

        ext_prop.unit = ""
        with self.assertRaises(ValueError):
            prop.extend(ext_prop)
        self.assertEqual(prop.values, [1, 2, 3, 4])

        # Test strict flag
        prop = Property(name="extend", value=[1, 2], dtype="int")
        with self.assertRaises(ValueError):
            prop.extend([3.14, True, "5.927"])
        self.assertEqual(prop.values, [1, 2])

        prop.extend([3.14, True, "5.927"], strict=False)
        self.assertEqual(prop.values, [1, 2, 3, 1, 5])

        # Make sure non-convertible values still raise an error
        with self.assertRaises(ValueError):
            prop.extend([6, "some text"])
Beispiel #8
0
    def test_value_extend(self):
        prop = Property(name="extend")

        # Test extend w/o Property value or dtype.
        val = [1, 2, 3]
        prop.extend(val)
        self.assertEqual(prop.dtype, DType.int)
        self.assertEqual(prop.values, val)

        # Extend with single value.
        prop.extend(4)
        self.assertEqual(prop.values, [1, 2, 3, 4])

        # Extend with list value.
        prop.extend([5, 6])
        self.assertEqual(prop.values, [1, 2, 3, 4, 5, 6])

        # Test extend w/o Property value
        prop = Property(name="extend", dtype="float")
        prop.extend([1.0, 2.0, 3.0])
        self.assertEqual(prop.values, [1.0, 2.0, 3.0])

        # Test extend with Property value
        prop = Property(name="extend", value=10)
        prop.extend([20, 30, '40'])
        self.assertEqual(prop.values, [10, 20, 30, 40])

        # Test extend fail with mismatching dtype
        with self.assertRaises(ValueError):
            prop.extend(['5', 6, 7])
        with self.assertRaises(ValueError):
            prop.extend([5, 6, 'a'])

        # Test extend via Property
        prop = Property(name="extend", value=["a", "b"])
        ext_prop = Property(name="value extend", value="c")
        prop.extend(ext_prop)
        self.assertEqual(prop.values, ["a", "b", "c"])

        ext_prop.values = ["d", "e"]
        prop.extend(ext_prop)
        self.assertEqual(prop.values, ["a", "b", "c", "d", "e"])

        ext_prop = Property(name="value extend", value=[1, 2, 3])
        with self.assertRaises(ValueError):
            prop.extend(ext_prop)
        self.assertEqual(prop.values, ["a", "b", "c", "d", "e"])

        # Test extend via Property unit check
        prop = Property(name="extend", value=[1, 2], unit="mV")
        ext_prop = Property(name="extend", value=[3, 4], unit="mV")
        prop.extend(ext_prop)
        self.assertEqual(prop.values, [1, 2, 3, 4])

        ext_prop.unit = "kV"
        with self.assertRaises(ValueError):
            prop.extend(ext_prop)
        self.assertEqual(prop.values, [1, 2, 3, 4])

        ext_prop.unit = ""
        with self.assertRaises(ValueError):
            prop.extend(ext_prop)
        self.assertEqual(prop.values, [1, 2, 3, 4])

        # Test strict flag
        prop = Property(name="extend", value=[1, 2], dtype="int")
        with self.assertRaises(ValueError):
            prop.extend([3.14, True, "5.927"])
        self.assertEqual(prop.values, [1, 2])

        prop.extend([3.14, True, "5.927"], strict=False)
        self.assertEqual(prop.values, [1, 2, 3, 1, 5])

        # Make sure non-convertible values still raise an error
        with self.assertRaises(ValueError):
            prop.extend([6, "some text"])

        p1 = Property(name="prop",
                      value=["A Abraham", "B Barnes", "C Clark"],
                      dtype=DType.person)
        p1.extend("D Dickins")
        self.assertEqual(len(p1), 4)
        self.assertRaises(ValueError, p1.extend, 1)
        self.assertRaises(ValueError, p1.extend, 1.3)
        self.assertRaises(ValueError, p1.extend, True)

        p2 = Property(name="prop",
                      value=["https://en.wikipedia.org/wiki/Earth"],
                      dtype=DType.url)
        p2.extend("https://en.wikipedia.org/wiki/Mars")
        self.assertEqual(len(p2), 2)
        self.assertRaises(ValueError, p2.append, 1)
        self.assertRaises(ValueError, p2.append, 1.3)
        self.assertRaises(ValueError, p2.append, True)

        p3 = Property(name="prop", value=["Earth is No. 3."], dtype=DType.text)
        p3.extend("Mars is No. 4.")
        self.assertEqual(len(p3), 2)
        self.assertRaises(ValueError, p3.append, 1)
        self.assertRaises(ValueError, p3.append, 1.3)
        self.assertRaises(ValueError, p3.append, True)