Debian Codegraph @

ansible_collections/community/dns/tests/unit/plugins/module_utils/test__zone.py

raw
1 # Copyright (c) 2021, Felix Fontein <[email protected]>
2 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
3 # SPDX-License-Identifier: GPL-3.0-or-later
4
5 from __future__ import annotations
6
7 from ansible_collections.community.dns.plugins.module_utils._record import DNSRecord
8 from ansible_collections.community.dns.plugins.module_utils._record_set import (
9 DNSRecordSet,
10 )
11 from ansible_collections.community.dns.plugins.module_utils._zone import (
12 DNSZone,
13 DNSZoneWithRecords,
14 DNSZoneWithRecordSets,
15 )
16
17
18 def test_zone_str_repr():
19 Z1 = DNSZone(zone_id=None, name="foo")
20 assert str(Z1) == "DNSZone(name: foo, info: {})"
21 assert repr(Z1) == "DNSZone(name: foo, info: {})"
22 Z2 = DNSZone(zone_id=42, name="foo")
23 Z2.info["foo"] = "bar"
24 assert str(Z2) == "DNSZone(id: 42, name: foo, info: {'foo': 'bar'})"
25 assert repr(Z2) == "DNSZone(id: 42, name: foo, info: {'foo': 'bar'})"
26
27
28 def test_zone_with_records_str_repr():
29 Z1 = DNSZone(zone_id=None, name="foo")
30 Z2 = DNSZone(zone_id=42, name="foo")
31 A1 = DNSRecord(record_id=None, record_type="A", target="1.2.3.4")
32 A1.prefix = None
33 A1.ttl = 300
34 A2 = DNSRecord(record_id=23, record_type="A", target="")
35 A2.prefix = "bar"
36 A2.ttl = 1
37 A2.extra["foo"] = 23
38 ZZ1 = DNSZoneWithRecords(Z1, [A1])
39 ZZ2 = DNSZoneWithRecords(Z2, [A1, A2])
40 assert (
41 str(ZZ1)
42 == "(DNSZone(name: foo, info: {}), [DNSRecord(type: A, prefix: (none), target: '1.2.3.4', ttl: 5m)])"
43 )
44 assert (
45 repr(ZZ1)
46 == "DNSZoneWithRecords(DNSZone(name: foo, info: {}), [DNSRecord(type: A, prefix: (none), target: '1.2.3.4', ttl: 5m)])"
47 )
48 assert str(ZZ2) == (
49 "(DNSZone(id: 42, name: foo, info: {}), [DNSRecord(type: A, prefix: (none), target: '1.2.3.4', ttl: 5m),"
50 " DNSRecord(id: 23, type: A, prefix: 'bar', target: '', ttl: 1s, extra: {'foo': 23})])"
51 )
52 assert repr(ZZ2) == (
53 "DNSZoneWithRecords(DNSZone(id: 42, name: foo, info: {}), [DNSRecord(type: A, prefix: (none), target: '1.2.3.4', ttl: 5m),"
54 " DNSRecord(id: 23, type: A, prefix: 'bar', target: '', ttl: 1s, extra: {'foo': 23})])"
55 )
56
57
58 def test_zone_with_record_sets_str_repr():
59 Z1 = DNSZone(zone_id=None, name="foo")
60 Z2 = DNSZone(zone_id=42, name="foo")
61 A1 = DNSRecord(record_id=None, record_type="A", target="1.2.3.4")
62 A1.prefix = None
63 A1.ttl = 300
64 A2 = DNSRecord(record_id=23, record_type="A", target="")
65 A2.prefix = "bar"
66 A2.ttl = 1
67 A2.extra["foo"] = 23
68 a1 = DNSRecordSet(record_set_id=None, record_type="A")
69 a1.prefix = None
70 a1.type = "A"
71 a1.ttl = 300
72 a1.records = [A1]
73 a2 = DNSRecordSet(record_set_id=None, record_type="A")
74 a2.prefix = "bar"
75 a2.type = "A"
76 a2.ttl = 1
77 a2.records = [A2]
78 a2.extra = {"baz": "bam"}
79 ZZ1 = DNSZoneWithRecordSets(Z1, [a1])
80 ZZ2 = DNSZoneWithRecordSets(Z2, [a1, a2])
81 print(repr(ZZ1))
82 print(str(ZZ2))
83 print(repr(ZZ2))
84 assert str(ZZ1) == (
85 "(DNSZone(name: foo, info: {}), [DNSRecordSet(type: A, prefix: (none), ttl: 5m, records:"
86 " [DNSRecord(type: A, prefix: (none), target: '1.2.3.4', ttl: 5m)])])"
87 )
88 assert repr(ZZ1) == (
89 "DNSZoneWithRecordSets(DNSZone(name: foo, info: {}), [DNSRecordSet(type: A, prefix: (none),"
90 " ttl: 5m, records: [DNSRecord(type: A, prefix: (none), target: '1.2.3.4', ttl: 5m)])])"
91 )
92 assert str(ZZ2) == (
93 "(DNSZone(id: 42, name: foo, info: {}), [DNSRecordSet(type: A, prefix: (none), ttl: 5m, records:"
94 " [DNSRecord(type: A, prefix: (none), target: '1.2.3.4', ttl: 5m)]), DNSRecordSet(type: A, prefix: 'bar', ttl: 1s,"
95 " records: [DNSRecord(id: 23, type: A, prefix: 'bar', target: '', ttl: 1s, extra: {'foo': 23})], extra: {'baz': 'bam'})])"
96 )
97 assert repr(ZZ2) == (
98 "DNSZoneWithRecordSets(DNSZone(id: 42, name: foo, info: {}), [DNSRecordSet(type: A, prefix: (none),"
99 " ttl: 5m, records: [DNSRecord(type: A, prefix: (none), target: '1.2.3.4', ttl: 5m)]), DNSRecordSet(type: A, prefix:"
100 " 'bar', ttl: 1s, records: [DNSRecord(id: 23, type: A, prefix: 'bar', target: '', ttl: 1s, extra: {'foo': 23})], extra: {'baz': 'bam'})])"
101 )