github.com/jbking/gohan@v0.0.0-20151217002006-b41ccf1c2a96/extension/framework/runner/runner_test.go (about)

     1  // Copyright (C) 2015 NTT Innovation Institute, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
    12  // implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  package runner_test
    17  
    18  import (
    19  	"github.com/cloudwan/gohan/extension/framework/runner"
    20  
    21  	. "github.com/onsi/ginkgo"
    22  	. "github.com/onsi/gomega"
    23  )
    24  
    25  var _ = Describe("Runner", func() {
    26  	const (
    27  		schemasVar = "SCHEMAS"
    28  		pathVar    = "PATH"
    29  	)
    30  
    31  	var (
    32  		testFile string
    33  		errors   map[string]error
    34  	)
    35  
    36  	JustBeforeEach(func() {
    37  		theRunner := runner.NewTestRunner(testFile)
    38  		errors = theRunner.Run()
    39  	})
    40  
    41  	Describe("With incorrect files", func() {
    42  		Context("When the file does not exist", func() {
    43  			BeforeEach(func() {
    44  				testFile = "./test_data/nonexising_file.js"
    45  			})
    46  
    47  			It("Should return the proper errors", func() {
    48  				Expect(errors).To(HaveLen(1))
    49  				Expect(errors).To(HaveKeyWithValue(runner.GeneralError, MatchError(ContainSubstring("no such file"))))
    50  			})
    51  		})
    52  
    53  		Context("When the file contains invalid javascript", func() {
    54  			BeforeEach(func() {
    55  				testFile = "./test_data/compilation_error.js"
    56  			})
    57  
    58  			It("Should return the proper errors", func() {
    59  				Expect(errors).To(HaveLen(1))
    60  				Expect(errors).To(HaveKeyWithValue(runner.GeneralError, MatchError(ContainSubstring("parse file"))))
    61  			})
    62  		})
    63  
    64  		Context("When the test schema is not specified", func() {
    65  			BeforeEach(func() {
    66  				testFile = "./test_data/schema_not_specified.js"
    67  			})
    68  
    69  			It("Should return the proper errors", func() {
    70  				Expect(errors).To(HaveLen(1))
    71  				Expect(errors).To(HaveKeyWithValue(runner.GeneralError, MatchError(ContainSubstring(schemasVar))))
    72  			})
    73  		})
    74  
    75  		Context("When the test schema is not a string", func() {
    76  			BeforeEach(func() {
    77  				testFile = "./test_data/schema_not_a_string.js"
    78  			})
    79  
    80  			It("Should return the proper errors", func() {
    81  				Expect(errors).To(HaveLen(1))
    82  				Expect(errors).To(HaveKeyWithValue(runner.GeneralError, MatchError(ContainSubstring(schemasVar))))
    83  			})
    84  		})
    85  
    86  		Context("When the test schema does not exist", func() {
    87  			BeforeEach(func() {
    88  				testFile = "./test_data/nonexisting_schema.js"
    89  			})
    90  
    91  			It("Should return the proper errors", func() {
    92  				Expect(errors).To(HaveLen(1))
    93  				Expect(errors).To(HaveKeyWithValue(runner.GeneralError, MatchError(ContainSubstring("no such file"))))
    94  			})
    95  		})
    96  
    97  		Context("When the test path is not specified", func() {
    98  			BeforeEach(func() {
    99  				testFile = "./test_data/path_not_specified.js"
   100  			})
   101  
   102  			It("Should return the proper errors", func() {
   103  				Expect(errors).To(HaveLen(1))
   104  				Expect(errors).To(HaveKeyWithValue(runner.GeneralError, MatchError(ContainSubstring(pathVar))))
   105  			})
   106  		})
   107  
   108  		Context("When the test path is not a string", func() {
   109  			BeforeEach(func() {
   110  				testFile = "./test_data/path_not_a_string.js"
   111  			})
   112  
   113  			It("Should return the proper errors", func() {
   114  				Expect(errors).To(HaveLen(1))
   115  				Expect(errors).To(HaveKeyWithValue(runner.GeneralError, MatchError(ContainSubstring(pathVar))))
   116  			})
   117  		})
   118  	})
   119  
   120  	Describe("With correct files", func() {
   121  		Context("When a runtime occurs", func() {
   122  			BeforeEach(func() {
   123  				testFile = "./test_data/runtime_error.js"
   124  			})
   125  
   126  			It("Should return the proper errors", func() {
   127  				Expect(errors).To(HaveLen(1))
   128  				Expect(errors).To(HaveKeyWithValue("testRuntimeError", MatchError("runtime error")))
   129  			})
   130  		})
   131  
   132  		Context("When loading multiple schemas", func() {
   133  			BeforeEach(func() {
   134  				testFile = "./test_data/multiple_schemas.js"
   135  			})
   136  
   137  			It("Should return no errors", func() {
   138  				Expect(errors).To(HaveLen(1))
   139  				Expect(errors).To(HaveKeyWithValue("testBothSchemasLoaded", BeNil()))
   140  			})
   141  		})
   142  
   143  		Context("When loading extensions", func() {
   144  			BeforeEach(func() {
   145  				testFile = "./test_data/extension_loading.js"
   146  			})
   147  
   148  			It("Should return no errors", func() {
   149  				Expect(errors).To(HaveLen(2))
   150  				Expect(errors).To(HaveKeyWithValue("testExtension1Loaded", BeNil()))
   151  				Expect(errors).To(HaveKeyWithValue("testExtension2NotLoaded", BeNil()))
   152  			})
   153  		})
   154  
   155  		Context("When the test fails", func() {
   156  			BeforeEach(func() {
   157  				testFile = "./test_data/fail.js"
   158  			})
   159  
   160  			It("Should return the proper errors", func() {
   161  				Expect(errors).To(HaveLen(2))
   162  				Expect(errors).To(HaveKeyWithValue("testFail", MatchError("called testFail")))
   163  				Expect(errors).To(HaveKeyWithValue("testFailNoMessage", HaveOccurred()))
   164  			})
   165  		})
   166  
   167  		Context("When fail is invoked incorrectly", func() {
   168  			BeforeEach(func() {
   169  				testFile = "./test_data/fail_error.js"
   170  			})
   171  
   172  			It("Should return the proper errors", func() {
   173  				Expect(errors).To(HaveLen(1))
   174  				Expect(errors).To(HaveKeyWithValue("testFailError", MatchError(ContainSubstring("format string expected"))))
   175  			})
   176  		})
   177  
   178  		Context("When using GohanTrigger", func() {
   179  			BeforeEach(func() {
   180  				testFile = "./test_data/gohan_trigger.js"
   181  			})
   182  
   183  			It("Should return no errors", func() {
   184  				Expect(errors).To(HaveLen(1))
   185  				Expect(errors).To(HaveKeyWithValue("testGohanTrigger", BeNil()))
   186  			})
   187  		})
   188  
   189  		Context("When incorrectly using the Gohan HTTP mock", func() {
   190  			BeforeEach(func() {
   191  				testFile = "./test_data/mock_validate.js"
   192  			})
   193  
   194  			It("Should return the proper errors", func() {
   195  				Expect(errors).To(HaveLen(4))
   196  				Expect(errors).To(HaveKeyWithValue(
   197  					"testMockExpectNotSpecified", MatchError(ContainSubstring("Expect() should be specified for each call to"))))
   198  				Expect(errors).To(HaveKeyWithValue(
   199  					"testMockReturnNotSpecified", MatchError(ContainSubstring("Return() should be specified for each call to"))))
   200  				Expect(errors).To(HaveKeyWithValue(
   201  					"testMockExpectEmpty", MatchError(ContainSubstring("Expect() should be called with at least one argument"))))
   202  				Expect(errors).To(HaveKeyWithValue(
   203  					"testMockReturnEmpty", MatchError(ContainSubstring("Return() should be called with exactly one argument"))))
   204  			})
   205  		})
   206  
   207  		Context("When correctly using the Gohan HTTP mock", func() {
   208  			BeforeEach(func() {
   209  				testFile = "./test_data/gohan_http_mock.js"
   210  			})
   211  
   212  			It("Should work", func() {
   213  				Expect(errors).To(HaveLen(4))
   214  				Expect(errors).To(HaveKeyWithValue(
   215  					"testUnexpectedCall", MatchError(ContainSubstring("Unexpected call"))))
   216  				Expect(errors).To(HaveKeyWithValue("testSingleReturnSingleCall", BeNil()))
   217  				Expect(errors).To(HaveKeyWithValue(
   218  					"testSingleReturnMultipleCalls", MatchError(ContainSubstring("Unexpected call"))))
   219  				Expect(errors).To(HaveKeyWithValue(
   220  					"testWrongArgumentsCall", MatchError(ContainSubstring("Wrong arguments"))))
   221  				Expect(errors).To(HaveKeyWithValue(
   222  					"testWrongArgumentsCall", MatchError(ContainSubstring("expected [POST, http://www.abc.com, map[a:a], map[]]"))))
   223  			})
   224  		})
   225  
   226  		Context("When Gohan HTTP mock expected calls do not occur", func() {
   227  			BeforeEach(func() {
   228  				testFile = "./test_data/mock_calls_not_made.js"
   229  			})
   230  
   231  			It("Should work", func() {
   232  				Expect(errors).To(HaveLen(2))
   233  				Expect(errors).To(HaveKeyWithValue(
   234  					"testFirstMockCallNotMade", MatchError("Expected call to gohan_http([POST]) with return value OK, but not made")))
   235  				Expect(errors).To(HaveKeyWithValue(
   236  					"testLastMockCallNotMade", MatchError("Expected call to gohan_http([GET]) with return value OK, but not made")))
   237  			})
   238  		})
   239  
   240  		Context("When using Gohan builtins", func() {
   241  			BeforeEach(func() {
   242  				testFile = "./test_data/gohan_builtins.js"
   243  			})
   244  
   245  			It("Should return no errors", func() {
   246  				Expect(errors).To(HaveLen(1))
   247  				Expect(errors).To(HaveKeyWithValue("testGohanBuiltins", BeNil()))
   248  			})
   249  		})
   250  
   251  		Context("When passing extension errors", func() {
   252  			BeforeEach(func() {
   253  				testFile = "./test_data/extension_error.js"
   254  			})
   255  
   256  			It("Should return no errors", func() {
   257  				Expect(errors).To(HaveLen(1))
   258  				Expect(errors).To(HaveKeyWithValue("testExtensionError", BeNil()))
   259  			})
   260  		})
   261  
   262  		Context("When using mock transactions", func() {
   263  			BeforeEach(func() {
   264  				testFile = "./test_data/mock_transactions.js"
   265  			})
   266  
   267  			It("Should return no errors", func() {
   268  				Expect(errors).To(HaveLen(1))
   269  				Expect(errors).To(HaveKeyWithValue("testMockTransactions", BeNil()))
   270  			})
   271  		})
   272  
   273  		Describe("Using setUp and tearDown", func() {
   274  			Context("When setUp is completed correctly", func() {
   275  				BeforeEach(func() {
   276  					testFile = "./test_data/set_up.js"
   277  				})
   278  
   279  				It("Should return no errors", func() {
   280  					Expect(errors).To(HaveLen(1))
   281  					Expect(errors).To(HaveKeyWithValue("testSetUp", BeNil()))
   282  				})
   283  			})
   284  
   285  			Context("When an error occurs in setUp", func() {
   286  				BeforeEach(func() {
   287  					testFile = "./test_data/set_up_error.js"
   288  				})
   289  
   290  				It("Should return the proper errors", func() {
   291  					Expect(errors).To(HaveLen(1))
   292  					Expect(errors).To(HaveKeyWithValue("testSetUpError", MatchError(ContainSubstring("setUp"))))
   293  				})
   294  			})
   295  
   296  			Context("When tearDown is completed correctly", func() {
   297  				BeforeEach(func() {
   298  					testFile = "./test_data/tear_down.js"
   299  				})
   300  
   301  				It("Should return no errors", func() {
   302  					Expect(errors).To(HaveLen(1))
   303  					Expect(errors).To(HaveKeyWithValue("testTearDown", MatchError("OK")))
   304  				})
   305  			})
   306  
   307  			Context("When an error occurs in tearDown", func() {
   308  				BeforeEach(func() {
   309  					testFile = "./test_data/tear_down_error.js"
   310  				})
   311  
   312  				It("Should return the proper errors", func() {
   313  					Expect(errors).To(HaveLen(1))
   314  					Expect(errors).To(HaveKeyWithValue("testTearDownError", MatchError(ContainSubstring("tearDown"))))
   315  				})
   316  			})
   317  
   318  			Context("When an error occurs in tearDown and during test execution", func() {
   319  				BeforeEach(func() {
   320  					testFile = "./test_data/tear_down_error_after_error.js"
   321  				})
   322  
   323  				It("Should return the original error", func() {
   324  					Expect(errors).To(HaveLen(1))
   325  					Expect(errors).To(HaveKeyWithValue("testTearDownErrorAfterError", MatchError(ContainSubstring("original"))))
   326  				})
   327  			})
   328  		})
   329  	})
   330  })