def test_reload_reloads(self, find): values = {'key': 'key', 'value_1': 'value_1'} model = models.SmallTestModel(values, persisted=False) updated_values = {'key': 'key', 'value_1': 'value_2'} find.return_value = models.SmallTestModel(updated_values) model.reload() self.assertEqual(model.value_1, updated_values['value_1']) self.assertEqual(model.changes(), {})
def test_reload_reloads(self, find): values = {"key": "key", "value_1": "value_1"} model = models.SmallTestModel(values, persisted=False) updated_values = {"key": "key", "value_1": "value_2"} find.return_value = models.SmallTestModel(updated_values) model.reload() self.assertEqual(model.value_1, updated_values["value_1"]) self.assertEqual(model.changes(), {})
def test_or_condition( self, condition_, expected_params, expected_types, expected_sql, expected_row_keys, ): models.SmallTestModel(dict(key='a', value_1='a', value_2='a')).save() models.SmallTestModel(dict(key='b', value_1='b', value_2='b')).save() rows = models.SmallTestModel.where(condition_) self.assertEqual(expected_params, condition_.params()) self.assertEqual(expected_types, condition_.types()) self.assertEqual(expected_sql, condition_.sql()) self.assertCountEqual(expected_row_keys, tuple(row.key for row in rows))
def test_reload(self, find): values = {'key': 'key', 'value_1': 'value_1'} model = models.SmallTestModel(values, persisted=False) find.return_value = None self.assertIsNone(model.reload()) find.assert_called_once_with(**model.id(), transaction=None)
def test_validation(self): with self.assertRaises(error.SpannerError): models.SmallTestModel( {'value_1': 'value'}, persisted=False, skip_validation=False, )
def test_changes(self): test_model = models.SmallTestModel({'key': 'key', 'value_1': 'value'}) test_model.value_1 = 'change' self.assertEqual(test_model.changes(), {'value_1': 'change'}) test_model.value_1 = 'value' self.assertEqual(test_model.changes(), {})
def test_create_raw_fields(self): test_model = models.SmallTestModel({ "key": "key", "value_1": "value", "extra_field": "value" }) self.assertEqual(test_model.extra_field, "value")
def test_save_batch_inserts(self, insert): mock_transaction = mock.Mock() values = {'key': 'key', 'value_1': 'value'} not_persisted = models.SmallTestModel(values) models.SmallTestModel.save_batch([not_persisted], transaction=mock_transaction) self.assert_api_called(insert, mock_transaction)
def test_save_batch_force_write_upserts(self, upsert): mock_transaction = mock.Mock() values = {'key': 'key', 'value_1': 'value'} not_persisted = models.SmallTestModel(values) models.SmallTestModel.save_batch( mock_transaction, [not_persisted], force_write=True) self.assert_api_called(upsert, mock_transaction)
def test_basic(self): test_model = models.SmallTestModel({'key': 'key', 'value_1': 'value'}) test_model.save() self.assertEqual( [x.values for x in models.SmallTestModel.all()], [{'key': 'key', 'value_1': 'value', 'value_2': None}], )
def test_save_batch_updates(self, update): mock_transaction = mock.Mock() values = {'key': 'key', 'value_1': 'value'} persisted = models.SmallTestModel(values, persisted=True) models.SmallTestModel.save_batch(mock_transaction, [persisted]) self.assert_api_called(update, mock_transaction)
def test_changes(self): test_model = models.SmallTestModel({"key": "key", "value_1": "value"}) test_model.value_1 = "change" self.assertEqual(test_model.changes(), {"value_1": "change"}) test_model.value_1 = "value" self.assertEqual(test_model.changes(), {})
def test_set_attr(self): test_model = models.SmallTestModel({"key": "key", "value_1": "value"}) test_model.value_1 = "value_1" test_model.value_2 = "value_2" self.assertEqual( test_model.values, {"key": "key", "value_1": "value_1", "value_2": "value_2",}, )
def test_save_creates(self, create): values = {'key': 'key', 'value_1': 'value_1'} model = models.SmallTestModel(values, persisted=False) model.save() create.assert_called_once_with(**values, value_2=None, transaction=None)
def test_save_creates(self, create): values = {'key': 'key', 'value_1': 'value_1'} model = models.SmallTestModel(values, persisted=False) model.save() create.assert_called_once() (transaction,), kwargs = create.call_args self.assertIsNone(transaction) self.assertEqual(kwargs, {**values, 'value_2': None})
def test_save_updates(self, update): values = {'key': 'key', 'value_1': 'value_1'} model = models.SmallTestModel(values, persisted=True) values['value_1'] = 'new_value' model.value_1 = values['value_1'] model.save() update.assert_called_once_with(**values, transaction=None)
def test_set_attr(self): test_model = models.SmallTestModel({'key': 'key', 'value_1': 'value'}) test_model.value_1 = 'value_1' test_model.value_2 = 'value_2' self.assertEqual(test_model.values, { 'key': 'key', 'value_1': 'value_1', 'value_2': 'value_2', })
def test_param_from_value_correctly_encodes(self, tautology): test_model = models.SmallTestModel( dict( key='some-key', value_1='some-value', value_2='other-value', )) test_model.save() self.assertCountEqual((test_model,), models.SmallTestModel.where(tautology))
def test_reload(self, find): values = {'key': 'key', 'value_1': 'value_1'} model = models.SmallTestModel(values, persisted=False) find.return_value = None self.assertIsNone(model.reload()) find.assert_called_once() (transaction,), kwargs = find.call_args self.assertIsNone(transaction) self.assertEqual(kwargs, model.id())
def test_delete_batch_deletes(self, delete): mock_transaction = mock.Mock() values = {"key": "key", "value_1": "value"} model = models.SmallTestModel(values) models.SmallTestModel.delete_batch(mock_transaction, [model]) delete.assert_called_once() (transaction, table, keyset), _ = delete.call_args self.assertEqual(transaction, mock_transaction) self.assertEqual(table, models.SmallTestModel.table) self.assertEqual(keyset.keys, [[model.key]])
def test_delete_deletes(self, delete): mock_transaction = mock.Mock() values = {'key': 'key', 'value_1': 'value_1'} model = models.SmallTestModel(values) model.delete(mock_transaction) delete.assert_called_once() (transaction, table, keyset), _ = delete.call_args self.assertEqual(transaction, mock_transaction) self.assertEqual(table, models.SmallTestModel.table) self.assertEqual(keyset.keys, [[model.key]])
def test_find_required(self): test_model = models.SmallTestModel( dict( key='some-key', value_1='foo', value_2='bar', )) test_model.save() self.assertEqual( test_model, models.SmallTestModel.find_required(key='some-key'), )
def test_save_updates(self, update): values = {'key': 'key', 'value_1': 'value_1'} model = models.SmallTestModel(values, persisted=True) values['value_1'] = 'new_value' model.value_1 = values['value_1'] model.save() update.assert_called_once() (transaction,), kwargs = update.call_args self.assertIsNone(transaction) self.assertEqual(kwargs, values)
def test_save_updates(self, update): values = {"key": "key", "value_1": "value_1"} model = models.SmallTestModel(values, persisted=True) values["value_1"] = "new_value" model.value_1 = values["value_1"] model.save() update.assert_called_once() (transaction, ), kwargs = update.call_args self.assertIsNone(transaction) self.assertEqual(kwargs, values)
def test_execute_partitioned_dml(self): update.CreateTable( models.SmallTestModelWithoutSecondaryIndexes).execute() test_model = models.SmallTestModel( dict( key='some-key', value_1='foo', value_2='bar', )) test_model.save() update.ExecutePartitionedDml( "UPDATE SmallTestModel SET value_2 = value_1 WHERE value_2 = 'bar'", ).execute() test_model.reload() self.assertEqual( models.SmallTestModel( dict( key='some-key', value_1='foo', value_2='foo', )), test_model, )
def test_contains( self, haystack, needle, expect_results, kwargs={}, ): test_model = models.SmallTestModel(dict(key='a', value_1='a', value_2='a')) test_model.save() self.assertCountEqual( ((test_model,) if expect_results else ()), models.SmallTestModel.where( spanner_orm.contains( condition.Param.from_value(haystack), condition.Param.from_value(needle), **kwargs, )), )
def test_key(self): models.SmallTestModel({'key': 'key', 'value_1': 'value'}).save() models.UnittestModel({ 'string': 'string', 'int_': 42, 'float_': 4.2, 'timestamp': datetime.datetime.now(tz=datetime.timezone.utc), }).save() models.ForeignKeyTestModel({ 'referencing_key_1': 'key', 'referencing_key_2': 'string', 'referencing_key_3': 42, 'value': 'value' }).save()
def test_arbitrary_condition( self, condition_, expected_params, expected_types, expected_sql, expected_row_keys, ): models.SmallTestModel( dict( key='some-key', value_1='some-value', value_2='other-value', )).save() rows = models.SmallTestModel.where(condition_) self.assertEqual(expected_params, condition_.params()) self.assertEqual(expected_types, condition_.types()) self.assertEqual(expected_sql, condition_.sql()) self.assertCountEqual(expected_row_keys, tuple(row.key for row in rows))
def test_get_attr(self): test_model = models.SmallTestModel({'key': 'key', 'value_1': 'value'}) self.assertEqual(test_model.key, 'key') self.assertEqual(test_model.value_1, 'value') self.assertEqual(test_model.value_2, None)
def test_set_error_on_primary_key(self): test_model = models.SmallTestModel({'key': 'key', 'value_1': 'value'}) with self.assertRaises(AttributeError): test_model.key = 'error'