def test_create(self): # create a new entry: with AssertModelCleanCalled() as cm: instance = create( ModelClass=CreateOrUpdateTestModel, name='First entry', slug='first' ) assert isinstance(instance, CreateOrUpdateTestModel) assert instance.pk is not None assert instance.name == 'First entry' assert instance.slug == 'first' assert instance.create_dt == parse_dt('2001-01-01T00:00:00+0000') assert instance.update_dt == parse_dt('2001-01-01T00:00:00+0000') cm.assert_no_missing_cleans()
def test_create_or_update_without_lookup(self): # create a new entry: with AssertModelCleanCalled() as cm: instance, created, updated_fields = create_or_update( ModelClass=CreateOrUpdateTestModel, lookup=None, name='First entry', slug='first' ) assert isinstance(instance, CreateOrUpdateTestModel) assert instance.pk is not None assert instance.name == 'First entry' assert instance.slug == 'first' assert instance.create_dt == parse_dt('2001-01-01T00:00:00+0000') assert instance.update_dt == parse_dt('2001-01-01T00:00:00+0000') assert created is True assert updated_fields is None cm.assert_no_missing_cleans()
def add_iteration(self): assert TaskModel.objects.count() == 1 instance = TaskModel.objects.all().first() executing_dt = instance.executing_dt assert executing_dt == parse_dt('2000-01-01T00:00:02+0000') self.progress_info.append( [self.count, instance.elapsed_sec, str(instance)]) self.count += 1
def test_set_name_by_request(self): with self.assertLogs('django_tools'): item = baker.make(ItemModel) link = ItemLinkModel(item=item, url='http://test.tld/foo/bar') offset = datetime.timedelta(seconds=30) with patch.object(timezone, 'now', MockDatetimeGenerator(offset)): with requests_mock.Mocker() as m: m.get('http://test.tld/foo/bar', text='No title') assert link.last_check is None with self.assertLogs('inventory.models.links', level=logging.WARNING) as logs: link.full_clean() assert link.page_title is None assert link.name is None assert link.last_check == parse_dt( '2000-01-01T00:00:30+0000') logs = logs.output assert logs == [ "WARNING:inventory.models.links:No title found in 'http://test.tld/foo/bar'" ] # We should not create request on every admin save call with self.assertLogs('inventory.models.links', level=logging.DEBUG) as logs: link.full_clean() assert link.page_title is None assert link.name is None logs = logs.output assert logs == [ 'DEBUG:inventory.models.links:Last check is 0:00:30 ago.', "INFO:inventory.models.links:Skip request for: 'http://test.tld/foo/bar'" ] # Next try after 1 Min m.get('http://test.tld/foo/bar', text='<title>A <boom>Title</boom>!</title>') with self.assertLogs('inventory.models.links', level=logging.INFO) as logs: link.full_clean() assert link.page_title == 'A Title!' assert link.name == 'A Title!' logs = logs.output assert logs == [ "INFO:inventory.models.links:Found title: 'A <boom>Title</boom>!'" ] # Don't make requests, if we have a link name! with requests_mock.Mocker(): with self.assertLogs('inventory.models.links', level=logging.DEBUG) as logs: link.full_clean() assert link.page_title == 'A Title!' assert link.name == 'A Title!' logs = logs.output assert logs == [ ("DEBUG:inventory.models.links:Skip link request:" " because we have a name: 'A Title!'") ]
def test_create_or_update(self): # create a new entry: with AssertModelCleanCalled() as cm: instance, created, updated_fields = create_or_update( ModelClass=CreateOrUpdateTestModel, lookup={'id': 1}, name='First entry', slug='first' ) assert isinstance(instance, CreateOrUpdateTestModel) assert instance.id == 1 assert instance.name == 'First entry' assert instance.slug == 'first' assert instance.create_dt == parse_dt('2001-01-01T00:00:00+0000') assert instance.update_dt == parse_dt('2001-01-01T00:00:00+0000') assert created is True assert updated_fields is None cm.assert_no_missing_cleans() # Change only 'slug' with AssertModelCleanCalled() as cm: instance, created, updated_fields = create_or_update( ModelClass=CreateOrUpdateTestModel, lookup={'id': 1}, name='First entry', slug='change-value' ) assert isinstance(instance, CreateOrUpdateTestModel) assert instance.id == 1 assert instance.name == 'First entry' assert instance.slug == 'change-value' assert instance.create_dt == parse_dt('2001-01-01T00:00:00+0000') # not changed! assert instance.update_dt == parse_dt('2002-01-01T00:00:00+0000') assert created is False assert updated_fields == ['slug'] cm.assert_no_missing_cleans() # Change 'name' and 'slug': with AssertModelCleanCalled() as cm: instance, created, updated_fields = create_or_update( ModelClass=CreateOrUpdateTestModel, lookup={'id': 1}, name='New name !', slug='new-slug' ) assert isinstance(instance, CreateOrUpdateTestModel) assert instance.id == 1 assert instance.name == 'New name !' assert instance.slug == 'new-slug' assert instance.create_dt == parse_dt('2001-01-01T00:00:00+0000') # not changed! assert instance.update_dt == parse_dt('2003-01-01T00:00:00+0000') assert created is False assert updated_fields == ['name', 'slug'] cm.assert_no_missing_cleans() # Nothing changed: instance, created, updated_fields = create_or_update( ModelClass=CreateOrUpdateTestModel, lookup={'id': 1}, name='New name !', slug='new-slug' ) assert isinstance(instance, CreateOrUpdateTestModel) assert instance.id == 1 assert instance.name == 'New name !' assert instance.slug == 'new-slug' assert instance.create_dt == parse_dt('2001-01-01T00:00:00+0000') assert instance.update_dt == parse_dt('2003-01-01T00:00:00+0000') # not changed! assert created is False assert updated_fields == []
def create_MigrationModel_data(self): """ Called only for tests! Normal run creates example data via migrations: reversion_compare_tests/migrations/0002_migration_model_1.py reversion_compare_tests/migrations/0003_migration_model_2.py Generate the same reversions here as in migrations. """ if MigrationModel.objects.count(): # Skip creation called by 'make run-test-server' return instance = MigrationModel.objects.create( # last version pk=1, info='Migration state 2 - version 4', number_then_text=111, text='Now this is a short text!!!', ) content_type = ContentType.objects.get_for_model(instance) def create_version(date, comment, data): Version.objects.create( revision=Revision.objects.create(date_created=date, comment=comment), object_id=1, content_type=content_type, db='default', format='json', serialized_data=json.dumps([{ 'model': 'reversion_compare_tests.migrationmodel', 'pk': 1, 'fields': data }]), object_repr='MigrationModel object (1)', ) create_version(date=parse_dt('2020-01-01T12:00:00+0000'), comment='Migration state 1 - version 1', data={ 'info': 'Migration state 1 - version 1', 'number_then_text': 'Not a number 1', 'text': LOREM_IPSUM }) create_version(date=parse_dt('2020-01-02T12:00:00+0000'), comment='Migration state 1 - version 2', data={ 'info': 'Migration state 1 - version 2', 'number_then_text': 'Not a number 2', 'text': LOREM_IPSUM }) create_version(date=parse_dt('2020-01-03T12:00:00+0000'), comment='Migration state 2 - version 3', data={ 'info': 'Migration state 2 - version 3', 'number_then_text': 789, 'text': 'Now this is a short text.' }) create_version(date=parse_dt('2020-01-04T12:00:00+0000'), comment='Migration state 2 - version 4', data={ 'info': 'Migration state 2 - version 4', 'number_then_text': 111, 'text': 'Now this is a short text!!!' }) return instance