|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
5 | | -import ipaddress |
6 | 5 | import sys |
7 | 6 | import typing as t |
8 | 7 | import uuid |
| 8 | +from ipaddress import IPv4Interface, IPv4Network, IPv6Interface |
9 | 9 |
|
10 | 10 | import pytest |
11 | | -from sqlalchemy import Column, Integer, MetaData, Table, create_engine, func, select |
| 11 | +from sqlalchemy import ( |
| 12 | + Column, |
| 13 | + Integer, |
| 14 | + MetaData, |
| 15 | + Table, |
| 16 | + create_engine, |
| 17 | + func, |
| 18 | + select, |
| 19 | +) |
12 | 20 |
|
13 | | -from sqlean_driver.custom_types import INET, UUID |
| 21 | +from sqlean_driver.custom_types import CIDR, INET, UUID |
14 | 22 |
|
15 | 23 | if t.TYPE_CHECKING: |
16 | 24 | from sqlalchemy import Select |
17 | 25 |
|
18 | 26 | metadata = MetaData() |
19 | 27 | table = Table( |
20 | | - "table", |
| 28 | + "test_table", |
21 | 29 | metadata, |
22 | 30 | Column("id", Integer, primary_key=True), |
23 | 31 | Column("ip", INET), |
| 32 | + Column("cidr", CIDR), |
24 | 33 | Column("uuid_col", UUID), |
25 | 34 | ) |
26 | 35 |
|
|
39 | 48 | id="nullable", |
40 | 49 | ), |
41 | 50 | pytest.param( |
42 | | - [{"ip": ipaddress.IPv4Network("192.168.1.1")}], |
| 51 | + [{"cidr": None}], |
| 52 | + select(table.c.cidr), |
| 53 | + (None,), |
| 54 | + id="nullable_cidr", |
| 55 | + ), |
| 56 | + pytest.param( |
| 57 | + [{"cidr": IPv4Network("192.168.16.3/32")}], |
| 58 | + select(table.c.cidr), |
| 59 | + (IPv4Network("192.168.16.3/32"),), |
| 60 | + id="cidr", |
| 61 | + ), |
| 62 | + pytest.param( |
| 63 | + [{"ip": IPv4Network("192.168.1.1")}], |
43 | 64 | select(func.ipfamily(table.c.ip), table.c.ip.ipfamily()), |
44 | 65 | (4, 4), |
45 | 66 | id="ipfamily", |
46 | 67 | ), |
47 | 68 | pytest.param( |
48 | | - [{"ip": ipaddress.IPv6Interface("2001:db8::123/64")}], |
| 69 | + [{"ip": IPv6Interface("2001:db8::123/64")}], |
49 | 70 | select(func.iphost(table.c.ip), table.c.ip.iphost()), |
50 | 71 | ("2001:db8::123", "2001:db8::123"), |
51 | 72 | id="iphost", |
52 | 73 | ), |
53 | 74 | pytest.param( |
54 | | - [{"ip": ipaddress.IPv4Interface("192.168.16.12/24")}], |
| 75 | + [{"ip": IPv4Interface("192.168.16.12/24")}], |
55 | 76 | select(func.ipmasklen(table.c.ip), table.c.ip.ipmasklen()), |
56 | 77 | (24, 24), |
57 | 78 | id="ipmasklen", |
58 | 79 | ), |
59 | 80 | pytest.param( |
60 | | - [{"ip": ipaddress.IPv4Interface("192.168.16.12/24")}], |
| 81 | + [{"ip": IPv4Interface("192.168.16.12/24")}], |
61 | 82 | select(func.ipnetwork(table.c.ip), table.c.ip.ipnetwork()), |
62 | 83 | ( |
63 | | - ipaddress.IPv4Network("192.168.16.0/24"), |
64 | | - ipaddress.IPv4Network("192.168.16.0/24"), |
| 84 | + IPv4Network("192.168.16.0/24"), |
| 85 | + IPv4Network("192.168.16.0/24"), |
65 | 86 | ), |
66 | 87 | id="ipnetwork", |
67 | 88 | ), |
68 | 89 | pytest.param( |
69 | | - [{"ip": ipaddress.IPv4Interface("192.168.16.0/24")}], |
| 90 | + [{"ip": IPv4Interface("192.168.16.0/24")}], |
70 | 91 | select( |
71 | 92 | func.ipcontains(table.c.ip, "192.168.16.3"), |
72 | 93 | table.c.ip.ipcontains("192.168.16.3"), |
|
75 | 96 | id="ipcontains_lhs", |
76 | 97 | ), |
77 | 98 | pytest.param( |
78 | | - [{"ip": ipaddress.IPv4Interface("192.168.16.3")}], |
| 99 | + [{"ip": IPv4Interface("192.168.16.3")}], |
79 | 100 | select( |
80 | 101 | func.ipcontains("192.168.16.0/24", table.c.ip), |
81 | 102 | ), |
|
0 commit comments