Django:优化违反DRY的测试[关闭](Django: optimize test that violates DRY [closed])
被测模型是
class A(models.Model): """ model A""" name = models.CharField(max_length=50, unique=True) slug = AutoSlugField(max_length=265, unique=True, populate_from='name') link = models.URLField(blank=True) class Meta: verbose_name = 'Model A' def __unicode__(self): return self.name class B(models.Model): """ model B""" name = models.CharField(max_length=50, unique=True) slug = AutoSlugField(max_length=265, unique=True, populate_from='name') link = models.URLField(blank=True) class Meta: verbose_name = 'Model B' def __unicode__(self): return self.name
对给定模型的简单测试,
class TestA(TestCase): """ Test the A model """ def setUp(self): self.name = 'A' self.slug = 'a' self.object = A.objects.create(name=self.name) def test_autoslug_generaton(self): """ test automatically generated slug """ assert self.object.slug == self.slug def test_return_correct_name(self): """ test the __unicode__() method """ assert self.object.__unicode__() == self.name class TestB(TestCase): """ Test the A model """ def setUp(self): self.name = 'B' self.slug = 'b' self.object = B.objects.create(name=self.name) def test_autoslug_generaton(self): """ test automatically generated slug """ assert self.object.slug == self.slug def test_return_correct_name(self): """ test the __unicode__() method """ assert self.object.__unicode__() == self.name
这里的测试违反了DRY,因为测试只是改变模型的重复。 如何重构测试,因为它不违反DRY?
DRY - D ont R epeat Y ourelf, 一种旨在减少冗余和代码重复的软件开发理念 。
谢谢。
The models under test are
class A(models.Model): """ model A""" name = models.CharField(max_length=50, unique=True) slug = AutoSlugField(max_length=265, unique=True, populate_from='name') link = models.URLField(blank=True) class Meta: verbose_name = 'Model A' def __unicode__(self): return self.name class B(models.Model): """ model B""" name = models.CharField(max_length=50, unique=True) slug = AutoSlugField(max_length=265, unique=True, populate_from='name') link = models.URLField(blank=True) class Meta: verbose_name = 'Model B' def __unicode__(self): return self.name
The simple tests for the given models,
class TestA(TestCase): """ Test the A model """ def setUp(self): self.name = 'A' self.slug = 'a' self.object = A.objects.create(name=self.name) def test_autoslug_generaton(self): """ test automatically generated slug """ assert self.object.slug == self.slug def test_return_correct_name(self): """ test the __unicode__() method """ assert self.object.__unicode__() == self.name class TestB(TestCase): """ Test the A model """ def setUp(self): self.name = 'B' self.slug = 'b' self.object = B.objects.create(name=self.name) def test_autoslug_generaton(self): """ test automatically generated slug """ assert self.object.slug == self.slug def test_return_correct_name(self): """ test the __unicode__() method """ assert self.object.__unicode__() == self.name
Here the tests violates DRY as tests are just duplicates with changed models. how do I refactor the tests as it does not violates DRY?
DRY- Dont Repeat Yourself, a software development philosophy which aims at reducing redundancy and code repetition.
Thanks.
原文:https://stackoverflow.com/questions/36516387
最满意答案
如果一行有一个前导标签,另一行没有,它们不相等,所以默认比较不起作用。 然而,
Except
有一个重载,它允许你指定一个自定义的Equality
比较器,你可以提供你自己的比较逻辑:Public Class LineComparer Implements IEqualityComparer(Of String) Public Function Equals1(x As String, y As String) As Boolean _ Implements IEqualityComparer(Of String).Equals Return (String.Compare(x.Trim(), y.Trim(), StringComparison.OrdinalIgnoreCase) = 0) End Function Public Function GetHashCode1(obj As String) As Integer _ Implements IEqualityComparer(Of String).GetHashCode Return obj.Trim().GetHashCode End Function End Class
请注意,
Except
使用GetHashCode1
进行比较。用法:
Dim justB = linesB.Except(linesA, New LineComparer())
这与Blackwood先生的回答之间的区别在于,这不会更改为
ListA
或ListB
加载的数据。 比较时,它会忽略制表符和空格。 目前还不清楚你是谁。If one line has an leading tab and the other doesnt, they are not equal so the default comparison doesn't work. However
Except
has an overload which allows you to specify a customEquality
comparer where you can provide your own comparison logic:Public Class LineComparer Implements IEqualityComparer(Of String) Public Function Equals1(x As String, y As String) As Boolean _ Implements IEqualityComparer(Of String).Equals Return (String.Compare(x.Trim(), y.Trim(), StringComparison.OrdinalIgnoreCase) = 0) End Function Public Function GetHashCode1(obj As String) As Integer _ Implements IEqualityComparer(Of String).GetHashCode Return obj.Trim().GetHashCode End Function End Class
Note that
Except
usesGetHashCode1
for the comparing.Usage:
Dim justB = linesB.Except(linesA, New LineComparer())
The difference between this and Mr Blackwood's answer is that this doesn't change the data loaded for
ListA
orListB
. It simply ignore tabs and spaces when comparing. It is not clear which one is what you are after.
相关问答
更多-
你在找什么叫做String Metric算法。 其中有相当数量的许多具有类似特征。 在更受欢迎的: Levenshtein距离 :将一个单词转换为另一个单词所需的单个字符编辑的最小数量。 字符串不必是相同的长度 汉明距离 :两个长度相等的字符串不同的字符数。 Smith-Waterman :用于计算可变子序列相似性的一系列算法。 Sørensen-Dice系数 :计算相邻字符对的差异系数的相似性算法。 在这个话题的维基页面上看看这些以及其他人。 What you're looking for are cal ...
-
惯例是,在您的Views文件夹中,有一个与控制器名称对应的文件夹。 例如,如果控制器是StudentRosterController则该文件夹将命名为StudentRoster 。 然后视图将对应于操作的名称 - FilterableIndex.cshtml 。 .cshtml文件将以。开头 @model MvcApp.Controllers.StudentRosterViewModel (其中MvcApp.Controllers是包含模型的命名空间。) The convention is that in ...
-
如果一行有一个前导标签,另一行没有,它们不相等,所以默认比较不起作用。 然而, Except有一个重载,它允许你指定一个自定义的Equality比较器,你可以提供你自己的比较逻辑: Public Class LineComparer Implements IEqualityComparer(Of String) Public Function Equals1(x As String, y As String) As Boolean _ Im ...
-
为什么我可以从RIA服务返回一个字符串,但不是IEnumerable
?(Why can I return a string from RIA Services, but not IEnumerable [2020-12-09]?) 尝试返回一个字符串数组并将该方法标记为InvokeOperation。 [Invoke] public string[] GetDestinations() { ... } Try returning an array of strings and marking the method as an InvokeOperation. [Invoke] public string[] GetDestinations() { ... } -
静态IEnumerable?(Static IEnumerable?)[2023-06-03]
哈! 解决了! public class TypedEnum{ public static IEnumerable ToEnumerable() { return typeof (TBase).GetFields(BindingFlags.Public | BindingFlags.Static) .Select(f => f.GetValue(null)).OfType (); } ... -
function commonChars($s1, $s2) { $IMAX = min(strlen($s1), strlen($s2)); for($i = 0; $i < $IMAX; $i++) if($s2[i] != $s1[i]) break; return $i; } function commonChars($s1, $s2) { $IMAX = min(strlen($s1), strlen($s2)); for($i = 0; ...
-
.NET为什么可以作为IEnumerable
参数传递一个字符串[]?(.NET Why can a string[] be passed as IEnumerable [2023-04-13]parameter?) 来自: msdn Array类 在.NET Framework 2.0版中, Array类实现了 IList, ICollection 和 IEnumerable 通用接口。 这些实现在运行时提供给数组 ,因此对文档构建工具不可见。 因此,通用接口不会出现在Array类的声明语法中,并且只有通过将数组转换为通用接口类型(显式接口实现)才能访问的接口成员没有参考主题。 将数组转换为其中一个接口时要注意的关键是要添加,插入或移除元素的成员会引发NotSupportedException 。 f ... -
为什么不测试? 足够容易地使用StopWatch类进行比较,并包含不同长度的字符串和不同的比较选项。 我期望在核心上没有显着差异,因为IsPrefix本质上是: public bool IsPrefix(string comp, string prefix) { return Compare(comp, 0, prefix.Length, prefix, 0, prefix.Length); } 但是,由于I18N的应用级别,String的运算符==和CompareInfo类可能有所不同。 最后,您需 ...
-
您引用的网站说: 如果两个字符串文字相等,则它们被放在相同的内存位置。 但 uSplit('theuser!host') 不是字符串文字 - 它是文字'theuser!host'上的操作的结果。 无论如何,你通常不应该使用is检查字符串相等is ,因为在任何情况下这种内存优化只是一个你不应该依赖的实现细节。 此外,您应该使用is来is None 。 用它来检查两个对象 - 你设计的类 - 是否是同一个实例。 您不能轻易地将它用于字符串或数字,因为创建这些内置类的规则很复杂。 一些字符串被实习。 类似地,一 ...
-
好吧,没有任何回复,除了一个非常错过这一点的评论,所以我只会发布我最终的结果。 此最终版本还会考虑没有属性的项目(例如“对象”本身),并且可以更好地处理空项/值: public static DataTable ToDataTable
(this IEnumerable items, string tableName = "", bool treatItemAsValue = false) { // We want a single extension method ...