def test_has_ngrams(self): store = tacl.DataStore(':memory:') store._conn = MagicMock(spec_set=sqlite3.Connection) cursor = store._conn.execute.return_value store._conn.execute.return_value = cursor # Path one: there are n-grams. cursor.fetchone.return_value = True actual_result = store._has_ngrams(sentinel.text_id, sentinel.size) self.assertEqual(store._conn.mock_calls, [ call.execute(tacl.constants.SELECT_HAS_NGRAMS_SQL, [sentinel.text_id, sentinel.size]), call.execute().fetchone() ]) self.assertEqual(actual_result, True) # Path two: there are no n-grams. store._conn.reset_mock() cursor.reset_mock() cursor.fetchone.return_value = None actual_result = store._has_ngrams(sentinel.text_id, sentinel.size) self.assertEqual(store._conn.mock_calls, [ call.execute(tacl.constants.SELECT_HAS_NGRAMS_SQL, [sentinel.text_id, sentinel.size]), call.execute().fetchone() ]) self.assertEqual(actual_result, False)
def test_with_previous(self): previous = Mock() previous.execute.return_value = ["3", "4"] ce = CommandExecutor(self._logger, self._command, previous) ce.execute(["1", "2"]) self.assertEqual([call.execute(["1", "2"])], previous.mock_calls) self.assertEqual([call.execute("3", "4")], self._command.mock_calls)
def test_add_temporary_ngrams (self): store = tacl.DataStore(':memory:') store._conn = MagicMock(spec_set=sqlite3.Connection) store._add_temporary_ngrams([sentinel.ngram1, sentinel.ngram2]) self.assertEqual( store._conn.mock_calls, [call.execute(tacl.constants.DROP_TEMPORARY_NGRAMS_TABLE_SQL), call.execute(tacl.constants.CREATE_TEMPORARY_NGRAMS_TABLE_SQL), call.executemany(tacl.constants.INSERT_TEMPORARY_NGRAM_SQL, [(sentinel.ngram1,), (sentinel.ngram2,)])])
def test_add_temporary_ngrams(self): store = tacl.DataStore(':memory:') store._conn = MagicMock(spec_set=sqlite3.Connection) store._add_temporary_ngrams(['A', 'B']) self.assertEqual(store._conn.mock_calls, [ call.execute(tacl.constants.DROP_TEMPORARY_NGRAMS_TABLE_SQL), call.execute(tacl.constants.CREATE_TEMPORARY_NGRAMS_TABLE_SQL), call.executemany(tacl.constants.INSERT_TEMPORARY_NGRAM_SQL, [('A', ), ('B', )]) ])
def test_delete_text_ngrams (self): store = tacl.DataStore(':memory:') store._conn = MagicMock(spec_set=sqlite3.Connection) store._delete_text_ngrams(sentinel.text_id) expected_calls = [ call.execute(tacl.constants.DELETE_TEXT_NGRAMS_SQL, [sentinel.text_id]), call.execute(tacl.constants.DELETE_TEXT_HAS_NGRAMS_SQL, [sentinel.text_id]), call.commit()] self.assertEqual(store._conn.mock_calls, expected_calls)
def test_delete_text_ngrams(self): store = tacl.DataStore(':memory:') store._conn = MagicMock(spec_set=sqlite3.Connection) store._delete_text_ngrams(sentinel.text_id) expected_calls = [ call.execute(tacl.constants.DELETE_TEXT_NGRAMS_SQL, [sentinel.text_id]), call.execute(tacl.constants.DELETE_TEXT_HAS_NGRAMS_SQL, [sentinel.text_id]), call.commit() ] self.assertEqual(store._conn.mock_calls, expected_calls)
def test_restore_shed_gitignore_with_sibling_jazzignore(self, shellmock): with open( testhelper.getrelativefilename( './resources/test_ignore_git_status_z.txt'), 'r') as file: with patch('os.path.exists', return_value=True ): # answer inquries for sibling .jazzignore with True Commiter.restore_shed_gitignore(file.readlines()) calls = [ call.execute('git checkout -- project1/src/.gitignore'), call.execute('git checkout -- project1/src/sub/.gitignore') ] shellmock.assert_has_calls(calls)
def test_diff_asymmetric(self): labels = {sentinel.label: 1, sentinel.prime_label: 1} set_labels = self._create_patch('tacl.DataStore._set_labels') set_labels.return_value = labels get_placeholders = self._create_patch( 'tacl.DataStore._get_placeholders', False) get_placeholders.return_value = sentinel.placeholders log_query_plan = self._create_patch('tacl.DataStore._log_query_plan', False) input_fh = MagicMock(name='fh') catalogue = MagicMock(name='catalogue') store = tacl.DataStore(':memory:') store._conn = MagicMock(spec_set=sqlite3.Connection) tokenizer = MagicMock(name='tokenizer') _diff = self._create_patch('tacl.DataStore._diff', False) _diff.return_value = input_fh output_fh = store.diff_asymmetric(catalogue, sentinel.prime_label, tokenizer, input_fh) set_labels.assert_called_once_with(store, catalogue) get_placeholders.assert_called_once_with([sentinel.label]) self.assertTrue(log_query_plan.called) sql = tacl.constants.SELECT_DIFF_ASYMMETRIC_SQL.format( sentinel.placeholders) self.assertEqual(store._conn.mock_calls, [call.execute(sql, [sentinel.prime_label, sentinel.prime_label, sentinel.label])]) self.assertTrue(_diff.called) self.assertEqual(input_fh, output_fh)
def test_diff_asymmetric (self): labels = {sentinel.label: 1, sentinel.prime_label: 1} set_labels = self._create_patch('tacl.DataStore._set_labels') set_labels.return_value = labels get_placeholders = self._create_patch( 'tacl.DataStore._get_placeholders', False) get_placeholders.return_value = sentinel.placeholders log_query_plan = self._create_patch('tacl.DataStore._log_query_plan', False) input_fh = MagicMock(name='fh') csv = self._create_patch('tacl.DataStore._csv', False) csv.return_value = input_fh catalogue = MagicMock(name='catalogue') store = tacl.DataStore(':memory:') store._conn = MagicMock(spec_set=sqlite3.Connection) cursor = store._conn.execute.return_value output_fh = store.diff_asymmetric(catalogue, sentinel.prime_label, input_fh) set_labels.assert_called_once_with(store, catalogue) get_placeholders.assert_called_once_with([sentinel.label]) log_query_plan.assert_called_once() sql = tacl.constants.SELECT_DIFF_ASYMMETRIC_SQL.format( sentinel.placeholders) self.assertEqual(store._conn.mock_calls, [call.execute(sql, [sentinel.prime_label, sentinel.prime_label, sentinel.label])]) csv.assert_called_once_with(cursor, tacl.constants.QUERY_FIELDNAMES, input_fh) self.assertEqual(input_fh, output_fh)
def test_intersection (self): labels = [sentinel.label1, sentinel.label2] set_labels = self._create_patch('tacl.DataStore._set_labels') set_labels.return_value = {} sort_labels = self._create_patch('tacl.DataStore._sort_labels', False) sort_labels.return_value = labels get_placeholders = self._create_patch( 'tacl.DataStore._get_placeholders', False) get_placeholders.return_value = sentinel.placeholders log_query_plan = self._create_patch('tacl.DataStore._log_query_plan', False) input_fh = MagicMock(name='fh') csv = self._create_patch('tacl.DataStore._csv', False) csv.return_value = input_fh catalogue = MagicMock(name='catalogue') store = tacl.DataStore(':memory:') store._conn = MagicMock(spec_set=sqlite3.Connection) cursor = store._conn.execute.return_value output_fh = store.intersection(catalogue, input_fh) set_labels.assert_called_once_with(store, catalogue) get_placeholders.assert_called_once_with(labels) log_query_plan.assert_called_once() sql = 'SELECT TextNGram.ngram, TextNGram.size, TextNGram.count, Text.name AS "text name", Text.siglum, Text.label FROM Text, TextNGram WHERE Text.label IN (sentinel.placeholders) AND Text.id = TextNGram.text AND TextNGram.ngram IN (SELECT TextNGram.ngram FROM Text, TextNGram WHERE Text.label = ? AND Text.id = TextNGram.text AND TextNGram.ngram IN (SELECT TextNGram.ngram FROM Text, TextNGram WHERE Text.label = ? AND Text.id = TextNGram.text))' self.assertEqual(store._conn.mock_calls, [call.execute(sql, labels * 2)]) csv.assert_called_once_with(cursor, tacl.constants.QUERY_FIELDNAMES, input_fh) self.assertEqual(input_fh, output_fh)
def test_intersection(self): labels = [sentinel.label1, sentinel.label2] set_labels = self._create_patch('tacl.DataStore._set_labels') set_labels.return_value = {} sort_labels = self._create_patch('tacl.DataStore._sort_labels', False) sort_labels.return_value = labels get_placeholders = self._create_patch( 'tacl.DataStore._get_placeholders', False) get_placeholders.return_value = sentinel.placeholders log_query_plan = self._create_patch('tacl.DataStore._log_query_plan', False) input_fh = MagicMock(name='fh') csv = self._create_patch('tacl.DataStore._csv', False) csv.return_value = input_fh catalogue = MagicMock(name='catalogue') store = tacl.DataStore(':memory:') store._conn = MagicMock(spec_set=sqlite3.Connection) cursor = store._conn.execute.return_value output_fh = store.intersection(catalogue, input_fh) set_labels.assert_called_once_with(store, catalogue) get_placeholders.assert_called_once_with(labels) log_query_plan.assert_called_once() sql = 'SELECT TextNGram.ngram, TextNGram.size, TextNGram.count, Text.name AS "text name", Text.siglum, Text.label FROM Text, TextNGram WHERE Text.label IN (sentinel.placeholders) AND Text.id = TextNGram.text AND TextNGram.ngram IN (SELECT TextNGram.ngram FROM Text, TextNGram WHERE Text.label = ? AND Text.id = TextNGram.text AND TextNGram.ngram IN (SELECT TextNGram.ngram FROM Text, TextNGram WHERE Text.label = ? AND Text.id = TextNGram.text))' self.assertEqual(store._conn.mock_calls, [call.execute(sql, labels * 2)]) csv.assert_called_once_with(cursor, tacl.constants.QUERY_FIELDNAMES, input_fh) self.assertEqual(input_fh, output_fh)
def test_diff_asymmetric(self): labels = {sentinel.label: 1, sentinel.prime_label: 1} set_labels = self._create_patch('tacl.DataStore._set_labels') set_labels.return_value = labels get_placeholders = self._create_patch( 'tacl.DataStore._get_placeholders', False) get_placeholders.return_value = sentinel.placeholders log_query_plan = self._create_patch('tacl.DataStore._log_query_plan', False) input_fh = MagicMock(name='fh') csv = self._create_patch('tacl.DataStore._csv', False) csv.return_value = input_fh catalogue = MagicMock(name='catalogue') store = tacl.DataStore(':memory:') store._conn = MagicMock(spec_set=sqlite3.Connection) cursor = store._conn.execute.return_value output_fh = store.diff_asymmetric(catalogue, sentinel.prime_label, input_fh) set_labels.assert_called_once_with(store, catalogue) get_placeholders.assert_called_once_with([sentinel.label]) log_query_plan.assert_called_once() sql = tacl.constants.SELECT_DIFF_ASYMMETRIC_SQL.format( sentinel.placeholders) self.assertEqual(store._conn.mock_calls, [ call.execute( sql, [sentinel.prime_label, sentinel.prime_label, sentinel.label]) ]) csv.assert_called_once_with(cursor, tacl.constants.QUERY_FIELDNAMES, input_fh) self.assertEqual(input_fh, output_fh)
def test_diff_asymmetric(self): labels = {sentinel.label: 1, sentinel.prime_label: 1} set_labels = self._create_patch('tacl.DataStore._set_labels') set_labels.return_value = labels get_placeholders = self._create_patch( 'tacl.DataStore._get_placeholders', False) get_placeholders.return_value = sentinel.placeholders log_query_plan = self._create_patch('tacl.DataStore._log_query_plan', False) input_fh = MagicMock(name='fh') catalogue = MagicMock(name='catalogue') store = tacl.DataStore(':memory:') store._conn = MagicMock(spec_set=sqlite3.Connection) tokenizer = MagicMock(name='tokenizer') _diff = self._create_patch('tacl.DataStore._diff', False) _diff.return_value = input_fh output_fh = store.diff_asymmetric(catalogue, sentinel.prime_label, tokenizer, input_fh) set_labels.assert_called_once_with(store, catalogue) get_placeholders.assert_called_once_with([sentinel.label]) self.assertTrue(log_query_plan.called) sql = tacl.constants.SELECT_DIFF_ASYMMETRIC_SQL.format( sentinel.placeholders) self.assertEqual(store._conn.mock_calls, [ call.execute( sql, [sentinel.prime_label, sentinel.prime_label, sentinel.label]) ]) self.assertTrue(_diff.called) self.assertEqual(input_fh, output_fh)
def test_define_none(self): self.filler.db_find_ref = Mock(side_effect=[None]) self.filler.db_cur.lastrowid = 1 keys, values = ['one', 'two'], ['one_val', 'two_val'] self.assertEqual(self.filler.db_define_ref('table', keys, values), 1) params = ['INSERT INTO table (id, one, two) VALUES (NULL, ?, ?)', ['one_val', 'two_val']] self.assertEqual(self.filler.db_cur.mock_calls, [call.execute(*params)]) self.assertEqual(self.filler.db_find_ref.mock_calls, [call('table', keys, values)])
def test_set_labels(self): catalogue = collections.OrderedDict([(sentinel.text1, sentinel.label1), (sentinel.text2, sentinel.label2), (sentinel.text3, sentinel.label1) ]) store = tacl.DataStore(':memory:') store._conn = MagicMock(spec_set=sqlite3.Connection) cursor = store._conn.execute.return_value store._conn.execute.return_value = cursor cursor.fetchone.return_value = {'token_count': 10} actual_labels = store._set_labels(catalogue) expected_labels = {sentinel.label1: 20, sentinel.label2: 10} connection_calls = [ call.execute(tacl.constants.UPDATE_LABELS_SQL, ['']), call.execute(tacl.constants.UPDATE_LABEL_SQL, [sentinel.label1, sentinel.text1]), call.execute(tacl.constants.SELECT_TEXT_TOKEN_COUNT_SQL, [sentinel.text1]), call.execute(tacl.constants.UPDATE_LABEL_SQL, [sentinel.label2, sentinel.text2]), call.execute(tacl.constants.SELECT_TEXT_TOKEN_COUNT_SQL, [sentinel.text2]), call.execute(tacl.constants.UPDATE_LABEL_SQL, [sentinel.label1, sentinel.text3]), call.execute(tacl.constants.SELECT_TEXT_TOKEN_COUNT_SQL, [sentinel.text3]), ] for connection_call in connection_calls: self.assertIn(connection_call, store._conn.mock_calls) self.assertEqual(actual_labels, expected_labels)
def test_set_labels (self): catalogue = collections.OrderedDict( [(sentinel.text1, sentinel.label1), (sentinel.text2, sentinel.label2), (sentinel.text3, sentinel.label1)]) store = tacl.DataStore(':memory:') store._conn = MagicMock(spec_set=sqlite3.Connection) cursor = store._conn.execute.return_value store._conn.execute.return_value = cursor cursor.fetchone.return_value = {'token_count': 10} actual_labels = store._set_labels(catalogue) expected_labels = {sentinel.label1: 20, sentinel.label2: 10} connection_calls = [ call.execute(tacl.constants.UPDATE_LABELS_SQL, ['']), call.execute(tacl.constants.UPDATE_LABEL_SQL, [sentinel.label1, sentinel.text1]), call.execute(tacl.constants.SELECT_TEXT_TOKEN_COUNT_SQL, [sentinel.text1]), call.execute(tacl.constants.UPDATE_LABEL_SQL, [sentinel.label2, sentinel.text2]), call.execute(tacl.constants.SELECT_TEXT_TOKEN_COUNT_SQL, [sentinel.text2]), call.execute(tacl.constants.UPDATE_LABEL_SQL, [sentinel.label1, sentinel.text3]), call.execute(tacl.constants.SELECT_TEXT_TOKEN_COUNT_SQL, [sentinel.text3]), call.commit()] for connection_call in connection_calls: self.assertIn(connection_call, store._conn.mock_calls) self.assertEqual(actual_labels, expected_labels)
def test_validate_mismatched_checksums (self): corpus = MagicMock(spec_set=tacl.Corpus) text = MagicMock(spec_set=tacl.Text) text.get_checksum.return_value = sentinel.checksum text.get_names.return_value = (sentinel.name, sentinel.siglum) corpus.get_texts.return_value = (text,) catalogue = {sentinel.text1: sentinel.label1} store = tacl.DataStore(':memory:') store._conn = MagicMock(spec_set=sqlite3.Connection) cursor = store._conn.execute.return_value cursor.fetchone.return_value = {'checksum': sentinel.checksum2} actual_result = store.validate(corpus, catalogue) corpus.get_texts.assert_has_calls([call(sentinel.text1)]) self.assertEqual(store._conn.mock_calls, [call.execute(tacl.constants.SELECT_TEXT_SQL, [sentinel.name, sentinel.siglum]), call.execute().fetchone()]) self.assertEqual(actual_result, False)
def test_validate_mismatched_checksums(self): corpus = MagicMock(spec_set=tacl.Corpus) text = MagicMock(spec_set=tacl.WitnessText) text.get_checksum.return_value = sentinel.checksum text.get_names.return_value = (sentinel.name, sentinel.siglum) corpus.get_witnesses.return_value = (text, ) catalogue = {sentinel.text1: sentinel.label1} store = tacl.DataStore(':memory:') store._conn = MagicMock(spec_set=sqlite3.Connection) cursor = store._conn.execute.return_value cursor.fetchone.return_value = {'checksum': sentinel.checksum2} actual_result = store.validate(corpus, catalogue) corpus.get_witnesses.assert_has_calls([call(sentinel.text1)]) self.assertEqual(store._conn.mock_calls, [ call.execute(tacl.constants.SELECT_TEXT_SQL, [sentinel.name, sentinel.siglum]), call.execute().fetchone() ]) self.assertEqual(actual_result, False)
def test_validate_true(self): corpus = MagicMock(spec_set=tacl.Corpus) text = MagicMock(spec_set=tacl.WitnessText) text.get_checksum.return_value = sentinel.checksum text.get_names.return_value = (sentinel.name, sentinel.siglum) corpus.get_witnesses.return_value = (text,) catalogue = collections.OrderedDict( [(sentinel.text1, sentinel.label1), (sentinel.text2, sentinel.label2), (sentinel.text3, sentinel.label1)]) store = tacl.DataStore(':memory:') store._conn = MagicMock(spec_set=sqlite3.Connection) cursor = store._conn.execute.return_value cursor.fetchone.return_value = {'checksum': sentinel.checksum} actual_result = store.validate(corpus, catalogue) corpus.get_witnesses.assert_has_calls([ call(sentinel.text1), call(sentinel.text2), call(sentinel.text3)]) self.assertEqual(store._conn.mock_calls, [call.execute(tacl.constants.SELECT_TEXT_SQL, [sentinel.name, sentinel.siglum]), call.execute().fetchone(), call.execute(tacl.constants.SELECT_TEXT_SQL, [sentinel.name, sentinel.siglum]), call.execute().fetchone(), call.execute(tacl.constants.SELECT_TEXT_SQL, [sentinel.name, sentinel.siglum]), call.execute().fetchone()]) self.assertEqual(actual_result, True)
def test_validate_true(self): corpus = MagicMock(spec_set=tacl.Corpus) text = MagicMock(spec_set=tacl.Text) text.get_checksum.return_value = sentinel.checksum text.get_names.return_value = (sentinel.name, sentinel.siglum) corpus.get_texts.return_value = (text, ) catalogue = collections.OrderedDict([(sentinel.text1, sentinel.label1), (sentinel.text2, sentinel.label2), (sentinel.text3, sentinel.label1) ]) store = tacl.DataStore(':memory:') store._conn = MagicMock(spec_set=sqlite3.Connection) cursor = store._conn.execute.return_value cursor.fetchone.return_value = {'checksum': sentinel.checksum} actual_result = store.validate(corpus, catalogue) corpus.get_texts.assert_has_calls( [call(sentinel.text1), call(sentinel.text2), call(sentinel.text3)]) self.assertEqual(store._conn.mock_calls, [ call.execute(tacl.constants.SELECT_TEXT_SQL, [sentinel.name, sentinel.siglum]), call.execute().fetchone(), call.execute(tacl.constants.SELECT_TEXT_SQL, [sentinel.name, sentinel.siglum]), call.execute().fetchone(), call.execute(tacl.constants.SELECT_TEXT_SQL, [sentinel.name, sentinel.siglum]), call.execute().fetchone() ]) self.assertEqual(actual_result, True)
def test_add_text_size_ngrams (self): store = tacl.DataStore(':memory:') store._conn = MagicMock(spec_set=sqlite3.Connection) size = 1 ngrams = collections.OrderedDict([('a', 2), ('b', 1)]) store._add_text_size_ngrams(sentinel.text_id, size, ngrams) self.assertEqual( store._conn.mock_calls, [call.execute(tacl.constants.INSERT_TEXT_HAS_NGRAM_SQL, [sentinel.text_id, size, len(ngrams)]), call.executemany(tacl.constants.INSERT_NGRAM_SQL, [[sentinel.text_id, 'a', size, 2], [sentinel.text_id, 'b', size, 1]]), call.commit()])
def get_mock(self, table_name, insert_mock=False): for mock_to_check in self.cursor_mocks: for execute_calls in mock_to_check.method_calls: if execute_calls[0] == 'execute': if insert_mock: for call_str in execute_calls[1]: if call_str.startswith( "INSERT INTO {}".format(table_name)): return mock_to_check else: if execute_calls == call.execute( "SHOW TABLES LIKE '{}'".format(table_name)): return mock_to_check return mock.MagicMock()
def test_has_ngrams (self): store = tacl.DataStore(':memory:') store._conn = MagicMock(spec_set=sqlite3.Connection) cursor = store._conn.execute.return_value store._conn.execute.return_value = cursor # Path one: there are n-grams. cursor.fetchone.return_value = True actual_result = store._has_ngrams(sentinel.text_id, sentinel.size) self.assertEqual(store._conn.mock_calls, [call.execute(tacl.constants.SELECT_HAS_NGRAMS_SQL, [sentinel.text_id, sentinel.size]), call.execute().fetchone()]) self.assertEqual(actual_result, True) # Path two: there are no n-grams. store._conn.reset_mock() cursor.reset_mock() cursor.fetchone.return_value = None actual_result = store._has_ngrams(sentinel.text_id, sentinel.size) self.assertEqual(store._conn.mock_calls, [call.execute(tacl.constants.SELECT_HAS_NGRAMS_SQL, [sentinel.text_id, sentinel.size]), call.execute().fetchone()]) self.assertEqual(actual_result, False)
def test_build(self, us): # prepare oBarModel = Mock() oTextModel = Mock() oDialogModel = Mock() oDialogModel.createInstance.side_effect = [oBarModel, oTextModel] oBar = Mock() oText = Mock() oDialog = Mock() oDialog.getControl.side_effect = [oBar, oText] oTK = Mock() us.side_effect = [oDialogModel, oDialog, oTK] # play pe = ProgressExecutorBuilder().title("bar").bar_dimensions( 100, 50).autoclose(False).dialog_rectangle( 10, 10, 120, 60).bar_progress(100, 1000).message("base msg").build() def func(h: ProgressHandler): h.progress(1000) h.message("foo") pe.execute(func) time.sleep(1) # verify self.assertEqual([ call('com.sun.star.awt.UnoControlDialogModel'), call('com.sun.star.awt.UnoControlDialog'), call('com.sun.star.awt.Toolkit') ], us.mock_calls) self.assertEqual([ call.createInstance('com.sun.star.awt.UnoControlProgressBarModel'), call.createInstance('com.sun.star.awt.UnoControlFixedTextModel'), call.insertByName('bar', oBarModel), call.insertByName('text', oTextModel), ], oDialogModel.mock_calls) self.assertEqual([ call.setModel(oDialogModel), call.getControl('bar'), call.getControl('text'), call.setVisible(True), call.createPeer(oTK, None), call.execute() ], oDialog.mock_calls) self.assertEqual(1000, oBar.Value) self.assertEqual("foo", oText.Text)
def test_simple(self, us): # prepare oTextModel = Mock() oDialogModel = Mock() oDialogModel.createInstance.side_effect = [oTextModel] oText = Mock() oDialog = Mock() oDialog.getControl.side_effect = [oText] oTK = Mock() us.side_effect = [oDialogModel, oDialog, oTK] # play ce = ConsoleExecutorBuilder().build() def func(h: ConsoleHandler): h.message("foo") ce.execute(func) time.sleep(1) # verify # TODO: might not # self.assertEqual([ # call.insertText(ANY, 'foo\n') # ], oText.mock_calls) self.assertEqual([ call('com.sun.star.awt.UnoControlDialogModel'), call('com.sun.star.awt.UnoControlDialog'), call('com.sun.star.awt.Toolkit') ], us.mock_calls) self.assertEqual([ call.createInstance('com.sun.star.awt.UnoControlEditModel'), call.insertByName('text', oTextModel), ], oDialogModel.mock_calls) self.assertEqual([ call.setModel(oDialogModel), call.getControl('text'), call.setVisible(True), call.createPeer(oTK, None), call.execute() ], oDialog.mock_calls) self.assertEqual(5, oTextModel.PositionX) self.assertEqual(5, oTextModel.PositionY) self.assertEqual(90, oTextModel.Height) self.assertEqual(240, oTextModel.Width) self.assertTrue(oTextModel.ReadOnly) self.assertTrue(oTextModel.MultiLine) self.assertTrue(oTextModel.VScroll)
def test_folder_dialog_none(self, us): # prepare ddir = "baz" oPicker = Mock() oPicker.execute.side_effect = [ExecutableDialogResults.CANCEL] us.side_effect = [oPicker] # play actual = folder_dialog("foo", ddir) # verify self.assertEqual("foo", oPicker.Title) self.assertEqual("baz", oPicker.DisplayDirectory) self.assertEqual([call.execute()], oPicker.mock_calls) self.assertIsNone(actual)
def test_add_text_size_ngrams(self): store = tacl.DataStore(':memory:') store._conn = MagicMock(spec_set=sqlite3.Connection) size = 1 ngrams = collections.OrderedDict([('a', 2), ('b', 1)]) store._add_text_size_ngrams(sentinel.text_id, size, ngrams) self.assertEqual(store._conn.mock_calls, [ call.execute( tacl.constants.INSERT_TEXT_HAS_NGRAM_SQL, [sentinel.text_id, size, len(ngrams)]), call.executemany(tacl.constants.INSERT_NGRAM_SQL, [[sentinel.text_id, 'a', size, 2], [sentinel.text_id, 'b', size, 1]]), call.commit() ])
def test_file_dialog_single_none(self, us): # prepare ddir = "baz" oPicker = Mock() oPicker.execute.side_effect = [ExecutableDialogResults.CANCEL] us.side_effect = [oPicker] # play actual = file_dialog("foo", [FileFilter("bar", "*.bar")], ddir) # verify self.assertEqual("foo", oPicker.Title) self.assertEqual("baz", oPicker.DisplayDirectory) self.assertFalse(oPicker.MultiSelectionMode) self.assertEqual([call.appendFilter('bar', '*.bar'), call.execute()], oPicker.mock_calls) self.assertIsNone(actual)
def test_file_dialog_single_no_filter(self, us): # prepare ddir = "baz" oPicker = Mock(SelectedFiles=["foo"]) oPicker.execute.side_effect = [ExecutableDialogResults.OK] us.side_effect = [oPicker] # play actual = file_dialog("foo", display_dir=ddir) # verify self.assertEqual("foo", oPicker.Title) self.assertEqual("baz", oPicker.DisplayDirectory) self.assertFalse(oPicker.MultiSelectionMode) self.assertEqual([ call.execute(), ], oPicker.mock_calls) self.assertEqual("foo", actual)
def test_file_dialog_multiple(self, us): # prepare ddir = "baz" oPicker = Mock(SelectedFiles=["foo", "bar"]) oPicker.execute.side_effect = [ExecutableDialogResults.OK] us.side_effect = [oPicker] # play actual = file_dialog("foo", [FileFilter("bar", "*.bar")], ddir, single=False) # verify self.assertEqual("foo", oPicker.Title) self.assertEqual("baz", oPicker.DisplayDirectory) self.assertTrue(oPicker.MultiSelectionMode) self.assertEqual([ call.appendFilter('bar', '*.bar'), call.execute(), ], oPicker.mock_calls) self.assertEqual(["foo", "bar"], actual)
def test_tester_calls(self): attrs = {'has_next_packet_size.side_effect': [True, False]} scenario = Mock(**attrs) switch_test_runner.get_scenarios = Mock(return_value=[scenario]) report_generator = Mock() switch_test_runner.get_report_generator = Mock( return_value=report_generator) switch_test_runner.main(self.config) scenario.assert_has_calls([call.has_next_packet_size(), call.next_packet_size(), call.execute(), call.has_next_packet_size(), call.cleanup_switch()], any_order=False) report_generator.assert_has_calls([call.collect_data(), call.report()], any_order=False) switch_test_runner.get_scenarios.assert_has_calls([call(self.config)]) switch_test_runner.get_report_generator.assert_has_calls( [call(scenario)])
def test_counts (self): labels = [sentinel.label] set_labels = self._create_patch('tacl.DataStore._set_labels') set_labels.return_value = labels get_placeholders = self._create_patch( 'tacl.DataStore._get_placeholders', False) get_placeholders.return_value = sentinel.placeholders input_fh = MagicMock(name='fh') csv = self._create_patch('tacl.DataStore._csv', False) csv.return_value = input_fh catalogue = MagicMock(name='catalogue') store = tacl.DataStore(':memory:') store._conn = MagicMock(spec_set=sqlite3.Connection) cursor = store._conn.execute.return_value output_fh = store.counts(catalogue, input_fh) set_labels.assert_called_once_with(store, catalogue) get_placeholders.assert_called_once_with(labels) sql = tacl.constants.SELECT_COUNTS_SQL.format(sentinel.placeholders) self.assertEqual(store._conn.mock_calls, [call.execute(sql, [sentinel.label])]) csv.assert_called_once_with(cursor, tacl.constants.COUNTS_FIELDNAMES, input_fh) self.assertEqual(input_fh, output_fh)
def test_counts(self): labels = [sentinel.label] set_labels = self._create_patch('tacl.DataStore._set_labels') set_labels.return_value = labels get_placeholders = self._create_patch( 'tacl.DataStore._get_placeholders', False) get_placeholders.return_value = sentinel.placeholders input_fh = MagicMock(name='fh') csv = self._create_patch('tacl.DataStore._csv', False) csv.return_value = input_fh catalogue = MagicMock(name='catalogue') store = tacl.DataStore(':memory:') store._conn = MagicMock(spec_set=sqlite3.Connection) cursor = store._conn.execute.return_value output_fh = store.counts(catalogue, input_fh) set_labels.assert_called_once_with(store, catalogue) get_placeholders.assert_called_once_with(labels) sql = tacl.constants.SELECT_COUNTS_SQL.format(sentinel.placeholders) self.assertEqual(store._conn.mock_calls, [call.execute(sql, [sentinel.label])]) csv.assert_called_once_with(cursor, tacl.constants.COUNTS_FIELDNAMES, input_fh) self.assertEqual(input_fh, output_fh)
def test_tester_calls_with_generator(self): scenario_timestamp = model.ScenarioTimestamps() scenario_timestamp.start = 1 scenario_timestamp.stop = 2 attrs = {'has_next_packet_size.side_effect': [True, False], 'environment.reports': 'plotly', 'time_metrics': [scenario_timestamp], 'get_current_packet_size_idx.return_value': 0} scenario = Mock(**attrs) switch_test_runner.get_scenarios = Mock(return_value=[scenario]) generator.PlotlyReportGenerator.get_points = Mock() generator.PlotlyReportGenerator.report = Mock() switch_test_runner.main(self.config) scenario.assert_has_calls([call.has_next_packet_size(), call.next_packet_size(), call.execute(), call.get_current_packet_size_idx(), call.current_packet_size(), call.has_next_packet_size(), call.cleanup_switch()], any_order=False) switch_test_runner.get_scenarios.assert_has_calls([call(self.config)])
def test_find_right_values(self): self.filler.db_cur.fetchone.return_value = [1] self.assertEqual(self.filler.db_find_ref('table', ['one', 'two'], ['one_val', 'two_val']), 1) params = ['SELECT id FROM table WHERE one = ? AND two = ?', ['one_val', 'two_val']] self.assertEqual(self.filler.db_cur.mock_calls, [call.execute(*params), call.fetchone()])
def test_transactional_mock_redis(mock_redis, mock_pipeline): rl = RedisTransactionalRateLimiter(mock_redis, codec=DummyCodec()) rl.configure(k1=RateLimit(Zone('z1', 2)), k2=RateLimit(Zone('z2', 1, expiry=10))) t0 = T0 mock_pipeline.mget.return_value = [None] mock_pipeline.time.return_value = _to_redis_time(t0) assert rl.request(k1='foo') == (True, 0) t1 = T0 + 0.1 mock_pipeline.mget.return_value = [None, None] mock_pipeline.time.return_value = _to_redis_time(t1) assert rl.request(k1='bar', k2='baz') == (True, 0) t2 = T0 + 0.3 mock_pipeline.mget.return_value = [State(t0, 1)] mock_pipeline.time.return_value = _to_redis_time(t2) assert rl.request(k1='foo') == (False, None) t3 = T0 + 0.51 mock_pipeline.time.return_value = _to_redis_time(t3) assert rl.request(k1='foo') == (True, 0) assert mock_redis.mock_calls == [ call.transaction(ANY, 'redbucket:z1:foo', value_from_callable=True), call.transaction(ANY, 'redbucket:z1:bar', 'redbucket:z2:baz', value_from_callable=True), call.transaction(ANY, 'redbucket:z1:foo', value_from_callable=True), call.transaction(ANY, 'redbucket:z1:foo', value_from_callable=True), ] assert mock_pipeline.mock_calls == [ # request 1 call.watch('redbucket:z1:foo'), call.mget(['redbucket:z1:foo']), call.time(), call.multi(), call.setex('redbucket:z1:foo', 60, State(approx(t0), 1)), call.execute(), # request 2 call.watch('redbucket:z1:bar', 'redbucket:z2:baz'), call.mget(['redbucket:z1:bar', 'redbucket:z2:baz']), call.time(), call.multi(), call.setex('redbucket:z1:bar', 60, State(approx(t1), 1)), call.setex('redbucket:z2:baz', 10, State(approx(t1), 1)), call.execute(), # request 3 call.watch('redbucket:z1:foo'), call.mget(['redbucket:z1:foo']), call.time(), call.unwatch(), call.execute(), # request 4 call.watch('redbucket:z1:foo'), call.mget(['redbucket:z1:foo']), call.time(), call.multi(), call.setex('redbucket:z1:foo', 60, State(approx(t3), 1)), call.execute(), ]
def test_update(self): self.filler.db_update('table', 1, ['one', 'two'], ['one_val', 'two_val']) params = ['UPDATE table SET one = ?, two = ? WHERE id = 1', ['one_val', 'two_val']] self.assertEqual(self.filler.db_cur.mock_calls, [call.execute(*params)])
def test_without_previous(self): ce = CommandExecutor(self._logger, self._command, None) ce.execute(["1", "2"]) self.assertEqual([call.execute()], self._command.mock_calls)
def test_get_text_id (self): add_text = self._create_patch('tacl.DataStore._add_text_record') add_text.return_value = sentinel.new_text_id update_text = self._create_patch('tacl.DataStore._update_text_record') delete_ngrams = self._create_patch('tacl.DataStore._delete_text_ngrams') text = MagicMock(spec_set=tacl.Text) text.get_checksum.return_value = sentinel.checksum text.get_filename.return_value = sentinel.filename text.get_names.return_value = (sentinel.name, sentinel.siglum) # There are three paths this method can take, depending on # whether a record already exists for the supplied text and, # if it does, whether the checksums match. store = tacl.DataStore(':memory:') store._conn = MagicMock(spec_set=sqlite3.Connection) cursor = store._conn.execute.return_value # Path one: there is no existing record. store._conn.execute.return_value = cursor cursor.fetchone.return_value = None actual_text_id = store._get_text_id(text) self.assertEqual(text.mock_calls, [call.get_names()]) self.assertEqual(store._conn.mock_calls, [call.execute(tacl.constants.SELECT_TEXT_SQL, [sentinel.name, sentinel.siglum]), call.execute().fetchone()]) add_text.assert_called_once_with(store, text) self.assertEqual(update_text.mock_calls, []) self.assertEqual(delete_ngrams.mock_calls, []) self.assertEqual(actual_text_id, sentinel.new_text_id) # Path two: there is an existing record, with a matching checksum. store._conn.reset_mock() text.reset_mock() add_text.reset_mock() update_text.reset_mock() cursor.fetchone.return_value = {'checksum': sentinel.checksum, 'id': sentinel.old_text_id} actual_text_id = store._get_text_id(text) self.assertEqual(store._conn.mock_calls, [call.execute(tacl.constants.SELECT_TEXT_SQL, [sentinel.name, sentinel.siglum]), call.execute().fetchone()]) self.assertEqual(text.mock_calls, [call.get_names(), call.get_checksum()]) self.assertEqual(add_text.mock_calls, []) self.assertEqual(update_text.mock_calls, []) self.assertEqual(delete_ngrams.mock_calls, []) self.assertEqual(actual_text_id, sentinel.old_text_id) # Path three: there is an existing record, with a different # checksum. store._conn.reset_mock() text.reset_mock() add_text.reset_mock() update_text.reset_mock() cursor.fetchone.return_value = {'checksum': sentinel.new_checksum, 'id': sentinel.old_text_id} actual_text_id = store._get_text_id(text) self.assertEqual(store._conn.mock_calls, [call.execute(tacl.constants.SELECT_TEXT_SQL, [sentinel.name, sentinel.siglum]), call.execute().fetchone()]) self.assertEqual(text.mock_calls, [call.get_names(), call.get_checksum(), call.get_filename()]) update_text.assert_called_once_with(store, text, sentinel.old_text_id) delete_ngrams.assert_called_once_with(store, sentinel.old_text_id) self.assertEqual(add_text.mock_calls, []) self.assertEqual(actual_text_id, sentinel.old_text_id)
def test_restore_shed_gitignore_with_sibling_jazzignore(self, shellmock): with open(testhelper.getrelativefilename('./resources/test_ignore_git_status_z.txt'), 'r') as file: with patch('os.path.exists', return_value=True): # answer inquries for sibling .jazzignore with True Commiter.restore_shed_gitignore(file.readlines()) calls = [call.execute('git checkout -- project1/src/.gitignore'), call.execute('git checkout -- project1/src/sub/.gitignore')] shellmock.assert_has_calls(calls)
def test_get_text_id(self): add_text = self._create_patch('tacl.DataStore._add_text_record') add_text.return_value = sentinel.new_text_id update_text = self._create_patch('tacl.DataStore._update_text_record') delete_ngrams = self._create_patch( 'tacl.DataStore._delete_text_ngrams') text = MagicMock(spec_set=tacl.WitnessText) text.get_checksum.return_value = sentinel.checksum text.get_filename.return_value = sentinel.filename text.get_names.return_value = (sentinel.name, sentinel.siglum) # There are three paths this method can take, depending on # whether a record already exists for the supplied text and, # if it does, whether the checksums match. store = tacl.DataStore(':memory:') store._conn = MagicMock(spec_set=sqlite3.Connection) cursor = store._conn.execute.return_value # Path one: there is no existing record. store._conn.execute.return_value = cursor cursor.fetchone.return_value = None actual_text_id = store._get_text_id(text) self.assertEqual(text.mock_calls, [call.get_names()]) self.assertEqual(store._conn.mock_calls, [ call.execute(tacl.constants.SELECT_TEXT_SQL, [sentinel.name, sentinel.siglum]), call.execute().fetchone() ]) add_text.assert_called_once_with(store, text) self.assertEqual(update_text.mock_calls, []) self.assertEqual(delete_ngrams.mock_calls, []) self.assertEqual(actual_text_id, sentinel.new_text_id) # Path two: there is an existing record, with a matching checksum. store._conn.reset_mock() text.reset_mock() add_text.reset_mock() update_text.reset_mock() cursor.fetchone.return_value = { 'checksum': sentinel.checksum, 'id': sentinel.old_text_id } actual_text_id = store._get_text_id(text) self.assertEqual(store._conn.mock_calls, [ call.execute(tacl.constants.SELECT_TEXT_SQL, [sentinel.name, sentinel.siglum]), call.execute().fetchone() ]) self.assertEqual( text.mock_calls, [call.get_names(), call.get_checksum()]) self.assertEqual(add_text.mock_calls, []) self.assertEqual(update_text.mock_calls, []) self.assertEqual(delete_ngrams.mock_calls, []) self.assertEqual(actual_text_id, sentinel.old_text_id) # Path three: there is an existing record, with a different # checksum. store._conn.reset_mock() text.reset_mock() add_text.reset_mock() update_text.reset_mock() cursor.fetchone.return_value = { 'checksum': sentinel.new_checksum, 'id': sentinel.old_text_id } actual_text_id = store._get_text_id(text) self.assertEqual(store._conn.mock_calls, [ call.execute(tacl.constants.SELECT_TEXT_SQL, [sentinel.name, sentinel.siglum]), call.execute().fetchone() ]) self.assertEqual( text.mock_calls, [call.get_names(), call.get_checksum(), call.get_filename()]) update_text.assert_called_once_with(store, text, sentinel.old_text_id) delete_ngrams.assert_called_once_with(store, sentinel.old_text_id) self.assertEqual(add_text.mock_calls, []) self.assertEqual(actual_text_id, sentinel.old_text_id)