bjorn %!s(int64=9) %!d(string=hai) anos
pai
achega
0f494589a0
Modificáronse 4 ficheiros con 181 adicións e 2 borrados
  1. 106 0
      tests/data/schema.lua
  2. 2 1
      tests/lust.lua
  3. 71 0
      tests/rules.lua
  4. 2 1
      tests/runner.lua

+ 106 - 0
tests/data/schema.lua

@@ -0,0 +1,106 @@
+local types = require 'types'
+local schema = require 'schema'
+
+local dogCommand = types.enum({
+  name = 'DogCommand',
+  values = {
+    SIT = true,
+    DOWN = true,
+    HEEL = true
+  }
+})
+
+local pet = types.interface({
+  name = 'Pet',
+  fields = {
+    name = types.string.nonNull,
+    nickname = types.int
+  }
+})
+
+local dog = types.object({
+  name = 'Dog',
+  interfaces = { pet },
+  fields = {
+    name = types.string,
+    nickname = types.string,
+    barkVolume = types.int,
+    doesKnowCommand = {
+      kind = types.boolean.nonNull,
+      arguments = {
+        dogCommand = dogCommand.nonNull
+      }
+    },
+    isHouseTrained = {
+      kind = types.boolean.nonNull,
+      arguments = {
+        atOtherHomes = types.boolean
+      }
+    }
+  }
+})
+
+local sentient = types.interface({
+  name = 'Sentient',
+  fields = {
+    name = types.string.nonNull
+  }
+})
+
+local alien = types.object({
+  name = 'Alien',
+  interfaces = sentient,
+  fields = {
+    name = types.string.nonNull,
+    homePlanet = types.string
+  }
+})
+
+local human = types.object({
+  name = 'Human',
+  fields = {
+    name = types.string.nonNull
+  }
+})
+
+local cat = types.object({
+  name = 'Cat',
+  fields = {
+    name = types.string.nonNull,
+    nickname = types.string,
+    meowVolume = types.int
+  }
+})
+
+local catOrDog = types.union({
+  name = 'CatOrDog',
+  types = {cat, dog}
+})
+
+local dogOrHuman = types.union({
+  name = 'DogOrHuman',
+  types = {dog, human}
+})
+
+local humanOrAlien = types.union({
+  name = 'HumanOrAlien',
+  types = {human, alien}
+})
+
+local query = types.object({
+  name = 'Query',
+  fields = {
+    dog = {
+      kind = dog,
+      args = {
+        name = {
+          kind = types.string
+        }
+      }
+    }
+  }
+})
+
+return schema.create({
+  query = query
+})

+ 2 - 1
tests/lust.lua

@@ -117,7 +117,8 @@ local paths = {
       return has(v, x), 'table "' .. tostring(v) .. '" does not have ' .. tostring(x)
     end
   },
-  fail = {f = function(v) return not pcall(v), tostring(v) .. ' did not fail' end}
+  fail = {'with', f = function(v) return not pcall(v), tostring(v) .. ' did not fail' end},
+  with = {f = function(v, x) local _, e = pcall(v) return e and e:find(x), tostring(v) .. ' did not fail with ' .. tostring(x) end}
 }
 
 function lust.expect(v)

+ 71 - 0
tests/rules.lua

@@ -0,0 +1,71 @@
+local parse = require 'parse'
+local validate = require 'validate'
+local schema = require 'tests/data/schema'
+
+local function run(query)
+  validate(schema, parse(query))
+end
+
+describe('rules', function()
+  describe('uniqueOperationNames', function()
+    it('errors if two operations have the same name', function()
+      local query = [[
+        query foo { }
+        query foo { }
+      ]]
+
+      expect(function() run(query) end).to.fail.with('Multiple operations exist named')
+    end)
+
+    it('passes if operations have different names', function()
+      local query = [[
+        query foo { }
+        query bar { }
+      ]]
+
+      expect(function() run(query) end).to_not.fail()
+    end)
+  end)
+
+  describe('argumentsDefinedOnType', function()
+    it('passes if no arguments are supplied', function()
+      local query = [[{
+        dog {
+          isHouseTrained
+        }
+      }]]
+
+      expect(function() run(query) end).to_not.fail()
+    end)
+
+    it('errors if an argument name does not match the schema', function()
+      local query = [[{
+        dog {
+          doesKnowCommand(doggyCommand: SIT)
+        }
+      }]]
+
+      expect(function() run(query) end).to.fail.with('Non%-existent argument')
+    end)
+
+    it('errors if an argument is supplied to a field that takes none', function()
+      local query = [[{
+        dog {
+          name(truncateToLength: 32)
+        }
+      }]]
+
+      expect(function() run(query) end).to.fail.with('Non%-existent argument')
+    end)
+
+    it('passes if all argument names match the schema', function()
+      local query = [[{
+        dog {
+          doesKnowCommand(dogCommand: SIT)
+        }
+      }]]
+
+      expect(function() run(query) end).to_not.fail()
+    end)
+  end)
+end)

+ 2 - 1
tests/runner.lua

@@ -5,7 +5,8 @@ for _, fn in pairs({'describe', 'it', 'test', 'expect', 'spy', 'before', 'after'
 end
 
 local files = {
-  'parse'
+  'parse',
+  'rules'
 }
 
 for i, file in ipairs(files) do