File: test_introspector.py

package info (click to toggle)
python-rebulk 3.3.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 752 kB
  • sloc: python: 7,497; makefile: 3
file content (138 lines) | stat: -rw-r--r-- 5,089 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Introspector tests
"""
# pylint: disable=pointless-statement,missing-docstring,protected-access,invalid-name,len-as-condition
from ..rebulk import Rebulk
from .. import introspector
from .default_rules_module import RuleAppend2, RuleAppend3


def test_string_introspector():
    rebulk = Rebulk().string('One', 'Two', 'Three', name='first').string('1', '2', '3', name='second')

    introspected = introspector.introspect(rebulk, None)

    assert len(introspected.patterns) == 2

    first_properties = introspected.patterns[0].properties
    assert len(first_properties) == 1
    first_properties['first'] == ['One', 'Two', 'Three']

    second_properties = introspected.patterns[1].properties
    assert len(second_properties) == 1
    second_properties['second'] == ['1', '2', '3']

    properties = introspected.properties
    assert len(properties) == 2
    assert properties['first'] == first_properties['first']
    assert properties['second'] == second_properties['second']


def test_string_properties():
    rebulk = Rebulk()\
        .string('One', 'Two', 'Three', name='first', properties={'custom': ['One']})\
        .string('1', '2', '3', name='second', properties={'custom': [1]})

    introspected = introspector.introspect(rebulk, None)

    assert len(introspected.patterns) == 2
    assert len(introspected.rules) == 2

    first_properties = introspected.patterns[0].properties
    assert len(first_properties) == 1
    first_properties['custom'] == ['One']

    second_properties = introspected.patterns[1].properties
    assert len(second_properties) == 1
    second_properties['custom'] == [1]

    properties = introspected.properties
    assert len(properties) == 1
    assert properties['custom'] == ['One', 1]


def test_various_pattern():
    rebulk = Rebulk()\
        .regex('One', 'Two', 'Three', name='first', value="string") \
        .string('1', '2', '3', name='second', value="digit") \
        .string('4', '5', '6', name='third') \
        .string('private', private=True) \
        .functional(lambda string: (0, 5), name='func', value='test') \
        .regex('One', 'Two', 'Three', name='regex_name') \
        .regex('(?P<one>One)(?P<two>Two)(?P<three>Three)') \
        .functional(lambda string: (6, 10), name='func2') \
        .string('7', name='third')

    introspected = introspector.introspect(rebulk, None)

    assert len(introspected.patterns) == 8
    assert len(introspected.rules) == 2

    first_properties = introspected.patterns[0].properties
    assert len(first_properties) == 1
    first_properties['first'] == ['string']

    second_properties = introspected.patterns[1].properties
    assert len(second_properties) == 1
    second_properties['second'] == ['digit']

    third_properties = introspected.patterns[2].properties
    assert len(third_properties) == 1
    third_properties['third'] == ['4', '5', '6']

    func_properties = introspected.patterns[3].properties
    assert len(func_properties) == 1
    func_properties['func'] == ['test']

    regex_name_properties = introspected.patterns[4].properties
    assert len(regex_name_properties) == 1
    regex_name_properties['regex_name'] == [None]

    regex_groups_properties = introspected.patterns[5].properties
    assert len(regex_groups_properties) == 3
    regex_groups_properties['one'] == [None]
    regex_groups_properties['two'] == [None]
    regex_groups_properties['three'] == [None]

    func2_properties = introspected.patterns[6].properties
    assert len(func2_properties) == 1
    func2_properties['func2'] == [None]

    append_third_properties = introspected.patterns[7].properties
    assert len(append_third_properties) == 1
    append_third_properties['third'] == [None]

    properties = introspected.properties
    assert len(properties) == 9
    assert properties['first'] == first_properties['first']
    assert properties['second'] == second_properties['second']
    assert properties['third'] == third_properties['third'] + append_third_properties['third']
    assert properties['func'] == func_properties['func']
    assert properties['regex_name'] == regex_name_properties['regex_name']
    assert properties['one'] == regex_groups_properties['one']
    assert properties['two'] == regex_groups_properties['two']
    assert properties['three'] == regex_groups_properties['three']
    assert properties['func2'] == func2_properties['func2']


def test_rule_properties():
    rebulk = Rebulk(default_rules=False).rules(RuleAppend2, RuleAppend3)

    introspected = introspector.introspect(rebulk, None)

    assert len(introspected.rules) == 2
    assert len(introspected.patterns) == 0

    rule_properties = introspected.rules[0].properties
    assert len(rule_properties) == 1
    assert rule_properties['renamed'] == [None]

    rule_properties = introspected.rules[1].properties
    assert len(rule_properties) == 1
    assert rule_properties['renamed'] == [None]

    properties = introspected.properties
    assert len(properties) == 1
    assert properties['renamed'] == [None]