c# - AutoFixture with Entity Framework - mapping int to enum -
i use autofixture (3.30.8) against entity framework (6.1.3) data model. have implemented autofixture.autoef (0.3.5) package fix entities , avoid circular references generated relationships.
however, tables have several int columns represented enums in code , able set int values based on enum values , have proxy class each of enum values.
here simplified example of schema:
public partial class mycontext : dbcontext { public virtual dbset<parent> parents { get; set; } public virtual dbset<child> children { get; set; } } public partial class parent { public parent() { this.children = new hashset<child>(); } public int id { get; set; } public string name { get; set; } public int status { get; set; } public virtual icollection<child> children { get; set; } } public partial class child { public int id { get; set; } public int parentid { get; set; } public string name { get; set; } public int type { get; set; } public virtual parent parent { get; set; } }
here enums:
public enum status { active = 1, inactive = 2 } public enum type { = 1, down = 2, left = 3, right = 4 }
and here how creating proxies:
var fixture = new fixture(); fixture.customize(new entitycustomization(new dbcontextentitytypesprovider(typeof(mycontext)))); var parents = fixture.createmany<parent>();
this works in have collection of 3 parent
classes each 3 child
classes , id properties nicely match up. however, expected, status
, type
properties random ints generated autofixture.
what have 2 parent
classes, 1 status
of 1
, 1 status
of 2
, each of have 4 child
classes, each 1 of have type
of 1
, 2
, 3
, 4
.
is possible automatically using autofixture?
edit: make clearer asking:
how can automatically map int property on class enum 1 proxy class each value of mapped enum.
this need work class has 2 or more mapped enums.
e.g. if child
has type
, status
properties, expect 8 children
per parent
:
status = 1, type = 1 status = 1, type = 2 status = 1, type = 3 status = 1, type = 4 status = 2, type = 1 status = 2, type = 2 status = 2, type = 3 status = 2, type = 4
and extrapolate further, if parent
had both status
, type
expect 8 parent
proxy classes, each 8 child
proxy classes.
edit 2: here sample of how manually coded substitutes generator looks, if i've put both enums on both classes. using autofixture can automate except looping generate every permutation of enums. that asking how do.
public class substitutes { private int parentidseed; private int childidseed; public substitutes() { this.parentidseed = 0; this.childidseed = 0; this.parents = new list<parent>(); this.children = new list<child>(); this.generateparents(); } private void generateparents() { foreach (type type in enum.getvalues(typeof(type))) { foreach (status status in enum.getvalues(typeof(status))) { this.parentidseed++; var parent = new parent { id = this.parentidseed, name = "parent " + this.parentidseed, status = (int)status, type = (int)type }; this.generatechildren(parent); this.parents.add(parent); } } } private void generatechildren(parent parent) { foreach (type type in enum.getvalues(typeof(type))) { foreach (status status in enum.getvalues(typeof(status))) { this.childidseed++; var child = new child { id = this.childidseed, name = "child " + this.childidseed, status = (int)status, type = (int)type, parent = parent, parentid = parent.id }; parent.children.add(child); this.children.add(child); } } } public list<child> children { get; set; } public list<parent> parents { get; set; } }
yes, possible:
var parents = fixture.createmany<parent>(2).tolist(); parents[1].status = 1; parents[1].children = fixture.createmany<child>(4).tolist(); parents[1].children.elementat(1).type = 1; parents[1].children.elementat(2).type = 2; parents[1].children.elementat(3).type = 3; parents[1].children.elementat(4).type = 4; parents[2].status = 2; parents[2].children = fixture.createmany<child>(4).tolist(); parents[2].children.elementat(1).type = 1; parents[2].children.elementat(2).type = 2; parents[2].children.elementat(3).type = 3; parents[2].children.elementat(4).type = 4;
Comments
Post a Comment