https://mooseframework.inl.gov
GeochemicalDatabaseValidatorTest.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #include "gtest/gtest.h"
11 #include <fstream>
12 
14 
15 void
16 testExceptionMessage(const FileName filename, const std::string msg)
17 {
18  // Read the faulty JSON database
19  std::ifstream jsondata(filename);
20  nlohmann::json root;
21  jsondata >> root;
22 
24 
25  // Check that validating throws the expected error message
26  try
27  {
29  FAIL() << "Missing expected exception.";
30  }
31  catch (const std::exception & err)
32  {
33  std::size_t pos = std::string(err.what()).find(msg);
34  ASSERT_TRUE(pos != std::string::npos);
35  }
36 }
37 
38 TEST(GeochemicalDatabaseValidatorTest, validateTestDB)
39 {
40  // Read the JSON database
41  FileName filename = "database/moose_testdb.json";
42  std::ifstream jsondata(filename);
43  nlohmann::json root;
44  jsondata >> root;
45 
46  // This database should have valid entries for all species and sections,
47  // so shouldn't throw any exceptions
49  EXPECT_NO_THROW(database.validate());
50 }
51 
52 TEST(GeochemicalDatabaseValidatorTest, missingHeader)
53 {
54  const FileName filename = "database/faultydbs/missing_header.json";
55  const std::string msg =
56  "The MOOSE database " + filename + " does not have a required \"Header\" field";
57  testExceptionMessage(filename, msg);
58 }
59 
60 TEST(GeochemicalDatabaseValidatorTest, missingTemperature)
61 {
62  const FileName filename = "database/faultydbs/missing_temperature.json";
63  const std::string msg =
64  "The MOOSE database " + filename + " does not have a required \"Header:temperatures\" field";
65  testExceptionMessage(filename, msg);
66 }
67 
68 TEST(GeochemicalDatabaseValidatorTest, missingActivityModel)
69 {
70  const FileName filename = "database/faultydbs/missing_activity.json";
71  const std::string msg = "The MOOSE database " + filename +
72  " does not have a required \"Header:activity model\" field";
73  testExceptionMessage(filename, msg);
74 }
75 
76 TEST(GeochemicalDatabaseValidatorTest, missingLogKValue)
77 {
78  const FileName filename = "database/faultydbs/missing_value.json";
79  const std::string msg =
80  "The number of values in the logk field of secondary species CO2(aq) in " + filename +
81  " is not equal to the number of temperature values";
82  testExceptionMessage(filename, msg);
83 }
84 
85 TEST(GeochemicalDatabaseValidatorTest, nonRealValue)
86 {
87  const FileName filename = "database/faultydbs/nonreal_value.json";
88  const std::string msg = "radius value \"4.5x\" of the secondary species CO3-- in "
89  "database/faultydbs/nonreal_value.json cannot be converted to Real";
90  testExceptionMessage(filename, msg);
91 }
92 
93 TEST(GeochemicalDatabaseValidatorTest, nonReaArraylValue)
94 {
95  const FileName filename = "database/faultydbs/nonreal_arrayvalue.json";
96  const std::string msg = "Array value \"abcd\" in the logk field of mineral species Calcite in "
97  "database/faultydbs/nonreal_arrayvalue.json cannot be converted to Real";
98  testExceptionMessage(filename, msg);
99 }
100 
101 TEST(GeochemicalDatabaseValidatorTest, nonReaWeight)
102 {
103  const FileName filename = "database/faultydbs/nonreal_weight.json";
104  const std::string msg =
105  "Weight value \"1.xyz\" of constituent HCO3- of the secondary species CO3-- in "
106  "database/faultydbs/nonreal_weight.json cannot be converted to Real";
107  testExceptionMessage(filename, msg);
108 }
109 
110 TEST(GeochemicalDatabaseValidatorTest, nonReaNeutralSpeciesArray)
111 {
112  const FileName filename = "database/faultydbs/nonreal_neutralspecies.json";
113  const std::string msg = "Array value \".1967cg\" in the Header:neutral species:co2:a field of " +
114  filename + " cannot be converted to Real";
115  testExceptionMessage(filename, msg);
116 }
117 
118 TEST(GeochemicalDatabaseValidatorTest, missingNeutralValue)
119 {
120  const FileName filename = "database/faultydbs/missing_neutral_value.json";
121  const std::string msg = "The number of values in the Header:neutral species:h2o:a field of " +
122  filename + " is not equal to the number of temperature values";
123  testExceptionMessage(filename, msg);
124 }
125 
126 TEST(GeochemicalDatabaseValidatorTest, noCharge)
127 {
128  const FileName filename = "database/faultydbs/no_charge.json";
129  const std::string msg = "The secondary species CO3-- in " + filename + " does not have a charge";
130  testExceptionMessage(filename, msg);
131 }
132 
133 TEST(GeochemicalDatabaseValidatorTest, noStoichiometry)
134 {
135  const FileName filename = "database/faultydbs/no_stoi.json";
136  const std::string msg = "The secondary species CO3-- in " + filename + " does not have a species";
137  testExceptionMessage(filename, msg);
138 }
OStreamProxy err
TEST(GeochemicalDatabaseValidatorTest, validateTestDB)
void testExceptionMessage(const FileName filename, const std::string msg)
void validate(const FileName filename, const nlohmann::json &db)
Validate the thermodynamic database.
const GeochemicalDatabaseReader database("database/moose_testdb.json", true, true, false)
Real root(std::function< Real(Real)> const &f, Real x1, Real x2, Real tol=1.0e-12)
Finds the root of a function using Brent&#39;s method.
Definition: BrentsMethod.C:66
Class for validating MOOSE geochemical database.