public void SwitchesToWrap_WhenResultsWrappingIsSetToNoWrap()
            {
                var vm = new Neo4jConsoleControlViewModel { ResultsWrapping = TextWrapping.NoWrap };

                vm.ChangeWrappingCommand.Execute(null);
                vm.ResultsWrapping.Should().Be(TextWrapping.Wrap);
            }
            public void CanAlwaysExecuteAndClearsTheCypherResults()
            {
                var vm = new Neo4jConsoleControlViewModel {CypherResults = "Blah"};

                vm.ClearCommand.CanExecute(null).Should().BeTrue();
                vm.ClearCommand.Execute(null);

                vm.CypherResults.Should().BeEmpty();
            }
            public void ShouldNotChangeTheCypherQuery_WhenUsedOnEmptyHistory()
            {
                var vm = new Neo4jConsoleControlViewModel(GetMockRestClient());
                vm.CypherHistory.Should().BeEmpty();
                vm.CypherQuery = "Foo";

                vm.NextHistoryCommand.Execute(null);
                vm.CypherQuery.Should().Be("Foo");
            }
            public void ShouldReturnAnEmptyString_RegardlessOfHowManyTimesItsCalled()
            {
                var vm = new Neo4jConsoleControlViewModel(GetMockRestClient());
                vm.CypherHistory.Add("Foo");

                vm.NextHistoryCommand.Execute(null);
                vm.CypherQuery.Should().Be("Foo");

                vm.PreviousHistoryCommand.Execute(null);
                vm.PreviousHistoryCommand.Execute(null);
                vm.CypherQuery.Should().Be("");
            }
            public void ShouldNavigateBackwards_WhenNextHistoryHasBeenUsed()
            {
                var vm = new Neo4jConsoleControlViewModel(GetMockRestClient());
                vm.CypherHistory.Add("Foo");
                vm.CypherHistory.Add("Bar");

                vm.NextHistoryCommand.Execute(null);
                vm.CypherQuery.Should().Be("Foo");

                vm.NextHistoryCommand.Execute(null);
                vm.CypherQuery.Should().Be("Bar");

                vm.PreviousHistoryCommand.Execute(null);
                vm.CypherQuery.Should().Be("Foo");
            }
            public void ShouldntInsertDuplicateQueriesIntoHistory()
            {
                var vm = new Neo4jConsoleControlViewModel(GetMockRestClient());
                vm.CypherHistory.Add("Bar");
                vm.CypherHistory.Add("Spam");
                vm.CypherHistory.Count.Should().Be(2);

                vm.NextHistoryCommand.Execute(null);

                vm.CypherQuery = "Spam";

                vm.PostCommand.Execute(null);
                vm.CypherHistory.Count.Should().Be(2);
                vm.CypherHistory.First().Should().Be("Bar");
                vm.CypherHistory.Skip(1).First().Should().Be("Spam");
            }
            public void ReturnsExceptionMessage_WhenPostingThrowsAnException()
            {
                const string expectedMessage = "Communicating to neo4j server threw a System.ArgumentException with this message: Exception Message";
                var rc = new Mock<IRestClient>();
                rc
                    .Setup(r => r.Execute(It.IsAny<IRestRequest>()))
                    .Throws(new ArgumentException("Exception Message"));

                var vm = new Neo4jConsoleControlViewModel(rc.Object);
                vm.PostCommand.Execute(null);

                vm.CypherResults.Should().Contain(expectedMessage);
            }
            public void DeserializesMixedValueResponses()
            {
                //Query: MATCH n RETURN n, COUNT(n) LIMIT 1
                const string successWithCount = "{ \"columns\" : [ \"n\", \"count(n)\" ], \"data\" : [ { \"data\" : { \"ItemId\" : 776 } }, 1 ]}";
                var vm = new Neo4jConsoleControlViewModel(GetMockRestClient(successWithCount));

                vm.PostCommand.Execute(null);
                vm.CypherResults.Should().Contain("1");
                vm.CypherResults.Should().Contain("776");
                vm.CypherResults.Should().NotContain("Couldn't deserialize");
            }
            public void ShouldSelectNextThenNext_WhenThereIsAQueryInTheHistory_AndItsCalledTwice()
            {
                var vm = new Neo4jConsoleControlViewModel(GetMockRestClient());
                vm.CypherHistory.Add("Cypher 1");
                vm.CypherHistory.Add("Cypher 2");
                vm.CypherHistory.Should().HaveCount(2);

                vm.CypherQuery = "Foo";
                vm.NextHistoryCommand.Execute(null);
                vm.NextHistoryCommand.Execute(null);
                vm.CypherQuery.Should().Be("Cypher 2");
            }
            public void InsertsCypherQueryIntoHistory_AtFront_WhenNoHistoryPresent()
            {
                var vm = new Neo4jConsoleControlViewModel(GetMockRestClient());
                vm.CypherHistory.Count.Should().Be(0);
                vm.CypherQuery = "Foo";

                vm.PostCommand.Execute(null);
                vm.CypherHistory.Count.Should().Be(1);
                vm.CypherHistory.First().Should().Be("Foo");
            }
            public void GivesValidResponse_WhenJsonCanBeDeserializedIntoNeo4jResponseButHasNoData()
            {
                const string successWithNoData = "{ \"columns\" : [ ], \"data\" : [ ] }";
                var vm = new Neo4jConsoleControlViewModel(GetMockRestClient(successWithNoData));

                vm.PostCommand.Execute(null);
                vm.CypherResults.Should().Contain("No data returned.");
            }
            public void GivesValidResponse_WhenJsonCanBeDeserializedIntoNeo4jResponse()
            {
                const string successWithData = "{ \"columns\" : [ \"n\" ], \"data\" : [ { \"data\" : { \"value\" : \"neo4jConsole\" } } ]}";
                var vm = new Neo4jConsoleControlViewModel(GetMockRestClient(successWithData));

                vm.PostCommand.Execute(null);
                vm.CypherResults.Should().Contain("{value:\"neo4jConsole\"}");
            }
            public void GivesNoResponseMessage_WhenResponseFromTheServerIsNullOrWhiteSpace()
            {
                const string expected = "No response from the server (THEURL), is it running?";
                var vm = new Neo4jConsoleControlViewModel(GetMockRestClient(null)) { Neo4jUrl = "THEURL" };

                vm.PostCommand.Execute(null);
                vm.CypherResults.Should().Contain(expected);
            }
            public void GivesErrorResponse_WhenJsonCantBeDeserializedIntoNeo4jResponseButCanBeNeo4jErrorResponse()
            {
                const string errorResponse = "{ \"message\" : \"errorNeo4jConsole\", \"exception\" : \"AnException\", \"fullname\" : \"oops.AnException\", \"stacktrace\" : [ \"CodeLines (999)\"] }";
                var vm = new Neo4jConsoleControlViewModel(GetMockRestClient(errorResponse));

                vm.PostCommand.Execute(null);
                vm.CypherResults.Should().Contain("errorNeo4jConsole");
            }
            public void GivesDefaultErrorMessageIfCantBeDeserializedIntoEitherNeo4jResponseOrNeo4jErrorResponse()
            {
                const string response = "non deserializable";
                var vm = new Neo4jConsoleControlViewModel(GetMockRestClient(response));

                vm.PostCommand.Execute(null);
                vm.CypherResults.Should().Contain("Couldn't deserialize");
                vm.CypherResults.Should().Contain(response);
            }
            public void DeserializesValueResponses()
            {
                //Query: MATCH n RETURN COUNT(n)
                var successWithCount = string.Format("{{ \"columns\" : [ \"count(n)\" ], \"data\" : [ {0} ]}}", 2075);
                var vm = new Neo4jConsoleControlViewModel(GetMockRestClient(successWithCount));

                vm.PostCommand.Execute(null);
                vm.CypherResults.Should().Contain("2075");
                vm.CypherResults.Should().NotContain("Couldn't deserialize");
            }
            public void InsertsCypherQueryIntoHistory_AtNextPosition()
            {
                var vm = new Neo4jConsoleControlViewModel(GetMockRestClient());
                vm.CypherHistory.Add("Bar");
                vm.CypherHistory.Count.Should().Be(1);

                vm.NextHistoryCommand.Execute(null);
                vm.CypherQuery = "Foo";

                vm.PostCommand.Execute(null);
                vm.CypherHistory.Count.Should().Be(2);
                vm.CypherHistory.First().Should().Be("Bar");
                vm.CypherHistory.Skip(1).First().Should().Be("Foo");
            }
            public void ReturnsAndDoesNothing_WhenNeo4jUrlIsNullOrWhiteSpace(string neo4jUrl)
            {
                var rc = new Mock<IRestClient>();
                var vm = new Neo4jConsoleControlViewModel(rc.Object) {Neo4jUrl = neo4jUrl};

                rc.ResetCalls();
                vm.PostCommand.Execute(null);
                rc.Verify(r => r.Execute(It.IsAny<IRestRequest>()), Times.Never);
            }
 public Neo4jConsoleControl()
 {
     InitializeComponent();
     DataContext = new Neo4jConsoleControlViewModel();
 }
            public void CallsBackOnTheCallbackMethod_WhenFinishing()
            {
                var vm = new Neo4jConsoleControlViewModel(GetMockRestClient());

                bool calledBack = false;
                Action callback = () => calledBack = true;

                vm.PostCommand.Execute(callback);
                calledBack.Should().BeTrue();
            }