def test_auto_timestamps(self): office = Office.objects.create(state="MD", name="House of Delegates", district="35B", chamber="lower") contest = Contest( result_type='certified', election_type='general', start_date=datetime.strptime("2012-11-06", "%Y-%m-%d"), end_date=datetime.strptime("2012-11-06", "%Y-%m-%d"), source='20121106__md__general__state_legislative.csv', state='MD', election_id='md-2012-11-06-general', office=office, ) # Check that created and updated timestamps are auto-set when a new # instance is created self.assertIsNotNone(contest.created) self.assertIsNotNone(contest.updated) self.assert_times_almost_equal(contest.created, contest.updated) # Test that the timestamp is updated when the contest is saved updated = contest.updated contest.save() self.assertGreater(contest.updated, updated)
def _get_or_create_contest(self, row, mapping): year = int(re.search(r'\d{4}', self.election_id).group()) elecs = self.datasource.elections(year)[year] # Get election metadata by matching on election slug elec_meta = [e for e in elecs if e['slug'] == self.election_id][0] party = row['Party'].strip() slug = self._build_contest_slug(row) key = (self.election_id, slug) try: contest = self.contest_lkup[key] except KeyError: kwargs = { 'created': self.timestamp, 'updated': self.timestamp, 'source': self.source, 'election_id': self.election_id, 'slug': slug, 'state': self.state.upper(), 'start_date': datetime.datetime.strptime(elec_meta['start_date'], "%Y-%m-%d"), 'end_date': datetime.datetime.strptime(elec_meta['end_date'], "%Y-%m-%d"), 'election_type': elec_meta['race_type'], 'result_type': elec_meta['result_type'], 'special': elec_meta['special'], 'raw_office': row['Office Name'].strip(), 'raw_district': row['Office District'].strip(), } contest = Contest(**kwargs) # Add party if it's a primary if 'primary' in self.election_id: kwargs['raw_party'] = party contest.save() self.contest_lkup[key] = contest return contest
def test_auto_timestamps(self): office = Office.objects.create(state="MD", name= "House of Delegates", district="35B", chamber="lower") contest = Contest( result_type='certified', election_type='general', start_date=datetime.strptime("2012-11-06", "%Y-%m-%d"), end_date=datetime.strptime("2012-11-06", "%Y-%m-%d"), source='20121106__md__general__state_legislative.csv', state='MD', election_id='md-2012-11-06-general', office=office, ) # Check that created and updated timestamps are auto-set when a new # instance is created self.assertIsNotNone(contest.created) self.assertIsNotNone(contest.updated) self.assert_times_almost_equal(contest.created, contest.updated) # Test that the timestamp is updated when the contest is saved updated = contest.updated contest.save() self.assertGreater(contest.updated, updated)