def test_svc_level_dupe(self): ''' check that service-level duplicate activities are caught (no DB involvement) ''' svcA, svcB = TestTools.create_mock_services() recA = TestTools.create_mock_svc_record(svcA) recB = TestTools.create_mock_svc_record(svcB) actA = Activity() actA.StartTime = datetime(1, 2, 3, 4, 5, 6, 7) actA.ServiceDataCollection = TestTools.create_mock_servicedatacollection( svcA, record=recA) actB = Activity() actB.StartTime = actA.StartTime actB.ServiceDataCollection = TestTools.create_mock_servicedatacollection( svcB, record=recB) actA.CalculateUID() actB.CalculateUID() s = SynchronizationTask(None) s._activities = [] s._accumulateActivities(recA, [actA]) s._accumulateActivities(recB, [actB]) self.assertEqual(len(s._activities), 1)
def test_svc_level_dupe_tz_irregular(self): ''' check that service-level duplicate activities with irregular TZs are caught ''' svcA, svcB = TestTools.create_mock_services() recA = TestTools.create_mock_svc_record(svcA) recB = TestTools.create_mock_svc_record(svcB) actA = Activity() actA.StartTime = pytz.timezone("America/Edmonton").localize( datetime(1, 2, 3, 4, 5, 6, 7)) actA.ServiceDataCollection = TestTools.create_mock_servicedatacollection( svcA, record=recA) actB = Activity() actB.StartTime = actA.StartTime.astimezone( pytz.timezone("America/Iqaluit")) actB.ServiceDataCollection = TestTools.create_mock_servicedatacollection( svcB, record=recB) actA.CalculateUID() actB.CalculateUID() s = SynchronizationTask(None) s._activities = [] s._accumulateActivities(recA, [actA]) s._accumulateActivities(recB, [actB]) self.assertEqual(len(s._activities), 1)
def test_activity_coalesce(self): ''' ensure that activity data is getting coalesced by _accumulateActivities ''' svcA, svcB = TestTools.create_mock_services() recA = TestTools.create_mock_svc_record(svcA) recB = TestTools.create_mock_svc_record(svcB) actA = TestTools.create_random_activity(svcA, tz=pytz.timezone("America/Iqaluit")) actB = Activity() actB.StartTime = actA.StartTime.replace(tzinfo=None) actB.ServiceDataCollection = TestTools.create_mock_servicedatacollection(svcB) actA.Name = "Not this" actA.Private = True actB.Name = "Heya" actB.Type = ActivityType.Walking actA.CalculateUID() actB.CalculateUID() activities = [] Sync._accumulateActivities(recB, [copy.deepcopy(actB)], activities) Sync._accumulateActivities(recA, [copy.deepcopy(actA)], activities) self.assertEqual(len(activities), 1) act = activities[0] self.assertEqual(act.StartTime, actA.StartTime) self.assertEqual(act.EndTime, actA.EndTime) self.assertEqual(act.EndTime.tzinfo, actA.StartTime.tzinfo) self.assertEqual(act.StartTime.tzinfo, actA.StartTime.tzinfo) self.assertEqual(act.Waypoints, actA.Waypoints) self.assertTrue(act.Private) # Most restrictive setting self.assertEqual(act.Name, actB.Name) # The first activity takes priority. self.assertEqual(act.Type, actB.Type) # Same here. self.assertTrue(list(actB.ServiceDataCollection.keys())[0] in act.ServiceDataCollection) self.assertTrue(list(actA.ServiceDataCollection.keys())[0] in act.ServiceDataCollection) activities = [] Sync._accumulateActivities(recA, [copy.deepcopy(actA)], activities) Sync._accumulateActivities(recB, [copy.deepcopy(actB)], activities) self.assertEqual(len(activities), 1) act = activities[0] self.assertEqual(act.StartTime, actA.StartTime) self.assertEqual(act.EndTime, actA.EndTime) self.assertEqual(act.EndTime.tzinfo, actA.StartTime.tzinfo) self.assertEqual(act.StartTime.tzinfo, actA.StartTime.tzinfo) self.assertEqual(act.Waypoints, actA.Waypoints) self.assertEqual(act.Name, actA.Name) # The first activity takes priority. self.assertEqual(act.Type, actB.Type) # Exception: ActivityType.Other does not take priority self.assertTrue(list(actB.ServiceDataCollection.keys())[0] in act.ServiceDataCollection) self.assertTrue(list(actA.ServiceDataCollection.keys())[0] in act.ServiceDataCollection) actA.Type = ActivityType.CrossCountrySkiing activities = [] Sync._accumulateActivities(recA, [copy.deepcopy(actA)], activities) Sync._accumulateActivities(recB, [copy.deepcopy(actB)], activities) self.assertEqual(len(activities), 1) act = activities[0] self.assertEqual(act.Type, actA.Type) # Here, it will take priority.
def create_blank_activity(svc=None, actType=ActivityType.Other, record=None): act = Activity() act.Type = actType if svc: act.UploadedTo = [TestTools.create_mock_upload_record(svc, record)] act.StartTime = datetime.now() act.EndTime = act.StartTime + timedelta(seconds=42) act.CalculateUID() return act
def create_blank_activity(svc=None, actType=ActivityType.Other, record=None): act = Activity() act.Type = actType if svc: record = record if record else TestTools.create_mock_svc_record(svc) act.ServiceDataCollection = TestTools.create_mock_servicedatacollection(svc, record=record) act.StartTime = datetime.now() act.EndTime = act.StartTime + timedelta(seconds=42) act.CalculateUID() return act
def test_activity_coalesce(self): ''' ensure that activity data is getting coalesced by _accumulateActivities ''' svcA, svcB = TestTools.create_mock_services() actA = TestTools.create_random_activity(svcA, tz=pytz.timezone("America/Iqaluit")) actB = Activity() actB.StartTime = actA.StartTime.replace(tzinfo=None) actB.UploadedTo = [TestTools.create_mock_upload_record(svcB)] actA.Name = "Not this" actB.Name = "Heya" actB.Type = ActivityType.Walking actA.CalculateUID() actB.CalculateUID() activities = [] Sync._accumulateActivities(Service.FromID("mockB"), [copy.deepcopy(actB)], activities) Sync._accumulateActivities(Service.FromID("mockA"), [copy.deepcopy(actA)], activities) self.assertEqual(len(activities), 1) act = activities[0] self.assertEqual(act.StartTime, actA.StartTime) self.assertEqual(act.EndTime, actA.EndTime) self.assertEqual(act.EndTime.tzinfo, actA.StartTime.tzinfo) self.assertEqual(act.StartTime.tzinfo, actA.StartTime.tzinfo) self.assertEqual(act.Waypoints, actA.Waypoints) self.assertEqual(act.Name, actB.Name) # The first activity takes priority. self.assertEqual(act.Type, actB.Type) # Same here. self.assertTrue(actB.UploadedTo[0] in act.UploadedTo) self.assertTrue(actA.UploadedTo[0] in act.UploadedTo) activities = [] Sync._accumulateActivities(Service.FromID("mockA"), [copy.deepcopy(actA)], activities) Sync._accumulateActivities(Service.FromID("mockB"), [copy.deepcopy(actB)], activities) self.assertEqual(len(activities), 1) act = activities[0] self.assertEqual(act.StartTime, actA.StartTime) self.assertEqual(act.EndTime, actA.EndTime) self.assertEqual(act.EndTime.tzinfo, actA.StartTime.tzinfo) self.assertEqual(act.StartTime.tzinfo, actA.StartTime.tzinfo) self.assertEqual(act.Waypoints, actA.Waypoints) self.assertEqual(act.Name, actA.Name) # The first activity takes priority. self.assertEqual(act.Type, actB.Type) # Exception: ActivityType.Other does not take priority self.assertTrue(actB.UploadedTo[0] in act.UploadedTo) self.assertTrue(actA.UploadedTo[0] in act.UploadedTo) actA.Type = ActivityType.CrossCountrySkiing activities = [] Sync._accumulateActivities(Service.FromID("mockA"), [copy.deepcopy(actA)], activities) Sync._accumulateActivities(Service.FromID("mockB"), [copy.deepcopy(actB)], activities) self.assertEqual(len(activities), 1) act = activities[0] self.assertEqual(act.Type, actA.Type) # Here, it will take priority.
def test_round_precise_time(self): ''' Some services might return really exact times, while others would round to the second - needs to be accounted for in hash algo ''' actA = Activity() actA.StartTime = datetime(1, 2, 3, 4, 5, 6, 7) actB = Activity() actB.StartTime = datetime(1, 2, 3, 4, 5, 6, 7) + timedelta(0, 0.1337) actA.CalculateUID() actB.CalculateUID() self.assertEqual(actA.UID, actB.UID)
def test_activity_deduplicate_tzerror(self): ''' Test that probably-duplicate activities with starttimes like 09:12:22 and 15:12:22 (on the same day) are recognized as one ''' svcA, svcB = TestTools.create_mock_services() recA = TestTools.create_mock_svc_record(svcA) recB = TestTools.create_mock_svc_record(svcB) actA = TestTools.create_random_activity( svcA, tz=pytz.timezone("America/Iqaluit")) actB = Activity() actB.StartTime = actA.StartTime.replace( tzinfo=pytz.timezone("America/Denver")) + timedelta(hours=5) actB.ServiceDataCollection = TestTools.create_mock_servicedatacollection( svcB) actA.Name = "Not this" actB.Name = "Heya" actB.Type = ActivityType.Walking actA.CalculateUID() actB.CalculateUID() s = SynchronizationTask(None) s._activities = [] s._accumulateActivities(recB, [copy.deepcopy(actB)]) s._accumulateActivities(recA, [copy.deepcopy(actA)]) self.assertEqual(len(s._activities), 1) # Ensure that it is deduplicated on non-exact match actB.StartTime = actA.StartTime.replace( tzinfo=pytz.timezone("America/Denver")) + timedelta(hours=5, seconds=1) s._activities = [] s._accumulateActivities(recB, [copy.deepcopy(actB)]) s._accumulateActivities(recA, [copy.deepcopy(actA)]) self.assertEqual(len(s._activities), 1) # Ensure that it is *not* deduplicated when it really doesn't match actB.StartTime = actA.StartTime.replace( tzinfo=pytz.timezone("America/Denver")) + timedelta(hours=5, minutes=7) s._activities = [] s._accumulateActivities(recB, [copy.deepcopy(actB)]) s._accumulateActivities(recA, [copy.deepcopy(actA)]) self.assertEqual(len(s._activities), 2) # Ensure that overly large differences >38hr - not possible via TZ differences & shamefully bad import/export code on the part of some services - are not deduplicated actB.StartTime = actA.StartTime.replace( tzinfo=pytz.timezone("America/Denver")) + timedelta(hours=50) s._activities = [] s._accumulateActivities(recB, [copy.deepcopy(actB)]) s._accumulateActivities(recA, [copy.deepcopy(actA)]) self.assertEqual(len(s._activities), 2)
def test_activity_deduplicate_tzerror(self): ''' Test that probably-duplicate activities with starttimes like 09:12:22 and 15:12:22 (on the same day) are recognized as one ''' svcA, svcB = TestTools.create_mock_services() actA = TestTools.create_random_activity(svcA, tz=pytz.timezone("America/Iqaluit")) actB = Activity() actB.StartTime = actA.StartTime.replace(tzinfo=pytz.timezone("America/Denver")) + timedelta(hours=5) actB.UploadedTo = [TestTools.create_mock_upload_record(svcB)] actA.Name = "Not this" actB.Name = "Heya" actB.Type = ActivityType.Walking actA.CalculateUID() actB.CalculateUID() activities = [] Sync._accumulateActivities(Service.FromID("mockB"), [copy.deepcopy(actB)], activities) Sync._accumulateActivities(Service.FromID("mockA"), [copy.deepcopy(actA)], activities) self.assertEqual(len(activities), 1) # Ensure that it is an exact match actB.StartTime = actA.StartTime.replace(tzinfo=pytz.timezone("America/Denver")) + timedelta(hours=5, seconds=1) activities = [] Sync._accumulateActivities(Service.FromID("mockB"), [copy.deepcopy(actB)], activities) Sync._accumulateActivities(Service.FromID("mockA"), [copy.deepcopy(actA)], activities) self.assertEqual(len(activities), 2) # Ensure that overly large differences >14hr - not possible via TZ differences - are not deduplicated actB.StartTime = actA.StartTime.replace(tzinfo=pytz.timezone("America/Denver")) + timedelta(hours=15) activities = [] Sync._accumulateActivities(Service.FromID("mockB"), [copy.deepcopy(actB)], activities) Sync._accumulateActivities(Service.FromID("mockA"), [copy.deepcopy(actA)], activities) self.assertEqual(len(activities), 2)
def test_svc_level_dupe(self): ''' check that service-level duplicate activities are caught (no DB involvement) ''' svcA, svcB = TestTools.create_mock_services() actA = Activity() actA.StartTime = datetime(1, 2, 3, 4, 5, 6, 7) actA.UploadedTo = [TestTools.create_mock_upload_record(svcA)] actB = Activity() actB.StartTime = actA.StartTime actB.UploadedTo = [TestTools.create_mock_upload_record(svcB)] actA.CalculateUID() actB.CalculateUID() activities = [] Sync._accumulateActivities(Service.FromID("mockA"), [actA], activities) Sync._accumulateActivities(Service.FromID("mockB"), [actB], activities) self.assertEqual(len(activities), 1)
def test_svc_level_dupe_tz_nonuniform(self): ''' check that service-level duplicate activities with non-uniform TZs are caught ''' svcA, svcB = TestTools.create_mock_services() actA = Activity() actA.StartTime = datetime(1, 2, 3, 4, 5, 6, 7) actA.UploadedTo = [TestTools.create_mock_upload_record(svcA)] actB = Activity() actB.StartTime = pytz.timezone("America/Denver").localize( actA.StartTime) actB.UploadedTo = [TestTools.create_mock_upload_record(svcB)] actA.CalculateUID() actB.CalculateUID() activities = [] Sync._accumulateActivities(Service.FromID("mockA"), [actA], activities) Sync._accumulateActivities(Service.FromID("mockB"), [actB], activities) self.assertEqual(len(activities), 1)
def test_svc_level_dupe_tz_irregular(self): ''' check that service-level duplicate activities with irregular TZs are caught ''' svcA, svcB = TestTools.create_mock_services() actA = Activity() actA.StartTime = pytz.timezone("America/Edmonton").localize( datetime(1, 2, 3, 4, 5, 6, 7)) actA.UploadedTo = [TestTools.create_mock_upload_record(svcA)] actB = Activity() actB.StartTime = actA.StartTime.astimezone( pytz.timezone("America/Iqaluit")) actB.UploadedTo = [TestTools.create_mock_upload_record(svcB)] actA.CalculateUID() actB.CalculateUID() activities = [] Sync._accumulateActivities(Service.FromID("mockA"), [actA], activities) Sync._accumulateActivities(Service.FromID("mockB"), [actB], activities) self.assertEqual(len(activities), 1)
def test_svc_level_dupe(self): ''' check that service-level duplicate activities are caught (no DB involvement) ''' svcA, svcB = TestTools.create_mock_services() recA = TestTools.create_mock_svc_record(svcA) recB = TestTools.create_mock_svc_record(svcB) actA = Activity() actA.StartTime = datetime(1, 2, 3, 4, 5, 6, 7) actA.ServiceDataCollection = TestTools.create_mock_servicedatacollection(svcA, record=recA) actB = Activity() actB.StartTime = actA.StartTime actB.ServiceDataCollection = TestTools.create_mock_servicedatacollection(svcB, record=recB) actA.CalculateUID() actB.CalculateUID() activities = [] Sync._accumulateActivities(recA, [actA], activities) Sync._accumulateActivities(recB, [actB], activities) self.assertEqual(len(activities), 1)
def test_activity_deduplicate_tzerror(self): ''' Test that probably-duplicate activities with starttimes like 09:12:22 and 15:12:22 (on the same day) are recognized as one ''' svcA, svcB = TestTools.create_mock_services() actA = TestTools.create_random_activity( svcA, tz=pytz.timezone("America/Iqaluit")) actB = Activity() actB.StartTime = actA.StartTime.replace( tzinfo=pytz.timezone("America/Denver")) + timedelta(hours=5) actB.UploadedTo = [TestTools.create_mock_upload_record(svcB)] actA.Name = "Not this" actB.Name = "Heya" actB.Type = ActivityType.Walking actA.CalculateUID() actB.CalculateUID() activities = [] Sync._accumulateActivities(Service.FromID("mockB"), [copy.deepcopy(actB)], activities) Sync._accumulateActivities(Service.FromID("mockA"), [copy.deepcopy(actA)], activities) self.assertEqual(len(activities), 1) # Ensure that it is an exact match actB.StartTime = actA.StartTime.replace( tzinfo=pytz.timezone("America/Denver")) + timedelta(hours=5, seconds=1) activities = [] Sync._accumulateActivities(Service.FromID("mockB"), [copy.deepcopy(actB)], activities) Sync._accumulateActivities(Service.FromID("mockA"), [copy.deepcopy(actA)], activities) self.assertEqual(len(activities), 2) # Ensure that overly large differences >38hr - not possible via TZ differences & shamefully bad import/export code on the part of some services - are not deduplicated actB.StartTime = actA.StartTime.replace( tzinfo=pytz.timezone("America/Denver")) + timedelta(hours=50) activities = [] Sync._accumulateActivities(Service.FromID("mockB"), [copy.deepcopy(actB)], activities) Sync._accumulateActivities(Service.FromID("mockA"), [copy.deepcopy(actA)], activities) self.assertEqual(len(activities), 2)
def test_svc_level_dupe_tz_nonuniform(self): ''' check that service-level duplicate activities with non-uniform TZs are caught ''' svcA, svcB = TestTools.create_mock_services() recA = TestTools.create_mock_svc_record(svcA) recB = TestTools.create_mock_svc_record(svcB) actA = Activity() actA.StartTime = datetime(1, 2, 3, 4, 5, 6, 7) actA.ServiceDataCollection = TestTools.create_mock_servicedatacollection( svcA, record=recA) actB = Activity() actB.StartTime = pytz.timezone("America/Denver").localize( actA.StartTime) actB.ServiceDataCollection = TestTools.create_mock_servicedatacollection( svcB, record=recB) actA.CalculateUID() actB.CalculateUID() activities = [] Sync._accumulateActivities(recA, [actA], activities) Sync._accumulateActivities(recB, [actB], activities) self.assertEqual(len(activities), 1)
def test_svc_supported_activity_types(self): ''' check that only activities are only sent to services which support them ''' svcA, svcB = TestTools.create_mock_services() recA = TestTools.create_mock_svc_record(svcA) recB = TestTools.create_mock_svc_record(svcB) svcA.SupportedActivities = [ActivityType.CrossCountrySkiing] svcB.SupportedActivities = [ActivityType.Cycling] actA = Activity() actA.StartTime = datetime(1, 2, 3, 4, 5, 6, 7) actA.ServiceDataCollection = TestTools.create_mock_servicedatacollection(svcA, record=recA) actA.Type = svcA.SupportedActivities[0] actB = Activity() actB.StartTime = datetime(5, 6, 7, 8, 9, 10, 11) actB.ServiceDataCollection = TestTools.create_mock_servicedatacollection(svcB, record=recB) actB.Type = [x for x in svcB.SupportedActivities if x != actA.Type][0] actA.CalculateUID() actB.CalculateUID() allConns = [recA, recB] activities = [] Sync._accumulateActivities(recA, [actA], activities) Sync._accumulateActivities(recB, [actB], activities) syncToA = Sync._determineRecipientServices(actA, allConns) syncToB = Sync._determineRecipientServices(actB, allConns) self.assertEqual(len(syncToA), 0) self.assertEqual(len(syncToB), 0) svcB.SupportedActivities = svcA.SupportedActivities syncToA = Sync._determineRecipientServices(actA, allConns) syncToB = Sync._determineRecipientServices(actB, allConns) self.assertEqual(len(syncToA), 1) self.assertEqual(len(syncToB), 0) svcB.SupportedActivities = svcA.SupportedActivities = [ActivityType.CrossCountrySkiing, ActivityType.Cycling] syncToA = Sync._determineRecipientServices(actA, allConns) syncToB = Sync._determineRecipientServices(actB, allConns) self.assertEqual(len(syncToA), 1) self.assertEqual(len(syncToB), 1)
def test_activity_deduplicate_tzerror(self): ''' Test that probably-duplicate activities with starttimes like 09:12:22 and 15:12:22 (on the same day) are recognized as one ''' svcA, svcB = TestTools.create_mock_services() recA = TestTools.create_mock_svc_record(svcA) recB = TestTools.create_mock_svc_record(svcB) actA = TestTools.create_random_activity(svcA, tz=pytz.timezone("America/Iqaluit")) actB = Activity() actB.StartTime = actA.StartTime.replace(tzinfo=pytz.timezone("America/Denver")) + timedelta(hours=5) actB.ServiceDataCollection = TestTools.create_mock_servicedatacollection(svcB) actA.Name = "Not this" actB.Name = "Heya" actB.Type = ActivityType.Walking actA.CalculateUID() actB.CalculateUID() s = SynchronizationTask(None) s._activities = [] s._accumulateActivities(recB, [copy.deepcopy(actB)]) s._accumulateActivities(recA, [copy.deepcopy(actA)]) self.assertEqual(len(s._activities), 1) # Ensure that it is deduplicated on non-exact match actB.StartTime = actA.StartTime.replace(tzinfo=pytz.timezone("America/Denver")) + timedelta(hours=5, seconds=1) s._activities = [] s._accumulateActivities(recB, [copy.deepcopy(actB)]) s._accumulateActivities(recA, [copy.deepcopy(actA)]) self.assertEqual(len(s._activities), 1) # Ensure that it is *not* deduplicated when it really doesn't match actB.StartTime = actA.StartTime.replace(tzinfo=pytz.timezone("America/Denver")) + timedelta(hours=5, minutes=7) s._activities = [] s._accumulateActivities(recB, [copy.deepcopy(actB)]) s._accumulateActivities(recA, [copy.deepcopy(actA)]) self.assertEqual(len(s._activities), 2) # Ensure that overly large differences >38hr - not possible via TZ differences & shamefully bad import/export code on the part of some services - are not deduplicated actB.StartTime = actA.StartTime.replace(tzinfo=pytz.timezone("America/Denver")) + timedelta(hours=50) s._activities = [] s._accumulateActivities(recB, [copy.deepcopy(actB)]) s._accumulateActivities(recA, [copy.deepcopy(actA)]) self.assertEqual(len(s._activities), 2)
def create_blank_activity(svc=None, actType=ActivityType.Other, record=None): act = Activity() act.Type = actType if svc: record = record if record else TestTools.create_mock_svc_record( svc) act.ServiceDataCollection = TestTools.create_mock_servicedatacollection( svc, record=record) act.StartTime = datetime.now() act.EndTime = act.StartTime + timedelta(seconds=42) act.CalculateUID() return act
def test_svc_level_dupe_time_leeway(self): ''' check that service-level duplicate activities within the defined time leeway are caught ''' svcA, svcB = TestTools.create_mock_services() recA = TestTools.create_mock_svc_record(svcA) recB = TestTools.create_mock_svc_record(svcB) actA = Activity() actA.StartTime = datetime(1, 2, 3, 4, 5, 6, 7) actA.ServiceDataCollection = TestTools.create_mock_servicedatacollection(svcA, record=recA) actA.Type = set(svcA.SupportedActivities).intersection(set(svcB.SupportedActivities)).pop() actB = Activity() actB.StartTime = datetime(1, 2, 3, 4, 6, 6, 7) actB.ServiceDataCollection = TestTools.create_mock_servicedatacollection(svcB, record=recB) actB.Type = actA.Type actA.CalculateUID() actB.CalculateUID() s = SynchronizationTask(None) s._activities = [] s._accumulateActivities(recA, [actA]) s._accumulateActivities(recB, [actB]) self.assertIn(actA.UID, actA.UIDs) self.assertIn(actB.UID, actA.UIDs) self.assertIn(actA.UID, actB.UIDs) self.assertIn(actB.UID, actB.UIDs) # we need to fake up the service records to avoid having to call the actual sync method where these values are normally preset recA = TestTools.create_mock_svc_record(svcA) recB = TestTools.create_mock_svc_record(svcB) recA.SynchronizedActivities = [actA.UID] recB.SynchronizedActivities = [actB.UID] s._serviceConnections = [recA, recB] recipientServicesA = s._determineRecipientServices(actA) recipientServicesB = s._determineRecipientServices(actB) self.assertEqual(len(recipientServicesA), 0) self.assertEqual(len(recipientServicesB), 0) self.assertEqual(len(s._activities), 1)
def test_activity_deduplicate_normaltz(self): ''' ensure that we can't deduplicate activities with non-pytz timezones ''' svcA, svcB = TestTools.create_mock_services() actA = TestTools.create_random_activity(svcA, tz=UTC()) actB = Activity() actB.StartTime = actA.StartTime.replace(tzinfo=None) + timedelta( seconds=10) actB.EndTime = actA.EndTime.replace(tzinfo=None) actB.UploadedTo = [TestTools.create_mock_upload_record(svcB)] actA.Name = "Not this" actB.Name = "Heya" actB.Type = ActivityType.Walking actA.CalculateUID() actB.CalculateUID() activities = [] Sync._accumulateActivities(Service.FromID("mockB"), [copy.deepcopy(actB)], activities) self.assertRaises(ValueError, Sync._accumulateActivities, Service.FromID("mockA"), [copy.deepcopy(actA)], activities)
def test_svc_level_dupe_time_leeway(self): ''' check that service-level duplicate activities within the defined time leeway are caught ''' svcA, svcB = TestTools.create_mock_services() actA = Activity() actA.StartTime = datetime(1, 2, 3, 4, 5, 6, 7) actA.UploadedTo = [TestTools.create_mock_upload_record(svcA)] actA.Type = set(svcA.SupportedActivities).intersection(set(svcB.SupportedActivities)).pop() actB = Activity() actB.StartTime = datetime(1, 2, 3, 4, 6, 6, 7) actB.UploadedTo = [TestTools.create_mock_upload_record(svcB)] actB.Type = actA.Type actA.CalculateUID() actB.CalculateUID() activities = [] Sync._accumulateActivities(Service.FromID("mockA"), [actA], activities) Sync._accumulateActivities(Service.FromID("mockB"), [actB], activities) self.assertIn(actA.UID, actA.UIDs) self.assertIn(actB.UID, actA.UIDs) self.assertIn(actA.UID, actB.UIDs) self.assertIn(actB.UID, actB.UIDs) # we need to fake up the service records to avoid having to call the actual sync method where these values are normally preset recA = TestTools.create_mock_svc_record(svcA) recB = TestTools.create_mock_svc_record(svcB) recA.SynchronizedActivities = [actA.UID] recB.SynchronizedActivities = [actB.UID] recipientServicesA = Sync._determineRecipientServices(actA, [recA, recB]) recipientServicesB = Sync._determineRecipientServices(actB, [recA, recB]) self.assertEqual(len(recipientServicesA), 0) self.assertEqual(len(recipientServicesB), 0) self.assertEqual(len(activities), 1)
def test_activity_deduplicate_normaltz(self): ''' ensure that we can't deduplicate activities with non-pytz timezones ''' svcA, svcB = TestTools.create_mock_services() recA = TestTools.create_mock_svc_record(svcA) recB = TestTools.create_mock_svc_record(svcB) actA = TestTools.create_random_activity(svcA, tz=UTC()) actB = Activity() actB.StartTime = actA.StartTime.replace(tzinfo=None) + timedelta( seconds=10) actB.EndTime = actA.EndTime.replace(tzinfo=None) actB.ServiceDataCollection = TestTools.create_mock_servicedatacollection( svcB, record=recB) actA.Name = "Not this" actB.Name = "Heya" actB.Type = ActivityType.Walking actA.CalculateUID() actB.CalculateUID() s = SynchronizationTask(None) s._activities = [] s._accumulateActivities(recB, [copy.deepcopy(actB)]) self.assertRaises(ValueError, s._accumulateActivities, recA, [copy.deepcopy(actA)])
def test_svc_level_dupe_tz_irregular(self): ''' check that service-level duplicate activities with irregular TZs are caught ''' svcA, svcB = TestTools.create_mock_services() actA = Activity() actA.StartTime = pytz.timezone("America/Edmonton").localize(datetime(1, 2, 3, 4, 5, 6, 7)) actA.UploadedTo = [TestTools.create_mock_upload_record(svcA)] actB = Activity() actB.StartTime = actA.StartTime.astimezone(pytz.timezone("America/Iqaluit")) actB.UploadedTo = [TestTools.create_mock_upload_record(svcB)] actA.CalculateUID() actB.CalculateUID() activities = [] Sync._accumulateActivities(Service.FromID("mockA"), [actA], activities) Sync._accumulateActivities(Service.FromID("mockB"), [actB], activities) self.assertEqual(len(activities), 1)
def test_svc_level_dupe_tz_nonuniform(self): ''' check that service-level duplicate activities with non-uniform TZs are caught ''' svcA, svcB = TestTools.create_mock_services() actA = Activity() actA.StartTime = datetime(1, 2, 3, 4, 5, 6, 7) actA.UploadedTo = [TestTools.create_mock_upload_record(svcA)] actB = Activity() actB.StartTime = pytz.timezone("America/Denver").localize(actA.StartTime) actB.UploadedTo = [TestTools.create_mock_upload_record(svcB)] actA.CalculateUID() actB.CalculateUID() activities = [] Sync._accumulateActivities(Service.FromID("mockA"), [actA], activities) Sync._accumulateActivities(Service.FromID("mockB"), [actB], activities) self.assertEqual(len(activities), 1)
def test_activity_deduplicate_normaltz(self): ''' ensure that we can't deduplicate activities with non-pytz timezones ''' svcA, svcB = TestTools.create_mock_services() actA = TestTools.create_random_activity(svcA, tz=UTC()) actB = Activity() actB.StartTime = actA.StartTime.replace(tzinfo=None) + timedelta(seconds=10) actB.EndTime = actA.EndTime.replace(tzinfo=None) actB.UploadedTo = [TestTools.create_mock_upload_record(svcB)] actA.Name = "Not this" actB.Name = "Heya" actB.Type = ActivityType.Walking actA.CalculateUID() actB.CalculateUID() activities = [] Sync._accumulateActivities(Service.FromID("mockB"), [copy.deepcopy(actB)], activities) self.assertRaises(ValueError, Sync._accumulateActivities, Service.FromID("mockA"), [copy.deepcopy(actA)], activities)
def test_svc_level_dupe_tz_irregular(self): ''' check that service-level duplicate activities with irregular TZs are caught ''' svcA, svcB = TestTools.create_mock_services() recA = TestTools.create_mock_svc_record(svcA) recB = TestTools.create_mock_svc_record(svcB) actA = Activity() actA.StartTime = pytz.timezone("America/Edmonton").localize(datetime(1, 2, 3, 4, 5, 6, 7)) actA.ServiceDataCollection = TestTools.create_mock_servicedatacollection(svcA, record=recA) actB = Activity() actB.StartTime = actA.StartTime.astimezone(pytz.timezone("America/Iqaluit")) actB.ServiceDataCollection = TestTools.create_mock_servicedatacollection(svcB, record=recB) actA.CalculateUID() actB.CalculateUID() activities = [] Sync._accumulateActivities(recA, [actA], activities) Sync._accumulateActivities(recB, [actB], activities) self.assertEqual(len(activities), 1)
def test_svc_level_dupe_tz_nonuniform(self): ''' check that service-level duplicate activities with non-uniform TZs are caught ''' svcA, svcB = TestTools.create_mock_services() recA = TestTools.create_mock_svc_record(svcA) recB = TestTools.create_mock_svc_record(svcB) actA = Activity() actA.StartTime = datetime(1, 2, 3, 4, 5, 6, 7) actA.ServiceDataCollection = TestTools.create_mock_servicedatacollection(svcA, record=recA) actB = Activity() actB.StartTime = pytz.timezone("America/Denver").localize(actA.StartTime) actB.ServiceDataCollection = TestTools.create_mock_servicedatacollection(svcB, record=recB) actA.CalculateUID() actB.CalculateUID() activities = [] Sync._accumulateActivities(recA, [actA], activities) Sync._accumulateActivities(recB, [actB], activities) self.assertEqual(len(activities), 1)
def test_activity_deduplicate_normaltz(self): ''' ensure that we can't deduplicate activities with non-pytz timezones ''' svcA, svcB = TestTools.create_mock_services() recA = TestTools.create_mock_svc_record(svcA) recB = TestTools.create_mock_svc_record(svcB) actA = TestTools.create_random_activity(svcA, tz=UTC()) actB = Activity() actB.StartTime = actA.StartTime.replace(tzinfo=None) + timedelta(seconds=10) actB.EndTime = actA.EndTime.replace(tzinfo=None) actB.ServiceDataCollection = TestTools.create_mock_servicedatacollection(svcB, record=recB) actA.Name = "Not this" actB.Name = "Heya" actB.Type = ActivityType.Walking actA.CalculateUID() actB.CalculateUID() s = SynchronizationTask(None) s._activities = [] s._accumulateActivities(recB, [copy.deepcopy(actB)]) self.assertRaises(ValueError, s._accumulateActivities, recA, [copy.deepcopy(actA)])
def test_svc_level_dupe_tz_uniform(self): ''' check that service-level duplicate activities with the same TZs are caught ''' svcA, svcB = TestTools.create_mock_services() recA = TestTools.create_mock_svc_record(svcA) recB = TestTools.create_mock_svc_record(svcB) actA = Activity() actA.StartTime = pytz.timezone("America/Denver").localize(datetime(1, 2, 3, 4, 5, 6, 7)) actA.ServiceDataCollection = TestTools.create_mock_servicedatacollection(svcA, record=recA) actB = Activity() actB.StartTime = actA.StartTime actB.ServiceDataCollection = TestTools.create_mock_servicedatacollection(svcB, record=recB) actA.CalculateUID() actB.CalculateUID() s = SynchronizationTask(None) s._activities = [] s._accumulateActivities(recA, [actA]) s._accumulateActivities(recB, [actB]) self.assertEqual(len(s._activities), 1)
def test_svc_level_dupe_time_leeway(self): ''' check that service-level duplicate activities within the defined time leeway are caught ''' svcA, svcB = TestTools.create_mock_services() recA = TestTools.create_mock_svc_record(svcA) recB = TestTools.create_mock_svc_record(svcB) actA = Activity() actA.StartTime = datetime(1, 2, 3, 4, 5, 6, 7) actA.ServiceDataCollection = TestTools.create_mock_servicedatacollection( svcA, record=recA) actA.Type = set(svcA.SupportedActivities).intersection( set(svcB.SupportedActivities)).pop() actB = Activity() actB.StartTime = datetime(1, 2, 3, 4, 6, 6, 7) actB.ServiceDataCollection = TestTools.create_mock_servicedatacollection( svcB, record=recB) actB.Type = actA.Type actA.CalculateUID() actB.CalculateUID() s = SynchronizationTask(None) s._activities = [] s._accumulateActivities(recA, [actA]) s._accumulateActivities(recB, [actB]) self.assertIn(actA.UID, actA.UIDs) self.assertIn(actB.UID, actA.UIDs) self.assertIn(actA.UID, actB.UIDs) self.assertIn(actB.UID, actB.UIDs) # we need to fake up the service records to avoid having to call the actual sync method where these values are normally preset recA = TestTools.create_mock_svc_record(svcA) recB = TestTools.create_mock_svc_record(svcB) recA.SynchronizedActivities = [actA.UID] recB.SynchronizedActivities = [actB.UID] s._serviceConnections = [recA, recB] recipientServicesA = s._determineRecipientServices(actA) recipientServicesB = s._determineRecipientServices(actB) self.assertEqual(len(recipientServicesA), 0) self.assertEqual(len(recipientServicesB), 0) self.assertEqual(len(s._activities), 1)
def test_svc_supported_activity_types(self): ''' check that only activities are only sent to services which support them ''' svcA, svcB = TestTools.create_mock_services() recA = TestTools.create_mock_svc_record(svcA) recB = TestTools.create_mock_svc_record(svcB) svcA.SupportedActivities = [ActivityType.CrossCountrySkiing] svcB.SupportedActivities = [ActivityType.Cycling] actA = Activity() actA.StartTime = datetime(1, 2, 3, 4, 5, 6, 7) actA.ServiceDataCollection = TestTools.create_mock_servicedatacollection( svcA, record=recA) actA.Type = svcA.SupportedActivities[0] actA.CalculateUID() actA.UIDs = set([actA.UID]) actA.Record = ActivityRecord.FromActivity(actA) actB = Activity() actB.StartTime = datetime(5, 6, 7, 8, 9, 10, 11) actB.ServiceDataCollection = TestTools.create_mock_servicedatacollection( svcB, record=recB) actB.Type = [x for x in svcB.SupportedActivities if x != actA.Type][0] actB.CalculateUID() actB.UIDs = set([actB.UID]) actB.Record = ActivityRecord.FromActivity(actB) s = SynchronizationTask(None) s._serviceConnections = [recA, recB] s._activities = [] s._accumulateActivities(recA, [actA]) s._accumulateActivities(recB, [actB]) syncToA = s._determineRecipientServices(actA) syncToB = s._determineRecipientServices(actB) self.assertEqual(len(syncToA), 0) self.assertEqual(len(syncToB), 0) svcB.SupportedActivities = svcA.SupportedActivities syncToA = s._determineRecipientServices(actA) syncToB = s._determineRecipientServices(actB) self.assertEqual(len(syncToA), 1) self.assertEqual(len(syncToB), 0) svcB.SupportedActivities = svcA.SupportedActivities = [ ActivityType.CrossCountrySkiing, ActivityType.Cycling ] syncToA = s._determineRecipientServices(actA) syncToB = s._determineRecipientServices(actB) self.assertEqual(len(syncToA), 1) self.assertEqual(len(syncToB), 1)
def test_activity_coalesce(self): ''' ensure that activity data is getting coalesced by _accumulateActivities ''' svcA, svcB = TestTools.create_mock_services() recA = TestTools.create_mock_svc_record(svcA) recB = TestTools.create_mock_svc_record(svcB) actA = TestTools.create_random_activity( svcA, tz=pytz.timezone("America/Iqaluit")) actB = Activity() actB.StartTime = actA.StartTime.replace(tzinfo=None) actB.ServiceDataCollection = TestTools.create_mock_servicedatacollection( svcB) actA.Name = "Not this" actA.Private = True actB.Name = "Heya" actB.Type = ActivityType.Walking actA.CalculateUID() actB.CalculateUID() s = SynchronizationTask(None) s._activities = [] s._accumulateActivities(recB, [copy.deepcopy(actB)]) s._accumulateActivities(recA, [copy.deepcopy(actA)]) self.assertEqual(len(s._activities), 1) act = s._activities[0] self.assertEqual(act.StartTime, actA.StartTime) self.assertEqual(act.EndTime, actA.EndTime) self.assertEqual(act.EndTime.tzinfo, actA.StartTime.tzinfo) self.assertEqual(act.StartTime.tzinfo, actA.StartTime.tzinfo) self.assertLapsListsEqual(act.Laps, actA.Laps) self.assertTrue(act.Private) # Most restrictive setting self.assertEqual(act.Name, actB.Name) # The first activity takes priority. self.assertEqual(act.Type, actB.Type) # Same here. self.assertTrue( list(actB.ServiceDataCollection.keys())[0] in act.ServiceDataCollection) self.assertTrue( list(actA.ServiceDataCollection.keys())[0] in act.ServiceDataCollection) s._activities = [] s._accumulateActivities(recA, [copy.deepcopy(actA)]) s._accumulateActivities(recB, [copy.deepcopy(actB)]) self.assertEqual(len(s._activities), 1) act = s._activities[0] self.assertEqual(act.StartTime, actA.StartTime) self.assertEqual(act.EndTime, actA.EndTime) self.assertEqual(act.EndTime.tzinfo, actA.StartTime.tzinfo) self.assertEqual(act.StartTime.tzinfo, actA.StartTime.tzinfo) self.assertLapsListsEqual(act.Laps, actA.Laps) self.assertEqual(act.Name, actA.Name) # The first activity takes priority. self.assertEqual( act.Type, actB.Type) # Exception: ActivityType.Other does not take priority self.assertTrue( list(actB.ServiceDataCollection.keys())[0] in act.ServiceDataCollection) self.assertTrue( list(actA.ServiceDataCollection.keys())[0] in act.ServiceDataCollection) # Similar activities should be coalesced (Hiking, Walking..).. actA.Type = ActivityType.Hiking s._activities = [] s._accumulateActivities(recA, [copy.deepcopy(actA)]) s._accumulateActivities(recB, [copy.deepcopy(actB)]) self.assertEqual(len(s._activities), 1) act = s._activities[0] self.assertEqual(act.Type, actA.Type) # Here, it will take priority. # Dissimilar should not.. actA.Type = ActivityType.CrossCountrySkiing s._activities = [] s._accumulateActivities(recA, [copy.deepcopy(actA)]) s._accumulateActivities(recB, [copy.deepcopy(actB)]) self.assertEqual(len(s._activities), 2) self.assertEqual(s._activities[0].Type, actB.Type) self.assertEqual(s._activities[1].Type, actA.Type)
def test_activity_coalesce(self): ''' ensure that activity data is getting coalesced by _accumulateActivities ''' svcA, svcB = TestTools.create_mock_services() actA = TestTools.create_random_activity( svcA, tz=pytz.timezone("America/Iqaluit")) actB = Activity() actB.StartTime = actA.StartTime.replace(tzinfo=None) actB.UploadedTo = [TestTools.create_mock_upload_record(svcB)] actA.Name = "Not this" actA.Private = True actB.Name = "Heya" actB.Type = ActivityType.Walking actA.CalculateUID() actB.CalculateUID() activities = [] Sync._accumulateActivities(Service.FromID("mockB"), [copy.deepcopy(actB)], activities) Sync._accumulateActivities(Service.FromID("mockA"), [copy.deepcopy(actA)], activities) self.assertEqual(len(activities), 1) act = activities[0] self.assertEqual(act.StartTime, actA.StartTime) self.assertEqual(act.EndTime, actA.EndTime) self.assertEqual(act.EndTime.tzinfo, actA.StartTime.tzinfo) self.assertEqual(act.StartTime.tzinfo, actA.StartTime.tzinfo) self.assertEqual(act.Waypoints, actA.Waypoints) self.assertTrue(act.Private) # Most restrictive setting self.assertEqual(act.Name, actB.Name) # The first activity takes priority. self.assertEqual(act.Type, actB.Type) # Same here. self.assertTrue(actB.UploadedTo[0] in act.UploadedTo) self.assertTrue(actA.UploadedTo[0] in act.UploadedTo) activities = [] Sync._accumulateActivities(Service.FromID("mockA"), [copy.deepcopy(actA)], activities) Sync._accumulateActivities(Service.FromID("mockB"), [copy.deepcopy(actB)], activities) self.assertEqual(len(activities), 1) act = activities[0] self.assertEqual(act.StartTime, actA.StartTime) self.assertEqual(act.EndTime, actA.EndTime) self.assertEqual(act.EndTime.tzinfo, actA.StartTime.tzinfo) self.assertEqual(act.StartTime.tzinfo, actA.StartTime.tzinfo) self.assertEqual(act.Waypoints, actA.Waypoints) self.assertEqual(act.Name, actA.Name) # The first activity takes priority. self.assertEqual( act.Type, actB.Type) # Exception: ActivityType.Other does not take priority self.assertTrue(actB.UploadedTo[0] in act.UploadedTo) self.assertTrue(actA.UploadedTo[0] in act.UploadedTo) actA.Type = ActivityType.CrossCountrySkiing activities = [] Sync._accumulateActivities(Service.FromID("mockA"), [copy.deepcopy(actA)], activities) Sync._accumulateActivities(Service.FromID("mockB"), [copy.deepcopy(actB)], activities) self.assertEqual(len(activities), 1) act = activities[0] self.assertEqual(act.Type, actA.Type) # Here, it will take priority.
def test_svc_level_dupe_time_leeway(self): ''' check that service-level duplicate activities within the defined time leeway are caught ''' svcA, svcB = TestTools.create_mock_services() actA = Activity() actA.StartTime = datetime(1, 2, 3, 4, 5, 6, 7) actA.UploadedTo = [TestTools.create_mock_upload_record(svcA)] actA.Type = set(svcA.SupportedActivities).intersection( set(svcB.SupportedActivities)).pop() actB = Activity() actB.StartTime = datetime(1, 2, 3, 4, 6, 6, 7) actB.UploadedTo = [TestTools.create_mock_upload_record(svcB)] actB.Type = actA.Type actA.CalculateUID() actB.CalculateUID() activities = [] Sync._accumulateActivities(Service.FromID("mockA"), [actA], activities) Sync._accumulateActivities(Service.FromID("mockB"), [actB], activities) self.assertIn(actA.UID, actA.UIDs) self.assertIn(actB.UID, actA.UIDs) self.assertIn(actA.UID, actB.UIDs) self.assertIn(actB.UID, actB.UIDs) # we need to fake up the service records to avoid having to call the actual sync method where these values are normally preset recA = TestTools.create_mock_svc_record(svcA) recB = TestTools.create_mock_svc_record(svcB) recA.SynchronizedActivities = [actA.UID] recB.SynchronizedActivities = [actB.UID] recipientServicesA = Sync._determineRecipientServices( actA, [recA, recB]) recipientServicesB = Sync._determineRecipientServices( actB, [recA, recB]) self.assertEqual(len(recipientServicesA), 0) self.assertEqual(len(recipientServicesB), 0) self.assertEqual(len(activities), 1)
def test_svc_supported_activity_types(self): ''' check that only activities are only sent to services which support them ''' svcA, svcB = TestTools.create_mock_services() svcA.SupportedActivities = [ActivityType.CrossCountrySkiing] svcB.SupportedActivities = [ActivityType.Cycling] actA = Activity() actA.StartTime = datetime(1, 2, 3, 4, 5, 6, 7) actA.UploadedTo = [TestTools.create_mock_upload_record(svcA)] actA.Type = svcA.SupportedActivities[0] actB = Activity() actB.StartTime = datetime(5, 6, 7, 8, 9, 10, 11) actB.UploadedTo = [TestTools.create_mock_upload_record(svcB)] actB.Type = [x for x in svcB.SupportedActivities if x != actA.Type][0] actA.CalculateUID() actB.CalculateUID() allConns = [ actA.UploadedTo[0]["Connection"], actB.UploadedTo[0]["Connection"] ] activities = [] Sync._accumulateActivities(svcA, [actA], activities) Sync._accumulateActivities(svcB, [actB], activities) syncToA = Sync._determineRecipientServices(actA, allConns) syncToB = Sync._determineRecipientServices(actB, allConns) self.assertEqual(len(syncToA), 0) self.assertEqual(len(syncToB), 0) svcB.SupportedActivities = svcA.SupportedActivities syncToA = Sync._determineRecipientServices(actA, allConns) syncToB = Sync._determineRecipientServices(actB, allConns) self.assertEqual(len(syncToA), 1) self.assertEqual(len(syncToB), 0) svcB.SupportedActivities = svcA.SupportedActivities = [ ActivityType.CrossCountrySkiing, ActivityType.Cycling ] syncToA = Sync._determineRecipientServices(actA, allConns) syncToB = Sync._determineRecipientServices(actB, allConns) self.assertEqual(len(syncToA), 1) self.assertEqual(len(syncToB), 1)