mirror of
https://github.com/simon987/hexlib.git
synced 2025-12-14 07:09:05 +00:00
add download_file, bool volatile state
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
from unittest import TestCase
|
||||
from hexlib.db import VolatileState
|
||||
from hexlib.db import VolatileState, VolatileBooleanState
|
||||
|
||||
|
||||
class TestVolatileState(TestCase):
|
||||
@@ -8,7 +8,7 @@ class TestVolatileState(TestCase):
|
||||
s = VolatileState(prefix="test1")
|
||||
val = {
|
||||
"field1": 1,
|
||||
"arr1": [1,2,3]
|
||||
"arr1": [1, 2, 3]
|
||||
}
|
||||
|
||||
s["a"]["1"] = val
|
||||
@@ -38,3 +38,33 @@ class TestVolatileState(TestCase):
|
||||
del s["c"]["1"]
|
||||
self.assertIsNone(s["c"]["1"])
|
||||
|
||||
|
||||
class TestVolatileBoolState(TestCase):
|
||||
|
||||
def test_get_set(self):
|
||||
s = VolatileBooleanState(prefix="test1")
|
||||
|
||||
s["a"]["1"] = True
|
||||
s["a"]["2"] = True
|
||||
|
||||
self.assertTrue(s["a"]["1"])
|
||||
self.assertTrue(s["a"]["2"])
|
||||
self.assertFalse(s["a"]["3"])
|
||||
|
||||
def test_iter(self):
|
||||
s = VolatileBooleanState(prefix="test2")
|
||||
|
||||
s["b"]["1"] = True
|
||||
s["b"]["2"] = True
|
||||
s["b"]["3"] = True
|
||||
s["b"]["4"] = True
|
||||
|
||||
self.assertEqual(sum(int(x) for x in s["b"]), 10)
|
||||
|
||||
def test_delete(self):
|
||||
s = VolatileBooleanState(prefix="test3")
|
||||
|
||||
s["c"]["1"] = True
|
||||
self.assertTrue(s["c"]["1"])
|
||||
del s["c"]["1"]
|
||||
self.assertFalse(s["c"]["1"])
|
||||
|
||||
29
test/test_download_file.py
Normal file
29
test/test_download_file.py
Normal file
@@ -0,0 +1,29 @@
|
||||
from unittest import TestCase
|
||||
import os
|
||||
|
||||
from hexlib.web import download_file
|
||||
|
||||
|
||||
class TestDownloadFile(TestCase):
|
||||
|
||||
def test_download_file(self):
|
||||
download_file("http://ovh.net/files/10Mb.dat", "/tmp/10Mb.dat")
|
||||
self.assertTrue(os.path.exists("/tmp/10Mb.dat"))
|
||||
os.remove("/tmp/10Mb.dat")
|
||||
|
||||
def test_download_file_error(self):
|
||||
exceptions = []
|
||||
|
||||
def cb(ex):
|
||||
exceptions.append(ex)
|
||||
|
||||
download_file("http://thisUrlIsInvalid", "/tmp/file.txt", err_cb=cb, retries=3)
|
||||
self.assertFalse(os.path.exists("/tmp/10Mb.dat"))
|
||||
self.assertEqual(len(exceptions), 3)
|
||||
|
||||
def test_download_file_meta(self):
|
||||
download_file("http://ovh.net/files/10Mb.dat", "/tmp/10Mb.dat", save_meta=True)
|
||||
self.assertTrue(os.path.exists("/tmp/10Mb.dat"))
|
||||
self.assertTrue(os.path.exists("/tmp/10Mb.dat.meta"))
|
||||
os.remove("/tmp/10Mb.dat")
|
||||
# os.remove("/tmp/10Mb.dat.meta")
|
||||
Reference in New Issue
Block a user