File: ContextManagerTest.py

package info (click to toggle)
adapt 1.0.0-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 344 kB
  • sloc: python: 2,112; sh: 50; makefile: 3
file content (112 lines) | stat: -rw-r--r-- 4,606 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
# Copyright 2018 Mycroft AI Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import unittest

from adapt.context import ContextManager, ContextManagerFrame


class ContextManagerFrameTest(unittest.TestCase):
    def setUp(self):
        pass

    def testMetadataMatches(self):
        frame1 = ContextManagerFrame(entities=['foo'],
                                     metadata={'domain': 'music',
                                               'foo': 'test'})

        self.assertTrue(frame1.metadata_matches({'domain': 'music'}),
                        "Should match subset of metadata")

        self.assertFalse(frame1.metadata_matches({'domain': 'weather'}),
                         "Should not match metadata value mismatch")
        self.assertTrue(
            frame1.metadata_matches({'domain': 'music', 'foo': 'test'}),
            "Should match exact metadata")
        self.assertFalse(frame1.metadata_matches(
            {'domain': 'music', 'foo': 'test', 'bar': 'test'}),
            "Should not match superset of metadata")

    def testMergeContext(self):
        frame1 = ContextManagerFrame(entities=['foo'],
                                     metadata={'domain': 'music',
                                               'foo': 'test'})

        self.assertFalse(frame1.metadata_matches({'bar': 'test'}),
                         "Should not match before merging context")

        frame1.merge_context('bar', {'domain': 'music', 'bar': 'test'})
        self.assertTrue(frame1.metadata_matches({'domain': 'music'}),
                        "Should continue to match subset of metadata")
        self.assertTrue(frame1.metadata_matches({'bar': 'test'}),
                        "Should match after merging context")


class ContextManagerTest(unittest.TestCase):
    def setUp(self):
        pass

    def testInjectRetrieveContext(self):
        manager = ContextManager()
        entity = {'key': 'Grapes of Wrath', 'data': 'Book', 'confidence': 1.0}
        manager.inject_context(entity)
        context = manager.get_context()
        assert len(context) == 1
        assert context[0].get('confidence') == 0.5

    def testInjectRetrieveContextMaxFrames(self):
        manager = ContextManager()
        entity = {'key': 'The hitchhikers guide to the galaxy',
                  'data': 'Book', 'confidence': 1.0}
        manager.inject_context(entity)
        context = manager.get_context(max_frames=2)
        assert len(context) == 1
        assert context[0].get('confidence') == 0.5

    def testNewContextNoMetadataResultsInNewFrame(self):
        manager = ContextManager()
        entity1 = {'key': 'Grapes of Wrath', 'data': 'Book', 'confidence': 1.0}
        manager.inject_context(entity1)
        context = manager.get_context()
        assert len(context) == 1
        assert context[0].get('confidence') == 0.5

        entity2 = {'key': 'Wrath of Khan', 'data': 'Film', 'confidence': 1.0}
        manager.inject_context(entity2)
        assert len(manager.frame_stack) == 2
        context = manager.get_context()
        assert len(context) == 2
        assert context[0].get('confidence') == 0.5
        assert context[0].get('data') == 'Film'
        assert context[1].get('confidence') == 1.0 / 3.0
        assert context[1].get('data') == 'Book'

    def testNewContextWithMetadataSameFrame(self):
        manager = ContextManager()
        entity1 = {'key': 'Grapes of Wrath', 'data': 'Book', 'confidence': 1.0}
        manager.inject_context(entity1, {'domain': 'media'})
        context = manager.get_context()
        assert len(context) == 1
        assert context[0].get('confidence') == 0.5

        entity2 = {'key': 'Wrath of Khan', 'data': 'Film', 'confidence': 1.0}
        manager.inject_context(entity2, {'domain': 'media'})
        assert len(manager.frame_stack) == 1
        context = manager.get_context()
        assert len(context) == 2
        assert context[0].get('confidence') == 0.5
        assert context[0].get('data') == 'Book'
        assert context[1].get('confidence') == 0.5
        assert context[1].get('data') == 'Film'