Exemplo n.º 1
0
    def test_it_works_with_normal_events(self, requests_mock):
        mock = requests_mock.post(AMP_BATCH_URL)

        with AmpEventUploader("myapikey", dry_run=False) as uploader:
            event = AmpEvent(
                1,
                "myevent",
                EPOCH,
                event_properties={"eventprop": EPOCH + timedelta(days=1)},
                user_properties={"userprop": 15},
            )
            uploader.queue(event)

        assert mock.call_count == 1
        payload = mock.last_request.json()
        assert payload["api_key"] == "myapikey"
        assert len(payload["events"]) == 1
        assert payload["events"][0] == {
            "event_type": "myevent",
            "insert_id": "user_1_myevent",
            "time": 0,
            "user_id": "justfix:1",
            "user_properties": {
                "userprop": 15
            },
            "event_properties": {
                "eventprop": "1970-01-02T00:00:00+00:00"
            },
        }
Exemplo n.º 2
0
    def test_it_does_not_upload_in_dry_run(self, requests_mock):
        mock = requests_mock.post(AMP_BATCH_URL)

        with AmpEventUploader("myapikey", dry_run=True) as uploader:
            event = AmpEvent(1, "myevent")
            uploader.queue(event)

        assert mock.call_count == 0
Exemplo n.º 3
0
    def sync(self, kind: str):
        sync, _ = Sync.objects.get_or_create(kind=kind, defaults={"last_synced_at": EPOCH})
        update_time = now()

        synchronizer = SYNCHRONIZERS[kind]

        with AmpEventUploader(settings.AMPLITUDE_API_KEY, dry_run=self.dry_run) as uploader:
            for event in synchronizer.iter_events(sync.last_synced_at):
                uploader.queue(event)

        if not self.dry_run:
            sync.last_synced_at = update_time
            sync.save()
Exemplo n.º 4
0
    def test_it_retries_on_timeout(self, requests_mock, monkeypatch):
        sleep = MagicMock()
        monkeypatch.setattr(api, "sleep", sleep)
        mock = requests_mock.post(AMP_BATCH_URL, [{
            "status_code": 429
        }, {
            "status_code": 200
        }])

        with AmpEventUploader("myapikey", dry_run=False) as uploader:
            uploader.queue(AmpEvent(2, "myevent"))

        sleep.assert_called_once_with(api.AMP_RATE_LIMIT_WAIT_SECS)
        assert mock.call_count == 2
        assert mock.request_history[0].json() == mock.request_history[1].json()
Exemplo n.º 5
0
    def test_it_batches_events(self, requests_mock):
        mock = requests_mock.post(AMP_BATCH_URL)

        with AmpEventUploader("myapikey", dry_run=False) as uploader:
            for i in range(AmpEventUploader.BATCH_SIZE):
                event = AmpEvent(i, "myevent")
                uploader.queue(event)
            uploader.queue(event)
            uploader.queue(event)
            uploader.queue(event)

        assert mock.call_count == 2
        assert len(mock.request_history[0].json()
                   ["events"]) == AmpEventUploader.BATCH_SIZE
        assert len(mock.request_history[1].json()["events"]) == 3
Exemplo n.º 6
0
    def test_it_works_with_identify_events(self, requests_mock):
        mock = requests_mock.post(AMP_BATCH_URL)

        with AmpEventUploader("myapikey", dry_run=False) as uploader:
            event = AmpEvent(2, IDENTIFY_EVENT, user_properties={"hi": 1})
            uploader.queue(event)

        assert mock.call_count == 1
        payload = mock.last_request.json()
        assert payload["api_key"] == "myapikey"
        assert len(payload["events"]) == 1
        assert payload["events"][0] == {
            "event_type": "$identify",
            "user_id": "justfix:2",
            "user_properties": {
                "hi": 1
            },
        }
Exemplo n.º 7
0
    def test_it_works_with_anonymous_events(self, requests_mock):
        mock = requests_mock.post(AMP_BATCH_URL)

        with AmpEventUploader("myapikey", dry_run=False) as uploader:
            event = AmpEvent(
                None,
                "myevent",
                EPOCH,
                device_id="boopy",
            )
            uploader.queue(event)

        assert mock.call_count == 1
        payload = mock.last_request.json()
        assert len(payload["events"]) == 1
        assert payload["events"][0] == {
            "event_type": "myevent",
            "insert_id": "user_boopy_myevent",
            "time": 0,
            "device_id": "boopy",
            "user_properties": {},
            "event_properties": {},
        }