From 050b160c1cdb73c09e5afaaa0924cb37d6a92e01 Mon Sep 17 00:00:00 2001 From: Tomos Griffiths Date: Thu, 20 Feb 2025 14:28:41 +0000 Subject: [PATCH 1/3] added information about dvla-browser-drivers to the open-source page --- content/open-source/index.md | 6 +- .../2024-12-using-max-by-in-ruby/index.md | 67 +++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 content/posts/2024-12-using-max-by-in-ruby/index.md diff --git a/content/open-source/index.md b/content/open-source/index.md index ad04ae7..bbcdd61 100644 --- a/content/open-source/index.md +++ b/content/open-source/index.md @@ -50,6 +50,10 @@ This gem encapsulates a way of having a predefined set of properties within a te Open Source library to apply Royal Mail Rules & Exceptions to PAF (Postcode Address File) addresses when converting to a printable format. +### [dvla-browser-drivers](https://github.com/dvla/dvla-browser-drivers) + +This gem has pre-configured browser drivers that you can use out-of-the-box for the development of your application. + # Dynamics 365 ### [dataverse-helper](https://github.com/dvla/dataverse-helper) @@ -60,4 +64,4 @@ This gem helps you integrate with Microsoft Dynamics using Microsoft Dataverse W ### [postman-paf-js](https://github.com/dvla/postman-paf-js) -Open Source library to apply Royal Mail Rules & Exceptions to PAF (Postcode Address File) addresses when converting to a printable format. \ No newline at end of file +Open Source library to apply Royal Mail Rules & Exceptions to PAF (Postcode Address File) addresses when converting to a printable format. diff --git a/content/posts/2024-12-using-max-by-in-ruby/index.md b/content/posts/2024-12-using-max-by-in-ruby/index.md new file mode 100644 index 0000000..24b7ea8 --- /dev/null +++ b/content/posts/2024-12-using-max-by-in-ruby/index.md @@ -0,0 +1,67 @@ +--- +author: "Tomos Griffiths" +title: "TiL: Using the max_by method in Ruby" +description: "Using max_by to filter out data based on the highest value for a particular field" +draft: false +date: 2024-12-02 +tags: ["Ruby", "max_by", "Testing", "Today I Learned"] +categories: ["TIL", "Ruby", "max_by", "Testing"] +ShowToc: true +TocOpen: true +--- + +Trying to run tests in parallel in your CI/CD pipeline is tricky at the best of times, but what if you have tests which should only be run in a certain environment and not as part of the CI build stage? + +It is fairly simple to just tag a feature file with a unique tag that can then be passed to the `bundle exec rake` command in the functional-tests step, as an environment variable. However, this tag will not stop these tests being run as part of the CI stage unless you make changes to how the tags are being used. + +A useful way is by adopting the approach described below: + +## Using 'included' and 'excluded' tags + +By using a set of `included` and `excluded` tags, we can pass in the tags we want to use within a particular rake task, and also the tags we want to exclude from this task. We can do this easily and elegantly by using an array for each as below: + +```ruby + # To exclude more tags in the running of the tests, add the argument to the 'excluded_tags' array below + task :run, %i[threads feature_path tags excluded_tags] do |_t, args| + args.with_defaults(threads: '1', feature_path: 'features', tags: '[]', excluded_tags: "['@exclude']") + # puts the array of excluded_tags into a string to use in the tags argument below + excluded_tags = JSON.parse(args.excluded_tags).map { |tag| "not #{tag}" } + included_tags = JSON.parse(args.tags) + + tags = included_tags.concat(excluded_tags).join(' and ') + + # tags = args.tag.nil? ? 'not @problematic and not @retention' : args.tag + + LOG.info { "Threads: '#{args.threads}' | Feature path: '#{args.feature_path}' | Tags: '#{tags}' | Formatter: '#{cucumber_formatter}'" } + + parallel.run(['-n', args.threads, '--type', 'cucumber', '--group-by', 'scenarios', '--serialize-stdout', + '--', '-f', cucumber_formatter, '--out', '/dev/null', '-f', 'progress', + '-t', tags, '--', args.feature_path]) + end +``` + +The `tags` can then be passed in to the `parallel.run` command we are all familiar with. This ensures that we are running the tests that have been tagged appropriately but also excludes any tags that should not be added to this command. + +In your `drone.yml` file, be sure to use the escape character (\) correctly in order to pass in the tags correctly, i.e. + +```ruby + bundle exec rake "functional_tests:run[1, features/tests, [\"@included_tag\"], [\"@excluded_tag\"]]" # 1 included tag, 1 excluded tag + OR + bundle exec rake "functional_tests:run[2, features/tests, [], [\"@excluded_tag1\" \,\ \"@excluded_tag2\"]]" # 0 included tags, 2 excluded tags +``` + +## Use case + +An example of this would be if your test pack runs a `cron` job, where you only want certain tests/feature files to be run within this cron......with the caveat that these `cron` tests SHOULD NOT also be run in your CI stage upon a `push` or `merge to main`. The `excluded_tags` would then contain the "cron test" tags as a default as these should be ignored in your CI stage. Then in your `cron` step, use the `bundle exec rake` command as before, but then pass the "cron tests" tag in here instead, ensuring that all other tags will be ignored and only your appropriately tagged tests will be run. + +This is more elegant than writing: + +```ruby + tags = args.tag.nil? ? 'not @exclude' : "#{args.tag} and not @excluded_tag1 and not @excluded_tag2 and not @excluded_tag3....." +``` + +By using the array, you can add an `excluded_tag`to the running of the tests without programmatically making any code changes to existing tests. This elegant method also looks cleaner. + +## Conclusion + +Using this method allows any test pack to run and/or ignore certain tests within certain environments when the need arises, without making any drastic code changes or amending the default `bundle exec rake` command to run the tests in parallel. From 6144d1c0e406a5b0fa94cc6452defcefab4b683c Mon Sep 17 00:00:00 2001 From: Tomos Griffiths Date: Thu, 20 Feb 2025 15:00:17 +0000 Subject: [PATCH 2/3] also added a TiL blog arounbd max_by method in ruby --- .../2024-12-using-max-by-in-ruby/index.md | 56 ++++--------------- 1 file changed, 11 insertions(+), 45 deletions(-) diff --git a/content/posts/2024-12-using-max-by-in-ruby/index.md b/content/posts/2024-12-using-max-by-in-ruby/index.md index 24b7ea8..bebd237 100644 --- a/content/posts/2024-12-using-max-by-in-ruby/index.md +++ b/content/posts/2024-12-using-max-by-in-ruby/index.md @@ -3,65 +3,31 @@ author: "Tomos Griffiths" title: "TiL: Using the max_by method in Ruby" description: "Using max_by to filter out data based on the highest value for a particular field" draft: false -date: 2024-12-02 +date: 2025-02-20 tags: ["Ruby", "max_by", "Testing", "Today I Learned"] categories: ["TIL", "Ruby", "max_by", "Testing"] ShowToc: true TocOpen: true --- -Trying to run tests in parallel in your CI/CD pipeline is tricky at the best of times, but what if you have tests which should only be run in a certain environment and not as part of the CI build stage? - -It is fairly simple to just tag a feature file with a unique tag that can then be passed to the `bundle exec rake` command in the functional-tests step, as an environment variable. However, this tag will not stop these tests being run as part of the CI stage unless you make changes to how the tags are being used. - -A useful way is by adopting the approach described below: - -## Using 'included' and 'excluded' tags - -By using a set of `included` and `excluded` tags, we can pass in the tags we want to use within a particular rake task, and also the tags we want to exclude from this task. We can do this easily and elegantly by using an array for each as below: +When trying to get an upper or lower value for a certain field in an object can be cumbersome. But when using the `max_by` method, this can actually be done quite elegantly. For example: ```ruby - # To exclude more tags in the running of the tests, add the argument to the 'excluded_tags' array below - task :run, %i[threads feature_path tags excluded_tags] do |_t, args| - args.with_defaults(threads: '1', feature_path: 'features', tags: '[]', excluded_tags: "['@exclude']") - # puts the array of excluded_tags into a string to use in the tags argument below - excluded_tags = JSON.parse(args.excluded_tags).map { |tag| "not #{tag}" } - included_tags = JSON.parse(args.tags) - - tags = included_tags.concat(excluded_tags).join(' and ') - - # tags = args.tag.nil? ? 'not @problematic and not @retention' : args.tag - - LOG.info { "Threads: '#{args.threads}' | Feature path: '#{args.feature_path}' | Tags: '#{tags}' | Formatter: '#{cucumber_formatter}'" } - - parallel.run(['-n', args.threads, '--type', 'cucumber', '--group-by', 'scenarios', '--serialize-stdout', - '--', '-f', cucumber_formatter, '--out', '/dev/null', '-f', 'progress', - '-t', tags, '--', args.feature_path]) - end +persons = [ + { "name" => "John McJohnson","age" => 34, "topScoreAtBowling" => 205}, + { "name" => "Davey Jones","age" => 304, "topScoreAtBowling" => 300}, + { "name" => "Willy Wonka","age" => 50, "topScoreAtBowling" => 200} +] ``` -The `tags` can then be passed in to the `parallel.run` command we are all familiar with. This ensures that we are running the tests that have been tagged appropriately but also excludes any tags that should not be added to this command. - -In your `drone.yml` file, be sure to use the escape character (\) correctly in order to pass in the tags correctly, i.e. - -```ruby - bundle exec rake "functional_tests:run[1, features/tests, [\"@included_tag\"], [\"@excluded_tag\"]]" # 1 included tag, 1 excluded tag - OR - bundle exec rake "functional_tests:run[2, features/tests, [], [\"@excluded_tag1\" \,\ \"@excluded_tag2\"]]" # 0 included tags, 2 excluded tags -``` - -## Use case - -An example of this would be if your test pack runs a `cron` job, where you only want certain tests/feature files to be run within this cron......with the caveat that these `cron` tests SHOULD NOT also be run in your CI stage upon a `push` or `merge to main`. The `excluded_tags` would then contain the "cron test" tags as a default as these should be ignored in your CI stage. Then in your `cron` step, use the `bundle exec rake` command as before, but then pass the "cron tests" tag in here instead, ensuring that all other tags will be ignored and only your appropriately tagged tests will be run. - -This is more elegant than writing: +If we had a bowling competition and wanted to see who had the `topScoreAtBowling` field, rather than looping through the array and looking at each object, we can simply use the `max_by` command as follows: ```ruby - tags = args.tag.nil? ? 'not @exclude' : "#{args.tag} and not @excluded_tag1 and not @excluded_tag2 and not @excluded_tag3....." +persons.max_by { |person| person['topScoreAtBowling']} #{"name"=>"Davey Jones", "age"=>304, "topScoreAtBowling"=>300} ``` -By using the array, you can add an `excluded_tag`to the running of the tests without programmatically making any code changes to existing tests. This elegant method also looks cleaner. +The resultant object would be returned, and could be processed into their "Hall of Fame"! ## Conclusion -Using this method allows any test pack to run and/or ignore certain tests within certain environments when the need arises, without making any drastic code changes or amending the default `bundle exec rake` command to run the tests in parallel. +The `max_by` method is very convenient when dealing with a large array of objects, and you are required to find the highest value for a certain field. Alternatively the `min_by` method can be used to find the lower value of a certain field From 69827be313bf41d242afd9a59598d60526b4413c Mon Sep 17 00:00:00 2001 From: Tomos Griffiths Date: Fri, 21 Feb 2025 08:01:17 +0000 Subject: [PATCH 3/3] tweaked the description slightly --- content/open-source/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/open-source/index.md b/content/open-source/index.md index bbcdd61..c3a1d99 100644 --- a/content/open-source/index.md +++ b/content/open-source/index.md @@ -52,7 +52,7 @@ Open Source library to apply Royal Mail Rules & Exceptions to PAF (Postcode Addr ### [dvla-browser-drivers](https://github.com/dvla/dvla-browser-drivers) -This gem has pre-configured browser drivers that you can use out-of-the-box for the development of your application. +This gem has pre-configured browser drivers that you can use out-of-the-box for the development of your automated test suite or application. # Dynamics 365