Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filling a text list results in zero entries (syntax worked on AngelaSmith version) #126

Closed
JimKay1941 opened this issue Oct 31, 2017 · 6 comments
Labels

Comments

@JimKay1941
Copy link

This syntax was copied from AngelaSmith, where it works, to GenFu where it produces an empty list as output. There are no error messages.

.Fill(p => p.Skills, () => new List() { "Math", "Science", "History" })

@dpaquette
Copy link
Collaborator

This is definitely a bug. That syntax should work

@dpaquette dpaquette added the bug label Oct 31, 2017
@stimms
Copy link
Collaborator

stimms commented Nov 6, 2017

I went and put in a test for this because it seemed odd that it didn't work.

        [Fact]
        public void Should_fill_from_a_configured_list()
        {
            GenFu.Reset();
            GenFu.Configure<BlogPost>().Fill(x => x.Tags, new List<string> { "default" });
            var result = A.New<BlogPost>();
            Assert.Equal("default", result.Tags.Single());
        }

That test passed but maybe I'm interpreting what is expected here incorrectly. I also tried

        [Fact]
        public void Should_fill_from_a_configured_list()
        {
            GenFu.Reset();
            GenFu.Configure<BlogPost>().Fill(x => x.Tags, () => new List<string> { "default" });
            var result = A.New<BlogPost>();
            Assert.Equal("default", result.Tags.Single());
        }

I wonder if it might not be the lack of generic in your example code. Could you try

.Fill(p => p.Skills, () => new List<string>() { "Math", "Science", "History" })

Instead?

@JimKay1941
Copy link
Author

Something has become confused. The "Could you try..." you have provided is PRECISELY what I submitted to you as the NOT WORKING version.

Here is the Person data model file:
`using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace SimpleSite.Models
{
public class Person
{
public Person()
{
Skills = new HashSet();
BirthDate = DateTime.Now.AddYears(-20);
}

    public int PersonId { get; set; }

    [Required]
    public string FirstName { get; set; }

    [Required]
    public string LastName { get; set; }

    [Required]
    public DateTime BirthDate { get; set; }

    [Required]
    [UIHint("BooleanButtonLabel")]
    public bool LikesMusic { get; set; }

    [Required]
    [EmailAddress]
    public string EmailAddress { get; set; }

    public ICollection<string> Skills { get; set; }
}

Here is the Angie version of the "PersonController" which does work. The 'Skills' list for every person contains the three items: "Math" "Science" and "History."using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Angela.Core;
using SimpleSite.Models;

namespace SimpleSite.Controllers
{
public class PersonController : BaseController
{
private static ICollection _people;

    static PersonController()
    {
        _people = Angie.Configure<Person>()
            .Fill(p => p.BirthDate)
            .AsPastDate()
            .Fill(p => p.LikesMusic)
            .WithRandom(new List<bool>() { true, true, true, false, false })
            .Fill(p => p.Skills, () => new List<string>() { "Math", "Science", "History" })
            .MakeList<Person>(20);
    }

    // GET: Person
    public ActionResult Index()
    {
        return View(_people);
    }

    public ActionResult SearchPeople(string searchText)
    {
        var term = searchText.ToLower();
        var result = _people
            .Where(p =>
                p.FirstName.ToLower().Contains(term) ||
                p.LastName.ToLower().Contains(term)
            );

        return PartialView("_SearchPeople", result);
    }

    public ActionResult Create()
    {
        var person = new Person();
        return View(person);
    }

    [HttpPost]
    public ActionResult Create(Person person)
    {
        if (ModelState.IsValid)
        {
            _people.Add(person);
            Success(string.Format("<b>{0}</b> was successfully added to the database.", person.FirstName), true);
            return RedirectToAction("Index");
        }
        Danger("Looks like something went wrong. Please check your form.");
        return View(person);
    }

}

}`
Each and every Person generated has the three Skills in their Skills list.

Here is the GenFu version of the PersonController which does NOT work because each and every Person generated has and empty Skills list.
`using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GenFu;
using SimpleSite.Models;

namespace SimpleSite.Controllers
{
public class PersonController : BaseController
{
private static ICollection _people;

    static PersonController()
    {
        GenFu.GenFu.Configure<Person>()
            .Fill(p => p.BirthDate)
            .Fill(p => p.LikesMusic)
            .WithRandom(new List<bool>() { true, true, true, false, false })
            .Fill(p => p.Skills, () => new List<string>() { "Math", "Science", "History" });
        _people = GenFu.GenFu.ListOf<Person>();
    }

    // GET: Person
    public ActionResult Index()
    {
        return View(_people);
    }

    public ActionResult SearchPeople(string searchText)
    {
        var term = searchText.ToLower();
        var result = _people
            .Where(p =>
                p.FirstName.ToLower().Contains(term) ||
                p.LastName.ToLower().Contains(term)
            );

        return PartialView("_SearchPeople", result);
    }

    public ActionResult Create()
    {
        var person = new Person();
        return View(person);
    }

    [HttpPost]
    public ActionResult Create(Person person)
    {
        if (ModelState.IsValid)
        {
            _people.Add(person);
            Success(string.Format("<b>{0}</b> was successfully added to the database.", person.FirstName), true);
            return RedirectToAction("Index");
        }
        Danger("Looks like something went wrong. Please check your form.");
        return View(person);
    }

}

}`

@stimms
Copy link
Collaborator

stimms commented Nov 7, 2017

I took your model

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace SimpleSite.Models {
  public class Person {
   public Person() {
    Skills = new HashSet();
    BirthDate = DateTime.Now.AddYears(-20);
   }

   public int PersonId { get; set; }

   [Required]
   public string FirstName { get; set; }

   [Required]
   public string LastName { get; set; }

   [Required]
   public DateTime BirthDate { get; set; }

   [Required]
   [UIHint("BooleanButtonLabel")]
   public bool LikesMusic { get; set; }

   [Required]
   [EmailAddress]
   public string EmailAddress { get; set; }

   public ICollection < string > Skills { get; set; }
  }
}

and, after fixing the incorrect initialization of the HashSet to a non-generic managed to duplicate your problem. It looks to me like initializing the HashSet in the constructor is the problem. I'd bet it is because we only fill properties that have default values and a non-null list isn't a default value.

We could fill lists which are empty, I think that's a safe change to make but we should also consider this in conjunction with #121 which would be a more global solution to this sort of problem. I'll go ahead and add the filling of empty lists.

@JimKay1941
Copy link
Author

For me, the puzzle arises because almost exactly the same code worked with the Alice version and that code came from a tutorial written by Mr. James, himself.

(The GenFu version was posted by another person who did not seem to realize it wasn't working.)

I'll look at #121 shortly.

@JimKay1941
Copy link
Author

I cloned GenFu (about 2 hours ago), built it, and pointed my own project's reference to that .dll.

Now GenFu is working as I had expected.

Thank you to everyone who participated in this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants