Test::Snapshot - automate externalising "expected" data

During the ongoing development of graphql-perl, I have found it valuable to generate data structures, and to compare those with expected values. Often, these have been highly detailed rather than a subset, because I wanted to know when anything changed.

When, however, something does validly have to change, it might change quite a few "expected" outputs. If those all need updating manually, that is a lot of repetitive activity. What if the computer could do that instead? What if Perl had "snapshot testing" as used in JavaScript frontend development, most popularly in Jest?

use Test::Snapshot;
my $got = function_generating_data();
is_deeply_snapshot $got, 'test description'; # could also be in a subtest

Simple tree walking in Perl 6

Check out Tree Surgery for a quick guide on how to easily walk generic trees of Perl 6 data.

Moose Retest 2.5

So in today's Moose pen I am going to stick to the testing tree and have a look at my brand new 20_load_dad.t file in my brand new database-accessor repository

Now in past test cases like this, I did a little creative coding and made up a fake test class for SQL and Mongo drivers that where used when I called this

my $fake_dbh = DBI::db->new();
ok(
    $address->retrieve( $fake_dbh, $result ) eq
      'SELECT  street, city, country FROM person  AS me',
    'SQL correct'
);
Now there is no problem with the use of the fake DBI::db in a test case, the problem that I will encounter is the fact I have to test Database::Accessor as an independent bundle. So that means I have to assume that there will be no DAD present, as we don't want to get into a tail wagging dog situation now do we?

Some Maths for Dobble

I wrote an article about the maths behind the game Dobble (known as Spot-It in some countries). It has no pretense of strict formality but it works for reminding me the though process that leads to designing Dobble-like games. The whole process prodded me to write Math::GF, a module on Galois Fields that can be used together with Math::Polynomial, so... there's also Perl in it!

Moose At Work

So today in the Moose-pen I am not going to any coding just another of those little planning review sessions one should take when creating a new system.

So for the two-bit review,

  • Planned out and amended my basic API
  • Settled on the basic architecture
  • Done some PoC programming
  • Now know where som of the problem area will be.
  • Have some workable tests

So my next step was to migrate all my code out from the blog repository up on git-hub and into its own git-hub repository which I have done. I also want to set this up the same way I would do a Perl distribution so I stubbed in some of the necessary files namely a README, a Makefile.PL and the GNU license.

Next I had a close look at the present state of the code. I only moved over the code directly related to Database::Accessor name-space, at this point is just the Accessor.pm file and its embedded classes.

Moose Test 9-5

So today I am going to follow along from my last post and come up with a little better way to test my Database::Accessor::Roles::DAD role.

This stems from my mistake of the other day of not addng in the 'Collections' attribute to my “Database::Accessor::Roles::DAD” role. Now the purpose of this Role is to supply all the necessary objects from my Accessor.pm down into the various DADs so it is importat that I pass that along.

So I guess my first test is to check that I have my Role in correct shape. A simple use ok will do that, but what I want is a way to test that all my attributes are present in a DAD. Now this is when the Meta-data part of Moose really comes into its own. All I need to do is;

    my @attributes =  $address->meta->get_all_attributes;

Trying to make confluence usable.

=head1 RESTfluence

I've tried to make this blog post copy/pastable as valid perl and valid markdown. So with luck it can be copy/pasted into an editor if you want to use this.

Confluence. I don't really like it, but the major thing it's got going for it is that it's not Sharepoint. As I am spending the summer holidays doing some documentation at work, one of the things I wanted to do was to make confluence less hateful. So I cracked open the REST API to see how far I could get.

There used to be good tools, but atlassian got rid of the XMLRPC API not that long ago.

Progress I made was:

  • Got a list of all spaces, and all pages in each space.
  • Worked out how to obtain the content of a page.
  • Worked out how to change the content of a page (for when the time comes).

Where I got stuck:

  • Working out how to round-trip the confluence markup to/from markdown.

The rest of this post describes the script I put together. It's not useful enough for me to put on the CPAN but it's worth putting up somewhere.

Even More Moosey

Today I am d going to start filling in my API a little and some might remember the table from this post looking at it a second time I think I need to make a few adjustments to this
+-------------------+-----------------+-----------------+
| Data Accessor     |  SQL            | Mongo           |
+-------------------+-----------------+-----------------+
| Param             | Param           | Param           |
| Element           | Field           | Name Value Pair |
| Predicate         | Predicate       | Options         |
| View              | Table (or view) | Collection      |
| Condition         | Where           | Find            |
| Link              | Join            | Lookup          |
| Gather            | Group           | Aggregate       |
| Filter            | having          | ?               |
| Sort              | Order           | Sort            |
 +-------------------+-----------------+-----------------+ 
Just so I updated the table to link the 'Condition' to the where clause in SQL and Find in Mongo, and just as a reminder here it is again; Condition
A logical Predicate supplied to the target database by the Data Accessor to filter data.
Now it makes no sense to have just one condition on a query so this should be a collection of some form so I think I have to redefine this attribute as Conditions An Arry-Ref of logical Predicates supplied to the target database by the Data Accessor to filter data. And in my code I added

Stack Overflow Considered as a Grimoire or Trove

Back in the day, when I came upon a particularly juicy tip/hint/trick/kludge/etc. I would write it down somewhere (way back -- in a paper notebook; more recently, somewhere like TiddlyWiki). But I don't do that anymore -- why?

The answer is Stack Overflow. When I have a question, a web search often has 1 or more Stack Overflow answers at the top - answers that usually help me fix (or work around) the problem I have. It really is impressive how often Stack Overflow has just the answer I need and in just enough detail. (Disclosure: I answer questions on Stack Overflow on a semi-regular basis.)

Stack Overflow Considered as a Grimoire or Trove

Back in the day, when I came upon a particularly juicy tip/hint/trick/kludge/etc. I would write it down somewhere (way back -- in a paper notebook; more recently, somewhere like TiddlyWiki). But I don't do that anymore -- why?

The answer is Stack Overflow. When I have a question, a web search often has 1 or more Stack Overflow answers at the top - answers that usually help me fix (or work around) the problem I have. It really is impressive how often Stack Overflow has just the answer I need and in just enough detail. (Disclosure: I answer questions on Stack Overflow on a semi-regular basis.)

New Website and URL for HTML::FormFu

Sitting on my TODO list has been a refresh (resurrection perhaps?) of the FormFu website.

In 2017 the various contributors consolidated into a single Group on GitHub - authors of related modules are invited to donate them into that group for future maintenance etc.

Having some time off has allowed me to get through much of that aforementioned TODO list and over the last few days, the FormFu website has been ticked off of that list.

GitHub has a "pages" feature, which is based upon Jekyll. This is a convenient system for generating a static site from templates and content from Git - which also includes the hosting part for free. Everyone in the group can make changes, with 3rd parties able to use Git's normal mechanisms to send PR's, issue reports etc.

So I donated a funky new .rocks domain and used the GitHub pages to create simple new website for FormFu. Any and all feedback is much appreciated.

And without further ado, http://formfu.rocks

Building Blocks of Moose

Well for today’s post I am going to continue along with one of the basic building blocks of a good Moose program coercion. We saw in the last post how I started to clean up my interface with coercion and I am going to do that to the next Accessor.pm attribute

has elements  => (
    isa => 'ArrayRef',
    is   => 'rw',
);

Year 2017 - Annual Report

Looking back year 2017 brought many sweet moments, both at professional and personal front. On personal front, I have been blessed with twin girls, Aabia & Aania. Raising twins without family support is not easy for me and my wife. I am told to be prepared for more surprises.

On professional front, there have been many positives to talk about. First I completed third consecutive years of CPAN PullRequest Challenge without missing a single month. I hope to carry on with the same spirit in the year 2018.

Took part in Hacktoberfest challenge, third consecutive years, and submitted 40 PR. Recently received the specially designed T-shirt, Thanks Digital Ocean.

I also completed 24PullRequest, again third consecutive years, and submitted 34 PR.

On the same line, submitting PR, I planned to do at least 1 PR each day on average most of the months in the year. Before year 2017, I was able to do that in 3 months at the most (in the year 2015).

Perl 5 Porters Mailing List Summary: December 11th - January 1st

Hey everyone,

Following is the p5p (Perl 5 Porters) mailing list summary for the past month.

Enjoy!

YAML::PP Grant Report December 2017

Hi there!

Thanks for reading my report. In the last report I included a mini tutorial about string quoting methods in YAML. This time I've written an introduction into YAML Schemas and Tags.

In December I have been working about 60 hours on YAML::PP, YAML::XS, libyaml and the Schema article.

See also my previous reports on blogs.perl.org (Aug/Sep, Oct, Nov ) and news.perlfoundation.org (Aug/Sep, Oct, Nov ).

Introduction to YAML Schemas and Tags

I wrote this article as part of my YAML::PP Grant report for December 2017.

This article will give you an overview how a YAML document is loaded into a data structure and how a processor decides if something is a string, a number, a boolean or something else.

If you look at JSON, the rules are quite obvious:

{
    "string": "just a string",
    "boolean": true,
    "integer": 42,
    "float": 3.14159,
    "undefined": null,
    "list": [1, 2, 3]
}

In YAML, it depends on the YAML version, and on the Schema the Processor implements. This makes it more powerful, but also more complicated to implement and use.

Some processors implement YAML 1.1, some implement 1.2, and often there are things that aren't implemented correctly. When writing YAML, it's good to know the official specification to understand why some things are supported in one processor, but not in others.

This article covers:

  • Standard Tags/Schema in YAML 1.2
  • Type System in YAML 1.1
  • How to write compatible YAML
  • Other types of Schemas

As an example, note how boolean handling differs in YAML 1.1/1.2 and the 1.2 schemas.

Simple Clean Moose

So today, to start the new year right, I really am going to have a look at Moose coercion which I have been promissing to do since this post . So to refresh your memory I had created my types role and one type and added it to my accessor like this

has view => (
    is     => 'rw',
    isa    => 'View',
);
So what exactly does coercion do for us? Well as I had illustrated in a early post what I want it to do it take an input hash and then create an object from that hash to save us from typing up such unsightly things as

Database::Accessor->new({
        view     =>  Database::Accessor::View->new( {
        name  => 'person',
        alias => 'me' })
…
by allowing us to this

Database::Accessor->new({
        view     => {
        name  => 'person',
        alias => 'me'}
…
So all we need to do in in the types role add in coerce command like this

coerce 'View', from 'HashRef', via { Database::Accessor::View->new( %{$_} ) };

Off The Moose Track

Well off track again. So I am not going to do my bit on coercion today after all. If anyone out there was actually downloading my code and running my tests you might see this;

Load of Database/Accessor/DAD/Types.pm failed:
Error=Can't locate Database/Accessor/DAD/Types.pm in @INC (@INC contains: D:\GitHub\DA-blog\lib D:\GitHub\Replay\lib C:/Dwimperl/perl/site/lib C:/Dwimperl/perl/vendor/lib C:/Dwimperl/perl/lib .) at (eval 378) line 2.

Database/Accessor/DAD/Types (Database::Accessor::DAD::Types) may not be an Database Accessor Driver (DAD)!

An alternative to HTML::Tagset

HTML::Tagset is a popular module on CPAN and has nearly nine thousand descendants in the "River of CPAN". Unfortunately its last version was in March 2008, nearly ten years ago, so it doesn't feature the HTML5 tags yet. There has been some discussion on the HTML tag set bug tracker but so far that has not come to fruition. As an alternative to HTML::Tagset, I made the module HTML::Valid::Tagset, which incorporates the data tables of the "Tidy HTML5" project.

It's partly based on HTML::Tagset's interface, so it can substitute for some of that module's functionality, and it also has specific tag sets for each version of HTML, including HTML5.

Having the Tidy HTML5 data tables means I was also able to add a function attributes which returns a list of valid attributes for a tag, and tag_attr_ok, which returns true or false depending on whether a particular tag can take a particular attribute. This is part of a larger distribution called HTML::Valid, based on the Tidy HTML5 project.

Moose in Sheepish Clothing

Now to get back on track to what I wanted before I made a little Boo-Boo, So in today's post I am going to have a quick look at Moose and its Coercion abilities. One thing about moose Coercion is it works along with Moose Types so you can't have one without the other.
So lets start wit the Accessor.pm 'View' attribute.

Right now we have this

has View => (
    is     => 'rw',
    isa    => 'Object',
); 
Which is fine but there is noting stopping my from entering any type of object in there so lets fix that

-- isa    => 'Object',
 ++isa    => 'Database::Accessor::View',
that will work but it is a good deal of typing that I do not really need and of course more typing means more chance for an error. Now Moose will let you define a custom type and ave you all that typing. All I have to do is create a new Moose role to hold these types. For now I am adding it to the Accessor.pm file like this

About blogs.perl.org

blogs.perl.org is a common blogging platform for the Perl community. Written in Perl and offering the modern features you’ve come to expect in blog platforms, the site is hosted by Dave Cross and Aaron Crane, with a design donated by Six Apart, Ltd.