def testParseCommandDefault(self): lds = QueryResult("ldslist") jsonObj = self.getJSONFromFile( "{}/bin/tests/json/_lds-api_v3_log-sources_cpcode-products.json". format(os.getcwd())) fetch = LdsFetch() jsonObj = fetch.adjustResponseJSON(jsonObj) result = lds.parseCommandDefault(jsonObj) self.assertEqual(len(result), 2) self.assertEqual(len(result[0]), 7) r = result[0] self.assertEqual(len(r), 7) dataset = [ "CPCODE", "Aggregation_Frequency", "Status", "Encoding", "Format", "Machine", "Directory" ] self.assertIn(r[0], dataset) self.assertIn(r[1], dataset) self.assertIn(r[2], dataset) self.assertIn(r[3], dataset) self.assertIn(r[4], dataset) self.assertIn(r[5], dataset) r = result[1] self.assertEqual(len(r), 7) dataset = ["200957", "Every 24 hours", "GZIP", "active"] self.assertIn(r[0], dataset) self.assertIn(r[1], dataset) self.assertIn(r[2], dataset) self.assertIn(r[3], dataset)
def testCPCodeNameParsing(self): fetch = LdsFetch() value = fetch.parseCPCODENameForCodeOnly("12322 - Somename") self.assertEqual(value, "12322") value = fetch.parseCPCODENameForCodeOnly("missing - Somename") self.assertIsNone(value)
def testJsonResponse(self, mockSessionObj): response = MockResponse() response.status_code = 200 fetch = LdsFetch() response.jsonObj = fetch.adjustResponseJSON( self.getJSONFromFile( "{}/bin/tests/json/_lds-api_v3_log-sources_cpcode-products.json" .format(os.getcwd()))) session = mockSessionObj() session.get.return_value = response edgeRc = "{}/bin/tests/other/.dummy_edgerc".format(os.getcwd()) args = [ "ldslist", "--section", "default", "--edgerc", edgeRc, "--show-json" ] saved_stdout = sys.stdout finaloutput = None try: out = StringIO() sys.stdout = out mainresult = main(args) outputStr = out.getvalue() output = list(outputStr.split("\n")) finaloutput = list(filter(lambda line: line != '', output)) outputJson = json.loads(outputStr) self.assertEquals("200957", outputJson[0]["logSource"]["cpCodeNumber"]) self.assertEquals("104523", outputJson[1]["logSource"]["cpCodeNumber"]) self.assertGreater( len(finaloutput), 0, "command args {} and its output should be greater than zero". format(args)) self.assertEqual( mainresult, 0, "command args {} should return successcode".format(args)) self.assertEqual(304, len(finaloutput)) finally: pass sys.stdout = saved_stdout
def ldslist(args): path = inspect.getframeinfo(inspect.currentframe()).function thread = Analytics().async_send_analytics(path=path, debug=args.debug) fetch = LdsFetch() queryresult = QueryResult("ldslist") checkFilterArgs(args, queryresult) (_, jsonObj) = fetch.fetchCPCodeProducts(edgerc=args.edgerc, section=args.section, account_key=args.account_key, debug=args.debug) thread.join() return handleresponse(args, jsonObj, queryresult, Debug=args.debug)
def testMainBootStrap(self, mockSessionObj): response = MockResponse() response.status_code = 200 fetch = LdsFetch() response.jsonObj = fetch.adjustResponseJSON( self.getJSONFromFile( "{}/bin/tests/json/_lds-api_v3_log-sources_cpcode-products.json" .format(os.getcwd()))) session = mockSessionObj() session.get.return_value = response edgeRc = "{}/bin/tests/other/.dummy_edgerc".format(os.getcwd()) args = [ "ldslist", "--section", "default", "--edgerc", edgeRc, "--debug", "--template", "all-default.json" ] self._testParseLDSCommandCombo(args, 3) templatename = "ldslist" file = "{}/bin/queries/{}/all-default.json".format( os.getcwd(), templatename) args = [ templatename, "--section", "default", "--edgerc", edgeRc, "--debug", "--file", file ] self._testParseLDSCommandCombo(args, 3) args = [ templatename, "--section", "default", "--edgerc", edgeRc, "--template", "all-default.json", "--debug" ] self._testParseLDSCommandCombo(args, 3) args = [ templatename, "--section", "default", "--edgerc", edgeRc, "--template", "all-default.json", "--debug" ] self._testParseLDSCommandCombo(args, 3)
def testFetchCPCodeProducts(self, mockSessionObj): response = MockResponse() response.status_code = 200 fetch = LdsFetch() response.jsonObj = fetch.adjustResponseJSON( self.getJSONFromFile( "{}/bin/tests/json/_lds-api_v3_log-sources_cpcode-products.json" .format(os.getcwd()))) session = mockSessionObj() session.get.return_value = response #updade tests fetch = LdsFetch() edgeRc = "{}/bin/tests/other/.dummy_edgerc".format(os.getcwd()) (code, json) = fetch.fetchCPCodeProducts(edgerc=edgeRc, section=None, account_key=None) self.assertEqual(response.status_code, code) self.runParseElement(json)
def testNewJsonPath(self): lds = QueryResult("ldslist") jsonObj = self.getJSONFromFile( "{}/bin/tests/json/_lds-api_v3_log-sources_cpcode-products.json". format(os.getcwd())) fetch = LdsFetch() jsonObj = fetch.adjustResponseJSON(jsonObj) result = lds.buildandParseExpression(jsonObj, "$[*].id") self.assertEqual(len(result), 2) self.assertEqual("163842", result[0]) self.assertEqual("143296", result[1]) result = lds.buildandParseExpression(jsonObj[0], "$.id") self.assertEqual(len(result), 1) self.assertEqual("163842", result[0]) result = lds.buildandParseExpression( jsonObj, "$[*][?(@.status=\"active\")].status") self.assertEqual(len(result), 1) self.assertEqual("active", result[0]) result = lds.buildandParseExpression( jsonObj[0], "$[?(@.status=\"active\")].status") self.assertEqual(len(result), 1) self.assertEqual("active", result[0]) result = lds.buildandParseExpression(jsonObj, "$[*].logSource.id") self.assertEqual(len(result), 2) self.assertEqual("200957-1", result[0]) self.assertEqual("104523-1", result[1]) result = lds.buildandParseExpression(jsonObj[0], "$.logSource.id") self.assertEqual(len(result), 1) self.assertEqual("200957-1", result[0]) result = lds.buildandParseExpression(jsonObj[1], "$.logSource.id") self.assertEqual(len(result), 1) self.assertEqual("104523-1", result[0]) error = None try: #enable - suppress expected error messages during tests saved_stdout = sys.stderr out = StringIO() sys.stderr = out result = lds.buildandParseExpression(jsonObj[1], ".logSource.id") error = True except ValueError as identifier: message = identifier.args[0] self.assertEqual( "JSON path: .logSource.id error: line 1:0 missing '$' at '.'", message) finally: #rollback - suppress expected error messages during tests sys.stderr = saved_stdout if (error == True): self.fail("buildandParseExpression() did not raise exception!")