Пример #1
0
    def test_only__should_return_instance_with_merged_filters(
            self, initial_filters, filters, expect
    ):
        obj = EventLog(self.sdk, 4096, 2, only_filters=initial_filters)

        res = obj.only(**filters)

        assert res.only_filters == expect
Пример #2
0
    def test_refresh__if_device_has_returned_events__should_always_skip_event_type_255(
            self, initial, events_str, expect, expect_len
    ):
        self.sdk.get_rt_log.side_effect = [events_str, []]
        obj = EventLog(self.sdk, 4096, _data=deque(initial))

        res = obj.refresh()

        assert tuple(obj.data) == expect
        assert res == expect_len
Пример #3
0
    def test_refresh__if_device_has_returned_events_and_maxlen_is_set__should_append_data_and_return_count_of_fetched_records(  # noqa
            self, initial, events_str, expect
    ):
        self.sdk.get_rt_log.side_effect = [events_str, []]
        obj = EventLog(self.sdk, 4096, _data=deque(initial, maxlen=3))

        res = obj.refresh()

        assert tuple(obj.data) == expect
        assert res == len(events_str)
Пример #4
0
    def test_only__should_return_new_instance(self):
        obj = EventLog(self.sdk, 4096, 2)

        res = obj.only(card=(2, ))

        assert type(res) == EventLog
        assert res is not obj
        assert res._sdk is obj._sdk
        assert res.buffer_size == obj.buffer_size
        assert res.data.maxlen == obj.data.maxlen
        assert res.only_filters == {'card': {2}}
        assert res.data is obj.data
Пример #5
0
    def test_clear__should_clear_data(self):
        data = deque((
            Event('2000-02-02 15:09:10,0,7125793,1,27,2,0'),
            Event('2000-02-02 15:09:15,0,7125794,3,27,2,0'),
            Event('2000-02-02 15:09:17,0,7125784,1,25,2,0'),
            Event('2000-02-02 15:09:20,0,7125793,3,27,2,0'),
            Event('2000-02-02 15:09:21,0,7125794,2,26,1,0')
        ))
        obj = EventLog(self.sdk, 4096, _data=data)

        obj.clear()

        assert len(obj.data) == 0
Пример #6
0
    def test_poll__if_refresh_returned_events__should_return_new_events(self):
        data = deque((
            Event('2000-02-02 15:09:10,0,7125793,1,27,2,0'),
            Event('2000-02-02 15:09:15,0,7125794,3,27,2,0'),
            Event('2000-02-02 15:09:17,0,7125784,1,25,2,0'),
            Event('2000-02-02 15:09:20,0,7125793,3,27,2,0'),
            Event('2000-02-02 15:09:21,0,7125794,2,26,1,0')
        ))
        obj = EventLog(self.sdk, 4096, _data=data)

        with patch.object(obj, 'refresh', Mock(side_effect=(0, 0, 2))):
            res = obj.poll(60, polling_interval=.5)

            assert res == list(data)[-2:]
Пример #7
0
    def test_poll__if_refresh_returned_events_on_3rd_time__should_hang_for_2_intervals(self):
        data = deque((
            Event('2000-02-02 15:09:10,0,7125793,1,27,2,0'),
            Event('2000-02-02 15:09:15,0,7125794,3,27,2,0'),
            Event('2000-02-02 15:09:17,0,7125784,1,25,2,0'),
            Event('2000-02-02 15:09:20,0,7125793,3,27,2,0'),
            Event('2000-02-02 15:09:21,0,7125794,2,26,1,0')
        ))
        obj = EventLog(self.sdk, 4096, _data=data)

        with patch.object(obj, 'refresh', Mock(side_effect=(0, 0, 2))):
            start = datetime.now()
            obj.poll(60, polling_interval=.5)
            seconds = (datetime.now() - start).seconds

            assert 1 <= seconds < 1.5
Пример #8
0
    def test_before_time__should_return_records_before_datetime_excluded(self):
        data = deque((
            Event('2000-02-02 15:09:10,0,7125793,1,27,2,0'),
            Event('2000-02-02 15:09:15,0,7125794,3,27,2,0'),
            Event('2000-02-02 15:09:17,0,7125784,1,25,2,0'),
            Event('2000-02-02 15:09:20,0,7125793,3,27,2,0'),
            Event('2000-02-02 15:09:21,0,7125794,2,26,1,0')
        ))
        obj = EventLog(self.sdk, 4096, _data=data)
        dt = datetime(2000, 2, 2, 15, 9, 17)

        res = obj.before_time(dt)

        assert tuple(res) == (
            Event('2000-02-02 15:09:10,0,7125793,1,27,2,0'),
            Event('2000-02-02 15:09:15,0,7125794,3,27,2,0')
        )
Пример #9
0
 def setup(self):
     self.sdk = Mock()
     self.event_log = EventLog(self.sdk, 4096)
     self.readers = (
         Reader(self.sdk, self.event_log, 1),
         Reader(self.sdk, self.event_log, 2),
         Reader(self.sdk, self.event_log, 3),
     )
     self.obj = ReaderList(self.sdk, self.event_log, self.readers)
Пример #10
0
    def test_between_time__should_return_records_betwenn_datetimes(self):
        data = deque((
            Event('2000-02-02 15:09:10,0,7125793,1,27,2,0'),
            Event('2000-02-02 15:09:15,0,7125794,3,27,2,0'),
            Event('2000-02-02 15:09:17,0,7125784,1,25,2,0'),
            Event('2000-02-02 15:09:20,0,7125793,3,27,2,0'),
            Event('2000-02-02 15:09:21,0,7125794,2,26,1,0')
        ))
        obj = EventLog(self.sdk, 4096, _data=data)
        dt1 = datetime(2000, 2, 2, 15, 9, 15)
        dt2 = datetime(2000, 2, 2, 15, 9, 20)

        res = obj.between_time(dt1, dt2)

        assert tuple(res) == (
            Event('2000-02-02 15:09:15,0,7125794,3,27,2,0'),
            Event('2000-02-02 15:09:17,0,7125784,1,25,2,0'),
        )
Пример #11
0
 def setup(self):
     self.sdk = Mock()
     self.event_log = EventLog(self.sdk, 4096)
     self.aux_inputs = (
         AuxInput(self.sdk, self.event_log, 1),
         AuxInput(self.sdk, self.event_log, 2),
         AuxInput(self.sdk, self.event_log, 3),
     )
     self.obj = AuxInputList(self.sdk, self.event_log, self.aux_inputs)
Пример #12
0
    def test_init__should_initialize_attributes(self):
        data = deque()
        filters = {'filter': {'value'}}
        obj = EventLog(self.sdk, 4096, 10, filters)

        assert obj._sdk is self.sdk
        assert obj.buffer_size == 4096
        assert obj.data.maxlen == 10
        assert obj.only_filters == filters
        assert type(obj.data) == deque
Пример #13
0
    def test_init__should_initialize_data_attribute(self):
        data = deque()
        filters = {'filter': {'value'}}
        obj = EventLog(self.sdk, 4096, 10, filters, data)

        assert obj._sdk is self.sdk
        assert obj.buffer_size == 4096
        assert obj.data.maxlen == None  # When _data is passed
        assert obj.only_filters == filters
        assert obj.data is data
Пример #14
0
    def test_refresh__if_device_has_returned_events__and_filters_and_maxlen_are_set__should_append_filtered_data_and_return_count_of_matched_records(self):  # noqa
        events_strings = [
            '2000-02-02 15:09:10,0,7125793,1,27,2,0',
            '2000-02-02 15:09:15,0,7125794,3,27,2,0',
            '2000-02-02 15:09:17,0,7125784,1,25,2,0',
            '2000-02-02 15:09:20,0,7125793,3,27,2,0',
            '2000-02-02 15:09:21,0,7125794,2,26,1,0'
        ]
        events_inserted = (
            Event('2000-02-02 15:09:20,0,7125793,3,27,2,0'),
            Event('2000-02-02 15:09:21,0,7125794,2,26,1,0')
        )
        self.sdk.get_rt_log.side_effect = [events_strings, []]
        obj = EventLog(self.sdk, 4096, 2, only_filters={'card': {'7125794', '7125784'}})

        res = obj.refresh()

        assert tuple(obj.data) == events_inserted  # 2 records inserted due to maxlen
        assert res == 3  # 3 records matched
Пример #15
0
    def test_getitem__if_slice_passed__should_return_items(self, idx):
        data = deque((
            Event('2000-02-02 15:09:10,0,7125793,1,27,2,0'),
            Event('2000-02-02 15:09:15,0,7125794,3,27,2,0'),
            Event('2000-02-02 15:09:17,0,7125784,1,25,2,0'),
            Event('2000-02-02 15:09:20,0,7125793,3,27,2,0'),
            Event('2000-02-02 15:09:21,0,7125794,2,26,1,0')
        ))
        obj = EventLog(self.sdk, 4096, _data=data)

        assert list(obj[idx]) == list(data)[idx]
Пример #16
0
    def test_len__should_return_data_length(self):
        data = deque((
            Event('2000-02-02 15:09:10,0,7125793,1,27,2,0'),
            Event('2000-02-02 15:09:15,0,7125794,3,27,2,0'),
            Event('2000-02-02 15:09:17,0,7125784,1,25,2,0'),
            Event('2000-02-02 15:09:20,0,7125793,3,27,2,0'),
            Event('2000-02-02 15:09:21,0,7125794,2,26,1,0')
        ))
        obj = EventLog(self.sdk, 4096, _data=data)

        assert len(obj) == len(data)
Пример #17
0
    def test_poll__if_refresh_does_not_returned_result__should_return_empty_result_on_timeout(self):
        def refresh_with_slow_network():
            time.sleep(.2)
            return 0

        data = deque((
            Event('2000-02-02 15:09:10,0,7125793,1,27,2,0'),
            Event('2000-02-02 15:09:15,0,7125794,3,27,2,0'),
            Event('2000-02-02 15:09:17,0,7125784,1,25,2,0'),
            Event('2000-02-02 15:09:20,0,7125793,3,27,2,0'),
            Event('2000-02-02 15:09:21,0,7125794,2,26,1,0')
        ))
        obj = EventLog(self.sdk, 4096, _data=data)

        with patch.object(obj, 'refresh', Mock(wraps=refresh_with_slow_network)):
            start = datetime.now()
            res = obj.poll(2, polling_interval=.5)
            seconds = (datetime.now() - start).seconds

            assert 2 <= seconds < 2.5
            assert res == []
Пример #18
0
    def test_poll__if_refresh_returned_events_and_filters_specified__should_return_new_filtered_events(  # noqa
            self
    ):
        data = [
            Event('2000-02-02 15:09:10,0,7125793,1,27,2,0'),
            Event('2000-02-02 15:09:15,0,7125794,3,27,2,0'),
            Event('2000-02-02 15:09:17,0,7125784,1,25,2,0'),
            Event('2000-02-02 15:09:20,0,7125793,3,27,2,0'),
            Event('2000-02-02 15:09:21,0,7125794,2,26,1,0')
        ]
        expect = (
            Event('2000-02-02 15:09:15,0,7125794,3,27,2,0'),
            Event('2000-02-02 15:09:17,0,7125784,1,25,2,0'),
            Event('2000-02-02 15:09:21,0,7125794,2,26,1,0')
        )
        obj = EventLog(self.sdk, 4096, only_filters={'card': {'7125794', '7125784'}})

        with patch.object(obj, '_pull_events', Mock(side_effect=([], [], data, []))):
            res = obj.poll(60, polling_interval=.5)

            assert tuple(res) == expect
Пример #19
0
    def test_getitem__if_index_passed_and_filters_specified__should_return_filtered_items(self):
        data = deque((
            Event('2000-02-02 15:09:10,0,7125793,1,27,2,0'),
            Event('2000-02-02 15:09:15,0,7125794,3,27,2,0'),
            Event('2000-02-02 15:09:17,0,7125784,1,25,2,0'),
            Event('2000-02-02 15:09:20,0,7125793,3,27,2,0'),
            Event('2000-02-02 15:09:21,0,7125794,2,26,1,0')
        ))
        obj = EventLog(self.sdk, 4096, only_filters={'card': {'7125794', '7125793'}}, _data=data)

        assert type(obj[2]) == Event
        assert obj[2] == data[3]
Пример #20
0
    def test_getitem__if_index_passed_and_out_or_range__should_raise_error(self):
        data = deque((
            Event('2000-02-02 15:09:10,0,7125793,1,27,2,0'),
            Event('2000-02-02 15:09:15,0,7125794,3,27,2,0'),
            Event('2000-02-02 15:09:17,0,7125784,1,25,2,0'),
            Event('2000-02-02 15:09:20,0,7125793,3,27,2,0'),
            Event('2000-02-02 15:09:21,0,7125794,2,26,1,0')
        ))
        obj = EventLog(self.sdk, 4096, _data=data)

        with pytest.raises(IndexError):
            _ = obj[10]
Пример #21
0
    def test_getitem__if_index_passed__should_return_item(self):
        data = deque((
            Event('2000-02-02 15:09:10,0,7125793,1,27,2,0'),
            Event('2000-02-02 15:09:15,0,7125794,3,27,2,0'),
            Event('2000-02-02 15:09:17,0,7125784,1,25,2,0'),
            Event('2000-02-02 15:09:20,0,7125793,3,27,2,0'),
            Event('2000-02-02 15:09:21,0,7125794,2,26,1,0')
        ))
        obj = EventLog(self.sdk, 4096, _data=data)

        assert type(obj[2]) == Event
        assert obj[2] == data[2]
Пример #22
0
    def test_getitem__if_index_passed_and_filters_specified_and_out_of_range__should_raise_error(
            self
    ):
        data = deque((
            Event('2000-02-02 15:09:10,0,7125793,1,27,2,0'),
            Event('2000-02-02 15:09:15,0,7125794,3,27,2,0'),
            Event('2000-02-02 15:09:17,0,7125784,1,25,2,0'),
            Event('2000-02-02 15:09:20,0,7125793,3,27,2,0'),
            Event('2000-02-02 15:09:21,0,7125794,2,26,1,0')
        ))
        obj = EventLog(self.sdk, 4096, only_filters={'card': {'7125794', '7125793'}}, _data=data)

        with pytest.raises(IndexError):
            _ = obj[4]
Пример #23
0
 def setup(self):
     self.sdk = Mock()
     self.event_log = EventLog(self.sdk, 4096)
     self.door_number = 2
     self.relays = (
         Relay(self.sdk, RelayGroup.lock, 1),
         Relay(self.sdk, RelayGroup.aux, 1),
     )
     self.relay_list = RelayList(self.sdk, self.relays)
     self.reader = Reader(self.sdk, self.event_log, self.door_number)
     self.aux_input = AuxInput(self.sdk, self.event_log, self.door_number)
     self.parameters = DoorParameters(self.sdk, ZK200, self.door_number)
     self.obj = Door(self.sdk, self.event_log, self.door_number,
                     self.relay_list, self.reader, self.aux_input,
                     self.parameters)
Пример #24
0
    def test_iter__if_filters_specified__should_iter_over_filtered_data(self):
        data = deque((
            Event('2000-02-02 15:09:10,0,7125793,1,27,2,0'),
            Event('2000-02-02 15:09:15,0,7125794,3,27,2,0'),
            Event('2000-02-02 15:09:17,0,7125784,1,25,2,0'),
            Event('2000-02-02 15:09:20,0,7125793,3,27,2,0'),
            Event('2000-02-02 15:09:21,0,7125794,2,26,1,0')
        ))
        consider_data = [
            Event('2000-02-02 15:09:10,0,7125793,1,27,2,0'),
            Event('2000-02-02 15:09:15,0,7125794,3,27,2,0'),
            Event('2000-02-02 15:09:20,0,7125793,3,27,2,0'),
            Event('2000-02-02 15:09:21,0,7125794,2,26,1,0')
        ]
        obj = EventLog(self.sdk, 4096, only_filters={'card': {'7125794', '7125793'}}, _data=data)

        assert list(obj) == consider_data
Пример #25
0
    def test_getitem__if_slice_passed_and_filters_specified__should_return_filtered_items(
            self, idx
    ):
        data = deque((
            Event('2000-02-02 15:09:10,0,7125793,1,27,2,0'),
            Event('2000-02-02 15:09:15,0,7125794,3,27,2,0'),
            Event('2000-02-02 15:09:17,0,7125784,1,25,2,0'),
            Event('2000-02-02 15:09:20,0,7125793,3,27,2,0'),
            Event('2000-02-02 15:09:21,0,7125794,2,26,1,0')
        ))
        consider_data = [
            Event('2000-02-02 15:09:10,0,7125793,1,27,2,0'),
            Event('2000-02-02 15:09:15,0,7125794,3,27,2,0'),
            Event('2000-02-02 15:09:20,0,7125793,3,27,2,0'),
            Event('2000-02-02 15:09:21,0,7125794,2,26,1,0')
        ]
        obj = EventLog(self.sdk, 4096, only_filters={'card': {'7125794', '7125793'}}, _data=data)

        assert list(obj[idx]) == consider_data[idx]
Пример #26
0
 def setup(self):
     self.sdk = Mock()
     self.event_log = EventLog(self.sdk, 4096)
     self.doors = (Door(
         self.sdk, self.event_log, 1,
         RelayList(self.sdk,
                   (Relay(self.sdk, RelayGroup.lock,
                          1), Relay(self.sdk, RelayGroup.aux, 1))),
         Reader(self.sdk, self.event_log, 1),
         AuxInput(self.sdk, self.event_log, 1),
         DoorParameters(self.sdk, ZK200, 1)),
                   Door(
                       self.sdk, self.event_log, 2,
                       RelayList(self.sdk,
                                 (Relay(self.sdk, RelayGroup.lock, 2),
                                  Relay(self.sdk, RelayGroup.aux, 2))),
                       Reader(self.sdk, self.event_log, 2),
                       AuxInput(self.sdk, self.event_log, 2),
                       DoorParameters(self.sdk, ZK200, 2)))
     self.obj = DoorList(self.sdk, self.event_log, self.doors)
Пример #27
0
    def test_init__if_data_is_omitted__should_create_empty_deque_with_given_maxlen(self):
        obj = EventLog(self.sdk, 4096, 10)

        assert type(obj.data) == deque
        assert obj.data.maxlen == 10
        assert len(obj.data) == 0
Пример #28
0
    def test_init__if_filters_are_omitted__should_create_empty_filters_dict(self):
        obj = EventLog(self.sdk, 4096)

        assert obj.only_filters == {}
Пример #29
0
    def test_repr__should_return_name_of_class(self):
        obj = EventLog(self.sdk, 4096)

        assert repr(obj).startswith('EventLog[')
Пример #30
0
 def setup(self):
     self.sdk = Mock()
     self.event_log = EventLog(self.sdk, 4096)