anki-connect/tests/test_models.py
Venkata Ramana 131a1c7cfc
Added findAndReplaceInModels (#228)
* added findAndReplaceInModels api call

* added documentation for findAndReplaceInModels

* added findAndReplaceInModels api call

* updatd findAndReplaceInModels function

* added unit test for findAndReplaceInModels in models

* updated findAndReplaceInModels

* added unit test for findAndReplaceInModels

* removed 'all_models' in findAndReplaceInModels
2021-02-26 21:17:51 -08:00

66 lines
2.6 KiB
Python
Executable File

#!/usr/bin/env python
import unittest
import util
import uuid
MODEL_1_NAME = str(uuid.uuid4())
MODEL_2_NAME = str(uuid.uuid4())
CSS = 'some random css'
NEW_CSS = 'new random css'
CARD_1_TEMPLATE = {'Front': 'field1', 'Back': 'field2'}
NEW_CARD_1_TEMPLATE = {'Front': 'question: field1', 'Back': 'answer: field2'}
TEXT_TO_REPLACE = "new random css"
REPLACE_WITH_TEXT = "new updated css"
class TestModels(unittest.TestCase):
def runTest(self):
# modelNames
modelNames = util.invoke('modelNames')
self.assertGreater(len(modelNames), 0)
# modelNamesAndIds
modelNamesAndIds = util.invoke('modelNamesAndIds')
self.assertGreater(len(modelNames), 0)
# modelFieldNames
modelFields = util.invoke('modelFieldNames', modelName=modelNames[0])
# modelFieldsOnTemplates
modelFieldsOnTemplates = util.invoke('modelFieldsOnTemplates', modelName=modelNames[0])
# createModel with css
newModel = util.invoke('createModel', modelName=MODEL_1_NAME, inOrderFields=['field1', 'field2'], cardTemplates=[CARD_1_TEMPLATE], css=CSS)
# createModel without css
newModel = util.invoke('createModel', modelName=MODEL_2_NAME, inOrderFields=['field1', 'field2'], cardTemplates=[CARD_1_TEMPLATE])
# modelStyling: get model 1 css
css = util.invoke('modelStyling', modelName=MODEL_1_NAME)
self.assertEqual({'css': CSS}, css)
# modelTemplates: get model 1 templates
templates = util.invoke('modelTemplates', modelName=MODEL_1_NAME)
self.assertEqual({'Card 1': CARD_1_TEMPLATE}, templates)
# updateModelStyling: change and verify model css
util.invoke('updateModelStyling', model={'name': MODEL_1_NAME, 'css': NEW_CSS})
new_css = util.invoke('modelStyling', modelName=MODEL_1_NAME)
self.assertEqual({'css': NEW_CSS}, new_css)
# updateModelTemplates: change and verify model 1 templates
util.invoke('updateModelTemplates', model={'name': MODEL_1_NAME, 'templates': {'Card 1': NEW_CARD_1_TEMPLATE}})
templates = util.invoke('modelTemplates', modelName=MODEL_1_NAME)
self.assertEqual({'Card 1': NEW_CARD_1_TEMPLATE}, templates)
# findAndReplaceInModels: find and replace text in all models or model by name
util.invoke('findAndReplaceInModels', modelName=MODEL_1_NAME, findText=TEXT_TO_REPLACE, replaceText=REPLACE_WITH_TEXT, front=True, back=True, css=True)
new_css = util.invoke('modelStyling', modelName=MODEL_1_NAME)
self.assertEqual({'css': REPLACE_WITH_TEXT}, new_css)
if __name__ == '__main__':
unittest.main()