From ab0dc28b0ded12ad4d0f35c20d30d4d4840d2b3f Mon Sep 17 00:00:00 2001 From: Michael Hilus Date: Tue, 28 Mar 2017 01:57:29 +0200 Subject: [PATCH] Added tests --- ...ComplexGraphTypeExtensionsSpecification.cs | 48 ++++ .../EnumerationTypeSpecification.cs | 44 ++++ .../InputGraphTypeExtensionsSpecification.cs | 31 +++ src/Nitrolize.Tests/Nitrolize.Tests.xproj | 7 +- .../ObjectExtensionsSpecification.cs | 116 +++++++++ .../ObjectGraphTypeExtensionsSpecification.cs | 48 ++++ .../TypeExtensionsSpecification.cs | 246 ++++++++++++++++++ 7 files changed, 537 insertions(+), 3 deletions(-) create mode 100644 src/Nitrolize.Tests/ComplexGraphTypeExtensionsSpecification.cs create mode 100644 src/Nitrolize.Tests/EnumerationTypeSpecification.cs create mode 100644 src/Nitrolize.Tests/InputGraphTypeExtensionsSpecification.cs create mode 100644 src/Nitrolize.Tests/ObjectExtensionsSpecification.cs create mode 100644 src/Nitrolize.Tests/ObjectGraphTypeExtensionsSpecification.cs create mode 100644 src/Nitrolize.Tests/TypeExtensionsSpecification.cs diff --git a/src/Nitrolize.Tests/ComplexGraphTypeExtensionsSpecification.cs b/src/Nitrolize.Tests/ComplexGraphTypeExtensionsSpecification.cs new file mode 100644 index 0000000..f5a5211 --- /dev/null +++ b/src/Nitrolize.Tests/ComplexGraphTypeExtensionsSpecification.cs @@ -0,0 +1,48 @@ +using FluentAssertions; +using GraphQL.Types; +using Machine.Fakes; +using Machine.Specifications; +using Nitrolize.Extensions; +using Nitrolize.Types; +using System; + +namespace Nitrolize.Tests +{ + [Subject(typeof(ComplexGraphTypeExtensions))] + public class ComplexGraphTypeExtensionsSpecification : WithSubject + { + public class TestGraphType : ObjectGraphType + { + } + + public class SomeModel + { + public Guid Id { get; set; } + } + + protected static TestGraphType Result; + + Establish context = () => Result = new TestGraphType(); + } + + public class When_adding_a_single_field : ComplexGraphTypeExtensionsSpecification + { + Because of = () => Result.AddSingleField>(context => null); + + It should_have_added_the_single_field = () => Result.Fields.Should().Contain(f => f.Name == "someModel"); + } + + public class When_adding_a_list_field : ComplexGraphTypeExtensionsSpecification + { + Because of = () => Result.AddListField>(); + + It should_have_added_the_list_field = () => Result.Fields.Should().Contain(f => f.Name == "someModels"); + } + + public class When_adding_a_connection_field : ComplexGraphTypeExtensionsSpecification + { + Because of = () => Result.AddConnectionField>(context => null); + + It should_have_added_the_list_field = () => Result.Fields.Should().Contain(f => f.Name == "someModels"); + } +} diff --git a/src/Nitrolize.Tests/EnumerationTypeSpecification.cs b/src/Nitrolize.Tests/EnumerationTypeSpecification.cs new file mode 100644 index 0000000..24bb4ca --- /dev/null +++ b/src/Nitrolize.Tests/EnumerationTypeSpecification.cs @@ -0,0 +1,44 @@ +using FluentAssertions; +using Machine.Fakes; +using Machine.Specifications; +using Nitrolize.Types; +using System.Linq; + +namespace Nitrolize.Tests +{ + public enum ExampleEnum + { + ExampleValue = 10, + EliteValue = 1337, + FailValue = 1338 + } + + [Subject(typeof(EnumerationType))] + public class EnumerationTypeSpecification : WithSubject> + { + } + + public class When_creating_the_type : EnumerationTypeSpecification + { + protected static EnumerationType Result; + + Because of = () => + { + Result = new EnumerationType(); + }; + + It should_set_correct_name = () => Result.Name.Should().Be("ExampleEnum"); + + It should_create_all_values = () => + { + Result.Values.ToList()[0].Name.ShouldBeEquivalentTo("ExampleValue"); + Result.Values.ToList()[0].Value.ShouldBeEquivalentTo(10); + + Result.Values.ToList()[1].Name.ShouldBeEquivalentTo("EliteValue"); + Result.Values.ToList()[1].Value.ShouldBeEquivalentTo(1337); + + Result.Values.ToList()[2].Name.ShouldBeEquivalentTo("FailValue"); + Result.Values.ToList()[2].Value.ShouldBeEquivalentTo(1338); + }; + } +} diff --git a/src/Nitrolize.Tests/InputGraphTypeExtensionsSpecification.cs b/src/Nitrolize.Tests/InputGraphTypeExtensionsSpecification.cs new file mode 100644 index 0000000..c1db7da --- /dev/null +++ b/src/Nitrolize.Tests/InputGraphTypeExtensionsSpecification.cs @@ -0,0 +1,31 @@ +using FluentAssertions; +using Machine.Fakes; +using Machine.Specifications; +using Nitrolize.Extensions; +using Nitrolize.Types.Input; +using System; + +namespace Nitrolize.Tests +{ + [Subject(typeof(InputObjectGraphTypeExtensions))] + public class InputObjectGraphTypeExtensionsSpecification : WithSubject + { + public class TestGraphType : InputType + { + } + + public class SomeModel + { + public string SomeProperty { get; set; } + } + + protected static TestGraphType Result; + } + + public class When_instantiating_an_input_type : InputObjectGraphTypeExtensionsSpecification + { + Because of = () => Result = new TestGraphType(); + + It should_have_added_all_simple_fields_from_its_model = () => Result.Fields.Should().Contain(f => f.Name == "someProperty"); + } +} diff --git a/src/Nitrolize.Tests/Nitrolize.Tests.xproj b/src/Nitrolize.Tests/Nitrolize.Tests.xproj index c42aa4c..0106ff4 100644 --- a/src/Nitrolize.Tests/Nitrolize.Tests.xproj +++ b/src/Nitrolize.Tests/Nitrolize.Tests.xproj @@ -4,7 +4,6 @@ 14.0 $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) - 969334e4-92f2-442d-a347-cb31f1e21e8a @@ -13,9 +12,11 @@ .\bin\ v4.6.1 - 2.0 + + + - + \ No newline at end of file diff --git a/src/Nitrolize.Tests/ObjectExtensionsSpecification.cs b/src/Nitrolize.Tests/ObjectExtensionsSpecification.cs new file mode 100644 index 0000000..d229fcf --- /dev/null +++ b/src/Nitrolize.Tests/ObjectExtensionsSpecification.cs @@ -0,0 +1,116 @@ +using FluentAssertions; +using Machine.Fakes; +using Machine.Specifications; +using Nitrolize.Extensions; +using Nitrolize.Identification; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Threading.Tasks; + +namespace Nitrolize.Tests +{ + [Subject(typeof(ObjectExtensions))] + public class ObjectExtensionsSpecification : WithSubject + { + } + + public class When_cloning_an_opject : ObjectExtensionsSpecification + { + [TypeDescriptionProvider(typeof(Guid))] + public class ModelA + { + public string Id { get; set; } + public double DoubleValue { get; set; } + public List Items { get; set; } + public List NullItems { get; set; } + + private string privateField = "private field"; + public string PrivateField + { + get + { + return this.privateField; + } + } + + private List fieldItems = new[] { new ItemA { Id = GlobalId.ToGlobalId("ItemB", "1338"), Value = 1338 } }.ToList(); + public List FieldItems + { + get + { + return this.fieldItems; + } + } + } + + [TypeDescriptionProvider(typeof(int))] + public class ItemA + { + public string Id { get; set; } + public int Value { get; set; } + } + + public class ModelB + { + public Guid Id { get; set; } + public double DoubleValue { get; set; } + public List Items { get; set; } + public List NullItems { get; set; } + + private string privateField; + public string PrivateField + { + get + { + return this.privateField; + } + } + + private List fieldItems; + public List FieldItems + { + get + { + return fieldItems; + } + } + } + + public class ItemB + { + public int Id { get; set; } + public int Value { get; set; } + } + + protected static ModelA A; + + Establish context = () => A = new ModelA + { + Id = GlobalId.ToGlobalId("ModelB", "0a25a77b-d43f-4744-8095-ff5567797082"), + DoubleValue = 13.37, + Items = new[] { new ItemA { Id = GlobalId.ToGlobalId("ItemB", "1337"), Value = 1337 } }.ToList() + }; + + protected static ModelB Result; + + Because of = () => Result = A.CloneAs(); + + It should_have_converted_simple_properties = () => Result.DoubleValue.Should().Be(13.37); + + It should_have_converted_id_property = () => Result.Id.Should().Be(Guid.Parse("0a25a77b-d43f-4744-8095-ff5567797082")); + + It should_have_cloned_list_properties = () => Result.Items.Should().NotBeNull(); + + It should_have_cloned_list_items = () => Result.Items.Should().NotBeEmpty(); + + It should_have_cloned_properties_of_list_items = () => Result.Items[0].Value.Should().Be(1337); + + It should_have_skipped_null_lists = () => Result.NullItems.Should().BeNull(); + + It should_have_cloned_fields = () => Result.PrivateField.Should().Be("private field"); + + It should_have_cloned_field_lists = () => Result.FieldItems.Should().NotBeNull(); + } +} diff --git a/src/Nitrolize.Tests/ObjectGraphTypeExtensionsSpecification.cs b/src/Nitrolize.Tests/ObjectGraphTypeExtensionsSpecification.cs new file mode 100644 index 0000000..9fa452d --- /dev/null +++ b/src/Nitrolize.Tests/ObjectGraphTypeExtensionsSpecification.cs @@ -0,0 +1,48 @@ +using FluentAssertions; +using Machine.Fakes; +using Machine.Specifications; +using Nitrolize.Extensions; +using Nitrolize.Types; +using System; + +namespace Nitrolize.Tests +{ + [Subject(typeof(ObjectGraphTypeExtensions))] + public class ObjectGraphTypeExtensionsSpecification : WithSubject + { + public class TestGraphType : NodeObjectType + { + } + + public class SomeModel + { + public bool BoolProperty { get; set; } + public Guid GuidProperty { get; set; } + public string StringProperty { get; set; } + public int IntProperty { get; set; } + public decimal DecimalProperty { get; set; } + public float FloatProperty { get; set; } + public double DoubleProperty { get; set; } + public DateTime DateTimeProperty { get; set; } + } + + protected static TestGraphType Result; + } + + public class When_instantiating_an_object_type : ObjectGraphTypeExtensionsSpecification + { + Because of = () => Result = new TestGraphType(); + + It should_have_added_all_primitive_fields_from_its_model = () => + { + Result.Fields.Should().Contain(f => f.Name == "boolProperty"); + Result.Fields.Should().Contain(f => f.Name == "guidProperty"); + Result.Fields.Should().Contain(f => f.Name == "stringProperty"); + Result.Fields.Should().Contain(f => f.Name == "intProperty"); + Result.Fields.Should().Contain(f => f.Name == "decimalProperty"); + Result.Fields.Should().Contain(f => f.Name == "floatProperty"); + Result.Fields.Should().Contain(f => f.Name == "doubleProperty"); + Result.Fields.Should().Contain(f => f.Name == "dateTimeProperty"); + }; + } +} diff --git a/src/Nitrolize.Tests/TypeExtensionsSpecification.cs b/src/Nitrolize.Tests/TypeExtensionsSpecification.cs new file mode 100644 index 0000000..82bf134 --- /dev/null +++ b/src/Nitrolize.Tests/TypeExtensionsSpecification.cs @@ -0,0 +1,246 @@ +using Machine.Fakes; +using Machine.Specifications; +using System; +using System.Collections.Generic; +using Nitrolize.Extensions; +using System.Reflection; +using FluentAssertions; +using System.ComponentModel; +using GraphQL.Types; +using Nitrolize.Types; + +namespace Nitrolize.Tests +{ + [Subject(typeof(Nitrolize.Extensions.TypeExtensions))] + public class TypeExtensionsSpecification : WithSubject + { + } + + public class When_requesting_exact_properties : TypeExtensionsSpecification + { + public abstract class ExactPropertyClassBase + { + public int BaseClassProperty { get; set; } + } + + public class ExactPropertyClass + { + public ExactPropertyClass() { } + public string Property1 { get; set; } + public double Property2 { get; set; } + } + + protected static PropertyInfo[] Result; + + Because of = () => + { + Result = typeof(ExactPropertyClass).GetExactProperies(); + }; + + It should_return_correct_number_of_properties = () => Result.Length.Should().Be(2); + + It should_return_all_class_properties = () => + { + Result.Should().Contain(p => p.Name == "Property1"); + Result.Should().Contain(p => p.Name == "Property2"); + }; + + It should_not_return_any_base_class_properties = () => Result.Should().NotContain(p => p.Name == "BaseClassProperty"); + + } + + public class When_converting_to_virtual_class : TypeExtensionsSpecification + { + public class GenericClass where T : class + { + } + + public class ExampleClass + { + } + + protected static Type Result; + + Because of = () => + { + Result = typeof(GenericClass).ConvertToVirtualType(); + }; + + It should_have_set_a_correct_name = () => Result.Name.Should().Be("GenericClassExampleClass"); + + It should_derive_from_original_type = () => Result.IsSubclassOf(typeof(GenericClass)).Should().BeTrue(); + } + + public abstract class GettingIdSpecification : TypeExtensionsSpecification + { + public class ModelClass + { + public Guid Id { get; set; } + } + + public class ModelClass2 + { + public Guid ModelClass2Id { get; set; } + } + + public class ModelClass3 + { + public Guid WrongNamedOrNotExistentId { get; set; } + } + + protected static ModelClass Model; + protected static ModelClass2 Model2; + protected static ModelClass3 Model3; + + Establish context = () => + { + Model = new ModelClass { Id = Guid.NewGuid() }; + Model2 = new ModelClass2 { ModelClass2Id = Guid.NewGuid() }; + Model3 = new ModelClass3 { WrongNamedOrNotExistentId = Guid.NewGuid() }; + }; + } + + public class When_getting_id : GettingIdSpecification + { + protected static Guid Result; + protected static Guid Result2; + + Because of = () => + { + Result = typeof(ModelClass).GetId(Model); + Result2 = typeof(ModelClass2).GetId(Model2); + }; + + It should_have_got_the_property_named_id = () => Result.Should().NotBeEmpty(); + + It should_have_got_the_property_named_typename_id = () => Result2.Should().NotBeEmpty(); + } + + public class When_getting_id_with_invalid_id_type : GettingIdSpecification + { + protected static Exception exception; + + Because of = () => exception = Catch.Exception(() => typeof(ModelClass).GetId(Model)); + + It should_throw_exception = () => exception.Should().NotBeNull(); + + It should_throw_cast_eception = () => exception.Message.Should().Be("The Id property of ModelClass is of type Guid and cannot be casted to Int32."); + } + + public class When_getting_id_with_non_existend_id_property : GettingIdSpecification + { + protected static Exception exception; + + Because of = () => exception = Catch.Exception(() => typeof(ModelClass3).GetId(Model)); + + It should_throw_exception = () => exception.Should().NotBeNull(); + + It should_throw_cast_eception = () => exception.Message.Should().Be("Could not find any id property on ModelClass3."); + } + + public class When_converting_id_properties_to_string : TypeExtensionsSpecification + { + public class Model + { + public Guid Id { get; set; } + public string Name { get; set; } + public List Items { get; set; } + public Guid SomeItemId { get; set; } + } + + public class Item + { + public Guid Id { get; set; } + public string ItemName { get; set; } + } + + public class InputModel : Model + { + public string ClientMutationId { get; set; } + } + + protected static Type Result; + protected static Type ResultWithoutId; + + Because of = () => + { + Result = typeof(InputModel).ConvertToInputType(); + ResultWithoutId = typeof(InputModel).ConvertToInputType(removeIdProperty: true); + }; + + It should_ship_with_subclass_properties = () => Result.GetProperty("ClientMutationId").Should().NotBeNull(); + + It should_have_an_id_property_of_type_string = () => Result.GetProperty("Id").PropertyType.Name.Should().Be("String"); + + It should_have_converted_the_SomeItem_id = () => Result.GetProperty("SomeItemId").PropertyType.Name.Should().Be("String"); + + It should_ship_with_list_properties = () => Result.GetProperty("Items").Should().NotBeNull(); + + It should_convert_ids_of_list_items = () => Result.GetProperty("Items").PropertyType.GetGenericArguments()[0].GetProperty("Id").PropertyType.Name.Should().Be("String"); + + It should_have_created_class_attribute = () => Result.GetCustomAttributes().Should().NotBeEmpty(); + + It should_have_stored_original_id_type_in_class_attribute = () => Result.GetCustomAttribute().TypeName.Should().Contain("Guid"); + + It should_have_omitted_id_property = () => ResultWithoutId.GetProperty("Id").Should().BeNull(); + } + + public class When_checking_for_id_property : TypeExtensionsSpecification + { + public class Model + { + public int Id { get; set; } + } + + protected static bool Result; + + Because of = () => Result = typeof(Model).HasId(); + + It should_have_found_id = () => Result.Should().Be(true); + } + + public class When_mapping_to_string_graph_type : TypeExtensionsSpecification + { + protected static Type Result; + + Because of = () => Result = typeof(string).MapToGraphType(); + + It should_have_mapped_to_string_graph_type = () => Result.Should().Be(typeof(StringGraphType)); + } + + public class When_mapping_to_object_graph_type : TypeExtensionsSpecification + { + public class Model + { + } + + protected static Type Result; + + Because of = () => Result = typeof(Model).MapToGraphType(); + + It should_have_mapped_to_string_graph_type = () => Result.Should().Be(typeof(ObjectType)); + } + + public class When_mapping_to_node_object_graph_type : TypeExtensionsSpecification + { + public class Model + { + public int Id { get; set; } + } + + protected static Type Result; + + Because of = () => Result = typeof(Model).MapToGraphType(); + + It should_have_mapped_to_string_graph_type = () => Result.Should().Be(typeof(NodeObjectType)); + } + + public class When_mapping_to_list_graph_type : TypeExtensionsSpecification + { + protected static Type Result; + + Because of = () => Result = typeof(List).MapToGraphType(); + + It should_have_mapped_to_string_graph_type = () => Result.Should().Be(typeof(ListGraphType)); + } +}