/eol-globi-rest/src/test/java/org/eol/globi/server/CypherReturnClauseBuilderTest.java

https://github.com/jhpoelen/eol-globi-data · Java · 490 lines · 463 code · 27 blank · 0 comment · 0 complexity · 61b199b2d0ec208cc3243e3376b2b03f MD5 · raw file

  1. package org.eol.globi.server;
  2. import org.junit.Ignore;
  3. import org.junit.Test;
  4. import java.util.TreeMap;
  5. import static org.hamcrest.CoreMatchers.is;
  6. import static org.hamcrest.MatcherAssert.assertThat;
  7. public class CypherReturnClauseBuilderTest {
  8. @Test
  9. public void multiTaxonAllUnknownReturnFields() {
  10. StringBuilder query = new StringBuilder();
  11. CypherReturnClauseBuilder.appendReturnClauseMap(
  12. query,
  13. QueryType.MULTI_TAXON_ALL,
  14. unknownFields());
  15. assertThat(query.toString(), is(" RETURN " +
  16. "sourceTaxon.externalId as source_taxon_external_id," +
  17. "sourceTaxon.name as source_taxon_name," +
  18. "sourceTaxon.path as source_taxon_path," +
  19. "sourceSpecimen.occurrenceId as source_specimen_occurrence_id," +
  20. "sourceSpecimen.institutionCode as source_specimen_institution_code," +
  21. "sourceSpecimen.collectionCode as source_specimen_collection_code," +
  22. "sourceSpecimen.catalogNumber as source_specimen_catalog_number,"+
  23. "sourceSpecimen.lifeStageLabel as source_specimen_life_stage," +
  24. "sourceSpecimen.basisOfRecordLabel as source_specimen_basis_of_record," +
  25. "interaction.label as interaction_type," +
  26. "targetTaxon.externalId as target_taxon_external_id," +
  27. "targetTaxon.name as target_taxon_name," +
  28. "targetTaxon.path as target_taxon_path," +
  29. "targetSpecimen.occurrenceId as target_specimen_occurrence_id," +
  30. "targetSpecimen.institutionCode as target_specimen_institution_code," +
  31. "targetSpecimen.collectionCode as target_specimen_collection_code," +
  32. "targetSpecimen.catalogNumber as target_specimen_catalog_number,"+
  33. "targetSpecimen.lifeStageLabel as target_specimen_life_stage," +
  34. "targetSpecimen.basisOfRecordLabel as target_specimen_basis_of_record," +
  35. "loc.latitude as latitude," +
  36. "loc.longitude as longitude," +
  37. "study.title as study_title"));
  38. }
  39. @Test
  40. public void multiTaxonAllDatasetFields() {
  41. StringBuilder query = new StringBuilder();
  42. CypherReturnClauseBuilder.appendReturnClauseMap(
  43. query,
  44. QueryType.MULTI_TAXON_ALL,
  45. new TreeMap<String, String[]>() {
  46. {
  47. put("field", new String[]{
  48. "study_source_citation",
  49. "study_source_archive_uri",
  50. "study_source_last_seen_at"
  51. });
  52. }
  53. });
  54. assertThat(query.toString(), is(" RETURN " +
  55. "dataset.citation as study_source_citation," +
  56. "dataset.archiveURI as study_source_archive_uri," +
  57. "dataset.lastSeenAt as study_source_last_seen_at"));
  58. }
  59. @Test
  60. public void datasetNamespace() {
  61. StringBuilder query = new StringBuilder();
  62. CypherReturnClauseBuilder.appendReturnClauseMap(
  63. query,
  64. QueryType.MULTI_TAXON_ALL,
  65. new TreeMap<String, String[]>() {
  66. {
  67. put("field", new String[]{
  68. "study_source_citation",
  69. "study_source_archive_uri",
  70. "study_source_last_seen_at",
  71. "study_source_id"
  72. });
  73. }
  74. });
  75. assertThat(query.toString(), is(" RETURN " +
  76. "dataset.citation as study_source_citation," +
  77. "dataset.archiveURI as study_source_archive_uri," +
  78. "dataset.lastSeenAt as study_source_last_seen_at," +
  79. "dataset.namespace as study_source_id"));
  80. }
  81. @Test
  82. public void multiTaxonAllUnknownReturnFieldsWithTaxonPrefix() {
  83. StringBuilder query = new StringBuilder();
  84. CypherReturnClauseBuilder.appendReturnClauseMap(
  85. query,
  86. QueryType.MULTI_TAXON_ALL,
  87. taxonomyOnly());
  88. assertThat(query.toString(), is(" WITH " +
  89. "sourceTaxon, " +
  90. "sourceSpecimen, " +
  91. "interaction, " +
  92. "targetTaxon, " +
  93. "targetSpecimen, " +
  94. "loc, " +
  95. "study " +
  96. "MATCH sourceTaxon-[:SAME_AS*0..1]->sourceTaxonSameAs, targetTaxon-[:SAME_AS*0..1]->targetTaxonSameAs " +
  97. "WHERE " +
  98. "sourceTaxonSameAs.externalId =~ {source_taxon_prefix} AND " +
  99. "targetTaxonSameAs.externalId =~ {target_taxon_prefix} " +
  100. "WITH sourceTaxonSameAs as sourceTaxon, " +
  101. "sourceSpecimen, " +
  102. "interaction, " +
  103. "targetTaxonSameAs as targetTaxon, " +
  104. "targetSpecimen, " +
  105. "loc, " +
  106. "study " +
  107. "RETURN " +
  108. "sourceTaxon.externalId as source_taxon_external_id," +
  109. "sourceTaxon.name as source_taxon_name," +
  110. "sourceTaxon.path as source_taxon_path," +
  111. "sourceSpecimen.occurrenceId as source_specimen_occurrence_id," +
  112. "sourceSpecimen.institutionCode as source_specimen_institution_code," +
  113. "sourceSpecimen.collectionCode as source_specimen_collection_code," +
  114. "sourceSpecimen.catalogNumber as source_specimen_catalog_number,"+
  115. "sourceSpecimen.lifeStageLabel as source_specimen_life_stage," +
  116. "sourceSpecimen.basisOfRecordLabel as source_specimen_basis_of_record," +
  117. "interaction.label as interaction_type," +
  118. "targetTaxon.externalId as target_taxon_external_id," +
  119. "targetTaxon.name as target_taxon_name," +
  120. "targetTaxon.path as target_taxon_path," +
  121. "targetSpecimen.occurrenceId as target_specimen_occurrence_id," +
  122. "targetSpecimen.institutionCode as target_specimen_institution_code," +
  123. "targetSpecimen.collectionCode as target_specimen_collection_code," +
  124. "targetSpecimen.catalogNumber as target_specimen_catalog_number,"+
  125. "targetSpecimen.lifeStageLabel as target_specimen_life_stage," +
  126. "targetSpecimen.basisOfRecordLabel as target_specimen_basis_of_record," +
  127. "loc.latitude as latitude," +
  128. "loc.longitude as longitude," +
  129. "study.title as study_title"));
  130. }
  131. @Test
  132. public void multiTaxonAllAllFieldsWithTaxonPrefix() {
  133. StringBuilder query = new StringBuilder();
  134. CypherReturnClauseBuilder.appendReturnClauseMap(
  135. query,
  136. QueryType.MULTI_TAXON_ALL,
  137. knownFieldsWithTaxonomy());
  138. assertThat(query.toString(), is(" WITH " +
  139. "sourceTaxon, sourceSpecimen, interaction, targetTaxon, targetSpecimen, loc, study " +
  140. "MATCH sourceTaxon-[:SAME_AS*0..1]->sourceTaxonSameAs, targetTaxon-[:SAME_AS*0..1]->targetTaxonSameAs " +
  141. "WHERE " +
  142. "sourceTaxonSameAs.externalId =~ {source_taxon_prefix} AND " +
  143. "targetTaxonSameAs.externalId =~ {target_taxon_prefix} " +
  144. "WITH sourceTaxonSameAs as sourceTaxon, sourceSpecimen, interaction, targetTaxonSameAs as targetTaxon, targetSpecimen, loc, study " +
  145. "RETURN " +
  146. "sourceTaxon.name as source_taxon_name," +
  147. "targetTaxon.name as target_taxon_name"));
  148. }
  149. @Test
  150. public void multiTaxonAllKnownReturnFields() {
  151. StringBuilder query = new StringBuilder();
  152. CypherReturnClauseBuilder.appendReturnClauseMap(
  153. query,
  154. QueryType.MULTI_TAXON_ALL,
  155. knownFields());
  156. assertThat(query.toString(), is(" RETURN " +
  157. "sourceTaxon.name as source_taxon_name," +
  158. "targetTaxon.name as target_taxon_name"));
  159. }
  160. @Ignore("see https://github.com/globalbioticinteractions/globalbioticinteractions/issues/330")
  161. @Test
  162. public void multiTaxonAllWithNumberOfStudies() {
  163. StringBuilder query = new StringBuilder();
  164. CypherReturnClauseBuilder.appendReturnClauseMap(
  165. query,
  166. QueryType.MULTI_TAXON_DISTINCT,
  167. new TreeMap<String, String[]>() {
  168. {
  169. put("field", new String[]{"source_taxon_name", "target_taxon_name", "number_of_studies"});
  170. put("accordingTo", new String[]{"someSource"});
  171. }
  172. });
  173. assertThat(query.toString(), is(" WITH distinct targetTaxon, interaction.label as iType, sourceTaxon, count(distinct(id(study))) as studyCount " +
  174. "RETURN sourceTaxon.name as source_taxon_name,targetTaxon.name as target_taxon_name,studyCount as number_of_studies " +
  175. "ORDER BY number_of_studies DESC"));
  176. }
  177. @Ignore("see https://github.com/globalbioticinteractions/globalbioticinteractions/issues/330")
  178. @Test
  179. public void multiTaxonAllWithNumberOfStudiesIncludeObservations() {
  180. StringBuilder query = new StringBuilder();
  181. CypherReturnClauseBuilder.appendReturnClauseMap(
  182. query,
  183. QueryType.MULTI_TAXON_ALL,
  184. new TreeMap<String, String[]>() {
  185. {
  186. put("field", new String[]{"source_taxon_name", "target_taxon_name", "number_of_studies"});
  187. put("accordingTo", new String[]{"someSource"});
  188. }
  189. });
  190. assertThat(query.toString(), is(" RETURN " +
  191. "sourceTaxon.name as source_taxon_name," +
  192. "targetTaxon.name as target_taxon_name," +
  193. "1 as number_of_studies " +
  194. "ORDER BY number_of_studies DESC"));
  195. }
  196. @Ignore("see https://github.com/globalbioticinteractions/globalbioticinteractions/issues/330")
  197. @Test
  198. public void multiTaxonAllWithNumberOfStudiesWithIdPrefix() {
  199. StringBuilder query = new StringBuilder();
  200. CypherReturnClauseBuilder.appendReturnClauseMap(
  201. query,
  202. QueryType.MULTI_TAXON_DISTINCT,
  203. new TreeMap<String, String[]>() {
  204. {
  205. put("field", new String[]{"source_taxon_name", "target_taxon_name", "number_of_studies"});
  206. put("accordingTo", new String[]{"someSource"});
  207. put("taxonIdPrefix", new String[]{"somePrefix"});
  208. }
  209. });
  210. assertThat(query.toString(), is(" WITH distinct targetTaxon, interaction.label as iType, sourceTaxon, count(distinct(id(study))) as studyCount " +
  211. "WITH sourceTaxon, iType, targetTaxon, studyCount " +
  212. "MATCH sourceTaxon-[:SAME_AS*0..1]->sourceTaxonSameAs, targetTaxon-[:SAME_AS*0..1]->targetTaxonSameAs " +
  213. "WHERE sourceTaxonSameAs.externalId =~ {source_taxon_prefix} AND targetTaxonSameAs.externalId =~ {target_taxon_prefix} " +
  214. "WITH sourceTaxonSameAs as sourceTaxon, iType, targetTaxonSameAs as targetTaxon, studyCount " +
  215. "RETURN sourceTaxon.name as source_taxon_name,targetTaxon.name as target_taxon_name,studyCount as number_of_studies " +
  216. "ORDER BY number_of_studies DESC"));
  217. }
  218. private TreeMap<String, String[]> knownFieldsWithTaxonomy() {
  219. return new TreeMap<String, String[]>(knownFields()) {
  220. {
  221. put("taxonIdPrefix", new String[]{"somePrefix"});
  222. }
  223. };
  224. }
  225. private TreeMap<String, String[]> taxonomyOnly() {
  226. return new TreeMap<String, String[]>() {
  227. {
  228. put("taxonIdPrefix", new String[]{"somePrefix"});
  229. }
  230. };
  231. }
  232. @Test
  233. public void multiTaxonDistinctKnownReturnFieldsWithTaxonomy() {
  234. StringBuilder query = new StringBuilder();
  235. CypherReturnClauseBuilder.appendReturnClauseMap(
  236. query,
  237. QueryType.MULTI_TAXON_DISTINCT,
  238. knownFieldsWithTaxonomy());
  239. assertThat(query.toString(), is(
  240. " WITH distinct targetTaxon, " +
  241. "interaction.label as iType, " +
  242. "sourceTaxon " +
  243. "WITH " +
  244. "sourceTaxon, iType, targetTaxon " +
  245. "MATCH sourceTaxon-[:SAME_AS*0..1]->sourceTaxonSameAs, targetTaxon-[:SAME_AS*0..1]->targetTaxonSameAs " +
  246. "WHERE " +
  247. "sourceTaxonSameAs.externalId =~ {source_taxon_prefix} AND " +
  248. "targetTaxonSameAs.externalId =~ {target_taxon_prefix} " +
  249. "WITH sourceTaxonSameAs as sourceTaxon, iType, targetTaxonSameAs as targetTaxon " +
  250. "RETURN sourceTaxon.name as source_taxon_name," +
  251. "targetTaxon.name as target_taxon_name"));
  252. }
  253. @Test
  254. public void multiTaxonDistinctKnownReturnFields() {
  255. StringBuilder query = new StringBuilder();
  256. CypherReturnClauseBuilder.appendReturnClauseMap(
  257. query,
  258. QueryType.MULTI_TAXON_DISTINCT,
  259. knownFields());
  260. assertThat(query.toString(), is(" WITH distinct targetTaxon, " +
  261. "interaction.label as iType, " +
  262. "sourceTaxon " +
  263. "RETURN sourceTaxon.name as source_taxon_name," +
  264. "targetTaxon.name as target_taxon_name"));
  265. }
  266. @Test
  267. public void multiTaxonDistinctByNameOnly() {
  268. StringBuilder query = new StringBuilder();
  269. CypherReturnClauseBuilder.appendReturnClauseMap(
  270. query,
  271. QueryType.MULTI_TAXON_DISTINCT_BY_NAME_ONLY,
  272. knownFields());
  273. assertThat(query.toString(), is(" RETURN " +
  274. "sourceTaxon.name as source_taxon_name," +
  275. "targetTaxon.name as target_taxon_name"));
  276. }
  277. @Test
  278. public void multiTaxonDistinctByNameOnlyDuplicates() {
  279. StringBuilder query = new StringBuilder();
  280. TreeMap<String, String[]> parameterMap = new TreeMap<String, String[]>() {
  281. {
  282. put("field", new String[]{
  283. "source_taxon_name",
  284. "target_taxon_name",
  285. "source_taxon_name"});
  286. }
  287. };
  288. CypherReturnClauseBuilder.appendReturnClauseMap(
  289. query,
  290. QueryType.MULTI_TAXON_DISTINCT_BY_NAME_ONLY,
  291. parameterMap);
  292. assertThat(query.toString(), is(" RETURN " +
  293. "sourceTaxon.name as source_taxon_name," +
  294. "targetTaxon.name as target_taxon_name"));
  295. }
  296. @Test
  297. public void multiTaxonSexLabelsIds() {
  298. StringBuilder query = new StringBuilder();
  299. TreeMap<String, String[]> parameterMap = new TreeMap<String, String[]>() {
  300. {
  301. put("field", new String[]{
  302. "source_taxon_name",
  303. "source_specimen_sex",
  304. "source_specimen_sex_id",
  305. "target_specimen_sex",
  306. "target_specimen_sex_id",
  307. "target_taxon_name"});
  308. }
  309. };
  310. CypherReturnClauseBuilder.appendReturnClauseMap(
  311. query,
  312. QueryType.MULTI_TAXON_ALL,
  313. parameterMap);
  314. assertThat(query.toString(), is(" RETURN " +
  315. "sourceTaxon.name as source_taxon_name," +
  316. "sourceSpecimen.sexLabel as source_specimen_sex," +
  317. "null as source_specimen_sex_id," +
  318. "targetSpecimen.sexLabel as target_specimen_sex," +
  319. "null as target_specimen_sex_id," +
  320. "targetTaxon.name as target_taxon_name"));
  321. }
  322. @Test
  323. public void multiTaxonDistinctUnknownReturnFields() {
  324. StringBuilder query = new StringBuilder();
  325. CypherReturnClauseBuilder.appendReturnClauseMap(
  326. query,
  327. QueryType.MULTI_TAXON_DISTINCT,
  328. unknownFields());
  329. assertThat(query.toString(), is(" WITH distinct targetTaxon, " +
  330. "interaction.label as iType, " +
  331. "sourceTaxon " +
  332. "RETURN sourceTaxon.externalId as source_taxon_external_id," +
  333. "sourceTaxon.name as source_taxon_name," +
  334. "sourceTaxon.path as source_taxon_path," +
  335. "NULL as source_specimen_occurrence_id," +
  336. "NULL as source_specimen_institution_code," +
  337. "NULL as source_specimen_collection_code," +
  338. "NULL as source_specimen_catalog_number,"+
  339. "NULL as source_specimen_life_stage," +
  340. "NULL as source_specimen_basis_of_record," +
  341. "iType as interaction_type," +
  342. "targetTaxon.externalId as target_taxon_external_id," +
  343. "targetTaxon.name as target_taxon_name," +
  344. "targetTaxon.path as target_taxon_path," +
  345. "NULL as target_specimen_occurrence_id," +
  346. "NULL as target_specimen_institution_code," +
  347. "NULL as target_specimen_collection_code," +
  348. "NULL as target_specimen_catalog_number,"+
  349. "NULL as target_specimen_life_stage," +
  350. "NULL as target_specimen_basis_of_record," +
  351. "NULL as latitude," +
  352. "NULL as longitude," +
  353. "NULL as study_title"));
  354. }
  355. private TreeMap<String, String[]> knownFields() {
  356. return new TreeMap<String, String[]>() {
  357. {
  358. put("field", new String[]{"source_taxon_name", "target_taxon_name"});
  359. }
  360. };
  361. }
  362. @Test
  363. public void manyFields() {
  364. StringBuilder query = new StringBuilder();
  365. CypherReturnClauseBuilder.appendReturnClauseMap(
  366. query,
  367. QueryType.MULTI_TAXON_ALL,
  368. new TreeMap<String, String[]>() {
  369. {
  370. put("field", new String[]{
  371. "source_taxon_name",
  372. "source_taxon_path",
  373. "source_taxon_path_ids",
  374. "source_specimen_occurrence_id",
  375. "source_specimen_institution_code",
  376. "source_specimen_collection_code",
  377. "source_specimen_catalog_number",
  378. "source_specimen_life_stage_id",
  379. "source_specimen_life_stage",
  380. "source_specimen_physiological_state_id",
  381. "source_specimen_physiological_state",
  382. "source_specimen_body_part_id",
  383. "source_specimen_body_part",
  384. "source_specimen_sex_id",
  385. "source_specimen_sex",
  386. "source_specimen_basis_of_record",
  387. "interaction_type",
  388. "target_taxon_name",
  389. "target_taxon_path",
  390. "target_taxon_path_ids",
  391. "target_specimen_occurrence_id",
  392. "target_specimen_institution_code",
  393. "target_specimen_collection_code",
  394. "target_specimen_catalog_number",
  395. "target_specimen_life_stage_id",
  396. "target_specimen_life_stage",
  397. "target_specimen_physiological_state_id",
  398. "target_specimen_physiological_state",
  399. "target_specimen_body_part_id",
  400. "target_specimen_body_part",
  401. "target_specimen_sex_id",
  402. "target_specimen_sex",
  403. "target_specimen_basis_of_record",
  404. "latitude",
  405. "longitude",
  406. "event_date",
  407. "study_citation",
  408. "study_url",
  409. "study_source_citation",
  410. "study_source_archive_uri"
  411. });
  412. }
  413. });
  414. assertThat(query.toString(), is(" RETURN " +
  415. "sourceTaxon.name as source_taxon_name," +
  416. "sourceTaxon.path as source_taxon_path," +
  417. "sourceTaxon.pathIds as source_taxon_path_ids," +
  418. "sourceSpecimen.occurrenceId as source_specimen_occurrence_id," +
  419. "sourceSpecimen.institutionCode as source_specimen_institution_code," +
  420. "sourceSpecimen.collectionCode as source_specimen_collection_code," +
  421. "sourceSpecimen.catalogNumber as source_specimen_catalog_number," +
  422. "sourceSpecimen.lifeStageId as source_specimen_life_stage_id," +
  423. "sourceSpecimen.lifeStageLabel as source_specimen_life_stage," +
  424. "sourceSpecimen.physiologicalStateId as source_specimen_physiological_state_id," +
  425. "sourceSpecimen.physiologicalStateLabel as source_specimen_physiological_state," +
  426. "sourceSpecimen.bodyPartId as source_specimen_body_part_id," +
  427. "sourceSpecimen.bodyPartLabel as source_specimen_body_part," +
  428. "null as source_specimen_sex_id," +
  429. "sourceSpecimen.sexLabel as source_specimen_sex," +
  430. "sourceSpecimen.basisOfRecordLabel as source_specimen_basis_of_record," +
  431. "interaction.label as interaction_type," +
  432. "targetTaxon.name as target_taxon_name," +
  433. "targetTaxon.path as target_taxon_path," +
  434. "targetTaxon.pathIds as target_taxon_path_ids," +
  435. "targetSpecimen.occurrenceId as target_specimen_occurrence_id," +
  436. "targetSpecimen.institutionCode as target_specimen_institution_code," +
  437. "targetSpecimen.collectionCode as target_specimen_collection_code," +
  438. "targetSpecimen.catalogNumber as target_specimen_catalog_number," +
  439. "targetSpecimen.lifeStageId as target_specimen_life_stage_id," +
  440. "targetSpecimen.lifeStageLabel as target_specimen_life_stage," +
  441. "targetSpecimen.physiologicalStateId as target_specimen_physiological_state_id," +
  442. "targetSpecimen.physiologicalStateLabel as target_specimen_physiological_state," +
  443. "targetSpecimen.bodyPartId as target_specimen_body_part_id," +
  444. "targetSpecimen.bodyPartLabel as target_specimen_body_part," +
  445. "null as target_specimen_sex_id," +
  446. "targetSpecimen.sexLabel as target_specimen_sex," +
  447. "targetSpecimen.basisOfRecordLabel as target_specimen_basis_of_record," +
  448. "loc.latitude as latitude," +
  449. "loc.longitude as longitude," +
  450. "collected_rel.eventDate as event_date," +
  451. "study.citation as study_citation," +
  452. "study.externalId as study_url," +
  453. "dataset.citation as study_source_citation," +
  454. "dataset.archiveURI as study_source_archive_uri"));
  455. }
  456. private TreeMap<String, String[]> unknownFields() {
  457. return new TreeMap<String, String[]>() {
  458. {
  459. put("field", new String[]{"foo", "bar"});
  460. }
  461. };
  462. }
  463. }