def test_translation_var(self, mock_logging_error: mock.MagicMock) -> None: """Check for unused template variables.""" self._db.translations.insert_one({ 'string': 'original template', 'en': 'translated template with %known variable', }) maintenance.check_template_variables(self._db) self.assertFalse(mock_logging_error.called, msg=mock_logging_error.call_args)
def test_unused_var(self, mock_logging_error: mock.MagicMock) -> None: """Check for unused template variables.""" self._db.has_template.insert_one({ '_id': 'using-no-variable', 'textTemplate': 'This either', }) maintenance.check_template_variables(self._db) mock_logging_error.assert_called_once() self.assertIn( '%known', mock_logging_error.call_args[0][0] % mock_logging_error.call_args[0][1:])
def test_airtable_dedup(self) -> None: """Template with the same variable twice should only be recorded once.""" self._db.has_template.insert_one({ '_id': 'using-known-variable', 'textTemplate': 'Using a %known variable: %known', }) maintenance.check_template_variables(self._db) records = self._get_airtable_records() self.assertEqual(1, len(records), msg=records)
def test_missing_var(self, mock_logging_error: mock.MagicMock) -> None: """Check for non-implemented template variables.""" self._db.has_template.insert_one({ '_id': 'using-missing', 'textTemplate': 'This is a %missingVariable and a %known variable', }) maintenance.check_template_variables(self._db) mock_logging_error.assert_called_once() error_message = mock_logging_error.call_args[0][0] % mock_logging_error.call_args[0][1:] self.assertIn('%missingVariable', error_message) self.assertIn('using-missing', error_message)
def test_translation_var_missing(self, mock_logging_error: mock.MagicMock) -> None: """Check for unused template variables.""" self._db.translations.insert_one({ 'string': 'original template with %known variable', 'en': 'translated template with %unknown variable', }) self._db.has_template.insert_one({ '_id': 'using-variable', 'textTemplate': 'original template with %known variable', }) maintenance.check_template_variables(self._db) mock_logging_error.assert_called_once() self.assertIn( '%unknown', mock_logging_error.call_args[0][0] % mock_logging_error.call_args[0][1:])
def test_airtable_create(self) -> None: """Check that records are created for each found variable.""" self._db.has_template.insert_one({ '_id': 'using-known-variable', 'textTemplate': 'Using a %known variable', }) maintenance.check_template_variables(self._db) records = self._get_airtable_records() self.assertEqual(1, len(records), msg=records) record = typing.cast(dict[str, str], records[0]['fields']) self.assertEqual('%known', record.get('variable')) self.assertEqual('Using a %known variable', record.get('template')) self.assertEqual('has_template:using-known-variable:textTemplate', record.get('origin'))
def test_airtable_update(self) -> None: """Airtable records are updated if the origin and variable are the same.""" self._db.has_template.insert_one({ '_id': 'using-known-variable', 'textTemplate': 'Using a %known variable', }) maintenance.check_template_variables(self._db) self._db.has_template.replace_one({'_id': 'using-known-variable'}, { 'textTemplate': 'Using a %known variable with another template', }) maintenance.check_template_variables(self._db) records = self._get_airtable_records() self.assertEqual(1, len(records), msg=records) record = typing.cast(dict[str, str], records[0]['fields']) self.assertEqual('%known', record.get('variable')) self.assertEqual('Using a %known variable with another template', record.get('template')) self.assertEqual('has_template:using-known-variable:textTemplate', record.get('origin'))
def test_missing_airtable_key(self, mock_warning: mock.MagicMock) -> None: """Check that user is warned of a missing airtable API key.""" maintenance.check_template_variables(self._db) mock_warning.assert_called_once() self.assertIn('AIRTABLE_API_KEY', mock_warning.call_args[0][0])