github.com/mmatczuk/gohan@v0.0.0-20170206152520-30e45d9bdb69/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  		schemaIncludesVar = "SCHEMA_INCLUDES"
    28  		schemasVar        = "SCHEMAS"
    29  		pathVar           = "PATH"
    30  	)
    31  
    32  	var (
    33  		testFile   string
    34  		testFilter string
    35  		errors     map[string]error
    36  	)
    37  
    38  	JustBeforeEach(func() {
    39  		theRunner := runner.NewTestRunner(testFile, true, testFilter)
    40  		errors = theRunner.Run()
    41  	})
    42  
    43  	AfterEach(func() {
    44  		testFilter = ""
    45  	})
    46  
    47  	Describe("With incorrect files", func() {
    48  		Context("When the file does not exist", func() {
    49  			BeforeEach(func() {
    50  				testFile = "./test_data/nonexising_file.js"
    51  			})
    52  
    53  			It("Should return the proper errors", func() {
    54  				Expect(errors).To(HaveLen(1))
    55  				Expect(errors).To(HaveKeyWithValue(runner.GeneralError, MatchError(ContainSubstring("no such file"))))
    56  			})
    57  		})
    58  
    59  		Context("When the file contains invalid javascript", func() {
    60  			BeforeEach(func() {
    61  				testFile = "./test_data/compilation_error.js"
    62  			})
    63  
    64  			It("Should return the proper errors", func() {
    65  				Expect(errors).To(HaveLen(1))
    66  				Expect(errors).To(HaveKeyWithValue(runner.GeneralError, MatchError(ContainSubstring("parse file"))))
    67  			})
    68  		})
    69  
    70  		Context("When the test schema is not specified", func() {
    71  			BeforeEach(func() {
    72  				testFile = "./test_data/schema_not_specified.js"
    73  			})
    74  
    75  			It("Should return the proper errors", func() {
    76  				Expect(errors).To(HaveLen(1))
    77  				Expect(errors).To(HaveKeyWithValue(runner.GeneralError, MatchError(ContainSubstring(schemasVar))))
    78  			})
    79  		})
    80  
    81  		Context("When the test schema is not a string", func() {
    82  			BeforeEach(func() {
    83  				testFile = "./test_data/schema_not_a_string.js"
    84  			})
    85  
    86  			It("Should return the proper errors", func() {
    87  				Expect(errors).To(HaveLen(1))
    88  				Expect(errors).To(HaveKeyWithValue(runner.GeneralError, MatchError(ContainSubstring(schemasVar))))
    89  			})
    90  		})
    91  
    92  		Context("When the test schema does not exist", func() {
    93  			BeforeEach(func() {
    94  				testFile = "./test_data/nonexisting_schema.js"
    95  			})
    96  
    97  			It("Should return the proper errors", func() {
    98  				Expect(errors).To(HaveLen(1))
    99  				Expect(errors).To(HaveKeyWithValue(runner.GeneralError, MatchError(ContainSubstring("no such file"))))
   100  			})
   101  		})
   102  
   103  		Context("When the test schema include is not specified", func() {
   104  			BeforeEach(func() {
   105  				testFile = "./test_data/schema_include_not_specified.js"
   106  			})
   107  
   108  			It("Should return the proper errors", func() {
   109  				Expect(errors).To(HaveLen(1))
   110  				Expect(errors).To(HaveKeyWithValue(runner.GeneralError, MatchError(ContainSubstring(schemaIncludesVar))))
   111  			})
   112  		})
   113  
   114  		Context("When the test schema include is not a string", func() {
   115  			BeforeEach(func() {
   116  				testFile = "./test_data/schema_include_not_a_string.js"
   117  			})
   118  
   119  			It("Should return the proper errors", func() {
   120  				Expect(errors).To(HaveLen(1))
   121  				Expect(errors).To(HaveKeyWithValue(runner.GeneralError, MatchError(ContainSubstring(schemaIncludesVar))))
   122  			})
   123  		})
   124  
   125  		Context("When the test schema include does not exist", func() {
   126  			BeforeEach(func() {
   127  				testFile = "./test_data/nonexisting_schema_include.js"
   128  			})
   129  
   130  			It("Should return the proper errors", func() {
   131  				Expect(errors).To(HaveLen(1))
   132  				Expect(errors).To(HaveKeyWithValue(runner.GeneralError, MatchError(ContainSubstring("no such file"))))
   133  			})
   134  		})
   135  
   136  		Context("When the test path is not specified", func() {
   137  			BeforeEach(func() {
   138  				testFile = "./test_data/path_not_specified.js"
   139  			})
   140  
   141  			It("Should return the proper errors", func() {
   142  				Expect(errors).To(HaveLen(1))
   143  				Expect(errors).To(HaveKeyWithValue(runner.GeneralError, MatchError(ContainSubstring(pathVar))))
   144  			})
   145  		})
   146  
   147  		Context("When the test path is not a string", func() {
   148  			BeforeEach(func() {
   149  				testFile = "./test_data/path_not_a_string.js"
   150  			})
   151  
   152  			It("Should return the proper errors", func() {
   153  				Expect(errors).To(HaveLen(1))
   154  				Expect(errors).To(HaveKeyWithValue(runner.GeneralError, MatchError(ContainSubstring(pathVar))))
   155  			})
   156  		})
   157  	})
   158  
   159  	Describe("With correct files", func() {
   160  		Context("When a runtime occurs", func() {
   161  			BeforeEach(func() {
   162  				testFile = "./test_data/runtime_error.js"
   163  			})
   164  
   165  			It("Should return the proper errors", func() {
   166  				Expect(errors).To(HaveLen(1))
   167  				Expect(errors).To(HaveKeyWithValue("testRuntimeError", MatchError("runtime error")))
   168  			})
   169  		})
   170  
   171  		Context("When loading multiple schemas", func() {
   172  			BeforeEach(func() {
   173  				testFile = "./test_data/multiple_schemas.js"
   174  			})
   175  
   176  			It("Should return no errors", func() {
   177  				Expect(errors).To(HaveLen(1))
   178  				Expect(errors).To(HaveKeyWithValue("testBothSchemasLoaded", BeNil()))
   179  			})
   180  		})
   181  
   182  		Context("When filling default values to create db data", func() {
   183  			BeforeEach(func() {
   184  				testFile = "./test_data/default_value_schema.js"
   185  			})
   186  
   187  			It("Should return no errors", func() {
   188  				Expect(errors).To(HaveLen(1))
   189  				Expect(errors).To(HaveKeyWithValue("testDBCreatePopulateDefault", BeNil()))
   190  			})
   191  		})
   192  
   193  		Context("When loading extensions", func() {
   194  			BeforeEach(func() {
   195  				testFile = "./test_data/extension_loading.js"
   196  			})
   197  
   198  			It("Should return no errors", func() {
   199  				Expect(errors).To(HaveLen(2))
   200  				Expect(errors).To(HaveKeyWithValue("testExtension1Loaded", BeNil()))
   201  				Expect(errors).To(HaveKeyWithValue("testExtension2NotLoaded", BeNil()))
   202  			})
   203  		})
   204  
   205  		Context("When loading extensions from schema includes", func() {
   206  			BeforeEach(func() {
   207  				testFile = "./test_data/load_schema_includes.js"
   208  			})
   209  
   210  			It("Should return no errors", func() {
   211  				Expect(errors).To(HaveLen(2))
   212  				Expect(errors).To(HaveKeyWithValue("testExtension1Loaded", BeNil()))
   213  				Expect(errors).To(HaveKeyWithValue("testExtension2NotLoaded", BeNil()))
   214  			})
   215  		})
   216  
   217  		Context("When using filter", func() {
   218  			BeforeEach(func() {
   219  				testFile = "./test_data/extension_loading.js"
   220  				testFilter = ".*1.*"
   221  			})
   222  
   223  			It("Should skip non-matching tests", func() {
   224  				Expect(errors).To(HaveLen(1))
   225  				Expect(errors).To(HaveKeyWithValue("testExtension1Loaded", BeNil()))
   226  				Expect(errors).NotTo(HaveKey("testExtension2NotLoaded"))
   227  			})
   228  		})
   229  
   230  		Context("When the test fails", func() {
   231  			BeforeEach(func() {
   232  				testFile = "./test_data/fail.js"
   233  			})
   234  
   235  			It("Should return the proper errors", func() {
   236  				Expect(errors).To(HaveLen(2))
   237  				Expect(errors).To(HaveKeyWithValue("testFail", MatchError("called testFail")))
   238  				Expect(errors).To(HaveKeyWithValue("testFailNoMessage", HaveOccurred()))
   239  			})
   240  		})
   241  
   242  		Context("When fail is invoked incorrectly", func() {
   243  			BeforeEach(func() {
   244  				testFile = "./test_data/fail_error.js"
   245  			})
   246  
   247  			It("Should return the proper errors", func() {
   248  				Expect(errors).To(HaveLen(1))
   249  				Expect(errors).To(HaveKeyWithValue("testFailError", MatchError(ContainSubstring("format string expected"))))
   250  			})
   251  		})
   252  
   253  		Context("When using GohanTrigger", func() {
   254  			BeforeEach(func() {
   255  				testFile = "./test_data/gohan_trigger.js"
   256  			})
   257  
   258  			It("Should return no errors", func() {
   259  				Expect(errors).To(HaveLen(1))
   260  				Expect(errors).To(HaveKeyWithValue("testGohanTrigger", BeNil()))
   261  			})
   262  		})
   263  
   264  		Context("When incorrectly using mock", func() {
   265  			BeforeEach(func() {
   266  				testFile = "./test_data/mock_validate.js"
   267  			})
   268  
   269  			It("Should return the proper errors", func() {
   270  				Expect(errors).To(HaveLen(3))
   271  				Expect(errors).To(HaveKeyWithValue(
   272  					"testMockExpectNotSpecified", MatchError(ContainSubstring("Expect() should be specified for each call to"))))
   273  				Expect(errors).To(HaveKeyWithValue(
   274  					"testMockReturnNotSpecified", MatchError(ContainSubstring("Return() should be specified for each call to"))))
   275  				Expect(errors).To(HaveKeyWithValue(
   276  					"testMockReturnEmpty", MatchError(ContainSubstring("Return() should be called with exactly one argument"))))
   277  			})
   278  		})
   279  
   280  		Context("When mock expected calls do not occur", func() {
   281  			BeforeEach(func() {
   282  				testFile = "./test_data/mock_calls_not_made.js"
   283  			})
   284  
   285  			It("Should work", func() {
   286  				Expect(errors).To(HaveLen(2))
   287  				Expect(errors).To(HaveKeyWithValue(
   288  					"testFirstMockCallNotMade", MatchError("Expected call to gohan_http([POST]) with return value OK, but not made")))
   289  				Expect(errors).To(HaveKeyWithValue(
   290  					"testLastMockCallNotMade", MatchError("Expected call to gohan_http([GET]) with return value OK, but not made")))
   291  			})
   292  		})
   293  
   294  		Context("When correctly using the Gohan HTTP mock", func() {
   295  			BeforeEach(func() {
   296  				testFile = "./test_data/gohan_http_mock.js"
   297  			})
   298  
   299  			It("Should work", func() {
   300  				Expect(errors).To(HaveLen(4))
   301  				Expect(errors).To(HaveKeyWithValue(
   302  					"testUnexpectedCall", MatchError(ContainSubstring("Unexpected call"))))
   303  				Expect(errors).To(HaveKeyWithValue("testSingleReturnSingleCall", BeNil()))
   304  				Expect(errors).To(HaveKeyWithValue(
   305  					"testSingleReturnMultipleCalls", MatchError(ContainSubstring("Unexpected call"))))
   306  				Expect(errors).To(HaveKeyWithValue(
   307  					"testWrongArgumentsCall", MatchError(ContainSubstring("Wrong arguments"))))
   308  				Expect(errors).To(HaveKeyWithValue(
   309  					"testWrongArgumentsCall", MatchError(ContainSubstring("expected [POST, http://www.abc.com, map[a:a], map[]]"))))
   310  			})
   311  		})
   312  
   313  		Context("When correctly using the Gohan DB Transaction mock", func() {
   314  			BeforeEach(func() {
   315  				testFile = "./test_data/gohan_db_transaction_mock.js"
   316  			})
   317  
   318  			It("Should work", func() {
   319  				Expect(errors).To(HaveLen(4))
   320  				Expect(errors).To(HaveKeyWithValue(
   321  					"testUnexpectedCall", MatchError(ContainSubstring("Unexpected call"))))
   322  				Expect(errors).To(HaveKeyWithValue("testSingleReturnSingleCall", BeNil()))
   323  				Expect(errors).To(HaveKeyWithValue(
   324  					"testSingleReturnMultipleCalls", MatchError(ContainSubstring("Unexpected call"))))
   325  				Expect(errors).To(HaveKeyWithValue(
   326  					"testWrongArgumentsCall", MatchError(ContainSubstring("Wrong arguments"))))
   327  				Expect(errors).To(HaveKeyWithValue(
   328  					"testWrongArgumentsCall", MatchError(ContainSubstring("expected []"))))
   329  			})
   330  		})
   331  
   332  		Context("When correctly using the Gohan Config mock", func() {
   333  			BeforeEach(func() {
   334  				testFile = "./test_data/gohan_config_mock.js"
   335  			})
   336  
   337  			It("Should work", func() {
   338  				Expect(errors).To(HaveLen(4))
   339  				Expect(errors).To(HaveKeyWithValue(
   340  					"testUnexpectedCall", MatchError(ContainSubstring("Unexpected call"))))
   341  				Expect(errors).To(HaveKeyWithValue("testSingleReturnSingleCall", BeNil()))
   342  				Expect(errors).To(HaveKeyWithValue(
   343  					"testSingleReturnMultipleCalls", MatchError(ContainSubstring("Unexpected call"))))
   344  				Expect(errors).To(HaveKeyWithValue(
   345  					"testWrongArgumentsCall", MatchError(ContainSubstring("Wrong arguments"))))
   346  				Expect(errors).To(HaveKeyWithValue(
   347  					"testWrongArgumentsCall", MatchError(ContainSubstring("expected [database/type, sqlite3]"))))
   348  			})
   349  		})
   350  
   351  		Context("When using Gohan builtins", func() {
   352  			BeforeEach(func() {
   353  				testFile = "./test_data/gohan_builtins.js"
   354  			})
   355  
   356  			It("Should return no errors", func() {
   357  				Expect(errors).To(HaveLen(1))
   358  				Expect(errors).To(HaveKeyWithValue("testGohanBuiltins", BeNil()))
   359  			})
   360  		})
   361  
   362  		Context("When passing extension errors", func() {
   363  			BeforeEach(func() {
   364  				testFile = "./test_data/extension_error.js"
   365  			})
   366  
   367  			It("Should return no errors", func() {
   368  				Expect(errors).To(HaveLen(1))
   369  				Expect(errors).To(HaveKeyWithValue("testExtensionError", BeNil()))
   370  			})
   371  		})
   372  
   373  		Context("When using mock transactions", func() {
   374  			BeforeEach(func() {
   375  				testFile = "./test_data/mock_transactions.js"
   376  			})
   377  
   378  			It("Should return no errors", func() {
   379  				Expect(errors).To(HaveLen(2))
   380  				Expect(errors).To(HaveKeyWithValue("testMockTransactions", BeNil()))
   381  				Expect(errors).To(HaveKeyWithValue("testMockTransactionsInOneTrigger", BeNil()))
   382  			})
   383  		})
   384  
   385  		Describe("Using setUp and tearDown", func() {
   386  			Context("When setUp is completed correctly", func() {
   387  				BeforeEach(func() {
   388  					testFile = "./test_data/set_up.js"
   389  				})
   390  
   391  				It("Should return no errors", func() {
   392  					Expect(errors).To(HaveLen(1))
   393  					Expect(errors).To(HaveKeyWithValue("testSetUp", BeNil()))
   394  				})
   395  			})
   396  
   397  			Context("When an error occurs in setUp", func() {
   398  				BeforeEach(func() {
   399  					testFile = "./test_data/set_up_error.js"
   400  				})
   401  
   402  				It("Should return the proper errors", func() {
   403  					Expect(errors).To(HaveLen(1))
   404  					Expect(errors).To(HaveKeyWithValue("testSetUpError", MatchError(ContainSubstring("setUp"))))
   405  				})
   406  			})
   407  
   408  			Context("When tearDown is completed correctly", func() {
   409  				BeforeEach(func() {
   410  					testFile = "./test_data/tear_down.js"
   411  				})
   412  
   413  				It("Should return no errors", func() {
   414  					Expect(errors).To(HaveLen(1))
   415  					Expect(errors).To(HaveKeyWithValue("testTearDown", MatchError("OK")))
   416  				})
   417  			})
   418  
   419  			Context("When an error occurs in tearDown", func() {
   420  				BeforeEach(func() {
   421  					testFile = "./test_data/tear_down_error.js"
   422  				})
   423  
   424  				It("Should return the proper errors", func() {
   425  					Expect(errors).To(HaveLen(1))
   426  					Expect(errors).To(HaveKeyWithValue("testTearDownError", MatchError(ContainSubstring("tearDown"))))
   427  				})
   428  			})
   429  
   430  			Context("When an error occurs in tearDown and during test execution", func() {
   431  				BeforeEach(func() {
   432  					testFile = "./test_data/tear_down_error_after_error.js"
   433  				})
   434  
   435  				It("Should return the original error", func() {
   436  					Expect(errors).To(HaveLen(1))
   437  					Expect(errors).To(HaveKeyWithValue("testTearDownErrorAfterError", MatchError(ContainSubstring("original"))))
   438  				})
   439  			})
   440  		})
   441  	})
   442  })