def test_org_not_found_bad_code(self):
     # Organisation doesn't exist
     cmd = Command()
     self.base_record["Organisation ID"] = "XXXX"  # this Org ID doesn't exist
     cmd.read_from_url = lambda x: [self.base_record]
     with self.assertRaises(Organisation.DoesNotExist):
         cmd.handle(**self.opts)
 def test_divset_not_found(self):
     # Organisation does exist, but has no associated DivisionSets
     cmd = Command()
     self.base_record["Organisation ID"] = "TEST1"
     cmd.read_from_url = lambda x: [self.base_record]
     with self.assertRaises(Exception):
         cmd.handle(**self.opts)
 def test_org_not_found_bad_date(self):
     # Organisation code exists, but not valid for this date
     cmd = Command()
     self.base_record["Organisation ID"] = "TEST1"
     self.base_record["Start Date"] = "2016-09-01"  # before TEST1 org start date
     cmd.read_from_url = lambda x: [self.base_record]
     with self.assertRaises(Organisation.DoesNotExist):
         cmd.handle(**self.opts)
 def test_divset_null_end_date(self):
     # Organisation does exist and has an associated DivisionSets
     # but the DivisionSet has a NULL end date
     OrganisationDivisionSet.objects.create(
         organisation=self.org1,
         start_date="2016-10-01",
         end_date=None,
         legislation_url="",
         consultation_url="",
         short_title="",
         notes="",
     )
     cmd = Command()
     self.base_record["Organisation ID"] = "TEST1"
     cmd.read_from_url = lambda x: [self.base_record]
     with self.assertRaises(Exception):
         cmd.handle(**self.opts)
    def test_valid(self):
        # all data is valid - should import cleanly
        cmd = Command()
        cmd.read_from_url = lambda x: self.valid_test_data
        cmd.get_division_type_from_registers = lambda x: "DIW"
        cmd.handle(**self.opts)

        # check it all imported correctly
        org3divset = (
            OrganisationDivisionSet.objects.all()
            .filter(organisation=self.org3)
            .order_by("-start_date")
        )
        self.assertEqual(2, len(org3divset))
        self.assertEqual("2017-05-04", org3divset[0].start_date.strftime("%Y-%m-%d"))
        self.assertIsNone(org3divset[0].end_date)
        self.assertEqual(2, len(org3divset[0].divisions.all()))

        org4divset = (
            OrganisationDivisionSet.objects.all()
            .filter(organisation=self.org4)
            .order_by("-start_date")
        )
        self.assertEqual(2, len(org4divset))
        self.assertEqual("2018-05-03", org4divset[0].start_date.strftime("%Y-%m-%d"))
        self.assertIsNone(org4divset[0].end_date)
        self.assertEqual(2, len(org4divset[0].divisions.all()))