def test_foreign_keys_bug(self): blog1 = Blog(title="Blog") blog1.save() entry1 = Entry(title="entry 1", blog=blog1) entry1.save() self.assertEqual( # this should work too list(Entry.objects.filter(blog=blog1)), [entry1] )
def test_simple_foreign_keys(self): now = datetime.datetime.now() blog1 = Blog(title="Blog") blog1.save() entry1 = Entry(title="entry 1", blog=blog1) entry1.save() entry2 = Entry(title="entry 2", blog=blog1) entry2.save() self.assertEqual(Entry.objects.count(), 2) for entry in Entry.objects.all(): self.assertEqual( blog1, entry.blog ) blog2 = Blog(title="Blog") blog2.save() entry3 = Entry(title="entry 3", blog=blog2) entry3.save() self.assertEqual( # it's' necessary to explicitly state the pk here list(Entry.objects.filter(blog=blog1.pk)), [entry1, entry2] )
def test_dates_less_and_more_than(self): now = datetime.datetime.now() before = now + datetime.timedelta(days=1) after = now - datetime.timedelta(days=1) entry1 = Entry(title="entry 1", date_published=now) entry1.save() entry2 = Entry(title="entry 2", date_published=before) entry2.save() entry3 = Entry(title="entry 3", date_published=after) entry3.save() a = list(Entry.objects.filter(date_published=now)) self.assertEqual( list(Entry.objects.filter(date_published=now)), [entry1] ) self.assertEqual( list(Entry.objects.filter(date_published__lt=now)), [entry3] ) self.assertEqual( list(Entry.objects.filter(date_published__gt=now)), [entry2] )
def test_simple_foreign_keys(self): now = datetime.datetime.now() blog1 = Blog(title="Blog") blog1.save() entry1 = Entry(title="entry 1", blog=blog1) entry1.save() entry2 = Entry(title="entry 2", blog=blog1) entry2.save() self.assertEqual(Entry.objects.count(), 2) for entry in Entry.objects.all(): self.assertEqual( blog1, entry.blog ) blog2 = Blog(title="Blog") blog2.save() entry3 = Entry(title="entry 3", blog=blog2) entry3.save() self.assertEqual( # it's' necessary to explicitly state the pk here list(Entry.objects.filter(blog=blog1.pk)), [entry1, entry2] ) # def test_foreign_keys_bug(self): # blog1 = Blog(title="Blog") # blog1.save() # entry1 = Entry(title="entry 1", blog=blog1) # entry1.save() # self.assertEqual( # # this should work too # list(Entry.objects.filter(blog=blog1)), # [entry1] # ) # def test_standard_autofield(self): # # sam1 = StandardAutoFieldModel(title="title 1") # sam1.save() # sam2 = StandardAutoFieldModel(title="title 2") # sam2.save() # # self.assertEqual( # StandardAutoFieldModel.objects.count(), # 2 # ) # # sam1_query = StandardAutoFieldModel.objects.get(title="title 1") # self.assertEqual( # sam1_query.pk, # sam1.pk # ) # # sam1_query = StandardAutoFieldModel.objects.get(pk=sam1.pk) #
def test_lazy_model_instance(self): l1 = LazyModelInstance(Entry, 'some-pk') l2 = LazyModelInstance(Entry, 'some-pk') self.assertEqual(l1, l2) obj = Entry(title='foobar') obj.save() l3 = LazyModelInstance(Entry, obj.id) self.assertEqual(l3._wrapped, None) self.assertEqual(obj, l3) self.assertNotEqual(l3._wrapped, None)
def test_update(self): blog1 = Blog(title="Blog") blog1.save() blog2 = Blog(title="Blog 2") blog2.save() entry1 = Entry(title="entry 1", blog=blog1) entry1.save() Entry.objects.filter(pk=entry1.pk).update(blog=blog2) self.assertEqual( # this should work too list(Entry.objects.filter(blog=blog2)), [entry1] ) Entry.objects.filter(blog=blog2).update(title="Title has been updated") self.assertEqual( # this should work too Entry.objects.filter()[0].title, "Title has been updated" ) Entry.objects.filter(blog=blog2).update(title="Last Update Test", blog=blog1) self.assertEqual( # this should work too Entry.objects.filter()[0].title, "Last Update Test" ) self.assertEqual( # this should work too Entry.objects.filter(blog=blog1).count(), 1 )
def test_dates_ordering(self): now = datetime.datetime.now() before = now - datetime.timedelta(days=1) entry1 = Entry(title="entry 1", date_published=now) entry1.save() entry2 = Entry(title="entry 2", date_published=before) entry2.save() self.assertEqual( list(Entry.objects.order_by('-date_published')), [entry1, entry2] ) self.assertEqual( list(Entry.objects.order_by('date_published')), [entry2, entry1] )