Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Espoo Importer #718

Merged
merged 2 commits into from
Oct 18, 2023
Merged

Espoo Importer #718

merged 2 commits into from
Oct 18, 2023

Conversation

voneiden
Copy link
Contributor

@voneiden voneiden commented Sep 25, 2023

Espoo importer is a rather generic LE importer. Handles various relations between objects etc. although in hindsight nobody has specified if this is necessary. Actually, at this point I still don't know what are the parameters that should be used for importing, so that's configurable via an environment variable..

https://helsinkisolutionoffice.atlassian.net/browse/LINK-1375

@codecov-commenter
Copy link

codecov-commenter commented Sep 25, 2023

Codecov Report

Merging #718 (30795b1) into main (2a62f70) will increase coverage by 1.77%.
The diff coverage is 97.68%.

@@            Coverage Diff             @@
##             main     #718      +/-   ##
==========================================
+ Coverage   80.26%   82.03%   +1.77%     
==========================================
  Files         274      275       +1     
  Lines       21056    21090      +34     
==========================================
+ Hits        16901    17302     +401     
+ Misses       4155     3788     -367     
Files Coverage Δ
events/tests/conftest.py 98.01% <ø> (-0.03%) ⬇️
linkedevents/settings.py 71.57% <100.00%> (+0.92%) ⬆️
events/tests/importers/test_espoo.py 99.45% <99.45%> (ø)
events/importer/espoo.py 96.22% <95.93%> (+96.22%) ⬆️

... and 1 file with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@voneiden voneiden force-pushed the link-1375-espoo branch 2 times, most recently from 925881e to b4279bf Compare September 26, 2023 04:38
@voneiden voneiden marked this pull request as ready for review September 26, 2023 04:47
@voneiden voneiden requested a review from a team September 26, 2023 04:48
events/importer/espoo.py Outdated Show resolved Hide resolved
events/importer/espoo.py Outdated Show resolved Hide resolved
events/importer/espoo.py Outdated Show resolved Hide resolved
events/importer/espoo.py Outdated Show resolved Hide resolved
events/importer/espoo.py Outdated Show resolved Hide resolved
events/importer/espoo.py Outdated Show resolved Hide resolved
events/importer/espoo.py Show resolved Hide resolved
events/importer/espoo.py Outdated Show resolved Hide resolved
events/importer/espoo.py Outdated Show resolved Hide resolved
events/importer/espoo.py Outdated Show resolved Hide resolved
events/importer/espoo.py Outdated Show resolved Hide resolved
events/importer/espoo.py Outdated Show resolved Hide resolved
events/tests/importers/test_espoo.py Outdated Show resolved Hide resolved
events/tests/importers/test_espoo.py Outdated Show resolved Hide resolved
events/importer/espoo.py Outdated Show resolved Hide resolved
events/importer/espoo.py Outdated Show resolved Hide resolved
events/tests/importers/test_espoo.py Outdated Show resolved Hide resolved
events/importer/espoo.py Outdated Show resolved Hide resolved
@voneiden
Copy link
Contributor Author

voneiden commented Oct 2, 2023

OK, I believe I've addressed all the comments. Requesting re-review and the new changes are in separate commits for now.

@voneiden voneiden requested review from tuomas777 and danipran October 2, 2023 08:53
Copy link
Contributor

@danipran danipran left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of the earlier comments were not addressed.

events/importer/espoo.py Outdated Show resolved Hide resolved
events/importer/espoo.py Outdated Show resolved Hide resolved
events/importer/espoo.py Outdated Show resolved Hide resolved
events/tests/importers/test_espoo.py Outdated Show resolved Hide resolved
events/importer/espoo.py Outdated Show resolved Hide resolved
events/importer/espoo.py Outdated Show resolved Hide resolved
events/importer/espoo.py Outdated Show resolved Hide resolved
places = Place.objects.filter(**filter_params).order_by(
"-data_source", "-n_events"
audiences_data = _get_origin_objs(
f"{settings.ESPOO_API_URL}v1/keyword/", missing_audience_ids
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are now three places where a URL is built from settings.ESPOO_API_URL, and this particular one is actually implemented a bit differently than the other two (f-string vs urljoin). From DRY viewpoint it would be better to have something like get_espoo_api_url(resource). Also, it would safer for the future, if the setting ESPOO_API_URL would work with and without a trailing /.

instances = []
instance_data_map = {}
for obj_data in origin_objs:
obj_data = dict(obj_data)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is here just to get a copy, I'm guessing it would be more robust to use deepcopy?

@@ -60,6 +60,15 @@ def get_git_revision_hash() -> str:
),
ENABLE_EXTERNAL_USER_EVENTS=(bool, True),
ENABLE_REGISTRATION_ENDPOINTS=(bool, False),
ESPOO_API_URL=(str, "https://api.espoo.fi/events/"),
ESPOO_API_QUERY_PARAMS=(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As these are only used with event resource queries, I would rather name this ESPOO_API_EVENT_QUERY_PARAMS (I actually first thought these were used in every query when I saw the setting 🙂)

Could not identify where it was needed anymore.
@voneiden voneiden force-pushed the link-1375-espoo branch 2 times, most recently from d0ff51b to 30795b1 Compare October 16, 2023 12:01
Copy link
Contributor

@danipran danipran left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some suggestions for the tests, otherwise LGTM 👍

Comment on lines 255 to 256
assert [d["id"] for d in origin_data[:1]] == [i.id for i in common_objs]
assert origin_data[1:] == origin_objs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are we asserting here? The first line especially is very cryptic 😅

Comment on lines +64 to +75
@pytest.fixture
def sleep(monkeypatch):
class Sleep:
call_count = 0

def sleep(self, t):
self.call_count += 1

sleep_instance = Sleep()

monkeypatch.setattr("time.sleep", sleep_instance.sleep)
return sleep_instance
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could use Mock instead:

Suggested change
@pytest.fixture
def sleep(monkeypatch):
class Sleep:
call_count = 0
def sleep(self, t):
self.call_count += 1
sleep_instance = Sleep()
monkeypatch.setattr("time.sleep", sleep_instance.sleep)
return sleep_instance
@pytest.fixture
def sleep(monkeypatch):
mock_sleep = Mock() # or a MagicMock
monkeypatch.setattr("time.sleep", mock_sleep)
return mock_sleep

Or if you don't like mixing & matching:

Suggested change
@pytest.fixture
def sleep(monkeypatch):
class Sleep:
call_count = 0
def sleep(self, t):
self.call_count += 1
sleep_instance = Sleep()
monkeypatch.setattr("time.sleep", sleep_instance.sleep)
return sleep_instance
@pytest.fixture
def sleep():
with patch("time.sleep") as mock: # not completely sure on the target; maybe "events.importer.sleep" if this one doesn't work?
yield mock



@pytest.fixture
def sleep(monkeypatch):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer the name mock_sleep, but up to you.

Suggested change
def sleep(monkeypatch):
def mock_sleep(monkeypatch):

A generic LE importer that handles relations and imports
Organizations, Places, Keywords and Events under a DataSource.
Unreferenced relations are removed using ModelSyncher.

Co-authored-by: Tuomas Haapala <[email protected]>
Co-authored-by: Daniel Prange <[email protected]>
@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

96.0% 96.0% Coverage
0.0% 0.0% Duplication

@voneiden voneiden merged commit 495b2ff into main Oct 18, 2023
3 checks passed
@voneiden voneiden deleted the link-1375-espoo branch October 18, 2023 07:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants