Uploaded image for project: 'Mesos'
  1. Mesos
  2. MESOS-2510

Add a function which test if a JSON object is contained in another JSON object

    Details

    • Type: Wish
    • Status: Resolved
    • Priority: Major
    • Resolution: Implemented
    • Affects Version/s: None
    • Fix Version/s: 0.23.0
    • Component/s: stout
    • Labels:
      None
    • Target Version/s:
    • Sprint:
      Mesosphere Q1 Sprint 6 - 4/3, Mesosphere Q1 Sprint 7 - 4/17, Mesosphere Q2 Sprint 8 - 5/1, Mesosphere Q1 Sprint 9 - 5/15

      Description

      It would be nice to check wether one json blob is contained by other blob. i.e. given the json blob a and the blob b, a contains b if every key x in b is also in a, and b[x] == a[x] if b[x] is not a json object itself or, if it is a json object, a[x] contains b[x].

      Rationale

      One of the most useful patterns while testing functions which return json, is to write the expected result and then compare if the expected blob is equal to the returned one:

      JSON::Value expected = JSON::parse(
          "{"
          "  \"key\" : true"   
          "}").get();
      
      JSON::Value actual = foo();
      CHECK_EQ(expected, actual);
      

      As can be seen in the example above, it is easy to read what the expected value is, and checking for failures if fairly easy.

      It is no easy, however, to compare returned blobs which contain at least one random values (for example time stamps), or a value which is uninteresting for the test. In such cases it is necessary to extract each value separately and compare them:

      // Returned json:
      // {
      //   "uptime" : 45234.123,
      //   "key" : true
      // }
      JSON::Value actual = bar();
      
      // I'm only interested on the "key" entry.
      EXPECT_SOME_EQ(true, actual.find<JSON::String>("key"));
      

      As seen above, is one is only interested in a subset of the keys/values pairs returned by bar the readability of the code decreases severely. It is worse if it weren't for the comments.

      The aim is to achieve the same level of readability on the first example while covering the case of the second:

      JSON::Value expected = JSON::parse(
          "{"
          "  \"key\" : true"   
          "}").get();
      
      // Returned json:
      // {
      //   "uptime" : 45234.123,
      //   "key" : true
      // }
      JSON::Value actual = bar();
      
      // I'm only interested on the "key" entry and ignore the rest.
      EXPECT_TRUE(contains(actual, expected));
      

        Attachments

          Activity

            People

            • Assignee:
              arojas Alexander Rojas
              Reporter:
              arojas Alexander Rojas
              Shepherd:
              Till Toenshoff
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: