class DMNoticesTest(TestCase): def setUp(self): self.dmn = DMNotices() def test_get_404(self): self.assertIsNone(self.dmn.get('docdoc')) def test_get_success(self): Notice(document_number='docdoc', fr_url='frfr', publication_date=date.today(), notice={"some": "body"}).save() self.assertEqual({"some": 'body'}, self.dmn.get('docdoc')) def test_listing(self): n = Notice(document_number='22', fr_url='fr1', notice={}, effective_on=date(2005, 5, 5), publication_date=date(2001, 3, 3)) n.save() n.noticecfrpart_set.create(cfr_part='876') n = Notice(document_number='9', fr_url='fr2', notice={}, publication_date=date(1999, 1, 1)) n.noticecfrpart_set.create(cfr_part='876') n.noticecfrpart_set.create(cfr_part='111') n.save() self.assertEqual([{'document_number': '22', 'fr_url': 'fr1', 'publication_date': '2001-03-03', 'effective_on': '2005-05-05'}, {'document_number': '9', 'fr_url': 'fr2', 'publication_date': '1999-01-01'}], self.dmn.listing()) self.assertEqual(self.dmn.listing(), self.dmn.listing('876')) self.assertEqual([], self.dmn.listing('888')) def test_put(self): """We can insert and replace a notice""" doc = {"some": "structure", 'effective_on': '2011-01-01', 'fr_url': 'http://example.com', 'publication_date': '2010-02-02', 'cfr_parts': ['222']} self.dmn.put('docdoc', doc) expected = {"document_number": "docdoc", "effective_on": date(2011, 1, 1), "fr_url": "http://example.com", "publication_date": date(2010, 2, 2), "noticecfrpart__cfr_part": '222', "notice": doc} fields = expected.keys() six.assertCountEqual(self, Notice.objects.all().values(*fields), [expected]) doc['fr_url'] = 'url2' self.dmn.put('docdoc', doc) expected['fr_url'] = 'url2' six.assertCountEqual(self, Notice.objects.all().values(*fields), [expected])
class SplitterNotices(object): """Implementation of Django+Elastic Search as notices backend""" def __init__(self): self.dm = DMNotices() self.es = ESNotices() self.get = self.dm.get self.listing = self.dm.listing def put(self, doc_number, part, notice): """Write to both""" self.dm.put(doc_number, part, notice) self.es.put(doc_number, notice)
def test_put(self): dmn = DMNotices() doc = {"some": "structure", 'effective_on': '2011-01-01', 'fr_url': 'http://example.com', 'publication_date': '2010-02-02', 'cfr_parts': ['222']} dmn.put('docdoc', doc) notices = Notice.objects.all() self.assertEqual(1, len(notices)) self.assertEqual('docdoc', notices[0].document_number) self.assertEqual(date(2011, 1, 1), notices[0].effective_on) self.assertEqual('http://example.com', notices[0].fr_url) self.assertEqual(date(2010, 2, 2), notices[0].publication_date) ncp = notices[0].noticecfrpart_set.all() self.assertEqual(1, len(ncp)) self.assertEqual('222', ncp[0].cfr_part) self.assertEqual(doc, notices[0].notice)
def test_put_overwrite(self): dmn = DMNotices() doc = {"some": "structure", 'effective_on': '2011-01-01', 'fr_url': 'http://example.com', 'publication_date': '2010-02-02', 'cfr_part': '222'} dmn.put('docdoc', doc) notices = Notice.objects.all() self.assertEqual(1, len(notices)) self.assertEqual('http://example.com', notices[0].fr_url) doc['fr_url'] = 'url2' dmn.put('docdoc', doc) notices = Notice.objects.all() self.assertEqual(1, len(notices)) self.assertEqual('url2', notices[0].fr_url)
class DMNoticesTest(TestCase): def setUp(self): self.dmn = DMNotices() def test_get_404(self): self.assertIsNone(self.dmn.get('docdoc')) def test_get_success(self): Notice(document_number='docdoc', fr_url='frfr', publication_date=date.today(), notice={ "some": "body" }).save() self.assertEqual({"some": 'body'}, self.dmn.get('docdoc')) def test_listing(self): n = Notice(document_number='22', fr_url='fr1', notice={}, effective_on=date(2005, 5, 5), publication_date=date(2001, 3, 3)) n.save() n.noticecfrpart_set.create(cfr_part='876') n = Notice(document_number='9', fr_url='fr2', notice={}, publication_date=date(1999, 1, 1)) n.noticecfrpart_set.create(cfr_part='876') n.noticecfrpart_set.create(cfr_part='111') n.save() self.assertEqual([{ 'document_number': '22', 'fr_url': 'fr1', 'publication_date': '2001-03-03', 'effective_on': '2005-05-05' }, { 'document_number': '9', 'fr_url': 'fr2', 'publication_date': '1999-01-01' }], self.dmn.listing()) self.assertEqual(self.dmn.listing(), self.dmn.listing('876')) self.assertEqual([], self.dmn.listing('888')) def test_put(self): """We can insert and replace a notice""" doc = { "some": "structure", 'effective_on': '2011-01-01', 'fr_url': 'http://example.com', 'publication_date': '2010-02-02', 'cfr_parts': ['222'] } self.dmn.put('docdoc', doc) expected = { "document_number": "docdoc", "effective_on": date(2011, 1, 1), "fr_url": "http://example.com", "publication_date": date(2010, 2, 2), "noticecfrpart__cfr_part": '222', "notice": doc } fields = expected.keys() six.assertCountEqual(self, Notice.objects.all().values(*fields), [expected]) doc['fr_url'] = 'url2' self.dmn.put('docdoc', doc) expected['fr_url'] = 'url2' six.assertCountEqual(self, Notice.objects.all().values(*fields), [expected])