示例#1
0
    def test_insert(self):
        prop1 = Property(name="prop1", dtype="int", values=[0,2])
        prop1.insert(1, 1)
        self.assertEqual(prop1.values, [0, 1, 2])
        prop1.insert(4, 3)
        self.assertEqual(prop1.values, [0, 1, 2, 3])

        with self.assertRaises(ValueError):
            prop1.append([4, 5])
        self.assertEqual(prop1.values, [0, 1, 2, 3])

        with self.assertRaises(ValueError):
            prop1.insert(1, 3.14)
        with self.assertRaises(ValueError):
            prop1.insert(1, True)
        with self.assertRaises(ValueError):
            prop1.insert(1, "5.927")
        self.assertEqual(prop1.values, [0, 1, 2, 3])

        prop2 = Property(name="prop2", dtype="int", values=[1, 2])
        prop2.insert(1, 3.14, strict=False)
        self.assertEqual(prop2.values, [1, 3, 2])
        prop2.insert(1, True, strict=False)
        self.assertEqual(prop2.values, [1, 1, 3, 2])
        prop2.insert(1, "5.927", strict=False)
        self.assertEqual(prop2.values, [1, 5, 1, 3, 2])
    
        prop3 = Property(name="prop3", dtype="string", values=["a", "c"])
        prop3.insert(1, "b")
        self.assertEqual(prop3.values, ["a", "b", "c"])
        prop3.insert(1, 1, strict=False)
        self.assertEqual(prop3.values, ["a", "1", "b", "c"])
        with self.assertRaises(ValueError):
            prop3.insert(1, 2, strict=True)
        self.assertEqual(prop3.values, ["a", "1", "b", "c"])

        prop4 = Property(name="prop4", dtype="float", values=[1.1, 1.3])
        prop4.insert(1, 1.2)
        self.assertEqual(prop4.values, [1.1, 1.2, 1.3])
        prop4.insert(1, 2, strict=False)
        self.assertEqual(prop4.values, [1.1, 2.0, 1.2, 1.3])
        with self.assertRaises(ValueError):
            prop4.insert(1, 2, strict=True)
        self.assertEqual(prop4.values, [1.1, 2.0, 1.2, 1.3])
    
        prop5 = Property(name="prop5", dtype="2-tuple", values=["(1; 2)", "(5; 6)"])
        prop5.insert(1, "(3; 4)", strict=True)
        self.assertEqual(prop5.values, [['1', '2'], ['3', '4'], ['5', '6']])
        prop5.insert(1, [['4', '5']], strict=True)
        self.assertEqual(prop5.values, [['1', '2'], ['4', '5'], ['3', '4'], ['5', '6']])
        
        prop6 = Property(name="prop6", dtype="boolean", values=[True, True])
        prop6.insert(1, False)
        self.assertEqual(prop6.values, [True, False, True])
        prop6.insert(1, 1, strict=False)
        self.assertEqual(prop6.values, [True, True, False, True])
        with self.assertRaises(ValueError):
            prop6.insert(1, 2, strict=False)
        self.assertEqual(prop6.values, [True, True, False, True])
        with self.assertRaises(ValueError):
            prop6.insert(1, 0, strict=True)
        self.assertEqual(prop6.values, [True, True, False, True])

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


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

        prop9 = Property(name="prop9", dtype="datetime",
                         values=['2011-12-01 12:00:01', '2011-12-01 12:00:04'])
        with self.assertRaises(ValueError):
            prop9.insert(1, '2011-12-01 12:00:03', strict=True)
        self.assertEqual(prop9.values, [datetime.datetime(2011, 12, 1, 12, 0, 1), 
                                        datetime.datetime(2011, 12, 1, 12, 0, 4)])
        prop9.insert(1, '2011-12-01 12:00:03', strict=False)
        self.assertEqual(prop9.values, [datetime.datetime(2011, 12, 1, 12, 0, 1), 
                                        datetime.datetime(2011, 12, 1, 12, 0, 3),
                                        datetime.datetime(2011, 12, 1, 12, 0, 4)])
        prop9.insert(1, [datetime.datetime(2011, 12, 1, 12, 0, 2)], strict=True)
        self.assertEqual(prop9.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)])
        
        prop10 = Property(name="prop", value=["Earth is\n No. 3."], dtype=DType.text)
        prop10.insert(1, "Mars is\n No. 4.", strict=False)
        self.assertEqual(len(prop10), 2)
        self.assertEqual(prop10.values, ["Earth is\n No. 3.", "Mars is\n No. 4."])
        prop10.insert(1, 'A new world emerged?', strict=True)
        self.assertEqual(prop10.values, ["Earth is\n No. 3.",
                                         "A new world emerged?",
                                         "Mars is\n No. 4."])
        prop10.insert(1, 1, strict=False)
        self.assertEqual(prop10.values, ["Earth is\n No. 3.", "1",
                                         "A new world emerged?",
                                         "Mars is\n No. 4."])
        with self.assertRaises(ValueError):
            prop10.insert(1, 1, strict=True)
        self.assertEqual(prop10.values, ["Earth is\n No. 3.", "1",
                                         "A new world emerged?",
                                         "Mars is\n No. 4."])
    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]")
示例#3
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)])
示例#4
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)