Skip to content

Commit b02d85c

Browse files
committed
fix(RelationshipAttribute): correct Equals and ToString
1 parent 2c7d07f commit b02d85c

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

src/JsonApiDotNetCore/Models/RelationshipAttribute.cs

+15
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,20 @@ protected RelationshipAttribute(string publicName)
1616
public bool IsHasOne { get { return this.GetType() == typeof(HasOneAttribute); } }
1717

1818
public abstract void SetValue(object entity, object newValue);
19+
20+
public override string ToString()
21+
{
22+
return base.ToString() + ":" + PublicRelationshipName;
23+
}
24+
25+
public override bool Equals(object obj)
26+
{
27+
RelationshipAttribute attr = obj as RelationshipAttribute;
28+
if (attr == null)
29+
{
30+
return false;
31+
}
32+
return IsHasMany == attr.IsHasMany && PublicRelationshipName.Equals(attr.PublicRelationshipName);
33+
}
1934
}
2035
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using JsonApiDotNetCore.Models;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
using Xunit;
6+
7+
namespace JsonApiDotNetCoreExampleTests.Unit.Models
8+
{
9+
public class AttributesEqualsTests
10+
{
11+
[Fact]
12+
public void HasManyAttribute_Equals_Returns_True_When_Same_Name()
13+
{
14+
var a = new HasManyAttribute("test");
15+
var b = new HasManyAttribute("test");
16+
17+
Assert.Equal(a, b);
18+
}
19+
20+
[Fact]
21+
public void HasManyAttribute_Equals_Returns_False_When_Different_Name()
22+
{
23+
var a = new HasManyAttribute("test");
24+
var b = new HasManyAttribute("test2");
25+
26+
Assert.NotEqual(a, b);
27+
}
28+
29+
[Fact]
30+
public void HasOneAttribute_Equals_Returns_True_When_Same_Name()
31+
{
32+
var a = new HasOneAttribute("test");
33+
var b = new HasOneAttribute("test");
34+
35+
Assert.Equal(a, b);
36+
}
37+
38+
[Fact]
39+
public void HasOneAttribute_Equals_Returns_False_When_Different_Name()
40+
{
41+
var a = new HasOneAttribute("test");
42+
var b = new HasOneAttribute("test2");
43+
44+
Assert.NotEqual(a, b);
45+
}
46+
47+
[Fact]
48+
public void AttrAttribute_Equals_Returns_True_When_Same_Name()
49+
{
50+
var a = new AttrAttribute("test");
51+
var b = new AttrAttribute("test");
52+
53+
Assert.Equal(a, b);
54+
}
55+
56+
[Fact]
57+
public void AttrAttribute_Equals_Returns_False_When_Different_Name()
58+
{
59+
var a = new AttrAttribute("test");
60+
var b = new AttrAttribute("test2");
61+
62+
Assert.NotEqual(a, b);
63+
}
64+
65+
[Fact]
66+
public void HasManyAttribute_Doesnt_Equal_HasOneAttribute_With_Same_Name()
67+
{
68+
RelationshipAttribute a = new HasManyAttribute("test");
69+
RelationshipAttribute b = new HasOneAttribute("test");
70+
71+
Assert.NotEqual(a, b);
72+
Assert.NotEqual(b, a);
73+
}
74+
}
75+
}

0 commit comments

Comments
 (0)