示例#1
0
    def test_store_succeed(self):

        test_db1 = [(1, 2, 'a', 'a'), (3, 4, 'a', 'a')]
        test_db2 = [(1, 2, 'a', 'a'), (3, 4, 'a', 'a'), (5, 6, 'a', 'a')]
        test_db3 = [(1, 2, 'b', 'c'), (3, 4, 'd', 'e')]

        ref_count = sys.getrefcount('a')
        ref_count_b = sys.getrefcount('b')

        def test_ok(_db, change, name):
            ip_store.load(_db)
            ip_store.search("0.0.0.1")
            self.assertTrue(ip_store.size() == len(_db),
                            (ip_store.size(), len(_db), name))
            ref_count_new = sys.getrefcount('a')
            self.assertTrue(ref_count + change == ref_count_new,
                            (name, ref_count, ref_count_new))

        test_ok(test_db1, 4, "test_db1")
        test_ok(test_db2, 6, "test_db2")
        test_ok(test_db3, 0, "test_db3")

        for i in range(2):
            self.assertTrue(ip_store.get(i) == test_db3[i])

        ip_store.load([])
        new_ref_count_b = sys.getrefcount('b')
        self.assertTrue(ref_count_b == new_ref_count_b)
示例#2
0
 def test_error(_db, error, name):
     with self.assertRaises(error):
         ip_store.load(_db)
     ref_count_new = sys.getrefcount('a')
     self.assertTrue(ref_count == ref_count_new,
                     (name, ref_count, ref_count_new))
     self.assertTrue(ip_store.size() == 0, (name, ip_store.size())),
示例#3
0
    def test_store_failed(self):
        with self.assertRaises(TypeError):
            ip_store.load(1)
        assert ip_store.size() == 0, ip_store.size()

        ip_store.load([])
        assert ip_store.size() == 0, ip_store.size()

        test_db1 = [(1, 2, 'a', 'a'), 3]
        test_db2 = [(1, 2, 'a', 'a'), ()]
        test_db3 = [(1, 2, 'a', 'a'), (1.1, 1.1, 'a', 'a')]
        test_db4 = [(1, 2, 'a', 'a'), (-10, 1.1, 'a', 'a')]
        test_db5 = [(1, 2, 'a', 'a'), (3, 4, 'a')]
        ref_count = sys.getrefcount('a')

        def test_error(_db, error, name):
            with self.assertRaises(error):
                ip_store.load(_db)
            ref_count_new = sys.getrefcount('a')
            self.assertTrue(ref_count == ref_count_new,
                            (name, ref_count, ref_count_new))
            self.assertTrue(ip_store.size() == 0, (name, ip_store.size())),

        test_error(test_db1, TypeError, "test_db1")
        test_error(test_db2, IndexError, "test_db2")
        test_error(test_db3, TypeError, "test_db3")
        if sys.version_info[0] == 3:
            test_error(test_db4, OverflowError, "test_db4")
        test_error(test_db5, IndexError, "test_db5")
示例#4
0
 def test_ok(_db, change, name):
     ip_store.load(_db)
     ip_store.search("0.0.0.1")
     self.assertTrue(ip_store.size() == len(_db),
                     (ip_store.size(), len(_db), name))
     ref_count_new = sys.getrefcount('a')
     self.assertTrue(ref_count + change == ref_count_new,
                     (name, ref_count, ref_count_new))
示例#5
0
    def test_(self):
        ip_store.load(db)
        tr = tracker.SummaryTracker()
        print(sys.getrefcount(None))

        for i in range(10):
            for probe in probe_list:
                ip_store.search(probe[0])

        tr.print_diff()
        print(sys.getrefcount(None))
示例#6
0
    def test_get_failed(self):

        with self.assertRaises(TypeError):
            ip_store.get(1.1)

        with self.assertRaises(IndexError):
            ip_store.get(-1)

        with self.assertRaises(IndexError):
            ip_store.get(0)

        ip_store.load([(1, 2, 'a', 'a')])

        ip_store.get(0)
        with self.assertRaises(IndexError):
            ip_store.get(1)
示例#7
0
    def test_speed(self):
        ip_store.load(db)
        raw_stmt_p = """
for probe in probe_list:
    search(db, probe[0])
        """
        raw_stmt_c = """
for probe in probe_list:
    ip_store.search(probe[0])
        """

        if sys.version_info[0] == 3:
            new_t = timeit.timeit(raw_stmt_p, number=1, globals=globals())
            print("Python time: %s" % round(new_t, 3))

            new_t = timeit.timeit(raw_stmt_c, number=1, globals=globals())
            print("C time: %s" % round(new_t, 3))
示例#8
0
 def test_c_search(self):
     ip_store.load(db)
     for probe in probe_list:
         self.assertTrue(probe[1] == ip_store.search(probe[0]),
                         (probe[1], ip_store.search(probe[0])))
示例#9
0
 def setUp(self):
     ip_store.load([])