예제 #1
0
 def test_bootStrap(self, get_prepositions_mock):
     get_prepositions_mock.return_value = ['of']
     MeasurementParser.bootstrap(TestConfig)
     self.assertEqual(
         {
             'acres': 'imperial_area',
             'yards': 'imperial_length',
             'yard': 'imperial_length',
             'acre': 'imperial_area'
         },
         registry.get('MP_units')
     )
     self.assertEqual(
         {
             'imperial_length': ('Imperial', 'Length'),
             'imperial_area': ('Imperial', 'Area')
         },
         registry.get('MP_systems')
     )
     self.assertIsInstance(
         registry.get('MP_preposition_parser'),
         pyparsing.And
     )
     self.assertIsInstance(
         registry.get('MP_measurement_parser'),
         pyparsing.And
     )
     get_prepositions_mock.assert_called_once_with()
예제 #2
0
 def test_bootStrap(self, get_prepositions_mock):
     get_prepositions_mock.return_value = ['of']
     MeasurementParser.bootstrap(TestConfig)
     self.assertEqual(
         {
             'acres': 'imperial_area',
             'yards': 'imperial_length',
             'yard': 'imperial_length',
             'acre': 'imperial_area'
         }, registry.get('MP_units'))
     self.assertEqual(
         {
             'imperial_length': ('Imperial', 'Length'),
             'imperial_area': ('Imperial', 'Area')
         }, registry.get('MP_systems'))
     self.assertIsInstance(registry.get('MP_preposition_parser'),
                           pyparsing.And)
     self.assertIsInstance(registry.get('MP_measurement_parser'),
                           pyparsing.And)
     get_prepositions_mock.assert_called_once_with()
예제 #3
0
 def setUp(self):
     MeasurementParser.bootstrap(TestConfig())
     self.mp = MeasurementParser(TestConfig())
예제 #4
0
class MeasurementParserTests(unittest.TestCase):
    """Unit Testing of the MeasurementParser"""

    mp = None

    def setUp(self):
        MeasurementParser.bootstrap(TestConfig())
        self.mp = MeasurementParser(TestConfig())

    def tearDown(self):
        registry.flush()
        self.mp = None

    @mock.patch('os.path.dirname', mock_miscTextReturnSingleParameter)
    @mock.patch('os.path.abspath', mock_miscTextReturnSingleParameter)
    @mock.patch('os.path.join', mock_miscTextReturnDoubleParameter)
    @mock.patch('glob.glob', mock_globGlob)
    @mock.patch(BUILTINS_NAME, mock_open)
    @mock.patch.object(DataHandler, 'get_prepositions')
    def test_bootStrap(self, get_prepositions_mock):
        get_prepositions_mock.return_value = ['of']
        MeasurementParser.bootstrap(TestConfig)
        self.assertEqual(
            {
                'acres': 'imperial_area',
                'yards': 'imperial_length',
                'yard': 'imperial_length',
                'acre': 'imperial_area'
            }, registry.get('MP_units'))
        self.assertEqual(
            {
                'imperial_length': ('Imperial', 'Length'),
                'imperial_area': ('Imperial', 'Area')
            }, registry.get('MP_systems'))
        self.assertIsInstance(registry.get('MP_preposition_parser'),
                              pyparsing.And)
        self.assertIsInstance(registry.get('MP_measurement_parser'),
                              pyparsing.And)
        get_prepositions_mock.assert_called_once_with()

    def test_parseBasicUnitYieldsProperResult(self):
        count = 0
        for result in self.mp.parse('inches'):
            self.assertEqual(result.confidence, 100)
            self.assertEqual(result.subtype, 'Imperial Length')
            self.assertEqual(result.result_value, {
                "amount": None,
                "unit": "inches",
                "subject": None
            })
            count += 1
        self.assertEqual(count, 1)

    def test_parseWithSubjectYieldsProperResult(self):
        count = 0
        for result in self.mp.parse('4 yards of fabric'):
            self.assertEqual(result.confidence, 100)
            self.assertEqual(result.subtype, 'Imperial Length')
            self.assertEqual(result.result_value, {
                "amount": "4",
                "unit": "yards",
                "subject": "fabric"
            })
            count += 1
        self.assertEqual(count, 1)

    def test_parseSimpleMeasurementYieldsExpectedConfidence(self):
        count = 0
        for result in self.mp.parse('4 inches'):
            self.assertEqual(result.confidence, 100)
            self.assertEqual(result.subtype, 'Imperial Length')
            self.assertEqual(result.result_value, {
                "amount": "4",
                "unit": "inches",
                "subject": None
            })
            count += 1
        self.assertEqual(count, 1)

    def test_parseWithMultipleUnitTypesYieldsProperSubtype(self):
        count = 0
        for result in self.mp.parse('6 inches and 50 liters of koolaid'):
            if count == 0:
                self.assertEqual(result.confidence, 100)
                self.assertEqual(result.subtype, 'Imperial Length')
                self.assertEqual(result.result_value, {
                    "amount": "6",
                    "unit": "inches",
                    "subject": None
                })
            elif count == 1:
                self.assertEqual(result.confidence, 95)
                self.assertEqual(result.subtype, 'Metric Volume')
                self.assertEqual(result.result_value, {
                    "amount": "50",
                    "unit": "liters",
                    "subject": "koolaid"
                })
            count += 1
        self.assertEqual(count, 2)

    def test_parse_with_long_string_returns(self):
        sample = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' + \
                 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'
        count = 0
        for _ in self.mp.parse(sample):
            count += 1
        self.assertEqual(count, 0)
예제 #5
0
 def setUp(self):
     MeasurementParser.bootstrap(TestConfig())
     self.mp = MeasurementParser(TestConfig())
예제 #6
0
class MeasurementParserTests(unittest.TestCase):
    """Unit Testing of the MeasurementParser"""

    mp = None

    def setUp(self):
        MeasurementParser.bootstrap(TestConfig())
        self.mp = MeasurementParser(TestConfig())

    def tearDown(self):
        registry.flush()
        self.mp = None

    @mock.patch('os.path.dirname', mock_miscTextReturnSingleParameter)
    @mock.patch('os.path.abspath', mock_miscTextReturnSingleParameter)
    @mock.patch('os.path.join', mock_miscTextReturnDoubleParameter)
    @mock.patch('glob.glob', mock_globGlob)
    @mock.patch(BUILTINS_NAME, mock_open)
    @mock.patch.object(DataHandler, 'get_prepositions')
    def test_bootStrap(self, get_prepositions_mock):
        get_prepositions_mock.return_value = ['of']
        MeasurementParser.bootstrap(TestConfig)
        self.assertEqual(
            {
                'acres': 'imperial_area',
                'yards': 'imperial_length',
                'yard': 'imperial_length',
                'acre': 'imperial_area'
            },
            registry.get('MP_units')
        )
        self.assertEqual(
            {
                'imperial_length': ('Imperial', 'Length'),
                'imperial_area': ('Imperial', 'Area')
            },
            registry.get('MP_systems')
        )
        self.assertIsInstance(
            registry.get('MP_preposition_parser'),
            pyparsing.And
        )
        self.assertIsInstance(
            registry.get('MP_measurement_parser'),
            pyparsing.And
        )
        get_prepositions_mock.assert_called_once_with()

    def test_parseBasicUnitYieldsProperResult(self):
        count = 0
        for result in self.mp.parse('inches'):
            self.assertEqual(result.confidence, 100)
            self.assertEqual(result.subtype, 'Imperial Length')
            self.assertEqual(result.result_value, {
                "amount": None,
                "unit": "inches",
                "subject": None
            })
            count += 1
        self.assertEqual(count, 1)

    def test_parseWithSubjectYieldsProperResult(self):
        count = 0
        for result in self.mp.parse('4 yards of fabric'):
            self.assertEqual(result.confidence, 100)
            self.assertEqual(result.subtype, 'Imperial Length')
            self.assertEqual(result.result_value, {
                "amount": "4",
                "unit": "yards",
                "subject": "fabric"
            })
            count += 1
        self.assertEqual(count, 1)

    def test_parseSimpleMeasurementYieldsExpectedConfidence(self):
        count = 0
        for result in self.mp.parse('4 inches'):
            self.assertEqual(result.confidence, 100)
            self.assertEqual(result.subtype, 'Imperial Length')
            self.assertEqual(result.result_value, {
                "amount": "4",
                "unit": "inches",
                "subject": None
            })
            count += 1
        self.assertEqual(count, 1)

    def test_parseWithMultipleUnitTypesYieldsProperSubtype(self):
        count = 0
        for result in self.mp.parse('6 inches and 50 liters of koolaid'):
            if count == 0:
                self.assertEqual(result.confidence, 100)
                self.assertEqual(result.subtype, 'Imperial Length')
                self.assertEqual(result.result_value, {
                    "amount": "6",
                    "unit": "inches",
                    "subject": None
                })
            elif count == 1:
                self.assertEqual(result.confidence, 95)
                self.assertEqual(result.subtype, 'Metric Volume')
                self.assertEqual(result.result_value, {
                    "amount": "50",
                    "unit": "liters",
                    "subject": "koolaid"
                })
            count += 1
        self.assertEqual(count, 2)

    def test_parse_with_long_string_returns(self):
        sample = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' + \
                 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'
        count = 0
        for _ in self.mp.parse(sample):
            count += 1
        self.assertEqual(count, 0)