def test_first(self): model = self.get_mock_model() builder = Builder(self.get_mock_query_builder()) builder.set_model(model) builder.take = mock.MagicMock(return_value=builder) builder.get = mock.MagicMock(return_value=Collection(['bar'])) result = builder.first() self.assertEqual('bar', result) builder.take.assert_called_once_with(1) builder.get.assert_called_once_with(['*'])
def test_find_or_fail_with_many_raises_model_not_found_exception(self): model = self.get_mock_model() builder = Builder(self.get_mock_query_builder()) builder.set_model(model) builder.get_query().where_in = mock.MagicMock() builder.get = mock.MagicMock(return_value=Collection([1])) self.assertRaises(ModelNotFound, builder.find_or_fail, [1, 2], ['column']) builder.get_query().where_in.assert_called_once_with( 'foo_table.foo', [1, 2]) builder.get.assert_called_once_with(['column'])
def test_get_does_not_eager_relations_when_no_results_are_returned(self): flexmock(Builder) builder = Builder(self.get_mock_query_builder()) builder.should_receive('get_models').with_args(['foo' ]).and_return(['bar']) builder.should_receive('eager_load_relations').with_args( ['bar']).and_return([]) builder.set_model(self.get_mock_model()) builder.get_model().new_collection = mock.MagicMock( return_value=Collection([])) results = builder.get(['foo']) self.assertEqual([], results.all()) builder.get_model().new_collection.assert_called_with([])
def test_find_with_many(self): model = self.get_mock_model() builder = Builder(self.get_mock_query_builder()) builder.set_model(model) builder.get_query().where_in = mock.MagicMock() builder.get = mock.MagicMock(return_value='baz') result = builder.find([1, 2], ['column']) self.assertEqual('baz', result) builder.get_query().where_in.assert_called_once_with( 'foo_table.foo', [1, 2]) builder.get.assert_called_once_with(['column'])
def test_chunk(self): builder = Builder(self.get_mock_query_builder()) results = [['foo1', 'foo2'], ['foo3'], []] builder.for_page = mock.MagicMock(return_value=builder) builder.get = mock.MagicMock(side_effect=results) i = 0 for result in builder.chunk(2): self.assertEqual(result, results[i]) i += 1 builder.for_page.assert_has_calls( [mock.call(1, 2), mock.call(2, 2), mock.call(3, 2)])