Exemplo n.º 1
0
    def test_bool_conversion(self):
        # Success tests
        p = Property(name='received', value=[1, 0, 1, 0, 1])
        assert(p.dtype == 'int')
        p.dtype = DType.boolean
        assert(p.dtype == 'boolean')
        assert(p.values == [True, False, True, False, True])

        q = Property(name='sent', value=['False', True, 'TRUE', '0', 't', 'F', '1'])
        assert(q.dtype == 'string')
        q.dtype = DType.boolean
        assert(q.dtype == 'boolean')
        assert(q.values == [False, True, True, False, True, False, True])

        # Failure tests
        curr_val = [3, 0, 1, 0, 8]
        curr_type = 'int'
        p = Property(name='received', value=curr_val)
        assert(p.dtype == curr_type)
        with self.assertRaises(ValueError):
            p.dtype = DType.boolean
        assert(p.dtype == curr_type)
        assert(p.values == curr_val)

        curr_type = 'string'
        q = Property(name='sent', value=['False', True, 'TRUE', '0', 't', '12', 'Ft'])
        assert(q.dtype == curr_type)
        with self.assertRaises(ValueError):
            q.dtype = DType.boolean
        assert(q.dtype == curr_type)
Exemplo n.º 2
0
    def test_dtype(self):
        prop = Property(name="prop")

        # Test assignment of all supported dtypes.
        for curr_type in DType:
            prop.dtype = curr_type
            self.assertEqual(prop.dtype, curr_type)

        # Test assignment of dtype alias.
        prop.dtype = "bool"
        self.assertEqual(prop.dtype, "bool")
        prop.dtype = "str"
        self.assertEqual(prop.dtype, "str")

        # Test assignment of tuple.
        prop.dtype = "2-tuple"
        self.assertEqual(prop.dtype, "2-tuple")

        # Test set None
        prop.dtype = None
        self.assertIsNone(prop.dtype)

        # Test assignment fails.
        with self.assertRaises(AttributeError):
            prop.dtype = 1
        with self.assertRaises(AttributeError):
            prop.dtype = "crash and burn"
        with self.assertRaises(AttributeError):
            prop.dtype = "x-tuple"

        # Test not setting None when a property contains values.
        prop.values = [1, 2, 3]
        self.assertIsNotNone(prop.dtype)
        prop.dtype = None
        self.assertIsNotNone(prop.dtype)
Exemplo n.º 3
0
    def test_bool_conversion(self):

        p = Property(name='received', value=[3, 0, 1, 0, 8])
        assert(p.dtype == 'int')
        p.dtype = DType.boolean
        assert(p.dtype == 'boolean')
        assert(p.value == [True, False, True, False, True])

        q = Property(name='sent',
                     value=['False', True, 'TRUE', '0', 't', 'F', 'Ft'])
        assert(q.dtype == 'string')
        q.dtype = DType.boolean
        assert(q.dtype == 'boolean')
        assert(q.value == [False, True, True, False, True, False, True])
    def test_str_to_int_convert(self):
        # Success Test
        p = Property(name='cats_onboard', value=['3', '0', '1', '0', '8'])
        assert(p.dtype == 'string')
        p.dtype = DType.int
        assert(p.dtype == 'int')
        assert(p.values == [3, 0, 1, 0, 8])

        # Failure Test
        p = Property(name='dogs_onboard', value=['7', '20', '1 Dog', 'Seven'])
        assert(p.dtype == 'string')

        with self.assertRaises(ValueError):
            p.dtype = DType.int

        assert(p.dtype == 'string')
        assert(p.values == ['7', '20', '1 Dog', 'Seven'])
Exemplo n.º 5
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)
Exemplo n.º 6
0
    def test_value_append(self):
        # Test append w/o Property value or dtype
        prop = Property(name="append")
        prop.append(1)
        self.assertEqual(prop.dtype, DType.int)
        self.assertEqual(prop.values, [1])

        # Test append with Property dtype.
        prop = Property(name="append", dtype="int")
        prop.append(3)
        self.assertEqual(prop.values, [3])

        # Test append with Property value
        prop = Property(name="append", value=[1, 2])
        prop.append(3)
        self.assertEqual(prop.values, [1, 2, 3])

        # Test append with Property list value
        prop = Property(name="append", value=[1, 2])
        prop.append([3])
        self.assertEqual(prop.values, [1, 2, 3])

        # Test append of empty values, make sure 0 and False are properly handled
        prop = Property(name="append")
        prop.append(None)
        prop.append("")
        prop.append([])
        prop.append({})
        self.assertEqual(prop.values, [])

        prop.append(0)
        self.assertEqual(prop.values, [0])

        prop.values = None
        prop.dtype = None
        prop.append(False)
        self.assertEqual(prop.values, [False])

        prop = Property(name="append", value=[1, 2])
        prop.append(None)
        prop.append("")
        prop.append([])
        prop.append({})
        self.assertEqual(prop.values, [1, 2])

        prop.append(0)
        self.assertEqual(prop.values, [1, 2, 0])

        # Test fail append with multiple values
        prop = Property(name="append", value=[1, 2, 3])
        with self.assertRaises(ValueError):
            prop.append([4, 5])
        self.assertEqual(prop.values, [1, 2, 3])

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

        # Test strict flag
        prop.append(3.14, strict=False)
        prop.append(True, strict=False)
        prop.append("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.append("invalid")
        self.assertEqual(prop.values, [1, 2, 3, 1, 5])

        prop5 = Property("test", value="a string")
        prop5.append("Freude")
        self.assertEqual(len(prop5), 2)
        self.assertRaises(ValueError, prop5.append, "[a, b, c]")

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

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

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

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

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


        prop11 = Property(name="prop11", dtype="time", values=['12:00:01', '12:00:02'])
        with self.assertRaises(ValueError):
            prop11.append('12:00:03', strict=True)
        self.assertEqual(prop11.values, [datetime.time(12, 0, 1), datetime.time(12, 0, 2)])
        prop11.append('12:00:03', strict=False)
        self.assertEqual(prop11.values, [datetime.time(12, 0, 1), datetime.time(12, 0, 2),
                                         datetime.time(12, 0, 3)])
        prop11.append([datetime.time(12, 0, 4)], strict=True)
        self.assertEqual(prop11.values, [datetime.time(12, 0, 1), datetime.time(12, 0, 2),
                                         datetime.time(12, 0, 3), datetime.time(12, 0, 4)])

        prop12 = Property(name="prop12", dtype="datetime",
                          values=['2011-12-01 12:00:01', '2011-12-01 12:00:02'])
        with self.assertRaises(ValueError):
            prop12.append('2011-12-01 12:00:03', strict=True)
        self.assertEqual(prop12.values, [datetime.datetime(2011, 12, 1, 12, 0, 1),
                                         datetime.datetime(2011, 12, 1, 12, 0, 2)])
        prop12.append('2011-12-01 12:00:03', strict=False)
        self.assertEqual(prop12.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)])
        prop12.append([datetime.datetime(2011, 12, 1, 12, 0, 4)], strict=True)
        self.assertEqual(prop12.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)])
Exemplo n.º 7
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_append(self):
        # Test append w/o Property value or dtype
        prop = Property(name="append")
        prop.append(1)
        self.assertEqual(prop.dtype, DType.int)
        self.assertEqual(prop.values, [1])

        # Test append with Property dtype.
        prop = Property(name="append", dtype="int")
        prop.append(3)
        self.assertEqual(prop.values, [3])

        # Test append with Property value
        prop = Property(name="append", value=[1, 2])
        prop.append(3)
        self.assertEqual(prop.values, [1, 2, 3])

        # Test append with Property list value
        prop = Property(name="append", value=[1, 2])
        prop.append([3])
        self.assertEqual(prop.values, [1, 2, 3])

        # Test append of empty values, make sure 0 and False are properly handled
        prop = Property(name="append")
        prop.append(None)
        prop.append("")
        prop.append([])
        prop.append({})
        self.assertEqual(prop.values, [])

        prop.append(0)
        self.assertEqual(prop.values, [0])

        prop.values = None
        prop.dtype = None
        prop.append(False)
        self.assertEqual(prop.values, [False])

        prop = Property(name="append", value=[1, 2])
        prop.append(None)
        prop.append("")
        prop.append([])
        prop.append({})
        self.assertEqual(prop.values, [1, 2])

        prop.append(0)
        self.assertEqual(prop.values, [1, 2, 0])

        # Test fail append with multiple values
        prop = Property(name="append", value=[1, 2, 3])
        with self.assertRaises(ValueError):
            prop.append([4, 5])
        self.assertEqual(prop.values, [1, 2, 3])

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

        # Test strict flag
        prop.append(3.14, strict=False)
        prop.append(True, strict=False)
        prop.append("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.append("invalid")
        self.assertEqual(prop.values, [1, 2, 3, 1, 5])

        p5 = Property("test", value="a string")
        p5.append("Freude")
        self.assertEqual(len(p5), 2)
        self.assertRaises(ValueError, p5.append, "[a, b, c]")
Exemplo n.º 9
0
    def test_value_append(self):
        # Test append w/o Property value or dtype
        prop = Property(name="append")
        prop.append(1)
        self.assertEqual(prop.dtype, DType.int)
        self.assertEqual(prop.values, [1])

        # Test append with Property dtype.
        prop = Property(name="append", dtype="int")
        prop.append(3)
        self.assertEqual(prop.values, [3])

        # Test append with Property value
        prop = Property(name="append", value=[1, 2])
        prop.append(3)
        self.assertEqual(prop.values, [1, 2, 3])

        # Test append with Property list value
        prop = Property(name="append", value=[1, 2])
        prop.append([3])
        self.assertEqual(prop.values, [1, 2, 3])

        # Test append of empty values, make sure 0 and False are properly handled
        prop = Property(name="append")
        prop.append(None)
        prop.append("")
        prop.append([])
        prop.append({})
        self.assertEqual(prop.values, [])

        prop.append(0)
        self.assertEqual(prop.values, [0])

        prop.values = None
        prop.dtype = None
        prop.append(False)
        self.assertEqual(prop.values, [False])

        prop = Property(name="append", value=[1, 2])
        prop.append(None)
        prop.append("")
        prop.append([])
        prop.append({})
        self.assertEqual(prop.values, [1, 2])

        prop.append(0)
        self.assertEqual(prop.values, [1, 2, 0])

        # Test fail append with multiple values
        prop = Property(name="append", value=[1, 2, 3])
        with self.assertRaises(ValueError):
            prop.append([4, 5])
        self.assertEqual(prop.values, [1, 2, 3])

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

        # Test strict flag
        prop.append(3.14, strict=False)
        prop.append(True, strict=False)
        prop.append("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.append("invalid")
        self.assertEqual(prop.values, [1, 2, 3, 1, 5])

        p5 = Property("test", value="a string")
        p5.append("Freude")
        self.assertEqual(len(p5), 2)
        self.assertRaises(ValueError, p5.append, "[a, b, c]")

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

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

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