def test_pull_by_start_end(self): ## Pulling by start / end should return None if none exists: self.assertIsNone(Timeframe.pull_by_start_end(datetime.datetime(2016, 1, 1), datetime.datetime(2017, 1, 1))) tf = Timeframe("yearly", datetime.datetime(2016, 1, 1)) tf.persist() self.assertTrue(tf.exists()) check_tf = Timeframe.pull_by_start_end(tf.start, tf.end).to_dict() for key, val in tf.to_dict().iteritems(): self.assertEqual(check_tf[key], val) ## Should *not* pull if the start is right and end is wrong or vice versa: self.assertIsNone(Timeframe.pull_by_start_end(tf.start, datetime.datetime(2016, 2, 1))) self.assertIsNone(Timeframe.pull_by_start_end(datetime.datetime(2016, 2, 1), tf.end))
def test_persist(self): ## Test that data persists to the database tf = Timeframe("yearly", datetime.datetime(2016, 1, 1)) tf.persist() self.assertTrue(tf.exists()) check_tf = Timeframe.pull_by_start_end(tf.start, tf.end).to_dict() for key, val in tf.to_dict().iteritems(): self.assertEqual(check_tf[key], val) ## Test that you can persist the same one twice without duplicate key, etc errors tf.persist()
def test_get_id(self): ## Test that a Timeframe that has just been persisted returns an ID tf = Timeframe("yearly", datetime.datetime(2016, 1, 1)) tf.persist() self.assertIsNotNone(tf.get_id()) ## Test that a timeframe pulled by id returns same id check_tf = Timeframe.pull_by_id(tf.get_id()) self.assertEqual(check_tf.get_id(), tf.get_id()) ## Test that a timeframe created by start / end persistes, then returns an ID tf = Timeframe("monthly", datetime.datetime(2016, 1, 1)) self.assertIsNotNone(tf.get_id()) self.assertTrue(tf.exists()) self.assertNotEqual(tf.get_id(), check_tf.get_id()) ## Test that a timeframe pulled by start / end returns an ID check_tf = Timeframe.pull_by_start_end(tf.start, tf.end) self.assertEqual(check_tf.get_id(), tf.get_id())