Skip to content

Commit

Permalink
Fix bug where search has total_results: 0
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeCohen committed Jul 25, 2024
1 parent bf68790 commit 311af40
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
19 changes: 13 additions & 6 deletions app/controllers/observations/inat_imports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,19 +142,26 @@ def import_requested_observations(inat_import)
ids: inat_ids, id_above: last_import_id,
user_login: inat_import.inat_username
)
import_page(page)
break if page_empty?(page)

import_page(page)
parsed_page = JSON.parse(page)
last_import_id = parsed_page["results"].last["id"]
if parsed_page["total_results"] >
parsed_page["page"] * parsed_page["per_page"]
next
end
break if done_with_page?(parsed_page)

break
next
end
end

def page_empty?(page)
JSON.parse(page)["total_results"].zero?
end

def done_with_page?(parsed_page)
parsed_page["total_results"] <=
parsed_page["page"] * parsed_page["per_page"]
end

def inat_id_list(inat_import)
inat_import.inat_ids.delete(" ")
end
Expand Down
20 changes: 20 additions & 0 deletions test/controllers/observations/inat_imports_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,26 @@ def test_import_plant
assert_flash_text(:inat_taxon_not_importable.l(id: inat_import_ids))
end

def test_import_zero_results
user = rolf
filename = "zero_results"
mock_search = File.read("test/inat/#{filename}.txt")
inat_import_ids = "123"

stub_inat_api_request(inat_import_ids, mock_search)
simulate_authorization(user: user, inat_import_ids: inat_import_ids)
stub_token_request

params = { inat_ids: inat_import_ids, code: "MockCode" }
login(user.login)

assert_no_difference(
"Observation.count", "Should not import iNat Plant observations"
) do
post(:authenticate, params: params)
end
end

def test_import_multiple
# NOTE: using obss without photos to avoid stubbing photo import
# amanita_flavorubens, calostoma lutescens
Expand Down
3 changes: 2 additions & 1 deletion test/inat/README_INAT_EXAMPLES.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ All data as of the time of importing. (The corresponding iNat Observation may ha
| lycoperdon.txt | [24970904](https://www.inaturalist.org/observations/24970904) | 2 | public | cc-by-nc, projects, Activity, many fields including **DNA**, place: E. side of Metolius River, Sisters Ranger District, Deschutes National Forest, Jefferson County, Oregon, US |
| russulaceae.txt | [216675045](https://www.inaturalist.org/observations/216675045) | 2 | public | **all rights reserved**, many projects, Activity; place: Point Defiance Park, Tacoma, WA, US |
| somion_unicolor.json | | | | Formatted version of following; facilitates viewing iNat API response key/values test/inat/somion_unicolor.json |
| somion_unicolor.txt | [202555552](https://www.inaturalist.org/observations/202555552) | 5 | public | Research Grade, Notes, Activity, >1 ID, 1 field (Mushroom Observer URL), **mirrored from MO** |
| somion_unicolor.txt | [**202555552**](https://www.inaturalist.org/observations/202555552) | 5 | public | Research Grade, Notes, Activity, >1 ID, 1 field (Mushroom Observer URL), **mirrored from MO** |
| trametes.txt | [220370929](https://www.inaturalist.org/observations/220370929) | 2 | public | D. Miller observation with different collector; Notes; **Observation Fields: Collector**, place: 25th Ave NE, Seattle, WA, US, with huge error |
| tremella_mesenterica.txt | [213508767](https://www.inaturalist.org/observations/213508767) | 1 | public | place: Lewisville, TX 75057, USA |
| zero_results.txt | n.a. | | n.a. | response with total_results: 0, to expose and prevent reversion of bug |

## TODO

Expand Down
1 change: 1 addition & 0 deletions test/inat/zero_results.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"total_results":0,"page":1,"per_page":200,"results":[]}

0 comments on commit 311af40

Please sign in to comment.