1try:
2    import btree
3    import uio
4    import uerrno
5except ImportError:
6    print("SKIP")
7    raise SystemExit
8
9# f = open("_test.db", "w+b")
10f = uio.BytesIO()
11db = btree.open(f, pagesize=512)
12
13db[b"foo3"] = b"bar3"
14db[b"foo1"] = b"bar1"
15db[b"foo2"] = b"bar2"
16db[b"bar1"] = b"foo1"
17
18dbstr = str(db)
19print(dbstr[:7], dbstr[-1:])
20
21print(db[b"foo2"])
22try:
23    print(db[b"foo"])
24except KeyError:
25    print("KeyError")
26print(db.get(b"foo"))
27print(db.get(b"foo", b"dflt"))
28
29del db[b"foo2"]
30try:
31    del db[b"foo"]
32except KeyError:
33    print("KeyError")
34
35for k, v in db.items():
36    print((k, v))
37
38print("---")
39for k, v in db.items(None, None):
40    print((k, v))
41
42print("---")
43for k, v in db.items(b"f"):
44    print((k, v))
45
46print("---")
47for k, v in db.items(b"f", b"foo3"):
48    print((k, v))
49
50print("---")
51for k, v in db.items(None, b"foo3"):
52    print((k, v))
53
54print("---")
55for k, v in db.items(b"f", b"foo3", btree.INCL):
56    print((k, v))
57
58print("---")
59for k, v in db.items(None, None, btree.DESC):
60    print((k, v))
61
62print(db.seq(1, b"foo1"))
63print(db.seq(1, b"qux"))
64
65try:
66    db.seq(b"foo1")
67except OSError as e:
68    print(e.args[0] == uerrno.EINVAL)
69
70print(list(db.keys()))
71print(list(db.values()))
72
73for k in db:
74    print(k)
75
76db.put(b"baz1", b"qux1")
77
78print("foo1", "foo1" in db)
79print("foo2", "foo2" in db)
80print("baz1", "baz1" in db)
81
82try:
83    print(db + db[b"foo1"])
84except TypeError:
85    print("TypeError")
86
87db.flush()
88db.close()
89f.close()
90